]> git.ozlabs.org Git - ccan/blobdiff - ccan/htable/htable_type.h
htable: htable_type add htable_copy.
[ccan] / ccan / htable / htable_type.h
index 6764c3f24ae103c8bccbcc8949c283ff56dcca1e..9c20531f2ab184c8a0fb834862a00945b3863264 100644 (file)
@@ -21,8 +21,9 @@
  *
  * It also defines initialization and freeing functions:
  *     void <name>_init(struct <name> *);
- *     void <name>_init_sized(struct <name> *, size_t);
+ *     bool <name>_init_sized(struct <name> *, size_t);
  *     void <name>_clear(struct <name> *);
+ *     bool <name>_copy(struct <name> *dst, const struct <name> *src);
  *
  * Add function only fails if we run out of memory:
  *     bool <name>_add(struct <name> *ht, const <type> *e);
  *     bool <name>_del(struct <name> *ht, const <type> *e);
  *     bool <name>_delkey(struct <name> *ht, const <keytype> *k);
  *
- * Find function return the matching element, or NULL:
+ * Find and return the (first) matching element, or NULL:
  *     type *<name>_get(const struct @name *ht, const <keytype> *k);
  *
+ * Find and return all matching elements, or NULL:
+ *     type *<name>_getfirst(const struct @name *ht, const <keytype> *k,
+ *                           struct <name>_iter *i);
+ *     type *<name>_getnext(const struct @name *ht, const <keytype> *k,
+ *                          struct <name>_iter *i);
+ *
  * Iteration over hashtable is also supported:
  *     type *<name>_first(const struct <name> *ht, struct <name>_iter *i);
  *     type *<name>_next(const struct <name> *ht, struct <name>_iter *i);
+ *     type *<name>_prev(const struct <name> *ht, struct <name>_iter *i);
  *
  * It's currently safe to iterate over a changing hashtable, but you might
  * miss an element.  Iteration isn't very efficient, either.
        struct name##_iter { struct htable_iter i; };                   \
        static inline size_t name##_hash(const void *elem, void *priv)  \
        {                                                               \
+               (void)priv;                                             \
                return hashfn(keyof((const type *)elem));               \
        }                                                               \
        static inline UNNEEDED void name##_init(struct name *ht)        \
        {                                                               \
                htable_init(&ht->raw, name##_hash, NULL);               \
        }                                                               \
-       static inline UNNEEDED void name##_init_sized(struct name *ht,  \
+       static inline UNNEEDED bool name##_init_sized(struct name *ht,  \
                                                      size_t s)         \
        {                                                               \
-               htable_init_sized(&ht->raw, name##_hash, NULL, s);      \
+               return htable_init_sized(&ht->raw, name##_hash, NULL, s); \
        }                                                               \
        static inline UNNEEDED void name##_clear(struct name *ht)       \
        {                                                               \
                htable_clear(&ht->raw);                                 \
        }                                                               \
+       static inline UNNEEDED bool name##_copy(struct name *dst,       \
+                                               const struct name *src) \
+       {                                                               \
+               return htable_copy(&dst->raw, &src->raw);               \
+       }                                                               \
        static inline bool name##_add(struct name *ht, const type *elem) \
        {                                                               \
                return htable_add(&ht->raw, hashfn(keyof(elem)), elem); \
@@ -74,7 +88,7 @@
                return htable_del(&ht->raw, hashfn(keyof(elem)), elem); \
        }                                                               \
        static inline UNNEEDED type *name##_get(const struct name *ht,  \
-                                      const HTABLE_KTYPE(keyof) k)     \
+                                      const HTABLE_KTYPE(keyof, type) k) \
        {                                                               \
                /* Typecheck for eqfn */                                \
                (void)sizeof(eqfn((const type *)NULL,                   \
                                  (bool (*)(const void *, void *))(eqfn), \
                                  k);                                   \
        }                                                               \
+       static inline UNNEEDED type *name##_getmatch_(const struct name *ht, \
+                                        const HTABLE_KTYPE(keyof, type) k, \
+                                        size_t h,                      \
+                                        type *v,                       \
+                                        struct name##_iter *iter)      \
+       {                                                               \
+               while (v) {                                             \
+                       if (eqfn(v, k))                                 \
+                               break;                                  \
+                       v = htable_nextval(&ht->raw, &iter->i, h);      \
+               }                                                       \
+               return v;                                               \
+       }                                                               \
+       static inline UNNEEDED type *name##_getfirst(const struct name *ht, \
+                                        const HTABLE_KTYPE(keyof, type) k, \
+                                        struct name##_iter *iter)      \
+       {                                                               \
+               size_t h = hashfn(k);                                   \
+               type *v = htable_firstval(&ht->raw, &iter->i, h);       \
+               return name##_getmatch_(ht, k, h, v, iter);                     \
+       }                                                               \
+       static inline UNNEEDED type *name##_getnext(const struct name *ht, \
+                                        const HTABLE_KTYPE(keyof, type) k, \
+                                        struct name##_iter *iter)      \
+       {                                                               \
+               size_t h = hashfn(k);                                   \
+               type *v = htable_nextval(&ht->raw, &iter->i, h);        \
+               return name##_getmatch_(ht, k, h, v, iter);             \
+       }                                                               \
        static inline UNNEEDED bool name##_delkey(struct name *ht,      \
-                                        const HTABLE_KTYPE(keyof) k)   \
+                                        const HTABLE_KTYPE(keyof, type) k) \
        {                                                               \
                type *elem = name##_get(ht, k);                         \
                if (elem)                                               \
                                        struct name##_iter *iter)       \
        {                                                               \
                return htable_next(&ht->raw, &iter->i);                 \
+       }                                                               \
+       static inline UNNEEDED type *name##_prev(const struct name *ht, \
+                                       struct name##_iter *iter)       \
+       {                                                               \
+               return htable_prev(&ht->raw, &iter->i);                 \
        }
 
 #if HAVE_TYPEOF
-#define HTABLE_KTYPE(keyof) typeof(keyof(NULL))
+#define HTABLE_KTYPE(keyof, type) typeof(keyof((const type *)NULL))
 #else
-#define HTABLE_KTYPE(keyof) void *
+#define HTABLE_KTYPE(keyof, type) void *
 #endif
 #endif /* CCAN_HTABLE_TYPE_H */