| 1 |
/*************************************************************************** |
| 2 |
hash.cpp - description |
| 3 |
------------------- |
| 4 |
begin : Thu Aug 17 2000 |
| 5 |
copyright : (C) 2000 by Waldemar Baraldi |
| 6 |
email : [email protected] |
| 7 |
***************************************************************************/ |
| 8 |
|
| 9 |
/*************************************************************************** |
| 10 |
* * |
| 11 |
* This program is free software; you can redistribute it and/or modify * |
| 12 |
* it under the terms of the GNU General Public License as published by * |
| 13 |
* the Free Software Foundation; either version 2 of the License, or * |
| 14 |
* (at your option) any later version. * |
| 15 |
* * |
| 16 |
***************************************************************************/ |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include "hash.h" |
| 21 |
#include <stdio.h> |
| 22 |
|
| 23 |
Hash::Bucket::Bucket(Str * str, Object * obj){ |
| 24 |
this->str=str; |
| 25 |
this->obj=obj; |
| 26 |
} |
| 27 |
|
| 28 |
Hash::Bucket::~Bucket(){ |
| 29 |
delete str; |
| 30 |
} |
| 31 |
|
| 32 |
void Hash::Bucket::setStr(Str * str){ |
| 33 |
if (this->str) delete this->str; |
| 34 |
this->str=str; |
| 35 |
} |
| 36 |
|
| 37 |
void Hash::Bucket::setObject(Object * obj){ |
| 38 |
this->obj=obj; |
| 39 |
} |
| 40 |
|
| 41 |
Str * Hash::Bucket::getStr(){ |
| 42 |
return str; |
| 43 |
} |
| 44 |
|
| 45 |
Object * Hash::Bucket::getObject(){ |
| 46 |
return obj; |
| 47 |
} |
| 48 |
|
| 49 |
int Hash::function(Str * str){ |
| 50 |
int i=0, sum=0; |
| 51 |
const char * aux=str->get(); |
| 52 |
|
| 53 |
while (aux[i]!='\0') sum+=aux[i++]; |
| 54 |
|
| 55 |
return(sum % nbuckets); |
| 56 |
} |
| 57 |
|
| 58 |
Hash::Hash(int bs=256){ |
| 59 |
int i; |
| 60 |
nbuckets=bs; |
| 61 |
lbuckets=new (List *)[nbuckets]; |
| 62 |
|
| 63 |
for (i=0;i<nbuckets;i++) |
| 64 |
lbuckets[i]=new List(); |
| 65 |
} |
| 66 |
|
| 67 |
Hash::~Hash(){ |
| 68 |
int i; |
| 69 |
List * list; |
| 70 |
for (i=0;i<nbuckets;i++){ |
| 71 |
list=lbuckets[i]; |
| 72 |
list->empty(false); |
| 73 |
delete list; |
| 74 |
} |
| 75 |
delete[] lbuckets; |
| 76 |
} |
| 77 |
|
| 78 |
Hash::Result Hash::add(Str * str, Object * obj){ |
| 79 |
if (str){ |
| 80 |
// Index * i; |
| 81 |
List * list; |
| 82 |
list=lbuckets[function(str)]; |
| 83 |
if (list->nObjects()>0){ |
| 84 |
/* |
| 85 |
for (i=list->getFirst();i!=list->getEnd();i=list->getNext(i)){ |
| 86 |
if (((Bucket*)list->getObject(i))->getStr()->isEqual(str)){ |
| 87 |
((Bucket*)list->getObject(i))->setObject(obj); |
| 88 |
return AddedOverwrite; |
| 89 |
} |
| 90 |
} |
| 91 |
*/ |
| 92 |
list->insert(list->getFirst(), new Bucket(str, obj)); |
| 93 |
return AddedCollision; |
| 94 |
}else{ |
| 95 |
list->insert(list->getFirst(), new Bucket(str, obj)); |
| 96 |
return AddedNoCollision; |
| 97 |
} |
| 98 |
} |
| 99 |
return NotAdded; |
| 100 |
} |
| 101 |
|
| 102 |
Hash::Result Hash::remove(Str * str, bool del=false){ |
| 103 |
if (str){ |
| 104 |
Index * i; |
| 105 |
List * list=lbuckets[function(str)]; |
| 106 |
|
| 107 |
for (i=list->getFirst();i!=list->getEnd();i=list->getNext(i)) |
| 108 |
if (((Bucket *)list->getObject(i))->getStr()->isEqual(str)){ |
| 109 |
if (del) |
| 110 |
delete ((Bucket *)list->getObject(i))->getObject(); |
| 111 |
list->remove(i, true); |
| 112 |
delete str; |
| 113 |
return Removed; |
| 114 |
} |
| 115 |
delete str; |
| 116 |
} |
| 117 |
return NotFound; |
| 118 |
} |
| 119 |
|
| 120 |
Object * Hash::find(Str * str){ |
| 121 |
if (str){ |
| 122 |
Index * i; |
| 123 |
List * list=lbuckets[function(str)]; |
| 124 |
|
| 125 |
i=list->getFirst(); |
| 126 |
while (i!=list->getEnd()){ |
| 127 |
if (((Bucket *)list->getObject(i))->getStr()->isEqual(str)){ |
| 128 |
delete str; |
| 129 |
return ((Bucket *)list->getObject(i))->getObject(); |
| 130 |
} |
| 131 |
i=list->getNext(i); |
| 132 |
} |
| 133 |
delete str; |
| 134 |
} |
| 135 |
return NULL; |
| 136 |
} |
| 137 |
|
| 138 |
void Hash::empty(bool del=true){ |
| 139 |
int i; |
| 140 |
List * list; |
| 141 |
for (i=0;i<nbuckets;i++){ |
| 142 |
list=lbuckets[i]; |
| 143 |
if (del){ |
| 144 |
Index * idx=list->getFirst(); |
| 145 |
while (idx!=list->getEnd()){ |
| 146 |
delete ((Bucket*)list->getObject(idx))->getObject(); |
| 147 |
idx=list->getNext(idx); |
| 148 |
} |
| 149 |
} |
| 150 |
list->empty(del); |
| 151 |
} |
| 152 |
} |
| 153 |
|