Hashtable.c 4.17 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
/*
Hisham Muhammad's avatar
Hisham Muhammad committed
2
htop - Hashtable.c
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
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "Hashtable.h"

#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
11
#include <assert.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
12
13

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
14
15
#include <stdbool.h>

Hisham Muhammad's avatar
Hisham Muhammad committed
16
17
18
19
20
typedef struct Hashtable_ Hashtable;

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

typedef struct HashtableItem {
21
   unsigned int key;
Hisham Muhammad's avatar
Hisham Muhammad committed
22
23
24
25
26
27
28
29
30
31
32
33
   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
#ifdef DEBUG

36
static bool Hashtable_isConsistent(Hashtable* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
37
38
39
40
41
42
43
44
45
46
47
   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
48
49
50
51
52
53
54
55
56
57
58
59
60
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
61
62
#endif

63
static HashtableItem* HashtableItem_new(unsigned int key, void* value) {
Hisham Muhammad's avatar
Hisham Muhammad committed
64
65
   HashtableItem* this;
   
66
   this = malloc(sizeof(HashtableItem));
Hisham Muhammad's avatar
Hisham Muhammad committed
67
68
69
70
71
72
73
74
75
   this->key = key;
   this->value = value;
   this->next = NULL;
   return this;
}

Hashtable* Hashtable_new(int size, bool owner) {
   Hashtable* this;
   
76
   this = malloc(sizeof(Hashtable));
Hisham Muhammad's avatar
Hisham Muhammad committed
77
   this->items = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
78
   this->size = size;
79
   this->buckets = (HashtableItem**) calloc(size, sizeof(HashtableItem*));
Hisham Muhammad's avatar
Hisham Muhammad committed
80
   this->owner = owner;
Hisham Muhammad's avatar
Hisham Muhammad committed
81
   assert(Hashtable_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
82
83
84
85
   return this;
}

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

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

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

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

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