1 | /*
|
---|
2 | * Copyright © 2001 Novell, Inc. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * You may distribute under the terms of either the GNU General Public
|
---|
5 | * License or the Artistic License, as specified in the README file.
|
---|
6 | *
|
---|
7 | */
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * FILENAME : hashcls.cpp
|
---|
11 | * DESCRIPTION : Implementation of Equivalent of Hash class, NWPerlHashList and
|
---|
12 | NWPerlKeyHashList
|
---|
13 | *
|
---|
14 | * Author : Srivathsa M
|
---|
15 | * Date Created : July 26 2001
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "nwhashcls.h"
|
---|
19 |
|
---|
20 | NWPerlHashList::NWPerlHashList()
|
---|
21 | {
|
---|
22 | //initialize the hash list to null
|
---|
23 | for(int i=0;i<BUCKET_SIZE;i++)
|
---|
24 | MemListHash[i] = NULL;
|
---|
25 | DEBUGPRINT("In constructor\n");
|
---|
26 | }
|
---|
27 |
|
---|
28 | NWPerlHashList::~NWPerlHashList()
|
---|
29 | {
|
---|
30 | DEBUGPRINT("In destructor\n");
|
---|
31 | removeAll();
|
---|
32 | }
|
---|
33 |
|
---|
34 | int
|
---|
35 | NWPerlHashList::insert(void *ldata)
|
---|
36 | {
|
---|
37 | HASHNODE *list = new HASHNODE;
|
---|
38 | if (list) {
|
---|
39 | list->data = ldata;
|
---|
40 | list->next = NULL;
|
---|
41 | unsigned long Bucket = ((unsigned long)ldata) % BUCKET_SIZE;
|
---|
42 | if (MemListHash[Bucket]) {
|
---|
43 | //Elements existing, insert at the beginning
|
---|
44 | list->next = MemListHash[Bucket];
|
---|
45 | MemListHash[Bucket] = list;
|
---|
46 | DEBUGPRINT("Inserted to %d\n",Bucket);
|
---|
47 | } else {
|
---|
48 | //First element
|
---|
49 | MemListHash[Bucket] = list;
|
---|
50 | DEBUGPRINT("Inserted first time to %d\n",Bucket);
|
---|
51 | }
|
---|
52 | return 1;
|
---|
53 | } else
|
---|
54 | return 0;
|
---|
55 | }
|
---|
56 |
|
---|
57 | int
|
---|
58 | NWPerlHashList::remove(void *ldata)
|
---|
59 | {
|
---|
60 | unsigned long Bucket = ((unsigned long)ldata) % BUCKET_SIZE;
|
---|
61 | HASHNODE *list = MemListHash[Bucket];
|
---|
62 | if (list) {
|
---|
63 | int found = 0;
|
---|
64 | HASHNODE *next =list;
|
---|
65 | HASHNODE *prev =NULL;
|
---|
66 | do
|
---|
67 | {
|
---|
68 | if (list->data != ldata) {
|
---|
69 | prev = list;
|
---|
70 | list = list->next;
|
---|
71 | }
|
---|
72 | else {
|
---|
73 | found = 1;
|
---|
74 | next = list->next;
|
---|
75 | /*if(list->data)
|
---|
76 | {
|
---|
77 | free(list->data);
|
---|
78 | list->data = NULL;
|
---|
79 | }*/
|
---|
80 | //ConsolePrintf ("A:%x;",list->data);
|
---|
81 | delete list;
|
---|
82 | list = NULL;
|
---|
83 | if (prev) {
|
---|
84 | prev->next = next;
|
---|
85 | } else {
|
---|
86 | MemListHash[Bucket]=next;
|
---|
87 | }
|
---|
88 | DEBUGPRINT("Removed element from %d\n",Bucket);
|
---|
89 | }
|
---|
90 | ThreadSwitchWithDelay();
|
---|
91 | } while(list && !found);
|
---|
92 | // if (!found)
|
---|
93 | // ConsolePrintf("Couldn;t find %x in Bucket %d\n",ldata,Bucket);
|
---|
94 | return(found);
|
---|
95 | }
|
---|
96 | return 1;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | void NWPerlHashList::forAll( register void (*user_fn)(void *, void*), void *data ) const
|
---|
101 | {
|
---|
102 |
|
---|
103 | for(int i=0; i<BUCKET_SIZE; i++)
|
---|
104 | {
|
---|
105 | HASHNODE *next = MemListHash[i];
|
---|
106 | while(next)
|
---|
107 | {
|
---|
108 | HASHNODE *temp = next->next;
|
---|
109 | if(next->data)
|
---|
110 | {
|
---|
111 | DEBUGPRINT("- To remove element from bucket %d\n",i);
|
---|
112 | user_fn( next->data, data );
|
---|
113 | }
|
---|
114 | next = temp;
|
---|
115 | ThreadSwitchWithDelay();
|
---|
116 | }
|
---|
117 | }
|
---|
118 | return ;
|
---|
119 | };
|
---|
120 |
|
---|
121 | void NWPerlHashList::removeAll( ) const
|
---|
122 | {
|
---|
123 |
|
---|
124 | for(int i=0; i<BUCKET_SIZE; i++)
|
---|
125 | {
|
---|
126 | HASHNODE *next = MemListHash[i];
|
---|
127 | while(next)
|
---|
128 | {
|
---|
129 | HASHNODE *temp = next->next;
|
---|
130 | delete next;
|
---|
131 | next = temp;
|
---|
132 | ThreadSwitchWithDelay();
|
---|
133 | }
|
---|
134 | }
|
---|
135 | return ;
|
---|
136 | };
|
---|
137 |
|
---|
138 | /**
|
---|
139 | NWPerlKeyHashList::NWPerlKeyHashList()
|
---|
140 | {
|
---|
141 | //initialize the hash list to null
|
---|
142 | for(int i=0;i<BUCKET_SIZE;i++)
|
---|
143 | MemListHash[i] = NULL;
|
---|
144 | DEBUGPRINT("In constructor\n");
|
---|
145 | }
|
---|
146 |
|
---|
147 | NWPerlKeyHashList::~NWPerlKeyHashList()
|
---|
148 | {
|
---|
149 | DEBUGPRINT("In destructor\n");
|
---|
150 | removeAll();
|
---|
151 | }
|
---|
152 |
|
---|
153 | int
|
---|
154 | NWPerlKeyHashList::insert(void *key, void *ldata)
|
---|
155 | {
|
---|
156 | KEYHASHNODE *list = new KEYHASHNODE;
|
---|
157 | if (list) {
|
---|
158 | list->key = key;
|
---|
159 | list->data = ldata;
|
---|
160 | list->next = NULL;
|
---|
161 | unsigned long Bucket = ((unsigned long)key) % BUCKET_SIZE;
|
---|
162 | if (MemListHash[Bucket]) {
|
---|
163 | //Elements existing, insert at the beginning
|
---|
164 | list->next = MemListHash[Bucket];
|
---|
165 | MemListHash[Bucket] = list;
|
---|
166 | DEBUGPRINT("Inserted to %d\n",Bucket);
|
---|
167 | } else {
|
---|
168 | //First element
|
---|
169 | MemListHash[Bucket] = list;
|
---|
170 | DEBUGPRINT("Inserted first time to %d\n",Bucket);
|
---|
171 | }
|
---|
172 | return 1;
|
---|
173 | } else
|
---|
174 | return 0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | int
|
---|
178 | NWPerlKeyHashList::remove(void *key)
|
---|
179 | {
|
---|
180 | unsigned long Bucket = ((unsigned long)key) % BUCKET_SIZE;
|
---|
181 | KEYHASHNODE *list = MemListHash[Bucket];
|
---|
182 | if (list) {
|
---|
183 | int found = 0;
|
---|
184 | KEYHASHNODE *next =list;
|
---|
185 | KEYHASHNODE *prev =NULL;
|
---|
186 | do
|
---|
187 | {
|
---|
188 | if (list->key != key) {
|
---|
189 | prev = list;
|
---|
190 | list = list->next;
|
---|
191 | }
|
---|
192 | else {
|
---|
193 | found = 1;
|
---|
194 | next = list->next;
|
---|
195 | delete list;
|
---|
196 | list = NULL;
|
---|
197 | if (prev) {
|
---|
198 | prev->next = next;
|
---|
199 | } else {
|
---|
200 | MemListHash[Bucket]=next;
|
---|
201 | }
|
---|
202 | DEBUGPRINT("Removed element from %d\n",Bucket);
|
---|
203 | }
|
---|
204 | } while(list && !found);
|
---|
205 | // if (!found)
|
---|
206 | // ConsolePrintf("Couldn;t find %x in Bucket %d\n",key,Bucket);
|
---|
207 | return(found);
|
---|
208 | }
|
---|
209 | return 1;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | void NWPerlKeyHashList::forAll( register void (*user_fn)(void *, void*), void *data ) const
|
---|
214 | {
|
---|
215 |
|
---|
216 | for(int i=0; i<BUCKET_SIZE; i++)
|
---|
217 | {
|
---|
218 | KEYHASHNODE *next = MemListHash[i];
|
---|
219 | while(next)
|
---|
220 | {
|
---|
221 | KEYHASHNODE *temp = next->next;
|
---|
222 | if(next->data)
|
---|
223 | {
|
---|
224 | DEBUGPRINT("- To remove element from bucket %d\n",i);
|
---|
225 | user_fn( next->data, data );
|
---|
226 | }
|
---|
227 | next = temp;
|
---|
228 | ThreadSwitchWithDelay();
|
---|
229 | }
|
---|
230 | }
|
---|
231 | return ;
|
---|
232 | };
|
---|
233 |
|
---|
234 | int NWPerlKeyHashList::find(void *key,void **pData)
|
---|
235 | {
|
---|
236 | for(int i=0; i<BUCKET_SIZE; i++)
|
---|
237 | {
|
---|
238 | KEYHASHNODE *next = MemListHash[i];
|
---|
239 | while(next)
|
---|
240 | {
|
---|
241 | if(next->key==key)
|
---|
242 | {
|
---|
243 | *pData=next->data;
|
---|
244 | return 1;
|
---|
245 | }
|
---|
246 | next = next->next;
|
---|
247 | ThreadSwitchWithDelay();
|
---|
248 | }
|
---|
249 | }
|
---|
250 | return 0;
|
---|
251 | }
|
---|
252 |
|
---|
253 | void NWPerlKeyHashList::removeAll( ) const
|
---|
254 | {
|
---|
255 |
|
---|
256 | for(int i=0; i<BUCKET_SIZE; i++)
|
---|
257 | {
|
---|
258 | KEYHASHNODE *next = MemListHash[i];
|
---|
259 | while(next)
|
---|
260 | {
|
---|
261 | KEYHASHNODE *temp = next->next;
|
---|
262 | delete next;
|
---|
263 | next = temp;
|
---|
264 | ThreadSwitchWithDelay();
|
---|
265 | }
|
---|
266 | }
|
---|
267 | return ;
|
---|
268 | };
|
---|
269 | **/
|
---|