From ddbc001415f7fa0d556b3e194a8882418b018503 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Tue, 21 Feb 2017 21:57:28 +0100 Subject: [PATCH] Add HASH functions for array & list --- m-array.h | 20 +++++++++++++++++++- m-list.h | 21 +++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/m-array.h b/m-array.h index 932748b4..0bb18bc7 100644 --- a/m-array.h +++ b/m-array.h @@ -77,6 +77,7 @@ ,M_IF_METHOD(OUT_STR, oplist)(OUT_STR(M_C3(array_, name, _out_str)),) \ ,M_IF_METHOD(IN_STR, oplist)(IN_STR(M_C3(array_, name, _in_str)),) \ ,M_IF_METHOD(EQUAL, oplist)(EQUAL(M_C3(array_, type, _equal_p)),) \ + ,M_IF_METHOD(HASH, oplist)(HASH(M_C3(array_, type, _hash)),) \ ) // Compute alloc from size. @@ -633,8 +634,25 @@ } \ , /* no equal */ ) \ \ + M_IF_METHOD(HASH, oplist)( \ + static inline size_t \ + M_C3(array_, name, _hash)(/*const*/ M_C3(array_, name,_t) array) \ + { \ + assert (array != NULL); \ + M_HASH_DECL(hash); \ + M_C3(array_it_, name, _t) it; \ + for(M_C3(array_, name, _it)(it, array) ; \ + !M_C3(array_, name, _end_p)(it); \ + M_C3(array_, name, _next)(it)) { \ + const type *item = M_C3(array_, name, _cref)(it); \ + size_t hi = M_GET_HASH oplist (*item); \ + M_HASH_UP(hash, hi); \ + } \ + return hash; \ + } \ + , /* no hash */ ) \ + \ -// TODO: Conditional HASH if HASH is present // TODO: 'remove' interface is different in list & array ==> erase for iterator & pop for size_t #endif diff --git a/m-list.h b/m-list.h index 6ff6a91d..ea5c4884 100644 --- a/m-list.h +++ b/m-list.h @@ -76,6 +76,7 @@ ,M_IF_METHOD(OUT_STR, oplist)(OUT_STR(M_C3(list_, name, _out_str)),) \ ,M_IF_METHOD(IN_STR, oplist)(IN_STR(M_C3(list_, name, _in_str)),) \ ,M_IF_METHOD(EQUAL, oplist)(EQUAL(M_C3(list_, name, _equal_p)),) \ + ,M_IF_METHOD(HASH, oplist)(HASH(M_C3(list_, type, _hash)),) \ ) #define LISTI_DEF2(name, type, oplist) \ @@ -553,7 +554,23 @@ } \ , /* no equal */ ) \ \ + M_IF_METHOD(HASH, oplist)( \ + static inline size_t \ + M_C3(list_, name, _hash)(/*const*/ M_C3(list_, name,_t) list) \ + { \ + assert (list != NULL); \ + M_HASH_DECL(hash); \ + M_C3(list_it_, name, _t) it; \ + for(M_C3(list_, name, _it)(it, list) ; \ + !M_C3(list_, name, _end_p)(it); \ + M_C3(list_, name, _next)(it)) { \ + const type *item = M_C3(list_, name, _cref)(it); \ + size_t hi = M_GET_HASH oplist (*item); \ + M_HASH_UP(hash, hi); \ + } \ + return hash; \ + } \ + , /* no hash */ ) \ + \ -// TODO: Conditional HASH if HASH is present - #endif