mirror of https://github.com/xzeldon/htop.git
Implement Hashtable_clear to empty an existing Hashtable
This commit is contained in:
parent
9549ca8c88
commit
c1563337ae
22
Hashtable.c
22
Hashtable.c
|
@ -98,23 +98,31 @@ Hashtable* Hashtable_new(unsigned int size, bool owner) {
|
||||||
this->size = size ? nextPrime(size) : 13;
|
this->size = size ? nextPrime(size) : 13;
|
||||||
this->buckets = (HashtableItem*) xCalloc(this->size, sizeof(HashtableItem));
|
this->buckets = (HashtableItem*) xCalloc(this->size, sizeof(HashtableItem));
|
||||||
this->owner = owner;
|
this->owner = owner;
|
||||||
|
|
||||||
assert(Hashtable_isConsistent(this));
|
assert(Hashtable_isConsistent(this));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hashtable_delete(Hashtable* this) {
|
void Hashtable_delete(Hashtable* this) {
|
||||||
|
Hashtable_clear(this);
|
||||||
assert(Hashtable_isConsistent(this));
|
|
||||||
|
|
||||||
if (this->owner) {
|
|
||||||
for (unsigned int i = 0; i < this->size; i++)
|
|
||||||
free(this->buckets[i].value);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(this->buckets);
|
free(this->buckets);
|
||||||
free(this);
|
free(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Hashtable_clear(Hashtable* this) {
|
||||||
|
assert(Hashtable_isConsistent(this));
|
||||||
|
|
||||||
|
if (this->owner)
|
||||||
|
for (unsigned int i = 0; i < this->size; i++)
|
||||||
|
free(this->buckets[i].value);
|
||||||
|
|
||||||
|
memset(this->buckets, 0, this->size * sizeof(HashtableItem));
|
||||||
|
this->items = 0;
|
||||||
|
|
||||||
|
assert(Hashtable_isConsistent(this));
|
||||||
|
}
|
||||||
|
|
||||||
static void insert(Hashtable* this, hkey_t key, void* value) {
|
static void insert(Hashtable* this, hkey_t key, void* value) {
|
||||||
unsigned int index = key % this->size;
|
unsigned int index = key % this->size;
|
||||||
unsigned int probe = 0;
|
unsigned int probe = 0;
|
||||||
|
|
|
@ -37,6 +37,8 @@ Hashtable* Hashtable_new(unsigned int size, bool owner);
|
||||||
|
|
||||||
void Hashtable_delete(Hashtable* this);
|
void Hashtable_delete(Hashtable* this);
|
||||||
|
|
||||||
|
void Hashtable_clear(Hashtable* this);
|
||||||
|
|
||||||
void Hashtable_setSize(Hashtable* this, unsigned int size);
|
void Hashtable_setSize(Hashtable* this, unsigned int size);
|
||||||
|
|
||||||
void Hashtable_put(Hashtable* this, hkey_t key, void* value);
|
void Hashtable_put(Hashtable* this, hkey_t key, void* value);
|
||||||
|
|
Loading…
Reference in New Issue