code reorganization phase - I
[csql.git] / src / storage / HashMap.cxx
blob9953aaa18aa63680701805e9204c00427a3a06c3
1 #include<HeapAllocator.h>
2 #include<ErrorType.h>
3 #include<Debug.h>
4 #include <Util.h>
5 #include <DataType.h>
7 DbRetVal HashMap::insert(void *element)
9 HashMapNode *newNode = new HashMapNode();
10 newNode->elem = element;
11 newNode->next = NULL;
12 int hashVal =0;
13 if (optGrpIntNoNull)
14 hashVal = (*(int*)element) % bucketSize;
15 else
16 hashVal = Util::hashBinary((char*)element, keySize) % bucketSize;
17 HashMapNode *node = (HashMapNode*) bucket[hashVal];
18 if (NULL == node)
20 bucket[hashVal] = newNode;
21 return OK;
23 while(node->next != NULL) { node=node->next; }
24 node->next = newNode;
25 return OK;
27 void* HashMap::find(void *element)
29 int hashVal = 0;
30 if (optGrpIntNoNull) {
31 int value = *(int*)element;
32 hashVal = value % bucketSize;
33 HashMapNode *node = (HashMapNode*) bucket[hashVal];
34 while(node != NULL) {
35 if (*(int*)node->elem == value) return node->elem;
36 node = node->next;
39 else {
40 hashVal = Util::hashBinary((char*)element, keySize) % bucketSize;
41 HashMapNode *node = (HashMapNode*) bucket[hashVal];
42 while(node != NULL) {
43 if (AllDataType::compareVal(node->elem, element, OpEquals,
44 typeBinary, keySize))
45 return node->elem;
46 node = node->next;
49 return NULL;
51 void HashMap::removeAll()
53 for (int i=0; i <bucketSize; i++) {
54 HashMapNode *node =(HashMapNode*) (bucket[i]);
55 HashMapNode *prev = NULL;
56 while(node != NULL) {
57 prev = node;
58 node = node->next;
59 delete prev;
61 bucket[i]=NULL;
63 return;