Hashtable.c 3.93 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "Hashtable.h"

#include <stdlib.h>
#include <stdbool.h>

#include "debug.h"

/*{
typedef struct Hashtable_ Hashtable;

typedef void(*Hashtable_PairFunction)(int, void*, void*);

typedef struct HashtableItem {
   int key;
   void* value;
   struct HashtableItem* next;
} HashtableItem;

struct Hashtable_ {
   int size;
   HashtableItem** buckets;
   int items;
   bool owner;
};
}*/

Hisham Muhammad's avatar
Hisham Muhammad committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifdef DEBUG

bool Hashtable_isConsistent(Hashtable* this) {
   int items = 0;
   for (int i = 0; i < this->size; i++) {
      HashtableItem* bucket = this->buckets[i];
      while (bucket) {
         items++;
         bucket = bucket->next;
      }
   }
   return items == this->items;
}

#endif

Hisham Muhammad's avatar
Hisham Muhammad committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
HashtableItem* HashtableItem_new(int key, void* value) {
   HashtableItem* this;
   
   this = (HashtableItem*) malloc(sizeof(HashtableItem));
   this->key = key;
   this->value = value;
   this->next = NULL;
   return this;
}

Hashtable* Hashtable_new(int size, bool owner) {
   Hashtable* this;
   
   this = (Hashtable*) malloc(sizeof(Hashtable));
Hisham Muhammad's avatar
Hisham Muhammad committed
64
   this->items = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
65
66
67
   this->size = size;
   this->buckets = (HashtableItem**) calloc(sizeof(HashtableItem*), size);
   this->owner = owner;
Hisham Muhammad's avatar
Hisham Muhammad committed
68
   assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
69
70
71
72
   return this;
}

void Hashtable_delete(Hashtable* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
73
   assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
   for (int i = 0; i < this->size; i++) {
      HashtableItem* walk = this->buckets[i];
      while (walk != NULL) {
         if (this->owner)
            free(walk->value);
         HashtableItem* savedWalk = walk;
         walk = savedWalk->next;
         free(savedWalk);
      }
   }
   free(this->buckets);
   free(this);
}

inline int Hashtable_size(Hashtable* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
89
   assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
90
91
92
93
   return this->items;
}

void Hashtable_put(Hashtable* this, int key, void* value) {
94
   int index = key % this->size;
Hisham Muhammad's avatar
Hisham Muhammad committed
95
96
97
98
99
100
101
102
103
104
105
106
107
   HashtableItem** bucketPtr = &(this->buckets[index]);
   while (true)
      if (*bucketPtr == NULL) {
         *bucketPtr = HashtableItem_new(key, value);
         this->items++;
         break;
      } else if ((*bucketPtr)->key == key) {
         if (this->owner)
            free((*bucketPtr)->value);
         (*bucketPtr)->value = value;
         break;
      } else
         bucketPtr = &((*bucketPtr)->next);
Hisham Muhammad's avatar
Hisham Muhammad committed
108
   assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
109
110
111
}

void* Hashtable_remove(Hashtable* this, int key) {
112
   int index = key % this->size;
Hisham Muhammad's avatar
Hisham Muhammad committed
113
114
115
116
117
118
119
120
121
122
   
   assert(Hashtable_isConsistent(this));

   HashtableItem** bucket; 
   for (bucket = &(this->buckets[index]); *bucket; bucket = &((*bucket)->next) ) {
      if ((*bucket)->key == key) {
         void* value = (*bucket)->value;
         HashtableItem* next = (*bucket)->next;
         free(*bucket);
         (*bucket) = next;
Hisham Muhammad's avatar
Hisham Muhammad committed
123
124
         this->items--;
         if (this->owner) {
Hisham Muhammad's avatar
Hisham Muhammad committed
125
126
            free(value);
            assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
127
128
            return NULL;
         } else {
Hisham Muhammad's avatar
Hisham Muhammad committed
129
130
            assert(Hashtable_isConsistent(this));
            return value;
Hisham Muhammad's avatar
Hisham Muhammad committed
131
         }
Hisham Muhammad's avatar
Hisham Muhammad committed
132
133
134
135
      }
   }
   assert(Hashtable_isConsistent(this));
   return NULL;
Hisham Muhammad's avatar
Hisham Muhammad committed
136
}
Hisham Muhammad's avatar
Hisham Muhammad committed
137

Hisham Muhammad's avatar
Hisham Muhammad committed
138
inline void* Hashtable_get(Hashtable* this, int key) {
139
   int index = key % this->size;
Hisham Muhammad's avatar
Hisham Muhammad committed
140
   HashtableItem* bucketPtr = this->buckets[index];
141
   while (true) {
Hisham Muhammad's avatar
Hisham Muhammad committed
142
      if (bucketPtr == NULL) {
Hisham Muhammad's avatar
Hisham Muhammad committed
143
         assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
144
145
         return NULL;
      } else if (bucketPtr->key == key) {
Hisham Muhammad's avatar
Hisham Muhammad committed
146
         assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
147
148
149
         return bucketPtr->value;
      } else
         bucketPtr = bucketPtr->next;
150
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
151
152
153
}

void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData) {
Hisham Muhammad's avatar
Hisham Muhammad committed
154
   assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
155
156
157
158
159
160
161
   for (int i = 0; i < this->size; i++) {
      HashtableItem* walk = this->buckets[i];
      while (walk != NULL) {
         f(walk->key, walk->value, userData);
         walk = walk->next;
      }
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
162
   assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
163
}