Glossygloss  0.2
Glossygloss is set of classes to use several data structure as Tree or hash table.
 All Classes Files Functions Variables Macros Pages
hashtable.hpp
Go to the documentation of this file.
1 
38 #ifndef HASHTABLE_HPP
39 #define HASHTABLE_HPP
40 
42 #ifndef END
43 #define END nullptr
44 #endif
45 
47 #ifndef ARRAYSIZE
48 #define ARRAYSIZE 25
49 #endif
50 
51 // included files section
52 #include <string>
53 #include <cassert>
54 #include <utility>
55 #include <forward_list>
56 
57 // will desactive cassert
58 #define NDEBUG
59 
60 using std::string;
61 using std::pair;
62 using std::forward_list;
63 
75 template <typename K> unsigned computehash(K element);
76 
80 class HashtableException : std::exception {
81  private:
82  const char* _cause;
83  public:
88  HashtableException(const char* cause):
89  _cause(cause)
90  {}
91 
95  virtual ~HashtableException() throw(){
96  // do nothing
97  }
98 
102  virtual const char* what()const throw(){
103  return _cause;
104  }
105 };
106 
112 // TODO: implement iterator to browse within alveoles very quickly and easely
113 template <typename K, typename V>
114 class Alveole{
115  private:
116  K _key;
120  public:
125  Alveole(const Alveole<K,V> &other):
126  _key(other._key),
127  _value(other._value)
128  {
129  // if there are elements coming next
130  if(END != other._next){
131  _next = new Alveole(other._next);
132  }
133  else {
134  _next = END;
135  }
136  }
137 
142  Alveole(K key, V value) :
143  _key(key), _value(value),
144  _next(END)
145  {}
146 
150  Alveole() : _next(END){};
151 
157  Alveole(K key, V value, Alveole<K,V>* next):
158  _key(key),
159  _value(value),
160  _next(next)
161  {}
162 
166  //delete &_key;
167  //delete &_value;
168  //delete _next;
169  }
170 
174  K getKey(){ return _key; }
175 
179  V getValue(){ return _value; }
180 
184  Alveole<K,V>* getNext(){ return _next; }
185 
189  void setValue(V n_value){ _value = n_value; }
190 
194  void setNext(Alveole<K,V>* n_next){ _next = n_next; }
195 
199  string toString(){
200  string desc = "{" + _key + ", " + _value + "}";
201  if(END == _next){
202  return desc;
203  } else {
204  return desc + ", " + _next->toString();
205  }
206  }
207 };
208 
211 template <typename K, typename V>
212 class Hashtable {
213 
214  private:
217  public:
221  _table = new Alveole<K,V>*[ARRAYSIZE];
222  for(int i = 0; i<ARRAYSIZE; ++i){
223  _table[i] = END;
224  }
225  }
226 
230  delete[] _table;
231  }
232 
237  bool contains(const K &key){
238  bool here = false;
239  int index = computehash<K>(key)%ARRAYSIZE;
240  assert(index>=0);
241  assert(index<ARRAYSIZE);
242  Alveole<K,V>* browser = _table[index];
243  while(not here and END != browser){
244  if(key == browser->getKey()){
245  here = true;
246  }
247  browser = browser->getNext();
248  }
249  return here;
250  }
251 
257  V get(const K &key){
258  int index = computehash<K>(key)%ARRAYSIZE;
259  assert(index>=0);
260  assert(index<ARRAYSIZE);
261  Alveole<K,V>* browser = _table[index];
262  bool undone = true;
263  while(undone and END != browser){
264  if(key == browser->getKey()){
265  undone = false;
266  }
267  else {
268  browser = browser->getNext();
269  }
270  }
271  if(undone){
272  throw HashtableException("Key not found!");
273  } else {
274  return browser->getValue();
275  }
276  }
277 
281  bool isEmpty(){
282  bool empty = true;
283  int i = 0;
284  while(empty and i<ARRAYSIZE){
285  empty = (END == _table[i++]);
286  }
287  return empty;
288  }
289 
295  // TODO: optimize this fucking code
296  void put(K key, V value){
297  // where to put the pair ?
298  int index = computehash<K>(key)%ARRAYSIZE;
299  assert(index>=0);
300  assert(index<ARRAYSIZE);
301  // browse alveoles at this place
302  Alveole<K,V>* browser = _table[index];
303  bool undone = true;
304  while(undone and END != browser){
305  if(key == browser->getKey()){
306  //std::cout << "Updating (" << key << ", " << value <<") at " << index << std::endl; // debug line
307  browser->setValue(value);
308  undone = false;
309  }
310  browser = browser->getNext();
311  }
312  if(undone){
313  _table[index] = new Alveole<K,V>(key, value, _table[index]);
314  //std::cout << "Adding (" << key << ", " << value <<") at " << index << std::endl; // debug line
315  }
316  }
317 
322  void remove(const K &key){
324  int index = computehash<K>(key)%ARRAYSIZE;
325  assert(index>=0);
326  assert(index<ARRAYSIZE);
327  Alveole<K,V>* bef = new Alveole<K,V>();
328  bef->setNext(_table[index]);
329  Alveole<K,V>* cur = _table[index];
330  bool undone = true;
331  while(undone and END != cur){
332  //std::cout << cur->toString() << std::endl; // debug line
333  if(key == cur->getKey()){
334  bef->setNext(cur->getNext());
335  assert(bef->getNext() == cur->getNext());
336  undone = false;
337  delete cur;
338  }
339  else {
340  assert(cur != END);
341  bef = cur;
342  cur = cur->getNext();
343  }
344  }
345  if(undone) throw HashtableException("Key is not here!");
346  }
347 
352  string toString(){
353  string desc = "[";
354  for(int i = 0; i<ARRAYSIZE; ++i){
355  if(END != _table[i]){
356  desc.size()<2?desc += (_table[i]->toString()):desc += ", " + (_table[i]->toString());
357  }
358  }
359  return desc + "]";
360  }
361 
365  void getPairs(forward_list<pair<string, int>> &pairs){
366  // we can browse Alveole with it
367  Alveole<K, V>* browser;
368  // For each cell in _table
369  for(int i=0; i<ARRAYSIZE; ++i){
370  // browse alveoli here
371  browser = _table[i];
372  while(END != browser){
373  pairs.push_front(pair<string, int>(browser->getKey(), browser->getValue()));
374  browser = browser->getNext();
375  }
376  }
377  }
378 
379 };
380 
381 #endif // HASHTABLE_HPP
virtual const char * what() const
Definition: hashtable.hpp:102
Maps a key to a value.
Definition: hashtable.hpp:212
virtual ~HashtableException()
Definition: hashtable.hpp:95
string toString()
Definition: hashtable.hpp:352
string toString()
Definition: hashtable.hpp:199
~Alveole()
Definition: hashtable.hpp:165
Alveole(K key, V value)
Definition: hashtable.hpp:142
~Hashtable()
Definition: hashtable.hpp:229
Alveole()
Definition: hashtable.hpp:150
bool contains(const K &key)
Definition: hashtable.hpp:237
Class to define Hashtable alveoles.
Definition: hashtable.hpp:114
const char * _cause
Definition: hashtable.hpp:82
Alveole< K, V > * getNext()
Definition: hashtable.hpp:184
Alveole(K key, V value, Alveole< K, V > *next)
Definition: hashtable.hpp:157
K _key
Definition: hashtable.hpp:116
void getPairs(forward_list< pair< string, int >> &pairs)
Definition: hashtable.hpp:365
K getKey()
Definition: hashtable.hpp:174
Exception class to manage Hashtable errors.
Definition: hashtable.hpp:80
#define K
Definition: sample_hashtable.cpp:40
#define V
Definition: sample_hashtable.cpp:41
V _value
Definition: hashtable.hpp:117
#define END
macro to define end of alveole chains
Definition: hashtable.hpp:43
void setNext(Alveole< K, V > *n_next)
Definition: hashtable.hpp:194
#define ARRAYSIZE
macro to define size of hash arrays
Definition: hashtable.hpp:48
Alveole< K, V > ** _table
Definition: hashtable.hpp:215
unsigned computehash(K element)
HashtableException(const char *cause)
Definition: hashtable.hpp:88
void put(K key, V value)
Definition: hashtable.hpp:296
Hashtable()
Definition: hashtable.hpp:220
unsigned computehash< K >(K element)
Definition: sample_hashtable.cpp:46
Alveole< K, V > * _next
Definition: hashtable.hpp:118
void setValue(V n_value)
Definition: hashtable.hpp:189
bool isEmpty()
Definition: hashtable.hpp:281
Alveole(const Alveole< K, V > &other)
Definition: hashtable.hpp:125
V getValue()
Definition: hashtable.hpp:179