Resolve many build warnings
[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 librararies 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   void remove (FolioObject &);
60 };
61
62
63 // /////////////////////////////////////////////////////////////////
64 // bucket find
65 // /////////////////////////////////////////////////////////////////
66
67 int
68 HashBucket::find (FolioObject &element)
69 {
70   register int i;
71
72   for (i = 0; i < f_length; i++)
73     {
74       if (((Hashable *)f_list_element[i])->equals ((Hashable &) element))
75         return (i);
76     }
77
78   return (-1);
79 }
80
81
82 // /////////////////////////////////////////////////////////////////
83 // bucket remove
84 // /////////////////////////////////////////////////////////////////
85
86 void
87 HashBucket::remove (FolioObject &element)
88 {
89   register int i;
90
91   for (i = 0; i < f_length; i++)
92     {
93       if (((Hashable *)f_list_element[i])->equals ((Hashable &) element))
94         {
95           // Shift back the array and overwrite deleted element. 
96           memcpy (&f_list_element[i],
97                   &f_list_element[i+1],
98                  sizeof (Hashable *) * (f_length - i - 1));
99           f_length--;
100           break;
101         }
102     }
103 }
104
105
106 // /////////////////////////////////////////////////////////////////
107 // constructor
108 // /////////////////////////////////////////////////////////////////
109
110 HashTbl::HashTbl (u_int num_buckets)
111 {
112   f_hash_bucket = (HashBucket **) calloc (num_buckets, sizeof (HashBucket *));
113   f_num_buckets = num_buckets;
114 }
115
116
117 // /////////////////////////////////////////////////////////////////
118 // class destructor
119 // /////////////////////////////////////////////////////////////////
120
121 HashTbl::~HashTbl()
122 {
123   remove_all(1);
124   free ((char *) f_hash_bucket);
125 }
126
127
128 // /////////////////////////////////////////////////////////////////
129 // add - add an entry to the hash table
130 // /////////////////////////////////////////////////////////////////
131
132 void
133 HashTbl::add (Hashable &element)
134 {
135   /* -------- Find the hash value for this element. -------- */
136   u_int where = element.hash_code(0, f_num_buckets-1);
137
138   /* -------- Create a bucket if it doesn't exist yet. -------- */
139   if (f_hash_bucket[where] == NULL)
140     f_hash_bucket[where] = new HashBucket();
141
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);
146 }
147
148
149 // /////////////////////////////////////////////////////////////////
150 // find - find an entry in the hash table
151 // /////////////////////////////////////////////////////////////////
152
153 int
154 HashTbl::find (Hashable &element) const
155 {
156   u_int where = element.hash_code (0, f_num_buckets - 1);
157
158   if (f_hash_bucket[where] != NULL)
159     return (f_hash_bucket[where]->find (element));
160   else
161     return (-1);
162 }
163
164
165 // /////////////////////////////////////////////////////////////////
166 // remove - remove an entry from the hash table
167 // /////////////////////////////////////////////////////////////////
168
169 void
170 HashTbl::remove (Hashable &element)
171 {
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);
175 }
176
177
178 // /////////////////////////////////////////////////////////////////
179 // remove_all - delete all elements in the hash table
180 // /////////////////////////////////////////////////////////////////
181
182 void
183 HashTbl::remove_all (bool delete_elements)
184 {
185   for (unsigned int i = 0; i < f_num_buckets; i++)
186     if (f_hash_bucket[i] != NULL)
187       {
188         if (delete_elements)
189           f_hash_bucket[i]->remove_all (TRUE);
190         delete f_hash_bucket[i];
191         f_hash_bucket[i] = NULL;
192       }
193 }