| 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++)
|
|---|
|
|---|