Permalink
Browse files

add hash_clear(hash)

  • Loading branch information...
1 parent 4dbca76 commit 2f1105bdb3c6e68f8ae61e81577fb71d788d4f04 @tj tj committed Jun 17, 2012
Showing with 27 additions and 0 deletions.
  1. +6 −0 Readme.md
  2. +12 −0 hash.c
  3. +9 −0 hash.h
View
6 Readme.md
@@ -54,6 +54,12 @@ typedef khash_t(ptr) hash_t;
#define hash_size kh_size
/*
+ * Remove all pairs in the hash.
+ */
+
+#define hash_clear(self) kh_clear(ptr, self)
+
+/*
* Iterate hash keys and ptrs, populating
* `key` and `val`.
*/
View
12 hash.c
@@ -107,6 +107,17 @@ test_hash_del() {
}
void
+test_hash_clear() {
+ hash_t *hash = hash_new();
+ hash_set(hash, "foo", "bar");
+ hash_set(hash, "bar", "baz");
+ hash_set(hash, "raz", "jaz");
+ assert(3 == hash_size(hash));
+ hash_clear(hash);
+ assert(0 == hash_size(hash));
+}
+
+void
test_hash_each() {
hash_t *hash = hash_new();
hash_set(hash, "name", "tj");
@@ -169,6 +180,7 @@ main(){
test_hash_has();
test_hash_del();
test_hash_size();
+ test_hash_clear();
test_hash_each();
test_hash_each_key();
test_hash_each_val();
View
9 hash.h
@@ -39,6 +39,12 @@ typedef khash_t(ptr) hash_t;
#define hash_size kh_size
/*
+ * Remove all pairs in the hash.
+ */
+
+#define hash_clear(self) kh_clear(ptr, self)
+
+/*
* Iterate hash keys and ptrs, populating
* `key` and `val`.
*/
@@ -94,4 +100,7 @@ hash_has(hash_t *self, char *key);
void
hash_del(hash_t *self, char *key);
+void
+hash_clear(hash_t *self);
+
#endif /* HASH */

0 comments on commit 2f1105b

Please sign in to comment.