]> git.sur5r.net Git - cc65/commitdiff
Added routines to remove an entry from the hash table.
authoruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sat, 11 Jun 2011 18:17:54 +0000 (18:17 +0000)
committeruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sat, 11 Jun 2011 18:17:54 +0000 (18:17 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@5048 b7a2c559-68d2-44c3-8de9-860c34a00d81

src/common/hashtab.c
src/common/hashtab.h

index 4bfa05689ae3f0d93fac692e174a4d7c0f309cf0..a96dc16527465f18ffb00eb8aa79cdcd43849610 100644 (file)
@@ -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
index 4e772fa7fe05efe36c1d4aa9bf9b93206f903b37..d5249a4c1711178292160bf4830d84701ecebb55 100644 (file)
@@ -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