From 43ed5f803586e5265042d2f3151d3c42f198309f Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 4 Aug 2020 09:00:06 +0200 Subject: Refinements and consistency fixes. --- examples/bits.c | 5 +++++ examples/rngbirthday.c | 2 +- examples/rngtest.c | 1 - stc/cbitset.h | 59 +++++++++++++++++++++++++++++--------------------- stc/cmap.h | 22 ++++++++++--------- 5 files changed, 52 insertions(+), 37 deletions(-) diff --git a/examples/bits.c b/examples/bits.c index 8b3ecd97..f436360f 100644 --- a/examples/bits.c +++ b/examples/bits.c @@ -4,8 +4,13 @@ int main() { cbitset_t set = cbitset_with_size(23, true); printf("count %zu, %zu\n", cbitset_count(set), set.size); + cbitset_reset(&set, 9); cbitset_resize(&set, 43, false); + cstr_t str = cbitset_to_str(set); + printf(" str: %s\n", str.str); + cstr_destroy(&str); + printf("%4zu: ", set.size); for (int i=0; i -#include #include -#include "cdefs.h" +#include "cstr.h" typedef struct { uint64_t* _arr; size_t size; } cbitset_t; #define cbitset_init {NULL, 0} -STC_API void cbitset_resize(cbitset_t* self, size_t size, bool value); +STC_API void cbitset_resize(cbitset_t* self, size_t size, bool value); STC_API size_t cbitset_count(cbitset_t set); -STC_API bool cbitset_is_disjoint(cbitset_t s, cbitset_t other); -STC_API bool cbitset_is_subset(cbitset_t s, cbitset_t other); -STC_API bool cbitset_is_superset(cbitset_t s, cbitset_t other); - -STC_INLINE void cbitset_set_all(cbitset_t *self, bool value); - -STC_INLINE cbitset_t cbitset_with_size(size_t size, bool value) { - cbitset_t set = {(uint64_t *) malloc(((size + 63) >> 6) * 8), size}; - cbitset_set_all(&set, value); - return set; -} -STC_INLINE cbitset_t cbitset_clone(cbitset_t other) { - size_t bytes = ((other.size + 63) >> 6) * 8; - cbitset_t set = {(uint64_t *) memcpy(malloc(bytes), other._arr, bytes), other.size}; - return set; -} -STC_INLINE void cbitset_destroy(cbitset_t* self) { - free(self->_arr); -} - -STC_INLINE size_t cbitset_size(cbitset_t set) {return set.size;} +STC_API bool cbitset_is_disjoint(cbitset_t set, cbitset_t other); +STC_API bool cbitset_is_subset(cbitset_t set, cbitset_t other); +STC_API bool cbitset_is_superset(cbitset_t set, cbitset_t other); STC_INLINE void cbitset_set(cbitset_t *self, size_t i) { self->_arr[i >> 6] |= 1ull << (i & 63); @@ -103,6 +83,35 @@ STC_INLINE void cbitset_flip_all(cbitset_t *self) { size_t n = (self->size + 63) >> 6; for (size_t i=0; i_arr[i] ^= ~0ull; } + + +STC_INLINE cbitset_t cbitset_with_size(size_t size, bool value) { + cbitset_t set = {(uint64_t *) malloc(((size + 63) >> 6) * 8), size}; + cbitset_set_all(&set, value); + return set; +} +STC_INLINE cbitset_t cbitset_from_str(const char* str) { + const char* p = str; while (*p) ++p; + cbitset_t set = cbitset_with_size(p - str, false); + for (size_t i=0; i> 6) * 8; + cbitset_t set = {(uint64_t *) memcpy(malloc(bytes), other._arr, bytes), other.size}; + return set; +} +STC_INLINE void cbitset_destroy(cbitset_t* self) { + free(self->_arr); +} + +STC_INLINE size_t cbitset_size(cbitset_t set) {return set.size;} + /* Intersection */ STC_INLINE void cbitset_intersect_with(cbitset_t *self, cbitset_t other) { assert(self->size == other.size); diff --git a/stc/cmap.h b/stc/cmap.h index bfb6336d..164e4144 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -163,27 +163,29 @@ typedef struct { \ STC_INLINE ctype##_##tag \ ctype##_##tag##_init(void) {ctype##_##tag x = cmap_init; return x;} \ STC_INLINE size_t \ -ctype##_##tag##_size(ctype##_##tag m) {return m.size;} \ +ctype##_##tag##_size(ctype##_##tag m) {return (size_t) m.size;} \ +STC_INLINE size_t \ +ctype##_##tag##_capacity(ctype##_##tag m) {return (size_t) (m.bucket_count * m.max_load_factor);} \ +STC_INLINE void \ +ctype##_##tag##_swap(ctype##_##tag* a, ctype##_##tag* b) {c_swap(ctype##_##tag, *a, *b);} \ +STC_INLINE void \ +ctype##_##tag##_set_load_factors(ctype##_##tag* self, float max, float shrink) { \ + self->max_load_factor = max; self->shrink_limit_factor = shrink; \ +} \ STC_API ctype##_##tag \ -ctype##_##tag##_make(size_t initialSize); \ +ctype##_##tag##_with_capacity(size_t cap); \ STC_API void \ ctype##_##tag##_push_n(ctype##_##tag* self, const ctype##_##tag##_input_t in[], size_t size); \ STC_API void \ ctype##_##tag##_destroy(ctype##_##tag* self); \ STC_API void \ ctype##_##tag##_clear(ctype##_##tag* self); \ -STC_INLINE void \ -ctype##_##tag##_set_load_factors(ctype##_##tag* self, float max, float shrink) { \ - self->max_load_factor = max; self->shrink_limit_factor = shrink; \ -} \ STC_API ctype##_##tag##_entry_t* \ ctype##_##tag##_find(const ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey); \ STC_API ctype##_##tag##_entry_t* /* like c++ std::map.insert_or_assign(): */ \ ctype##_##tag##_put(ctype##_##tag* self, OPT_2_##ctype(ctype##_##tag##_rawkey_t rawKey, Value value)); \ STC_API ctype##_##tag##_entry_t* /* like c++ std::map.insert(): */ \ ctype##_##tag##_insert(ctype##_##tag* self, OPT_2_##ctype(ctype##_##tag##_rawkey_t rawKey, Value value)); \ -STC_INLINE void \ -ctype##_##tag##_swap(ctype##_##tag* a, ctype##_##tag* b) { c_swap(ctype##_##tag, *a, *b); } \ STC_API size_t \ ctype##_##tag##_reserve(ctype##_##tag* self, size_t size); \ STC_API bool \ @@ -206,9 +208,9 @@ typedef Value ctype##_##tag##_value_t #define implement_CHASH(tag, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyGetRaw, keyInitRaw) \ STC_API ctype##_##tag \ -ctype##_##tag##_make(size_t initialSize) { \ +ctype##_##tag##_with_capacity(size_t cap) { \ ctype##_##tag h = ctype##_init; \ - ctype##_##tag##_reserve(&h, initialSize); \ + ctype##_##tag##_reserve(&h, cap); \ return h; \ } \ STC_API void \ -- cgit v1.2.3