summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-01-30 23:05:36 +0100
committerTyge Løvset <[email protected]>2021-01-30 23:05:36 +0100
commitb3d9e68fa2c2ec9ebe7e3f08045d01db5b720eea (patch)
tree100c3d8b5a2df433efd9cf148d3c66506f1d288b
parent5c196be3cfa172ca521ced0cf5d9b7d0acec5b83 (diff)
downloadSTC-modified-b3d9e68fa2c2ec9ebe7e3f08045d01db5b720eea.tar.gz
STC-modified-b3d9e68fa2c2ec9ebe7e3f08045d01db5b720eea.zip
Internal changes: removed use of cmap_inits and set_inits. Use cmap_x_init() and cset_X_init(). Minor changes in cvec, csmap, cstr, crandom.
-rw-r--r--benchmarks/cmap_benchmark.cpp2
-rw-r--r--benchmarks/cmap_benchmark2.cpp10
-rw-r--r--docs/cset_api.md4
-rw-r--r--examples/complex.c4
-rw-r--r--examples/convert.c2
-rw-r--r--examples/demos.c8
-rw-r--r--examples/inits.c4
-rw-r--r--examples/mapmap.c4
-rw-r--r--examples/phonebook.c3
-rw-r--r--examples/words.c2
-rw-r--r--stc/cmap.h11
-rw-r--r--stc/crandom.h12
-rw-r--r--stc/csmap.h16
-rw-r--r--stc/cstr.h2
-rw-r--r--stc/cvec.h17
15 files changed, 54 insertions, 47 deletions
diff --git a/benchmarks/cmap_benchmark.cpp b/benchmarks/cmap_benchmark.cpp
index 967c8abe..1f5c6fa1 100644
--- a/benchmarks/cmap_benchmark.cpp
+++ b/benchmarks/cmap_benchmark.cpp
@@ -33,7 +33,7 @@ stc64_t rng;
#define RAND(N) (stc64_rand(&rng) & ((1 << N) - 1))
-#define CMAP_SETUP(X, Key, Value) cmap_##X map = cmap_inits \
+#define CMAP_SETUP(X, Key, Value) cmap_##X map = cmap_##X##_init() \
; cmap_##X##_set_load_factors(&map, 0.0, max_load_factor)
#define CMAP_PUT(X, key, val) cmap_##X##_put(&map, key, val).first->second
#define CMAP_EMPLACE(X, key, val) cmap_##X##_emplace(&map, key, val).first->second
diff --git a/benchmarks/cmap_benchmark2.cpp b/benchmarks/cmap_benchmark2.cpp
index cda8c15b..8624b741 100644
--- a/benchmarks/cmap_benchmark2.cpp
+++ b/benchmarks/cmap_benchmark2.cpp
@@ -66,7 +66,7 @@ static void ctor_and_ins_one_cmap_i(picobench::state& s)
picobench::scope scope(s);
c_forrange (n, s.iterations()) {
- cmap_i map = cmap_inits;
+ cmap_i map = cmap_i_init();
cmap_i_emplace(&map, n, 0);
result += cmap_i_size(map);
cmap_i_del(&map);
@@ -109,7 +109,7 @@ static void ins_and_erase_i(picobench::state& s)
static void ins_and_erase_cmap_i(picobench::state& s)
{
- cmap_i map = cmap_inits;
+ cmap_i map = cmap_i_init();
cmap_i_set_load_factors(&map, 0.0, MaxLoadFactor100 / 100.0);
stc64_srandom(seed);
@@ -158,7 +158,7 @@ static void ins_and_access_cmap_i(picobench::state& s)
{
uint64_t mask = (1ull << s.arg()) - 1;
size_t result = 0;
- cmap_i map = cmap_inits;
+ cmap_i map = cmap_i_init();
cmap_i_set_load_factors(&map, 0.0, MaxLoadFactor100 / 100.0);
stc64_srandom(seed);
@@ -214,7 +214,7 @@ static void ins_and_access_cmap_s(picobench::state& s)
{
cstr str = cstr_with_size(s.arg(), 'x');
size_t result = 0;
- cmap_s map = cmap_inits;
+ cmap_s map = cmap_s_init();
cmap_s_set_load_factors(&map, 0.0, MaxLoadFactor100 / 100.0);
stc64_srandom(seed);
@@ -278,7 +278,7 @@ static void iterate_x(picobench::state& s)
static void iterate_cmap_x(picobench::state& s)
{
- cmap_x map = cmap_inits;
+ cmap_x map = cmap_x_init();
cmap_x_set_load_factors(&map, 0.3, MaxLoadFactor100 / 100.0);
uint64_t K = (1ull << s.arg()) - 1;
diff --git a/docs/cset_api.md b/docs/cset_api.md
index a1303597..06c3f552 100644
--- a/docs/cset_api.md
+++ b/docs/cset_api.md
@@ -93,11 +93,11 @@ using_cset_str();
int main ()
{
- cset_str first = cset_inits; // empty
+ cset_str first = cset_str_init(); // empty
c_init (cset_str, second, {"red", "green", "blue"});
c_init (cset_str, third, {"orange", "pink", "yellow"});
- cset_str fourth = cset_inits;
+ cset_str fourth = cset_str_init();
cset_str_emplace(&fourth, "potatoes");
cset_str_emplace(&fourth, "milk");
cset_str_emplace(&fourth, "flour");
diff --git a/examples/complex.c b/examples/complex.c
index 67397669..cbf07af2 100644
--- a/examples/complex.c
+++ b/examples/complex.c
@@ -14,9 +14,9 @@ int main() {
int xdim = 4, ydim = 6;
int x = 1, y = 5, tableKey = 42;
const char* strKey = "first";
- cmap_l listMap = cmap_inits;
+ cmap_l listMap = cmap_l_init();
- cmap_s myMap = cmap_inits;
+ cmap_s myMap = cmap_s_init();
// Construct.
carray2f arr_a = carray2f_init(ydim, xdim, 0.f);
diff --git a/examples/convert.c b/examples/convert.c
index f5dd5af6..25ae9dcd 100644
--- a/examples/convert.c
+++ b/examples/convert.c
@@ -12,7 +12,7 @@ using_clist_str();
int main()
{
- cmap_str map = cmap_inits;
+ cmap_str map = cmap_str_init();
cmap_str_emplace(&map, "green", "#00ff00");
cmap_str_emplace(&map, "blue", "#0000ff");
cmap_str_emplace(&map, "yellow", "#ffff00");
diff --git a/examples/demos.c b/examples/demos.c
index 2ee267e6..aa1fa28b 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -108,7 +108,7 @@ using_cset(i, int);
void setdemo1()
{
printf("\nSETDEMO1\n");
- cset_i nums = cset_inits;
+ cset_i nums = cset_i_init();
cset_i_insert(&nums, 8);
cset_i_insert(&nums, 11);
@@ -123,7 +123,7 @@ using_cmap(ii, int, int);
void mapdemo1()
{
printf("\nMAPDEMO1\n");
- cmap_ii nums = cmap_inits;
+ cmap_ii nums = cmap_ii_init();
cmap_ii_put(&nums, 8, 64);
cmap_ii_put(&nums, 11, 121);
printf("val 8: %d\n", *cmap_ii_at(&nums, 8));
@@ -136,7 +136,7 @@ using_cmap_strkey(si, int); // Shorthand macro for the general using_cmap expans
void mapdemo2()
{
printf("\nMAPDEMO2\n");
- cmap_si nums = cmap_inits;
+ cmap_si nums = cmap_si_init();
cmap_si_put(&nums, "Hello", 64);
cmap_si_put(&nums, "Groovy", 121);
cmap_si_put(&nums, "Groovy", 200); // overwrite previous
@@ -158,7 +158,7 @@ using_cmap_str();
void mapdemo3()
{
printf("\nMAPDEMO3\n");
- cmap_str table = cmap_inits;
+ cmap_str table = cmap_str_init();
cmap_str_put(&table, "Map", "test");
cmap_str_put(&table, "Make", "my");
cmap_str_put(&table, "Sunny", "day");
diff --git a/examples/inits.c b/examples/inits.c
index 759fc7a7..2c138a43 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -45,7 +45,7 @@ int main(void)
// CMAP ID
int year = 2020;
- cmap_id idnames = cmap_inits;
+ cmap_id idnames = cmap_id_init();
c_push_items(&idnames, cmap_id, {
{100, cstr_from("Hello")},
{110, cstr_from("World")},
@@ -59,7 +59,7 @@ int main(void)
// CMAP CNT
- cmap_cnt countries = cmap_inits;
+ cmap_cnt countries = cmap_cnt_init();
c_push_items(&countries, cmap_cnt, {
{"Norway", 100},
{"Denmark", 50},
diff --git a/examples/mapmap.c b/examples/mapmap.c
index a56a30db..7782f5bf 100644
--- a/examples/mapmap.c
+++ b/examples/mapmap.c
@@ -7,8 +7,8 @@ using_cmap_str();
using_cmap_strkey(cfg, cmap_str, cmap_str_del, cmap_str_clone);
int main(void) {
- cmap_cfg config = cmap_inits;
- cmap_str init = cmap_inits;
+ cmap_cfg config = cmap_cfg_init();
+ cmap_str init = cmap_str_init();
cmap_str_put(&cmap_cfg_emplace(&config, "user", init).first->second, "name", "Joe");
cmap_str_put(&cmap_cfg_emplace(&config, "user", init).first->second, "groups", "proj1,proj3");
cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->second, "proj1", "Energy");
diff --git a/examples/phonebook.c b/examples/phonebook.c
index 91d01072..1bedcce9 100644
--- a/examples/phonebook.c
+++ b/examples/phonebook.c
@@ -36,8 +36,7 @@ void print_phone_book(cmap_str phone_book)
int main(int argc, char **argv)
{
bool erased;
- cmap_str phone_book = cmap_inits;
- c_push_items(&phone_book, cmap_str, {
+ c_init (cmap_str, phone_book, {
{"Lilia Friedman", "(892) 670-4739"},
{"Tariq Beltran", "(489) 600-7575"},
{"Laiba Juarez", "(303) 885-5692"},
diff --git a/examples/words.c b/examples/words.c
index 693e302e..08563cfc 100644
--- a/examples/words.c
+++ b/examples/words.c
@@ -26,7 +26,7 @@ int main1()
"this", "sentence", "is", "a", "hoax"
});
- cmap_si word_map = cmap_inits;
+ cmap_si word_map = cmap_si_init();
c_foreach (w, cvec_str, words)
cmap_si_emplace(&word_map, w.ref->str, 0).first->second += 1;
diff --git a/stc/cmap.h b/stc/cmap.h
index a36cd70a..79277405 100644
--- a/stc/cmap.h
+++ b/stc/cmap.h
@@ -31,14 +31,14 @@ using_cset(sx, int); // Set of int
using_cmap(mx, int, char); // Map of int -> char
int main(void) {
- cset_sx s = cset_inits;
+ cset_sx s = cset_sx_init();
cset_sx_insert(&s, 5);
cset_sx_insert(&s, 8);
c_foreach (i, cset_sx, s)
printf("set %d\n", i.ref->second);
cset_sx_del(&s);
- cmap_mx m = cmap_inits;
+ cmap_mx m = cmap_mx_init();
cmap_mx_put(&m, 5, 'a');
cmap_mx_put(&m, 8, 'b');
cmap_mx_put(&m, 12, 'c');
@@ -55,8 +55,7 @@ int main(void) {
#include <stdlib.h>
#include <string.h>
-#define cmap_inits {NULL, NULL, 0, 0, 0.15f, 0.85f}
-#define cset_inits cmap_inits
+#define _cmap_inits {NULL, NULL, 0, 0, 0.15f, 0.85f}
/* https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction */
#define chash_reduce(x, N) ((uint32_t) (((uint64_t) (x) * (N)) >> 32))
@@ -185,7 +184,7 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t;
} C##_##X##_iter_t; \
\
STC_INLINE C##_##X \
- C##_##X##_init(void) {C##_##X m = cmap_inits; return m;} \
+ C##_##X##_init(void) {C##_##X m = _cmap_inits; return m;} \
STC_INLINE bool \
C##_##X##_empty(C##_##X m) {return m.size == 0;} \
STC_INLINE size_t \
@@ -318,7 +317,7 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t;
keyFromRaw, keyToRaw, RawKey, mappedFromRaw, mappedToRaw, RawMapped) \
STC_DEF C##_##X \
C##_##X##_with_capacity(size_t cap) { \
- C##_##X h = C##_inits; \
+ C##_##X h = _cmap_inits; \
C##_##X##_reserve(&h, cap); \
return h; \
} \
diff --git a/stc/crandom.h b/stc/crandom.h
index c92a7de6..b43fdd46 100644
--- a/stc/crandom.h
+++ b/stc/crandom.h
@@ -84,14 +84,14 @@ STC_INLINE double stc64_uniformf(stc64_t* rng, stc64_uniformf_t* dist) {
}
#if defined(__SIZEOF_INT128__)
- #define cmul128(a, b, lo, hi) \
+ #define cumul128(a, b, lo, hi) \
do { __uint128_t _z = (__uint128_t)(a) * (b); \
*(lo) = (uint64_t)_z, *(hi) = _z >> 64; } while(0)
#elif defined(_MSC_VER) && defined(_WIN64)
#include <intrin.h>
- #define cmul128(a, b, lo, hi) (*(lo) = _umul128(a, b, hi), (void)0)
+ #define cumul128(a, b, lo, hi) (*(lo) = _umul128(a, b, hi), (void)0)
#elif defined(__x86_64__)
- #define cmul128(a, b, lo, hi) \
+ #define cumul128(a, b, lo, hi) \
asm("mulq %[rhs]" : "=a" (*(lo)), "=d" (*(hi)) \
: [lhs] "0" (a), [rhs] "rm" (b))
#endif
@@ -99,7 +99,11 @@ STC_INLINE double stc64_uniformf(stc64_t* rng, stc64_uniformf_t* dist) {
/* Unbiased bounded uniform distribution. */
STC_INLINE int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* d) {
uint64_t lo, hi;
- do { cmul128(stc64_rand(rng), d->range, &lo, &hi); } while (lo < d->threshold);
+#ifdef cumul128
+ do { cumul128(stc64_rand(rng), d->range, &lo, &hi); } while (lo < d->threshold);
+#else
+ hi = stc64_rand(rng) % d->range;
+#endif
return d->lower + hi;
}
diff --git a/stc/csmap.h b/stc/csmap.h
index 97183abf..2e45f1a4 100644
--- a/stc/csmap.h
+++ b/stc/csmap.h
@@ -167,8 +167,8 @@ int main(void) {
bool second; \
} C##_##X##_result_t; \
\
- STC_INLINE C##_##X \
- C##_##X##_init(void) {C##_##X m = {(C##_##X##_node_t *) &cbst_nil, 0}; return m;} \
+ STC_API C##_##X \
+ C##_##X##_init(void); \
STC_INLINE bool \
C##_##X##_empty(C##_##X m) {return m.size == 0;} \
STC_INLINE size_t \
@@ -305,14 +305,16 @@ int main(void) {
keyFromRaw, keyToRaw, RawKey, mappedFromRaw, mappedToRaw, RawMapped) \
typedef C##_##X C##_##X##_t
-_using_CBST_types(_, csmap, int, int);
-static csmap___node_t cbst_nil = {&cbst_nil, &cbst_nil, 0};
-
/* -------------------------- IMPLEMENTATION ------------------------- */
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
#define _implement_CBST(X, C, Key, Mapped, keyCompareRaw, mappedDel, keyDel, \
keyFromRaw, keyToRaw, RawKey, mappedFromRaw, mappedToRaw, RawMapped) \
+ STC_DEF C##_##X \
+ C##_##X##_init(void) { \
+ C##_##X m = {(C##_##X##_node_t *) &cbst_nil, 0}; \
+ return m; \
+ } \
\
STC_DEF C##_##X##_value_t* \
C##_##X##_find_it(const C##_##X* self, C##_##X##_rawkey_t rkey, C##_##X##_iter_t* out) { \
@@ -447,6 +449,10 @@ static csmap___node_t cbst_nil = {&cbst_nil, &cbst_nil, 0};
} \
}
+
+_using_CBST_types(_, csmap, int, int);
+static csmap___node_t cbst_nil = {&cbst_nil, &cbst_nil, 0};
+
#else
#define _implement_CBST(X, C, Key, Mapped, keyCompareRaw, mappedDel, keyDel, \
keyFromRaw, keyToRaw, RawKey, mappedFromRaw, mappedToRaw, RawMapped)
diff --git a/stc/cstr.h b/stc/cstr.h
index 9c3d4f1c..8c29c9b3 100644
--- a/stc/cstr.h
+++ b/stc/cstr.h
@@ -260,7 +260,7 @@ uint32_t cstr_hash_raw(const char* const* p, size_t none) {
STC_DEF size_t
cstr_reserve(cstr_t* self, size_t cap) {
- size_t len = cstr_size(*self), oldcap = cstr_capacity(*self);
+ size_t oldcap = cstr_capacity(*self);
if (cap > oldcap) {
size_t* rep = (size_t *) c_realloc(oldcap ? _cstr_rep(self) : NULL, _cstr_mem(cap));
self->str = (char *) &rep[2];
diff --git a/stc/cvec.h b/stc/cvec.h
index e8f24ac7..d17876f1 100644
--- a/stc/cvec.h
+++ b/stc/cvec.h
@@ -216,18 +216,17 @@
STC_DEF void \
cvec_##X##_del(cvec_##X* self) { \
cvec_##X##_clear(self); \
- if (_cvec_alloced(self) != _cvec_inits) \
- c_free(_cvec_alloced(self)); \
+ if (_cvec_rep(self) != _cvec_inits) \
+ c_free(_cvec_rep(self)); \
} \
\
STC_DEF void \
cvec_##X##_reserve(cvec_##X* self, size_t cap) { \
- size_t* rep; \
- if (cap > cvec_##X##_capacity(*self)) { \
- size_t len = _cvec_size(self); \
- rep = (size_t *) c_realloc(_cvec_alloced(self) != _cvec_inits ? _cvec_alloced(self) : NULL, \
- 2*sizeof(size_t) + cap*sizeof(Value)); \
- self->data = (Value *) (rep + 2); \
+ size_t len = _cvec_size(self); \
+ if (cap > _cvec_cap(self)) { \
+ size_t* rep = (size_t *) c_realloc(_cvec_rep(self) != _cvec_inits ? _cvec_rep(self) : NULL, \
+ 2*sizeof(size_t) + cap*sizeof(Value)); \
+ self->data = (cvec_##X##_value_t*) (rep + 2); \
rep[0] = len; \
rep[1] = cap; \
} \
@@ -305,7 +304,7 @@
}
static size_t _cvec_inits[2] = {0, 0};
-#define _cvec_alloced(self) (((size_t *) (self)->data) - 2)
+#define _cvec_rep(self) (((size_t *) (self)->data) - 2)
#else
#define _c_implement_cvec_7(X, Value, valueCompareRaw, valueDestroy, valueFromRaw, valueToRaw, RawValue)