Adding a remove_at method to the hash table.
authorTobias Brunner <tobias@strongswan.org>
Mon, 7 Jun 2010 14:36:26 +0000 (16:36 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 7 Jun 2010 14:36:26 +0000 (16:36 +0200)
This allows to remove key-value pairs while enumerating them.

src/libstrongswan/utils/hashtable.c
src/libstrongswan/utils/hashtable.h

index 9d582d3..dde57dc 100644 (file)
@@ -129,6 +129,11 @@ struct private_enumerator_t {
        u_int row;
 
        /**
+        * current pair
+        */
+       pair_t *pair;
+
+       /**
         * enumerator for the current row
         */
        enumerator_t *current;
@@ -313,6 +318,22 @@ METHOD(hashtable_t, remove_, void*,
        return value;
 }
 
+METHOD(hashtable_t, remove_at, void,
+          private_hashtable_t *this, private_enumerator_t *enumerator)
+{
+       if (enumerator->table == this && enumerator->current)
+       {
+               linked_list_t *list;
+               list = this->table[enumerator->row];
+               if (list)
+               {
+                       list->remove_at(list, enumerator->current);
+                       free(enumerator->pair);
+                       this->count--;
+               }
+       }
+}
+
 METHOD(hashtable_t, get_count, u_int,
           private_hashtable_t *this)
 {
@@ -326,17 +347,15 @@ METHOD(enumerator_t, enumerate, bool,
        {
                if (this->current)
                {
-                       pair_t *pair;
-
-                       if (this->current->enumerate(this->current, &pair))
+                       if (this->current->enumerate(this->current, &this->pair))
                        {
                                if (key)
                                {
-                                       *key = pair->key;
+                                       *key = this->pair->key;
                                }
                                if (value)
                                {
-                                       *value = pair->value;
+                                       *value = this->pair->value;
                                }
                                return TRUE;
                        }
@@ -346,7 +365,6 @@ METHOD(enumerator_t, enumerate, bool,
                else
                {
                        linked_list_t *list;
-
                        list = this->table->table[this->row];
                        if (list)
                        {
@@ -416,6 +434,7 @@ hashtable_t *hashtable_create(hashtable_hash_t hash, hashtable_equals_t equals,
                        .put = _put,
                        .get = _get,
                        .remove = _remove_,
+                       .remove_at = (void*)_remove_at,
                        .get_count = _get_count,
                        .create_enumerator = _create_enumerator,
                        .destroy = _destroy,
index 142ea63..27aca9b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Tobias Brunner
+ * Copyright (C) 2008-2010 Tobias Brunner
  * Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -87,9 +87,17 @@ struct hashtable_t {
        void *(*remove) (hashtable_t *this, void *key);
 
        /**
+        * Removes the key and value pair from the hash table at which the given
+        * enumerator currently points.
+        *
+        * @param enumerator    enumerator, from create_enumerator
+        */
+       void (*remove_at) (hashtable_t *this, enumerator_t *enumerator);
+
+       /**
         * Gets the number of items in the hash table.
         *
-        * @return                      number of items
+        * @return                      number of items
         */
        u_int (*get_count) (hashtable_t *this);
 
@@ -106,7 +114,7 @@ struct hashtable_t {
  * @param hash                 hash function
  * @param equals               equals function
  * @param capacity             initial capacity
- * @return                             hashtable_t object.
+ * @return                             hashtable_t object.
  */
 hashtable_t *hashtable_create(hashtable_hash_t hash, hashtable_equals_t equals,
                                                          u_int capacity);