From 0b8aa9246f9111f86d1ed7a2be6f7de4fe2dd3d7 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Mon, 20 Feb 2017 21:09:13 +0100 Subject: [PATCH] Add OPLIST for bitset. Fix prototype for _cref to match what is expected. --- m-bitset.h | 37 ++++++++++++++++++++++++++++++++----- tests/test-mbitset.c | 11 ++++++++++- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/m-bitset.h b/m-bitset.h index 475dead3..b3e319c9 100644 --- a/m-bitset.h +++ b/m-bitset.h @@ -42,6 +42,7 @@ typedef struct bitset_s { typedef struct bitset_it_s { size_t index; + bool value; struct bitset_s *set; } bitset_it_t[1]; @@ -391,13 +392,13 @@ bitset_previous(bitset_it_t it) it->index--; } -static inline bool -bitset_ref(const bitset_it_t it) +static inline const bool * +bitset_cref(bitset_it_t it) { assert (it != NULL && it->set != NULL); - return bitset_get(it->set, it->index); + it->value = bitset_get(it->set, it->index); + return &it->value; } -#define bitset_cref bitset_ref static inline void bitset_out_str(FILE *file, const bitset_t set) @@ -446,7 +447,33 @@ bitset_get_str(string_t str, const bitset_t set, bool append) string_push_back (str, ']'); } -// TODO: OPLIST +#define BITSET_OPLIST \ + (INIT(bitset_init) \ + ,INIT_SET(bitset_init_set) \ + ,SET(bitset_set) \ + ,CLEAR(bitset_clear) \ + ,INIT_MOVE(bitset_init_move) \ + ,MOVE(bitset_move) \ + ,TYPE(bitset_t) \ + ,SUBTYPE(bool) \ + ,IT_TYPE(bitset_it_t) \ + ,IT_FIRST(bitset_it) \ + ,IT_SET(bitset_it_set) \ + ,IT_END_P(bitset_end_p) \ + ,IT_LAST_P(bitset_last_p) \ + ,IT_EQUAL_P(bitset_it_equal_p) \ + ,IT_NEXT(bitset_next) \ + ,IT_PREVIOUS(bitset_previous) \ + ,IT_CREF(bitset_cref) \ + ,CLEAN(bitset_clean) \ + ,PUSH(bitset_push_back) \ + ,POP(bitset_pop_back) \ + ,GET_STR(bitset_get_str) \ + ,OUT_STR(bitset_out_str) \ + ,IN_STR(bitset_in_str) \ + ,EQUAL(bitset_equal_p) \ + ) + // TODO: set_at2, insert_v, remove_v, shrink_to_fit, #endif diff --git a/tests/test-mbitset.c b/tests/test-mbitset.c index a376aa0e..5540a0be 100644 --- a/tests/test-mbitset.c +++ b/tests/test-mbitset.c @@ -99,10 +99,19 @@ static void test1(void) int n = 0; for(bitset_it(it, set); ! bitset_end_p(it); bitset_next(it)) { bool v = (n*17547854547UL)&1; - bool c = bitset_ref(it); + bool c = *bitset_cref(it); assert (v == c); n++; } +#if 0 + n = 0; + for M_CEACH (item, set, BITSET_OPLIST) { + bool v = (n*17547854547UL)&1; + bool c = *item; + assert (v == c); + n++; + } +#endif bitset_clear(set); bitset_clear(set2);