2006-03-04 18:16:49 +00:00
|
|
|
/*
|
2011-12-26 21:35:57 +00:00
|
|
|
htop - Hashtable.c
|
2011-05-26 16:35:07 +00:00
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
2020-10-05 07:51:32 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
2006-03-04 18:16:49 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Hashtable.h"
|
2020-10-14 22:56:22 +00:00
|
|
|
#include "XUtils.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2006-11-08 21:49:07 +00:00
|
|
|
#include <assert.h>
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
|
2020-09-17 20:27:33 +00:00
|
|
|
#ifndef NDEBUG
|
2006-11-08 20:09:48 +00:00
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static bool Hashtable_isConsistent(Hashtable* this) {
|
2006-11-08 20:09:48 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-11-12 21:52:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-17 20:27:33 +00:00
|
|
|
#endif /* NDEBUG */
|
2006-11-08 20:09:48 +00:00
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static HashtableItem* HashtableItem_new(unsigned int key, void* value) {
|
2006-03-04 18:16:49 +00:00
|
|
|
HashtableItem* this;
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2016-02-02 14:53:02 +00:00
|
|
|
this = xMalloc(sizeof(HashtableItem));
|
2006-03-04 18:16:49 +00:00
|
|
|
this->key = key;
|
|
|
|
this->value = value;
|
|
|
|
this->next = NULL;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hashtable* Hashtable_new(int size, bool owner) {
|
|
|
|
Hashtable* this;
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2016-02-02 14:53:02 +00:00
|
|
|
this = xMalloc(sizeof(Hashtable));
|
2006-11-08 20:09:48 +00:00
|
|
|
this->items = 0;
|
2006-03-04 18:16:49 +00:00
|
|
|
this->size = size;
|
2016-02-02 14:53:02 +00:00
|
|
|
this->buckets = (HashtableItem**) xCalloc(size, sizeof(HashtableItem*));
|
2006-03-04 18:16:49 +00:00
|
|
|
this->owner = owner;
|
2006-11-08 20:09:48 +00:00
|
|
|
assert(Hashtable_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Hashtable_delete(Hashtable* this) {
|
2006-11-08 20:09:48 +00:00
|
|
|
assert(Hashtable_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
for (int i = 0; i < this->size; i++) {
|
|
|
|
HashtableItem* walk = this->buckets[i];
|
|
|
|
while (walk != NULL) {
|
|
|
|
if (this->owner)
|
|
|
|
free(walk->value);
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
HashtableItem* savedWalk = walk;
|
|
|
|
walk = savedWalk->next;
|
|
|
|
free(savedWalk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(this->buckets);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2007-04-05 19:53:23 +00:00
|
|
|
void Hashtable_put(Hashtable* this, unsigned int key, void* value) {
|
|
|
|
unsigned int index = key % this->size;
|
2006-03-04 18:16:49 +00:00
|
|
|
HashtableItem** bucketPtr = &(this->buckets[index]);
|
|
|
|
while (true)
|
|
|
|
if (*bucketPtr == NULL) {
|
|
|
|
*bucketPtr = HashtableItem_new(key, value);
|
|
|
|
this->items++;
|
|
|
|
break;
|
|
|
|
} else if ((*bucketPtr)->key == key) {
|
2020-10-08 20:48:35 +00:00
|
|
|
if (this->owner && (*bucketPtr)->value != value)
|
2006-03-04 18:16:49 +00:00
|
|
|
free((*bucketPtr)->value);
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
(*bucketPtr)->value = value;
|
|
|
|
break;
|
2020-11-01 00:09:51 +00:00
|
|
|
} else {
|
2006-03-04 18:16:49 +00:00
|
|
|
bucketPtr = &((*bucketPtr)->next);
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2006-11-08 20:09:48 +00:00
|
|
|
assert(Hashtable_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2007-04-05 19:53:23 +00:00
|
|
|
void* Hashtable_remove(Hashtable* this, unsigned int key) {
|
|
|
|
unsigned int index = key % this->size;
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2006-11-08 20:09:48 +00:00
|
|
|
assert(Hashtable_isConsistent(this));
|
|
|
|
|
2019-10-31 16:39:12 +00:00
|
|
|
HashtableItem** bucket;
|
2006-11-08 20:09:48 +00:00
|
|
|
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;
|
2006-03-04 18:16:49 +00:00
|
|
|
this->items--;
|
|
|
|
if (this->owner) {
|
2006-11-08 20:09:48 +00:00
|
|
|
free(value);
|
|
|
|
assert(Hashtable_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
return NULL;
|
|
|
|
} else {
|
2006-11-08 20:09:48 +00:00
|
|
|
assert(Hashtable_isConsistent(this));
|
|
|
|
return value;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2006-11-08 20:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(Hashtable_isConsistent(this));
|
|
|
|
return NULL;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2006-11-08 20:09:48 +00:00
|
|
|
|
2020-09-17 20:27:33 +00:00
|
|
|
void* Hashtable_get(Hashtable* this, unsigned int key) {
|
2007-04-05 19:53:23 +00:00
|
|
|
unsigned int index = key % this->size;
|
2006-03-04 18:16:49 +00:00
|
|
|
HashtableItem* bucketPtr = this->buckets[index];
|
2006-07-11 06:13:32 +00:00
|
|
|
while (true) {
|
2006-03-04 18:16:49 +00:00
|
|
|
if (bucketPtr == NULL) {
|
2006-11-08 20:09:48 +00:00
|
|
|
assert(Hashtable_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
return NULL;
|
|
|
|
} else if (bucketPtr->key == key) {
|
2006-11-08 20:09:48 +00:00
|
|
|
assert(Hashtable_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
return bucketPtr->value;
|
2020-11-01 00:09:51 +00:00
|
|
|
} else {
|
2006-03-04 18:16:49 +00:00
|
|
|
bucketPtr = bucketPtr->next;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2006-07-11 06:13:32 +00:00
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData) {
|
2006-11-08 20:09:48 +00:00
|
|
|
assert(Hashtable_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2006-11-08 20:09:48 +00:00
|
|
|
assert(Hashtable_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|