dtinfo: remove register keyword
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Basic / HashTbl.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /*
24  * $XConsortium: HashTbl.cc /main/3 1996/06/11 16:18:57 cde-hal $
25  *
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.
30  * 
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.
34  * 
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
41  * 
42  */
43
44 #define C_HashTbl
45 #define L_Basic
46
47 #include "Prelude.h"
48
49 #include <memory.h>
50
51
52 class HashBucket : public List
53 {
54 public: // functions
55   HashBucket()
56     // NOTE: remove 3rd param after testing
57     : List (10, 10, List::GROW_ADD) { };
58   int find (FolioObject &);
59   using List::remove;
60   void remove (FolioObject &);
61 };
62
63
64 // /////////////////////////////////////////////////////////////////
65 // bucket find
66 // /////////////////////////////////////////////////////////////////
67
68 int
69 HashBucket::find (FolioObject &element)
70 {
71   int i;
72
73   for (i = 0; i < f_length; i++)
74     {
75       if (((Hashable *)f_list_element[i])->equals ((Hashable &) element))
76         return (i);
77     }
78
79   return (-1);
80 }
81
82
83 // /////////////////////////////////////////////////////////////////
84 // bucket remove
85 // /////////////////////////////////////////////////////////////////
86
87 void
88 HashBucket::remove (FolioObject &element)
89 {
90   int i;
91
92   for (i = 0; i < f_length; i++)
93     {
94       if (((Hashable *)f_list_element[i])->equals ((Hashable &) element))
95         {
96           // Shift back the array and overwrite deleted element. 
97           memcpy (&f_list_element[i],
98                   &f_list_element[i+1],
99                  sizeof (Hashable *) * (f_length - i - 1));
100           f_length--;
101           break;
102         }
103     }
104 }
105
106
107 // /////////////////////////////////////////////////////////////////
108 // constructor
109 // /////////////////////////////////////////////////////////////////
110
111 HashTbl::HashTbl (u_int num_buckets)
112 {
113   f_hash_bucket = (HashBucket **) calloc (num_buckets, sizeof (HashBucket *));
114   f_num_buckets = num_buckets;
115 }
116
117
118 // /////////////////////////////////////////////////////////////////
119 // class destructor
120 // /////////////////////////////////////////////////////////////////
121
122 HashTbl::~HashTbl()
123 {
124   remove_all(1);
125   free ((char *) f_hash_bucket);
126 }
127
128
129 // /////////////////////////////////////////////////////////////////
130 // add - add an entry to the hash table
131 // /////////////////////////////////////////////////////////////////
132
133 void
134 HashTbl::add (Hashable &element)
135 {
136   /* -------- Find the hash value for this element. -------- */
137   u_int where = element.hash_code(0, f_num_buckets-1);
138
139   /* -------- Create a bucket if it doesn't exist yet. -------- */
140   if (f_hash_bucket[where] == NULL)
141     f_hash_bucket[where] = new HashBucket();
142
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);
147 }
148
149
150 // /////////////////////////////////////////////////////////////////
151 // find - find an entry in the hash table
152 // /////////////////////////////////////////////////////////////////
153
154 int
155 HashTbl::find (Hashable &element) const
156 {
157   u_int where = element.hash_code (0, f_num_buckets - 1);
158
159   if (f_hash_bucket[where] != NULL)
160     return (f_hash_bucket[where]->find (element));
161   else
162     return (-1);
163 }
164
165
166 // /////////////////////////////////////////////////////////////////
167 // remove - remove an entry from the hash table
168 // /////////////////////////////////////////////////////////////////
169
170 void
171 HashTbl::remove (Hashable &element)
172 {
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);
176 }
177
178
179 // /////////////////////////////////////////////////////////////////
180 // remove_all - delete all elements in the hash table
181 // /////////////////////////////////////////////////////////////////
182
183 void
184 HashTbl::remove_all (bool delete_elements)
185 {
186   for (unsigned int i = 0; i < f_num_buckets; i++)
187     if (f_hash_bucket[i] != NULL)
188       {
189         if (delete_elements)
190           f_hash_bucket[i]->remove_all (TRUE);
191         delete f_hash_bucket[i];
192         f_hash_bucket[i] = NULL;
193       }
194 }