From 279ad0515094c36cf70f6e84b721265cedd22da8 Mon Sep 17 00:00:00 2001 From: uz Date: Sat, 11 Jun 2011 18:17:54 +0000 Subject: [PATCH] Added routines to remove an entry from the hash table. git-svn-id: svn://svn.cc65.org/cc65/trunk@5048 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/common/hashtab.c | 52 ++++++++++++++++++++++++++++++++++++++++---- src/common/hashtab.h | 14 ++++++++---- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/common/hashtab.c b/src/common/hashtab.c index 4bfa05689..a96dc1652 100644 --- a/src/common/hashtab.c +++ b/src/common/hashtab.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2003 Ullrich von Bassewitz */ -/* Römerstrasse 52 */ -/* D-70794 Filderstadt */ -/* EMail: uz@cc65.org */ +/* (C) 2003-2011, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -34,6 +34,7 @@ /* common */ +#include "check.h" #include "hashtab.h" #include "xmalloc.h" @@ -164,6 +165,34 @@ void HT_Insert (HashTable* T, HashNode* N) +void HT_Remove (HashNode* N) +/* Remove a node from a hash table. */ +{ + /* Get the table from the node */ + HashTable* T = N->Owner; + + /* Calculate the reduced hash, which is also the slot number */ + unsigned Slot = N->Hash % T->Slots; + + /* Remove the entry from the single linked list */ + HashNode** Q = &T->Table[Slot]; + while (1) { + /* If the pointer is NULL, the node is not in the table which we will + * consider a serious error. + */ + CHECK (*Q != 0); + if (*Q == N) { + /* Found - remove it */ + *Q = N->Next; + break; + } + /* Next node */ + Q = &(*Q)->Next; + } +} + + + void HT_InsertEntry (HashTable* T, void* Entry) /* Insert an entry into the given hash table */ { @@ -172,6 +201,21 @@ void HT_InsertEntry (HashTable* T, void* Entry) +void HT_RemoveEntry (HashTable* T, void* Entry) +/* Remove an entry from the given hash table */ +{ + /* Get the node from the entry */ + HashNode* N = T->Func->GetHashNode (Entry); + + /* Make sure the entry is actually in the given table */ + CHECK (N->Owner == T); + + /* Remove the node */ + HT_Remove (N); +} + + + void HT_Walk (HashTable* T, void (*F) (void* Entry, void* Data), void* Data) /* Walk over all nodes of a hash table. For each node, the user supplied * function F is called, passing a pointer to the entry, and the data pointer diff --git a/src/common/hashtab.h b/src/common/hashtab.h index 4e772fa7f..d5249a4c1 100644 --- a/src/common/hashtab.h +++ b/src/common/hashtab.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2003-2008 Ullrich von Bassewitz */ -/* Roemerstrasse 52 */ -/* D-70794 Filderstadt */ -/* EMail: uz@cc65.org */ +/* (C) 2003-2011, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -196,9 +196,15 @@ void* HT_FindEntry (const HashTable* T, const void* Key); void HT_Insert (HashTable* T, HashNode* N); /* Insert a node into the given hash table */ +void HT_Remove (HashNode* N); +/* Remove a node from its hash table */ + void HT_InsertEntry (HashTable* T, void* Entry); /* Insert an entry into the given hash table */ +void HT_RemoveEntry (HashTable* T, void* Entry); +/* Remove an entry from the given hash table */ + void HT_Walk (HashTable* T, void (*F) (void* Entry, void* Data), void* Data); /* Walk over all nodes of a hash table. For each node, the user supplied * function F is called, passing a pointer to the entry, and the data pointer