]> git.ozlabs.org Git - ccan/blobdiff - ccan/htable/htable_type.h
htable: add htable_count().
[ccan] / ccan / htable / htable_type.h
index 9c20531f2ab184c8a0fb834862a00945b3863264..9dad4b3927186ab661f6099eeb7e801ed9204277 100644 (file)
@@ -25,6 +25,9 @@
  *     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);
  *
        {                                                               \
                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 type *name##_get(const struct name *ht,  \
                                       const HTABLE_KTYPE(keyof, type) k) \
        {                                                               \
-               /* Typecheck for eqfn */                                \
-               (void)sizeof(eqfn((const type *)NULL,                   \
-                                 keyof((const type *)NULL)));          \
-               return htable_get(&ht->raw,                             \
-                                 hashfn(k),                            \
-                                 (bool (*)(const void *, void *))(eqfn), \
-                                 k);                                   \
+               struct htable_iter i;                                   \
+               size_t h = hashfn(k);                                   \
+               void *c;                                                \
+                                                                       \
+               for (c = htable_firstval(&ht->raw,&i,h);                \
+                    c;                                                 \
+                    c = htable_nextval(&ht->raw,&i,h)) {               \
+                       if (eqfn(c, k))                                 \
+                               return c;                               \
+               }                                                       \
+               return NULL;                                            \
        }                                                               \
        static inline UNNEEDED type *name##_getmatch_(const struct name *ht, \
                                         const HTABLE_KTYPE(keyof, type) k, \
 #if HAVE_TYPEOF
 #define HTABLE_KTYPE(keyof, type) typeof(keyof((const type *)NULL))
 #else
+/* Assumes keys are a pointer: if not, override. */
+#ifndef HTABLE_KTYPE
 #define HTABLE_KTYPE(keyof, type) void *
 #endif
+#endif
 #endif /* CCAN_HTABLE_TYPE_H */