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 libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * $XConsortium: HashTbl.cc /main/3 1996/06/11 16:18:57 cde-hal $
26 * Copyright (c) 1992 HaL Computer Systems, Inc. All rights reserved.
27 * UNPUBLISHED -- rights reserved under the Copyright Laws of the United
28 * States. Use of a copyright notice is precautionary only and does not
29 * imply publication or disclosure.
31 * This software contains confidential information and trade secrets of HaL
32 * Computer Systems, Inc. Use, disclosure, or reproduction is prohibited
33 * without the prior express written permission of HaL Computer Systems, Inc.
35 * RESTRICTED RIGHTS LEGEND
36 * Use, duplication, or disclosure by the Government is subject to
37 * restrictions as set forth in subparagraph (c)(l)(ii) of the Rights in
38 * Technical Data and Computer Software clause at DFARS 252.227-7013.
39 * HaL Computer Systems, Inc.
40 * 1315 Dell Avenue, Campbell, CA 95008
52 class HashBucket : public List
56 // NOTE: remove 3rd param after testing
57 : List (10, 10, List::GROW_ADD) { };
58 int find (FolioObject &);
60 void remove (FolioObject &);
64 // /////////////////////////////////////////////////////////////////
66 // /////////////////////////////////////////////////////////////////
69 HashBucket::find (FolioObject &element)
73 for (i = 0; i < f_length; i++)
75 if (((Hashable *)f_list_element[i])->equals ((Hashable &) element))
83 // /////////////////////////////////////////////////////////////////
85 // /////////////////////////////////////////////////////////////////
88 HashBucket::remove (FolioObject &element)
92 for (i = 0; i < f_length; i++)
94 if (((Hashable *)f_list_element[i])->equals ((Hashable &) element))
96 // Shift back the array and overwrite deleted element.
97 memcpy (&f_list_element[i],
99 sizeof (Hashable *) * (f_length - i - 1));
107 // /////////////////////////////////////////////////////////////////
109 // /////////////////////////////////////////////////////////////////
111 HashTbl::HashTbl (u_int num_buckets)
113 f_hash_bucket = (HashBucket **) calloc (num_buckets, sizeof (HashBucket *));
114 f_num_buckets = num_buckets;
118 // /////////////////////////////////////////////////////////////////
120 // /////////////////////////////////////////////////////////////////
125 free ((char *) f_hash_bucket);
129 // /////////////////////////////////////////////////////////////////
130 // add - add an entry to the hash table
131 // /////////////////////////////////////////////////////////////////
134 HashTbl::add (Hashable &element)
136 /* -------- Find the hash value for this element. -------- */
137 u_int where = element.hash_code(0, f_num_buckets-1);
139 /* -------- Create a bucket if it doesn't exist yet. -------- */
140 if (f_hash_bucket[where] == NULL)
141 f_hash_bucket[where] = new HashBucket();
143 /* -------- See if it exists. -------- */
144 if (f_hash_bucket[where]->find (element) == -1)
145 /* -------- Finally, add it. -------- */
146 f_hash_bucket[where]->append (element);
150 // /////////////////////////////////////////////////////////////////
151 // find - find an entry in the hash table
152 // /////////////////////////////////////////////////////////////////
155 HashTbl::find (Hashable &element) const
157 u_int where = element.hash_code (0, f_num_buckets - 1);
159 if (f_hash_bucket[where] != NULL)
160 return (f_hash_bucket[where]->find (element));
166 // /////////////////////////////////////////////////////////////////
167 // remove - remove an entry from the hash table
168 // /////////////////////////////////////////////////////////////////
171 HashTbl::remove (Hashable &element)
173 u_int where = element.hash_code (0, f_num_buckets - 1);
174 if (f_hash_bucket[where] != NULL)
175 f_hash_bucket[where]->remove (element);
179 // /////////////////////////////////////////////////////////////////
180 // remove_all - delete all elements in the hash table
181 // /////////////////////////////////////////////////////////////////
184 HashTbl::remove_all (bool delete_elements)
186 for (unsigned int i = 0; i < f_num_buckets; i++)
187 if (f_hash_bucket[i] != NULL)
190 f_hash_bucket[i]->remove_all (TRUE);
191 delete f_hash_bucket[i];
192 f_hash_bucket[i] = NULL;