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
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 &);
59 void remove (FolioObject &);
63 // /////////////////////////////////////////////////////////////////
65 // /////////////////////////////////////////////////////////////////
68 HashBucket::find (FolioObject &element)
72 for (i = 0; i < f_length; i++)
74 if (((Hashable *)f_list_element[i])->equals ((Hashable &) element))
82 // /////////////////////////////////////////////////////////////////
84 // /////////////////////////////////////////////////////////////////
87 HashBucket::remove (FolioObject &element)
91 for (i = 0; i < f_length; i++)
93 if (((Hashable *)f_list_element[i])->equals ((Hashable &) element))
95 // Shift back the array and overwrite deleted element.
96 memcpy (&f_list_element[i],
98 sizeof (Hashable *) * (f_length - i - 1));
106 // /////////////////////////////////////////////////////////////////
108 // /////////////////////////////////////////////////////////////////
110 HashTbl::HashTbl (u_int num_buckets)
112 f_hash_bucket = (HashBucket **) calloc (num_buckets, sizeof (HashBucket *));
113 f_num_buckets = num_buckets;
117 // /////////////////////////////////////////////////////////////////
119 // /////////////////////////////////////////////////////////////////
124 free ((char *) f_hash_bucket);
128 // /////////////////////////////////////////////////////////////////
129 // add - add an entry to the hash table
130 // /////////////////////////////////////////////////////////////////
133 HashTbl::add (Hashable &element)
135 /* -------- Find the hash value for this element. -------- */
136 u_int where = element.hash_code(0, f_num_buckets-1);
138 /* -------- Create a bucket if it doesn't exist yet. -------- */
139 if (f_hash_bucket[where] == NULL)
140 f_hash_bucket[where] = new HashBucket();
142 /* -------- See if it exists. -------- */
143 if (f_hash_bucket[where]->find (element) == -1)
144 /* -------- Finally, add it. -------- */
145 f_hash_bucket[where]->append (element);
149 // /////////////////////////////////////////////////////////////////
150 // find - find an entry in the hash table
151 // /////////////////////////////////////////////////////////////////
154 HashTbl::find (Hashable &element) const
156 u_int where = element.hash_code (0, f_num_buckets - 1);
158 if (f_hash_bucket[where] != NULL)
159 return (f_hash_bucket[where]->find (element));
165 // /////////////////////////////////////////////////////////////////
166 // remove - remove an entry from the hash table
167 // /////////////////////////////////////////////////////////////////
170 HashTbl::remove (Hashable &element)
172 u_int where = element.hash_code (0, f_num_buckets - 1);
173 if (f_hash_bucket[where] != NULL)
174 f_hash_bucket[where]->remove (element);
178 // /////////////////////////////////////////////////////////////////
179 // remove_all - delete all elements in the hash table
180 // /////////////////////////////////////////////////////////////////
183 HashTbl::remove_all (bool delete_elements)
185 for (unsigned int i = 0; i < f_num_buckets; i++)
186 if (f_hash_bucket[i] != NULL)
189 f_hash_bucket[i]->remove_all (TRUE);
190 delete f_hash_bucket[i];
191 f_hash_bucket[i] = NULL;