* Added hash_dump_table() function, dumping the whole table.
* Fixed hash_remove_current(): It didn't update "lastElement" and thus always also removed all elements in the same bucket preceding the one to be removed. Also got rid of the useless "for" loop. Fixes #2757. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27689 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
0be39a3a0d
commit
65a9d40a9d
@ -41,6 +41,7 @@ void *hash_next(struct hash_table *table, struct hash_iterator *i);
|
||||
void hash_rewind(struct hash_table *table, struct hash_iterator *i);
|
||||
uint32 hash_count_elements(struct hash_table *table);
|
||||
uint32 hash_count_used_slots(struct hash_table *table);
|
||||
void hash_dump_table(struct hash_table* table);
|
||||
|
||||
/* function pointers must look like this:
|
||||
*
|
||||
|
@ -236,32 +236,34 @@ hash_remove_current(struct hash_table *table, struct hash_iterator *iterator)
|
||||
{
|
||||
uint32 index = iterator->bucket;
|
||||
void *element;
|
||||
void *lastElement = NULL;
|
||||
|
||||
if (iterator->current == NULL)
|
||||
panic("hash_remove_current() called too early.");
|
||||
if (iterator->current == NULL || (element = table->table[index]) == NULL) {
|
||||
panic("hash_remove_current(): invalid iteration state");
|
||||
return;
|
||||
}
|
||||
|
||||
for (element = table->table[index]; index < table->table_size; index++) {
|
||||
void *lastElement = NULL;
|
||||
while (element != NULL) {
|
||||
if (element == iterator->current) {
|
||||
iterator->current = lastElement;
|
||||
|
||||
while (element != NULL) {
|
||||
if (element == iterator->current) {
|
||||
iterator->current = lastElement;
|
||||
|
||||
if (lastElement != NULL) {
|
||||
// connect the previous entry with the next one
|
||||
PUT_IN_NEXT(table, lastElement, NEXT(table, element));
|
||||
} else {
|
||||
table->table[index] = (struct hash_element *)NEXT(table,
|
||||
element);
|
||||
}
|
||||
|
||||
table->num_elements--;
|
||||
return;
|
||||
if (lastElement != NULL) {
|
||||
// connect the previous entry with the next one
|
||||
PUT_IN_NEXT(table, lastElement, NEXT(table, element));
|
||||
} else {
|
||||
table->table[index] = (struct hash_element *)NEXT(table,
|
||||
element);
|
||||
}
|
||||
|
||||
element = NEXT(table, element);
|
||||
table->num_elements--;
|
||||
return;
|
||||
}
|
||||
|
||||
lastElement = element;
|
||||
element = NEXT(table, element);
|
||||
}
|
||||
|
||||
panic("hash_remove_current(): current element not found!");
|
||||
}
|
||||
|
||||
|
||||
@ -408,3 +410,25 @@ hash_count_used_slots(struct hash_table *table)
|
||||
|
||||
return usedSlots;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
hash_dump_table(struct hash_table* table)
|
||||
{
|
||||
uint32 i;
|
||||
|
||||
dprintf("hash table %p, table size: %lu, elements: %u\n", table,
|
||||
table->table_size, table->num_elements);
|
||||
|
||||
for (i = 0; i < table->table_size; i++) {
|
||||
struct hash_element* element = table->table[i];
|
||||
if (element != NULL) {
|
||||
dprintf("%6lu:", i);
|
||||
while (element != NULL) {
|
||||
dprintf(" %p", element);
|
||||
element = NEXT(table, element);
|
||||
}
|
||||
dprintf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user