2 * Copyright © 2001 Novell, Inc. All Rights Reserved.
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.
10 * FILENAME : hashcls.cpp
11 * DESCRIPTION : Implementation of Equivalent of Hash class, NWPerlHashList and
14 * Author : Srivathsa M
15 * Date Created : July 26 2001
18 #include "nwhashcls.h"
20 NWPerlHashList::NWPerlHashList()
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");
28 NWPerlHashList::~NWPerlHashList()
30 DEBUGPRINT("In destructor\n");
35 NWPerlHashList::insert(void *ldata)
37 HASHNODE *list = new HASHNODE;
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);
49 MemListHash[Bucket] = list;
50 DEBUGPRINT("Inserted first time to %d\n",Bucket);
58 NWPerlHashList::remove(void *ldata)
60 unsigned long Bucket = ((unsigned long)ldata) % BUCKET_SIZE;
61 HASHNODE *list = MemListHash[Bucket];
68 if (list->data != ldata) {
80 //ConsolePrintf ("A:%x;",list->data);
86 MemListHash[Bucket]=next;
88 DEBUGPRINT("Removed element from %d\n",Bucket);
90 ThreadSwitchWithDelay();
91 } while(list && !found);
93 // ConsolePrintf("Couldn;t find %x in Bucket %d\n",ldata,Bucket);
100 void NWPerlHashList::forAll( register void (*user_fn)(void *, void*), void *data ) const
103 for(int i=0; i<BUCKET_SIZE; i++)
105 HASHNODE *next = MemListHash[i];
108 HASHNODE *temp = next->next;
111 DEBUGPRINT("- To remove element from bucket %d\n",i);
112 user_fn( next->data, data );
115 ThreadSwitchWithDelay();
121 void NWPerlHashList::removeAll( ) const
124 for(int i=0; i<BUCKET_SIZE; i++)
126 HASHNODE *next = MemListHash[i];
129 HASHNODE *temp = next->next;
132 ThreadSwitchWithDelay();
139 NWPerlKeyHashList::NWPerlKeyHashList()
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");
147 NWPerlKeyHashList::~NWPerlKeyHashList()
149 DEBUGPRINT("In destructor\n");
154 NWPerlKeyHashList::insert(void *key, void *ldata)
156 KEYHASHNODE *list = new KEYHASHNODE;
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);
169 MemListHash[Bucket] = list;
170 DEBUGPRINT("Inserted first time to %d\n",Bucket);
178 NWPerlKeyHashList::remove(void *key)
180 unsigned long Bucket = ((unsigned long)key) % BUCKET_SIZE;
181 KEYHASHNODE *list = MemListHash[Bucket];
184 KEYHASHNODE *next =list;
185 KEYHASHNODE *prev =NULL;
188 if (list->key != key) {
200 MemListHash[Bucket]=next;
202 DEBUGPRINT("Removed element from %d\n",Bucket);
204 } while(list && !found);
206 // ConsolePrintf("Couldn;t find %x in Bucket %d\n",key,Bucket);
213 void NWPerlKeyHashList::forAll( register void (*user_fn)(void *, void*), void *data ) const
216 for(int i=0; i<BUCKET_SIZE; i++)
218 KEYHASHNODE *next = MemListHash[i];
221 KEYHASHNODE *temp = next->next;
224 DEBUGPRINT("- To remove element from bucket %d\n",i);
225 user_fn( next->data, data );
228 ThreadSwitchWithDelay();
234 int NWPerlKeyHashList::find(void *key,void **pData)
236 for(int i=0; i<BUCKET_SIZE; i++)
238 KEYHASHNODE *next = MemListHash[i];
247 ThreadSwitchWithDelay();
253 void NWPerlKeyHashList::removeAll( ) const
256 for(int i=0; i<BUCKET_SIZE; i++)
258 KEYHASHNODE *next = MemListHash[i];
261 KEYHASHNODE *temp = next->next;
264 ThreadSwitchWithDelay();