2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
27 * $TOG: Dictionary.C /main/5 1997/12/22 16:32:11 bill $
29 * RESTRICTED CONFIDENTIAL INFORMATION:
31 * The information in this document is subject to special
32 * restrictions in a confidential disclosure agreement bertween
33 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
34 * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
35 * Sun's specific written approval. This documment and all copies
36 * and derivative works thereof must be returned or destroyed at
39 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
47 #include <DtMail/Threads.hh>
48 #include <DtMail/Dictionary.hh>
50 DtMailDictionaryImpl::DtMailDictionaryImpl(int size)
54 _hash_table = (HashTable_t *)malloc(sizeof(HashTable_t) * _hash_size);
56 for (int h = 0; h < _hash_size; h++) {
57 _hash_table[h] = NULL;
60 _obj_mutex = MutexInit();
63 DtMailDictionaryImpl::~DtMailDictionaryImpl(void)
65 MutexLock lock_scope(_obj_mutex);
68 for (int h = 0; h < _hash_size; h++) {
70 delete _hash_table[h];
77 lock_scope.unlock_and_destroy();
81 DtMailDictionaryImpl::set(const char * key, const void * value)
83 MutexLock lock_scope(_obj_mutex);
85 int hash = hashValue(key);
87 // See if this key is already there. If so, simply update the value.
90 locate(hash, key, &ent);
97 // New key/value. Add it to the appropriate slot.
99 if (_hash_table[hash] == NULL) {
100 _hash_table[hash] = new DtVirtArray<Entry *>(16);
103 DtVirtArray<Entry *> * slot = _hash_table[hash];
106 ent->key = strdup(key);
114 DtMailDictionaryImpl::lookup(const char * key)
116 MutexLock lock_scope(_obj_mutex);
118 int hash = hashValue(key);
121 locate(hash, key, &ent);
122 const void * value = NULL;
131 DtMailDictionaryImpl::remove(const char * key)
133 MutexLock lock_scope(_obj_mutex);
135 int hash = hashValue(key);
137 // Locate the entry. This approach will cause us to search
138 // the hash list twice, but that should be cheap enough that
139 // we can afford to do it.
142 locate(hash, key, &ent);
144 // Not here, just return.
148 DtVirtArray<Entry *> * slot = _hash_table[hash];
149 int loc = slot->indexof(ent);
156 DtMailDictionaryImpl::hashValue(const char * key)
159 for (const char * cur = key; *cur; cur++) {
169 DtMailDictionaryImpl::locate(int hash, const char * key, Entry ** ent)
173 DtVirtArray<Entry *> * slot = _hash_table[hash];
179 for (int col = 0; col < slot->length(); col++) {
180 Entry * t_ent = (*slot)[col];
182 if (strcmp(t_ent->key, key) == 0) {