diff options
| author | Tyge Løvset <[email protected]> | 2020-08-04 09:00:06 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-08-04 09:00:06 +0200 |
| commit | 43ed5f803586e5265042d2f3151d3c42f198309f (patch) | |
| tree | 972ee1a290a80cffae30da877af72c4f0fc964e8 | |
| parent | 4cff2c39e1bac80d9ab95f293c144bb6c0f30961 (diff) | |
| download | STC-modified-43ed5f803586e5265042d2f3151d3c42f198309f.tar.gz STC-modified-43ed5f803586e5265042d2f3151d3c42f198309f.zip | |
Refinements and consistency fixes.
| -rw-r--r-- | examples/bits.c | 5 | ||||
| -rw-r--r-- | examples/rngbirthday.c | 2 | ||||
| -rw-r--r-- | examples/rngtest.c | 1 | ||||
| -rw-r--r-- | stc/cbitset.h | 59 | ||||
| -rw-r--r-- | 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<set.size; ++i)
printf("%d", cbitset_test(set, i));
diff --git a/examples/rngbirthday.c b/examples/rngbirthday.c index c9305fc0..595bacdb 100644 --- a/examples/rngbirthday.c +++ b/examples/rngbirthday.c @@ -36,7 +36,7 @@ void distribution(void) {
crandom_eng32_t rng = crandom_eng32_init(seed); // time(NULL), time(NULL));
const size_t N = 1ull << 28, M = 1ull << 9; // 1ull << 10;
- cmap_x map = cmap_x_make(M);
+ cmap_x map = cmap_x_with_capacity(M);
clock_t now = clock();
crandom_uniform_i32_t dist = crandom_uniform_i32_init(0, M);
for (size_t i = 0; i < N; ++i) {
diff --git a/examples/rngtest.c b/examples/rngtest.c index caced0c3..e19147c7 100644 --- a/examples/rngtest.c +++ b/examples/rngtest.c @@ -42,6 +42,5 @@ int main(void) crandom_uniform_f64_t fdist = crandom_uniform_f64_init(10, 20);
for (int i=0; i<8; ++i) printf("%f ", crandom_uniform_f64(&sfc, fdist));
- //for (int i=0; i<8; ++i) printf("%zu ", crandom_i64(&sfc));
puts("");
}
\ No newline at end of file diff --git a/stc/cbitset.h b/stc/cbitset.h index 4271c101..7e37ce3b 100644 --- a/stc/cbitset.h +++ b/stc/cbitset.h @@ -43,38 +43,18 @@ int main() { #ifndef CBITSET__H__
#define CBITSET__H__
-#include <string.h>
-#include <stdlib.h>
#include <assert.h>
-#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<n; ++i) self->_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<set.size; ++i) if (str[i] == '1') cbitset_set(&set, i);
+ return set;
+}
+STC_INLINE cstr_t cbitset_to_str(cbitset_t set) {
+ cstr_t out = cstr_with_size(set.size, '0');
+ for (size_t i=0; i<set.size; ++i) if (cbitset_test(set, i)) out.str[i] = '1';
+ return out;
+}
+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;}
+
/* Intersection */
STC_INLINE void cbitset_intersect_with(cbitset_t *self, cbitset_t other) {
assert(self->size == other.size);
@@ -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 \
|
