mirror of
https://github.com/xzeldon/htop.git
synced 2024-12-23 22:55:46 +00:00
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->buckets = (HashtableItem*) xCalloc(this->size, sizeof(HashtableItem));
|
||||
this->owner = owner;
|
||||
|
||||
assert(Hashtable_isConsistent(this));
|
||||
return this;
|
||||
}
|
||||
|
||||
void Hashtable_delete(Hashtable* this) {
|
||||
|
||||
assert(Hashtable_isConsistent(this));
|
||||
|
||||
if (this->owner) {
|
||||
for (unsigned int i = 0; i < this->size; i++)
|
||||
free(this->buckets[i].value);
|
||||
}
|
||||
Hashtable_clear(this);
|
||||
|
||||
free(this->buckets);
|
||||
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) {
|
||||
unsigned int index = key % this->size;
|
||||
unsigned int probe = 0;
|
||||
|
@ -37,6 +37,8 @@ Hashtable* Hashtable_new(unsigned int size, bool owner);
|
||||
|
||||
void Hashtable_delete(Hashtable* this);
|
||||
|
||||
void Hashtable_clear(Hashtable* this);
|
||||
|
||||
void Hashtable_setSize(Hashtable* this, unsigned int size);
|
||||
|
||||
void Hashtable_put(Hashtable* this, hkey_t key, void* value);
|
||||
|
Loading…
Reference in New Issue
Block a user