summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-12-27 16:03:58 +0100
committerTyge Løvset <[email protected]>2020-12-27 16:03:58 +0100
commit83b7be31a1d0fc0be4e013dbfc97bb6cdc3600db (patch)
treedf69b4e6a7a85b5ed8c8bbd6d1baf52794b44966
parent5a444c90db6372749cbdc629ec999871cd20af72 (diff)
downloadSTC-modified-83b7be31a1d0fc0be4e013dbfc97bb6cdc3600db.tar.gz
STC-modified-83b7be31a1d0fc0be4e013dbfc97bb6cdc3600db.zip
Removed MACRO functions in API, like cvec_size(c), cvec_empty(c). Use cvec_X_size(c) etc. Restructured benchmarks / examples.
-rw-r--r--benchmarks/cmap_benchmark.cpp2
-rw-r--r--benchmarks/crand_benchmark2.cpp2
-rw-r--r--benchmarks/others/bytell_hash_map.hpp (renamed from examples/others/bytell_hash_map.hpp)0
-rw-r--r--benchmarks/others/flat_hash_map.hpp (renamed from examples/others/flat_hash_map.hpp)0
-rw-r--r--benchmarks/others/hopscotch_growth_policy.h (renamed from examples/others/hopscotch_growth_policy.h)0
-rw-r--r--benchmarks/others/hopscotch_hash.h (renamed from examples/others/hopscotch_hash.h)0
-rw-r--r--benchmarks/others/hopscotch_map.h (renamed from examples/others/hopscotch_map.h)0
-rw-r--r--benchmarks/others/khash.h (renamed from examples/others/khash.h)0
-rw-r--r--benchmarks/others/khashl.h (renamed from examples/others/khashl.h)0
-rw-r--r--benchmarks/others/robin_hood.hpp (renamed from examples/others/robin_hood.hpp)0
-rw-r--r--benchmarks/others/sparsepp/spp.h (renamed from examples/others/sparsepp/spp.h)0
-rw-r--r--benchmarks/others/sparsepp/spp_config.h (renamed from examples/others/sparsepp/spp_config.h)0
-rw-r--r--benchmarks/others/sparsepp/spp_dlalloc.h (renamed from examples/others/sparsepp/spp_dlalloc.h)0
-rw-r--r--benchmarks/others/sparsepp/spp_memory.h (renamed from examples/others/sparsepp/spp_memory.h)0
-rw-r--r--benchmarks/others/sparsepp/spp_smartptr.h (renamed from examples/others/sparsepp/spp_smartptr.h)0
-rw-r--r--benchmarks/others/sparsepp/spp_stdint.h (renamed from examples/others/sparsepp/spp_stdint.h)0
-rw-r--r--benchmarks/others/sparsepp/spp_timer.h (renamed from examples/others/sparsepp/spp_timer.h)0
-rw-r--r--benchmarks/others/sparsepp/spp_traits.h (renamed from examples/others/sparsepp/spp_traits.h)0
-rw-r--r--benchmarks/others/sparsepp/spp_utils.h (renamed from examples/others/sparsepp/spp_utils.h)0
-rw-r--r--docs/clist_api.md3
-rw-r--r--docs/cmap_api.md3
-rw-r--r--docs/cset_api.md4
-rw-r--r--docs/cvec_api.md4
-rw-r--r--examples/demos.c6
-rw-r--r--stc/cdeq.h4
-rw-r--r--stc/clist.h3
-rw-r--r--stc/cmap.h4
-rw-r--r--stc/cvec.h53
28 files changed, 31 insertions, 57 deletions
diff --git a/benchmarks/cmap_benchmark.cpp b/benchmarks/cmap_benchmark.cpp
index a159d24b..aaf9fc73 100644
--- a/benchmarks/cmap_benchmark.cpp
+++ b/benchmarks/cmap_benchmark.cpp
@@ -41,7 +41,7 @@ crand_t rng;
#define CMAP_FIND(X, key) (cmap_##X##_find(map, key) != NULL)
#define CMAP_FOR(X, i) c_foreach (i, cmap_##X, map)
#define CMAP_ITEM(X, i) i.ref->second
-#define CMAP_SIZE(X) cmap_size(map)
+#define CMAP_SIZE(X) cmap_##X##_size(map)
#define CMAP_BUCKETS(X) cmap_##X##_bucket_count(map)
#define CMAP_CLEAR(X) cmap_##X##_clear(&map)
#define CMAP_DTOR(X) cmap_##X##_del(&map)
diff --git a/benchmarks/crand_benchmark2.cpp b/benchmarks/crand_benchmark2.cpp
index 3ae6f8ab..ac7296fc 100644
--- a/benchmarks/crand_benchmark2.cpp
+++ b/benchmarks/crand_benchmark2.cpp
@@ -30,7 +30,7 @@ void test1(void)
diff = clock() - before;
printf("std::uniform:\t\t%.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, sum);
- c_forrange (30) printf("%02zd ", idist(rng));
+ c_forrange (30) printf("%02d ", idist(rng));
puts("");
c_forrange (8) printf("%f ", fdist(rng));
puts("\n");
diff --git a/examples/others/bytell_hash_map.hpp b/benchmarks/others/bytell_hash_map.hpp
index 2e348cdb..2e348cdb 100644
--- a/examples/others/bytell_hash_map.hpp
+++ b/benchmarks/others/bytell_hash_map.hpp
diff --git a/examples/others/flat_hash_map.hpp b/benchmarks/others/flat_hash_map.hpp
index ea20af93..ea20af93 100644
--- a/examples/others/flat_hash_map.hpp
+++ b/benchmarks/others/flat_hash_map.hpp
diff --git a/examples/others/hopscotch_growth_policy.h b/benchmarks/others/hopscotch_growth_policy.h
index 8c9f9694..8c9f9694 100644
--- a/examples/others/hopscotch_growth_policy.h
+++ b/benchmarks/others/hopscotch_growth_policy.h
diff --git a/examples/others/hopscotch_hash.h b/benchmarks/others/hopscotch_hash.h
index a97fa2b4..a97fa2b4 100644
--- a/examples/others/hopscotch_hash.h
+++ b/benchmarks/others/hopscotch_hash.h
diff --git a/examples/others/hopscotch_map.h b/benchmarks/others/hopscotch_map.h
index f9fa41f0..f9fa41f0 100644
--- a/examples/others/hopscotch_map.h
+++ b/benchmarks/others/hopscotch_map.h
diff --git a/examples/others/khash.h b/benchmarks/others/khash.h
index 61dabc4d..61dabc4d 100644
--- a/examples/others/khash.h
+++ b/benchmarks/others/khash.h
diff --git a/examples/others/khashl.h b/benchmarks/others/khashl.h
index 3542ba98..3542ba98 100644
--- a/examples/others/khashl.h
+++ b/benchmarks/others/khashl.h
diff --git a/examples/others/robin_hood.hpp b/benchmarks/others/robin_hood.hpp
index 2cf9e029..2cf9e029 100644
--- a/examples/others/robin_hood.hpp
+++ b/benchmarks/others/robin_hood.hpp
diff --git a/examples/others/sparsepp/spp.h b/benchmarks/others/sparsepp/spp.h
index 35d58492..35d58492 100644
--- a/examples/others/sparsepp/spp.h
+++ b/benchmarks/others/sparsepp/spp.h
diff --git a/examples/others/sparsepp/spp_config.h b/benchmarks/others/sparsepp/spp_config.h
index 46eeee5c..46eeee5c 100644
--- a/examples/others/sparsepp/spp_config.h
+++ b/benchmarks/others/sparsepp/spp_config.h
diff --git a/examples/others/sparsepp/spp_dlalloc.h b/benchmarks/others/sparsepp/spp_dlalloc.h
index f88aab7c..f88aab7c 100644
--- a/examples/others/sparsepp/spp_dlalloc.h
+++ b/benchmarks/others/sparsepp/spp_dlalloc.h
diff --git a/examples/others/sparsepp/spp_memory.h b/benchmarks/others/sparsepp/spp_memory.h
index cfaa108d..cfaa108d 100644
--- a/examples/others/sparsepp/spp_memory.h
+++ b/benchmarks/others/sparsepp/spp_memory.h
diff --git a/examples/others/sparsepp/spp_smartptr.h b/benchmarks/others/sparsepp/spp_smartptr.h
index fba3acfb..fba3acfb 100644
--- a/examples/others/sparsepp/spp_smartptr.h
+++ b/benchmarks/others/sparsepp/spp_smartptr.h
diff --git a/examples/others/sparsepp/spp_stdint.h b/benchmarks/others/sparsepp/spp_stdint.h
index 3adced9c..3adced9c 100644
--- a/examples/others/sparsepp/spp_stdint.h
+++ b/benchmarks/others/sparsepp/spp_stdint.h
diff --git a/examples/others/sparsepp/spp_timer.h b/benchmarks/others/sparsepp/spp_timer.h
index 48180f4d..48180f4d 100644
--- a/examples/others/sparsepp/spp_timer.h
+++ b/benchmarks/others/sparsepp/spp_timer.h
diff --git a/examples/others/sparsepp/spp_traits.h b/benchmarks/others/sparsepp/spp_traits.h
index 792f52f2..792f52f2 100644
--- a/examples/others/sparsepp/spp_traits.h
+++ b/benchmarks/others/sparsepp/spp_traits.h
diff --git a/examples/others/sparsepp/spp_utils.h b/benchmarks/others/sparsepp/spp_utils.h
index 4f2e9257..4f2e9257 100644
--- a/examples/others/sparsepp/spp_utils.h
+++ b/benchmarks/others/sparsepp/spp_utils.h
diff --git a/docs/clist_api.md b/docs/clist_api.md
index f3f17270..9ed0ea31 100644
--- a/docs/clist_api.md
+++ b/docs/clist_api.md
@@ -36,7 +36,6 @@ using_clist(str, cstr_t, cstr_del, cstr_compare_raw, const char*, cstr_to_raw, c
| Name | Purpose |
|:---------------------------|:---------------------|
| `clist_inits` | Initializer constant |
-| `clist_empty(list)` | `true` if list empty |
## Header file
@@ -55,7 +54,7 @@ void clist_X_clear(clist_X* self);
void clist_X_del(clist_X* self);
bool clist_X_empty(clist_X list);
-size_t clist_X_size(clist_X list);
+size_t clist_X_size(clist_X list); // note: O(n)
clist_X_value_t* clist_X_front(clist_X* self);
clist_X_value_t* clist_X_back(clist_X* self);
diff --git a/docs/cmap_api.md b/docs/cmap_api.md
index b1fd7a3a..322a4104 100644
--- a/docs/cmap_api.md
+++ b/docs/cmap_api.md
@@ -58,9 +58,6 @@ using_cmap(str, cstr_t, cstr_t, cstr_del, cstr_equals_raw, cstr_hash_raw,
| Name | Purpose |
|:-----------------------------------------|:-----------------------|
| `cmap_inits` | Initializer const |
-| `cmap_empty(map)` | Test for empty map |
-| `cmap_size(map)` | Get map size |
-| `cmap_capacity(map)` | Get map capacity |
| `c_try_emplace(self, ctype, key, ref)` | Emplace if key exist |
## Header file
diff --git a/docs/cset_api.md b/docs/cset_api.md
index 223dd1cf..ef87bef7 100644
--- a/docs/cset_api.md
+++ b/docs/cset_api.md
@@ -39,10 +39,6 @@ be replaced by `my` in all of the following documentation.
| Name | Purpose |
|:------------------------------------------------|:-------------------------|
| `cset_inits` | Initializer const |
-| `cset_empty(set)` | Test for empty set |
-| `cset_size(set)` | Get set size |
-| `cset_capacity(set)` | Get set capacity |
-| `c_try_emplace(self, ctype, key, ref)` | Emplace if key exist |
## Header file
diff --git a/docs/cvec_api.md b/docs/cvec_api.md
index 413b49c3..27667f13 100644
--- a/docs/cvec_api.md
+++ b/docs/cvec_api.md
@@ -37,10 +37,6 @@ using_cvec(str, cstr_t, cstr_del, cstr_compare_raw, const char*, cstr_to_raw, cs
| Name | Purpose |
|:---------------------------|:---------------------|
| `cvec_inits` | Initializer constant |
-| `cvec_empty(vec)` | true if vec is empty |
-| `cvec_size(vec)` | return vec length |
-| `cvec_capacity(vec)` | return vec capacity |
-
## Header file
diff --git a/examples/demos.c b/examples/demos.c
index cf32ca9d..5833bc5c 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -50,7 +50,7 @@ void vectordemo1()
cvec_ix_pop_back(&bignums); // erase the last
cvec_ix_erase_n(&bignums, 0, 1); // erase the first
- for (size_t i = 0; i < cvec_size(bignums); ++i) {
+ for (size_t i = 0; i < cvec_ix_size(bignums); ++i) {
printf("%zu: %zu\n", i, bignums.data[i]);
}
cvec_ix_del(&bignums);
@@ -165,10 +165,10 @@ void mapdemo3()
cmap_str_value_t *e = cmap_str_find(&table, "Make");
c_foreach (i, cmap_str, table)
printf("entry: %s: %s\n", i.ref->first.str, i.ref->second.str);
- printf("size %zu: remove: Make: %s\n", cmap_size(table), e->second.str);
+ printf("size %zu: remove: Make: %s\n", cmap_str_size(table), e->second.str);
cmap_str_erase(&table, "Make");
- printf("size %zu\n", cmap_size(table));
+ printf("size %zu\n", cmap_str_size(table));
c_foreach (i, cmap_str, table)
printf("entry: %s: %s\n", i.ref->first.str, i.ref->second.str);
cmap_str_del(&table); // frees key and value cstrs, and hash table.
diff --git a/stc/cdeq.h b/stc/cdeq.h
index b95bb45f..55168a81 100644
--- a/stc/cdeq.h
+++ b/stc/cdeq.h
@@ -359,7 +359,7 @@ STC_INLINE size_t _cdeq_safe_capacity(const void* base) {
return base ? ((const size_t *) base)[-1] : 0;
}
-static inline c_minf(double x, double y) { return x < y ? x : y; }
-static inline c_maxf(double x, double y) { return x > y ? x : y; }
+static inline double c_minf(double x, double y) { return x < y ? x : y; }
+static inline double c_maxf(double x, double y) { return x > y ? x : y; }
#endif
diff --git a/stc/clist.h b/stc/clist.h
index af31bf96..a4636a29 100644
--- a/stc/clist.h
+++ b/stc/clist.h
@@ -86,7 +86,6 @@
} clist_##X##_iter_t
#define clist_inits {NULL}
-#define clist_empty(list) ((list).last == NULL)
#define c_emplace_after(self, ctype, pos, ...) do { \
ctype* __self = self; \
@@ -110,7 +109,7 @@ STC_API size_t _clist_size(const clist_void* self);
STC_INLINE clist_##X \
clist_##X##_init(void) {clist_##X x = clist_inits; return x;} \
STC_INLINE bool \
- clist_##X##_empty(clist_##X ls) {return clist_empty(ls);} \
+ clist_##X##_empty(clist_##X ls) {return ls.last == NULL;} \
STC_INLINE size_t \
clist_##X##_size(clist_##X ls) {return _clist_size((const clist_void*) &ls);} \
STC_INLINE Value \
diff --git a/stc/cmap.h b/stc/cmap.h
index dfaf0447..8d5fd7cd 100644
--- a/stc/cmap.h
+++ b/stc/cmap.h
@@ -55,11 +55,7 @@ int main(void) {
#include <string.h>
#define cmap_inits {NULL, NULL, 0, 0, 0.85f, 0.15f}
-#define cmap_empty(m) ((m).size == 0)
-#define cmap_size(m) ((size_t) (m).size)
#define cset_inits cmap_inits
-#define cset_empty(s) cmap_empty(s)
-#define cset_size(s) cmap_size(s)
#define c_try_emplace(self, ctype, key, val) do { \
ctype##_result_t __r = ctype##_insert_key_(self, key); \
diff --git a/stc/cvec.h b/stc/cvec.h
index 9d68e869..7c693f0d 100644
--- a/stc/cvec.h
+++ b/stc/cvec.h
@@ -28,9 +28,6 @@
#include <string.h>
#define cvec_inits {NULL}
-#define cvec_size(vec) _cvec_safe_size((vec).data)
-#define cvec_capacity(vec) _cvec_safe_capacity((vec).data)
-#define cvec_empty(vec) (cvec_size(vec) == 0)
#define using_cvec(...) c_MACRO_OVERLOAD(using_cvec, __VA_ARGS__)
#define using_cvec_2(X, Value) \
@@ -56,12 +53,14 @@
\
STC_INLINE cvec_##X \
cvec_##X##_init(void) {cvec_##X vec = cvec_inits; return vec;} \
- STC_INLINE bool \
- cvec_##X##_empty(cvec_##X vec) {return cvec_empty(vec);} \
STC_INLINE size_t \
- cvec_##X##_size(cvec_##X vec) {return cvec_size(vec);} \
+ cvec_##X##_size(cvec_##X vec) \
+ {return vec.data ? ((const size_t *) vec.data)[-2] : 0;} \
STC_INLINE size_t \
- cvec_##X##_capacity(cvec_##X vec) {return cvec_capacity(vec);} \
+ cvec_##X##_capacity(cvec_##X vec) \
+ {return vec.data ? ((const size_t *) vec.data)[-1] : 0;} \
+ STC_INLINE bool \
+ cvec_##X##_empty(cvec_##X vec) {return !cvec_##X##_size(vec);} \
STC_INLINE Value \
cvec_##X##_value_from_raw(RawValue rawValue) {return valueFromRaw(rawValue);} \
STC_INLINE void \
@@ -159,7 +158,7 @@
cvec_##X##_back(cvec_##X* self) {return self->data + _cvec_size(self) - 1;} \
STC_INLINE cvec_##X##_value_t* \
cvec_##X##_at(cvec_##X* self, size_t i) { \
- assert(i < cvec_size(*self)); \
+ assert(i < cvec_##X##_size(*self)); \
return self->data + i; \
} \
\
@@ -171,7 +170,7 @@
} \
STC_INLINE void \
cvec_##X##_sort(cvec_##X* self) { \
- cvec_##X##_sort_with(self, 0, cvec_size(*self), cvec_##X##_value_compare); \
+ cvec_##X##_sort_with(self, 0, cvec_##X##_size(*self), cvec_##X##_value_compare); \
} \
\
STC_INLINE cvec_##X##_iter_t \
@@ -180,7 +179,7 @@
} \
STC_INLINE cvec_##X##_iter_t \
cvec_##X##_end(const cvec_##X* self) { \
- cvec_##X##_iter_t it = {self->data + cvec_size(*self)}; return it; \
+ cvec_##X##_iter_t it = {self->data + cvec_##X##_size(*self)}; return it; \
} \
STC_INLINE void \
cvec_##X##_next(cvec_##X##_iter_t* it) {++it->ref;} \
@@ -199,8 +198,8 @@
\
STC_DEF void \
cvec_##X##_push_n(cvec_##X *self, const cvec_##X##_input_t arr[], size_t n) { \
- cvec_##X##_reserve(self, cvec_size(*self) + n); \
- cvec_##X##_value_t* p = self->data + cvec_size(*self); \
+ cvec_##X##_reserve(self, cvec_##X##_size(*self) + n); \
+ cvec_##X##_value_t* p = self->data + _cvec_size(self); \
for (size_t i=0; i < n; ++i) *p++ = valueFromRaw(arr[i]); \
_cvec_size(self) += n; \
} \
@@ -220,9 +219,10 @@
\
STC_DEF void \
cvec_##X##_reserve(cvec_##X* self, size_t cap) { \
- if (cap > cvec_capacity(*self)) { \
- size_t len = cvec_size(*self); \
- size_t* rep = (size_t *) c_realloc(_cvec_alloced(self->data), 2 * sizeof(size_t) + cap * sizeof(Value)); \
+ size_t* rep; \
+ if (cap > cvec_##X##_capacity(*self)) { \
+ size_t len = cvec_##X##_size(*self); \
+ rep = (size_t *) c_realloc(_cvec_alloced(self->data), 2 * sizeof(size_t) + cap * sizeof(Value)); \
self->data = (Value *) (rep + 2); \
rep[0] = len; \
rep[1] = cap; \
@@ -231,21 +231,21 @@
STC_DEF void \
cvec_##X##_resize(cvec_##X* self, size_t size, Value null_val) { \
cvec_##X##_reserve(self, size); \
- for (size_t i=cvec_size(*self); i<size; ++i) self->data[i] = null_val; \
+ for (size_t i=cvec_##X##_size(*self); i<size; ++i) self->data[i] = null_val; \
if (self->data) _cvec_size(self) = size; \
} \
\
STC_DEF void \
cvec_##X##_push_back(cvec_##X* self, Value value) { \
- size_t len = cvec_size(*self); \
- if (len == cvec_capacity(*self)) \
+ size_t len = cvec_##X##_size(*self); \
+ if (len == cvec_##X##_capacity(*self)) \
cvec_##X##_reserve(self, 4 + len * 3 / 2); \
self->data[_cvec_size(self)++] = value; \
} \
\
STC_DEF cvec_##X \
cvec_##X##_clone(cvec_##X vec) { \
- size_t len = cvec_size(vec); \
+ size_t len = cvec_##X##_size(vec); \
cvec_##X out = cvec_##X##_with_capacity(len); \
cvec_##X##_insert_range_p(&out, out.data, vec.data, vec.data + len); \
return out; \
@@ -253,8 +253,8 @@
\
STC_DEF cvec_##X##_iter_t \
cvec_##X##_insert_range_p(cvec_##X* self, cvec_##X##_value_t* pos, const cvec_##X##_value_t* first, const cvec_##X##_value_t* finish) { \
- size_t len = finish - first, idx = pos - self->data, size = cvec_size(*self); \
- if (size + len > cvec_capacity(*self)) \
+ size_t len = finish - first, idx = pos - self->data, size = cvec_##X##_size(*self); \
+ if (size + len > cvec_##X##_capacity(*self)) \
cvec_##X##_reserve(self, 4 + (size + len) * 3 / 2); \
_cvec_size(self) += len; \
pos = self->data + idx; \
@@ -311,15 +311,6 @@ typedef int(*_cvec_cmp)(const void*, const void*);
STC_EXTERN_IMPORT void qsort(void *base, size_t nitems, size_t size, _cvec_cmp cmp);
#define _cvec_size(self) ((size_t *) (self)->data)[-2]
-
-STC_INLINE size_t* _cvec_alloced(void* data) {
- return data ? ((size_t *) data) - 2 : NULL;
-}
-STC_INLINE size_t _cvec_safe_size(const void* data) {
- return data ? ((const size_t *) data)[-2] : 0;
-}
-STC_INLINE size_t _cvec_safe_capacity(const void* data) {
- return data ? ((const size_t *) data)[-1] : 0;
-}
+#define _cvec_alloced(data) ((data) ? ((size_t *) (data)) - 2 : NULL)
#endif