code reorg and moving files to csql base directory
[csql.git] / src / base / HashMap.cxx
blobb9ae6fc9270f69984536467c73fafbf1297823f0
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 bool HashMap::remove(void *element)
53 int hashVal = 0;
54 HashMapNode *delNode=NULL;
55 HashMapNode *head = NULL, *prev = NULL;
56 if (optGrpIntNoNull) {
57 int value = *(int*)element;
58 hashVal = value % bucketSize;
59 HashMapNode *node = (HashMapNode*) bucket[hashVal];
60 head = node;
61 prev = node;
62 while(node != NULL) {
63 if (*(int*)node->elem == value) {
64 if (head == node) {
65 *(long*)(&(bucket[hashVal])) = (long)node->next;
66 }else {
67 prev->next= node->next;
69 delete node;
70 return true;
72 prev = node;
73 node = node->next;
76 else {
77 hashVal = Util::hashBinary((char*)element, keySize) % bucketSize;
78 HashMapNode *node = (HashMapNode*) bucket[hashVal];
79 head = node; prev = node;
80 while(node != NULL) {
81 if (AllDataType::compareVal(node->elem, element, OpEquals,
82 typeBinary, keySize)) {
83 if (head == node) {
84 *(long*)(&(bucket[hashVal])) = (long)node->next;
85 }else{
86 prev->next = node->next;
88 delete node;
89 return true;
91 prev = node;
92 node = node->next;
95 return false;
98 void HashMap::removeAll()
100 for (int i=0; i <bucketSize; i++) {
101 HashMapNode *node =(HashMapNode*) (bucket[i]);
102 HashMapNode *prev = NULL;
103 while(node != NULL) {
104 prev = node;
105 node = node->next;
106 delete prev;
108 bucket[i]=NULL;
110 return;