Hashtable.c 4.2 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
/*
htop
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2004-2011 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
8
9
10
11
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>
Hisham Muhammad's avatar
Hisham Muhammad committed
12
#include <assert.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
13
14
15
16
17
18
19
20
21

#include "debug.h"

/*{
typedef struct Hashtable_ Hashtable;

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

typedef struct HashtableItem {
22
   unsigned int key;
Hisham Muhammad's avatar
Hisham Muhammad committed
23
24
25
26
27
28
29
30
31
32
33
34
   void* value;
   struct HashtableItem* next;
} HashtableItem;

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

Hisham Muhammad's avatar
Hisham Muhammad committed
35
36
#ifdef DEBUG

37
static bool Hashtable_isConsistent(Hashtable* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
38
39
40
41
42
43
44
45
46
47
48
   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;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
49
50
51
52
53
54
55
56
57
58
59
60
61
int Hashtable_count(Hashtable* this) {
   int items = 0;
   for (int i = 0; i < this->size; i++) {
      HashtableItem* bucket = this->buckets[i];
      while (bucket) {
         items++;
         bucket = bucket->next;
      }
   }
   assert(items == this->items);
   return items;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
62
63
#endif

64
static HashtableItem* HashtableItem_new(unsigned int key, void* value) {
Hisham Muhammad's avatar
Hisham Muhammad committed
65
66
67
68
69
70
71
72
73
74
75
76
77
   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
78
   this->items = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
79
80
81
   this->size = size;
   this->buckets = (HashtableItem**) calloc(sizeof(HashtableItem*), size);
   this->owner = owner;
Hisham Muhammad's avatar
Hisham Muhammad committed
82
   assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
83
84
85
86
   return this;
}

void Hashtable_delete(Hashtable* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
87
   assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
   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);
}

102
103
void Hashtable_put(Hashtable* this, unsigned int key, void* value) {
   unsigned int index = key % this->size;
Hisham Muhammad's avatar
Hisham Muhammad committed
104
105
106
107
108
109
110
111
112
113
114
115
116
   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
117
   assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
118
119
}

120
121
void* Hashtable_remove(Hashtable* this, unsigned int key) {
   unsigned int index = key % this->size;
Hisham Muhammad's avatar
Hisham Muhammad committed
122
123
124
125
126
127
128
129
130
131
   
   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
132
133
         this->items--;
         if (this->owner) {
Hisham Muhammad's avatar
Hisham Muhammad committed
134
135
            free(value);
            assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
136
137
            return NULL;
         } else {
Hisham Muhammad's avatar
Hisham Muhammad committed
138
139
            assert(Hashtable_isConsistent(this));
            return value;
Hisham Muhammad's avatar
Hisham Muhammad committed
140
         }
Hisham Muhammad's avatar
Hisham Muhammad committed
141
142
143
144
      }
   }
   assert(Hashtable_isConsistent(this));
   return NULL;
Hisham Muhammad's avatar
Hisham Muhammad committed
145
}
Hisham Muhammad's avatar
Hisham Muhammad committed
146

147
148
inline void* Hashtable_get(Hashtable* this, unsigned int key) {
   unsigned int index = key % this->size;
Hisham Muhammad's avatar
Hisham Muhammad committed
149
   HashtableItem* bucketPtr = this->buckets[index];
150
   while (true) {
Hisham Muhammad's avatar
Hisham Muhammad committed
151
      if (bucketPtr == NULL) {
Hisham Muhammad's avatar
Hisham Muhammad committed
152
         assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
153
154
         return NULL;
      } else if (bucketPtr->key == key) {
Hisham Muhammad's avatar
Hisham Muhammad committed
155
         assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
156
157
158
         return bucketPtr->value;
      } else
         bucketPtr = bucketPtr->next;
159
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
160
161
162
}

void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData) {
Hisham Muhammad's avatar
Hisham Muhammad committed
163
   assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
164
165
166
167
168
169
170
   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
171
   assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
172
}