Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
size_t (*rehash)(const void *elem, void *priv),
void *priv, size_t size);
size_t (*rehash)(const void *elem, void *priv),
void *priv, size_t size);
+/**
+ * htable_count - count number of entries in a hash table.
+ * @ht: the hash table
+ */
+static inline size_t htable_count(const struct htable *ht)
+{
+ return ht->elems;
+}
+
/**
* htable_clear - empty a hash table.
* @ht: the hash table to clear
/**
* htable_clear - empty a hash table.
* @ht: the hash table to clear
* void <name>_clear(struct <name> *);
* bool <name>_copy(struct <name> *dst, const struct <name> *src);
*
* void <name>_clear(struct <name> *);
* bool <name>_copy(struct <name> *dst, const struct <name> *src);
*
+ * Count entries:
+ * size_t <name>_count(const struct <name> *ht);
+ *
* Add function only fails if we run out of memory:
* bool <name>_add(struct <name> *ht, const <type> *e);
*
* Add function only fails if we run out of memory:
* bool <name>_add(struct <name> *ht, const <type> *e);
*
{ \
return htable_init_sized(&ht->raw, name##_hash, NULL, s); \
} \
{ \
return htable_init_sized(&ht->raw, name##_hash, NULL, s); \
} \
+ static inline UNNEEDED size_t name##_count(const struct name *ht) \
+ { \
+ return htable_count(&ht->raw); \
+ } \
static inline UNNEEDED void name##_clear(struct name *ht) \
{ \
htable_clear(&ht->raw); \
static inline UNNEEDED void name##_clear(struct name *ht) \
{ \
htable_clear(&ht->raw); \
- pass("Found %u numbers in hash", i);
+ ok1(htable_obj_count(ht) == i);
}
static void del_vals(struct htable_obj *ht,
}
static void del_vals(struct htable_obj *ht,
void *p;
struct htable_obj_iter iter;
void *p;
struct htable_obj_iter iter;
for (i = 0; i < NUM_VALS; i++)
val[i].key = i;
dne = i;
htable_obj_init(&ht);
for (i = 0; i < NUM_VALS; i++)
val[i].key = i;
dne = i;
htable_obj_init(&ht);
+ ok1(htable_obj_count(&ht) == 0);
ok1(ht_max(&ht.raw) == 0);
ok1(ht.raw.bits == 0);
ok1(ht_max(&ht.raw) == 0);
ok1(ht.raw.bits == 0);
+ ok1(htable_obj_count(&ht) == 0);
+ ok1(htable_obj_count(&ht2) == 0);
- pass("Found %llu numbers in hash", (long long)i);
+ ok1(htable_count(ht) == i);
}
static void del_vals(struct htable *ht,
}
static void del_vals(struct htable *ht,
void *p;
struct htable_iter iter;
void *p;
struct htable_iter iter;
for (i = 0; i < NUM_VALS; i++)
val[i] = i;
dne = i;
htable_init(&ht, hash, NULL);
for (i = 0; i < NUM_VALS; i++)
val[i] = i;
dne = i;
htable_init(&ht, hash, NULL);
+ ok1(htable_count(&ht) == 0);
ok1(ht_max(&ht) == 0);
ok1(ht.bits == 0);
ok1(ht_max(&ht) == 0);
ok1(ht.bits == 0);
ok1(htable_init_sized(&ht, hash, NULL, 1025));
ok1(ht_max(&ht) >= 1025);
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1025));
ok1(ht_max(&ht) >= 1025);
htable_clear(&ht);
+
+ ok1(htable_count(&ht) == 0);
+