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