summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--README.md156
-rw-r--r--examples/README.md14
-rw-r--r--examples/advanced.c16
-rw-r--r--examples/benchmark.c24
-rw-r--r--examples/bits.c12
-rw-r--r--examples/complex.c20
-rw-r--r--examples/demos.c58
-rw-r--r--examples/geek1.c6
-rw-r--r--examples/geek2.c12
-rw-r--r--examples/geek3.c8
-rw-r--r--examples/geek4.c34
-rw-r--r--examples/geek5.c18
-rw-r--r--examples/geek6.c4
-rw-r--r--examples/geek7.c14
-rw-r--r--examples/heap.c16
-rw-r--r--examples/inits.c16
-rw-r--r--examples/list.c31
-rw-r--r--examples/mapmap.c8
-rw-r--r--examples/prime.c13
-rw-r--r--examples/priority.c12
-rw-r--r--examples/rngbirthday.c21
-rw-r--r--examples/rngtest.c22
-rw-r--r--stc/carray.h84
-rw-r--r--stc/cbitset.h74
-rw-r--r--stc/cdefs.h22
-rw-r--r--stc/clist.h178
-rw-r--r--stc/cmap.h240
-rw-r--r--stc/copt.h (renamed from stc/coption.h)38
-rw-r--r--stc/crand.h148
-rw-r--r--stc/crandom.h108
-rw-r--r--stc/cstr.h162
-rw-r--r--stc/cvec.h100
-rw-r--r--stc/cvecpq.h62
33 files changed, 896 insertions, 855 deletions
diff --git a/README.md b/README.md
index 808ddbe2..03ef8bf3 100644
--- a/README.md
+++ b/README.md
@@ -19,14 +19,14 @@ in *O*(1). Also contains various *splice* functions and (merge) *sort*.
The usage of the containers is similar to the C++ standard containers, so it should be easier for those who are familiar with them.
-All containers mentioned above, except for CStr are generic (similar to templates in C++). A simple example:
+All containers mentioned above, except for cstr are generic (similar to templates in C++). A simple example:
```
#include <stc/cvec.h>
-declare_CVec(i, int);
+declare_cvec(i, int);
int main(void) {
- CVec_i vec = cvec_init;
- cvec_i_pushBack(&vec, 42);
+ cvec_i vec = cvec_init;
+ cvec_i_push_back(&vec, 42);
cvec_i_destroy(&vec);
}
```
@@ -52,17 +52,17 @@ Because it is headers only, files can simply be included in your program. The fu
#include <stc/cmap.h>
#include <stc/cvec.h>
-declare_CMap(ii, int, int); // map
-declare_CMap(ix, int64_t); // set
-declare_CVec(i, int);
+declare_cmap(ii, int, int); // map
+declare_cmap(ix, int64_t); // set
+declare_cvec(i, int);
...
```
Performance
-----------
-This library is very efficent. Containers have templated intrusive elements. One of the most performance critical containers is the **CMap / CSet**. Luckily, CMap is among the fastest C/C++ map implementations available: **examples/benchmark.c** compiled with g++ v9.2.0 -O3 on windows (the results are similar with VC++ and g++ on linux):
+This library is very efficent. Containers have templated intrusive elements. One of the most performance critical containers is the **cmap / cset**. Luckily, cmap is among the fastest C/C++ map implementations available: **examples/benchmark.c** compiled with g++ v9.2.0 -O3 on windows (the results are similar with VC++ and g++ on linux):
-**CMAP**=*CMap*, KMAP=*khash*, UMAP=*std::unordered_map*, BMAP=*ska::bytell_hash_map*, FMAP=*ska::flat_hash_map*, RMAP=*robin_hood::unordered_map*
+**CMAP**=*cmap*, KMAP=*khash*, UMAP=*std::unordered_map*, BMAP=*ska::bytell_hash_map*, FMAP=*ska::flat_hash_map*, RMAP=*robin_hood::unordered_map*
```
Random keys are in range [0, 2^20):
map<uint64_t, uint64_t>: 7000000 repeats of Insert random key + (try to) remove a different random key:
@@ -93,49 +93,49 @@ Memory efficiency
-----------------
The containers are memory efficent, i.e. they occupy as little memory as practical possible.
-- **CStr**, **CVec**: Representaion: one pointer size. The size and capacity is stored as part of the heap allocation that also holds the vector elements.
-- **CList**: Representation: one pointer size. Each node allocates block storing value and next pointer.
-- **CSet**: Representation: 4 pointers size. CSet uses one table of keys, and one table of "used/hash-value", which occupies only one byte per bucket.
-- **CMap**: Same as CSet, but this uses a table of (key, value) pairs, not only keys.
-- **CArray**: CArray1, CArray2 and CArray3. Representation: One pointers, plus 1, 2, or 3 size_t variables to store dimensions. Elements are stored as one block of heap memory.
+- **cstr**, **cvec**: Representaion: one pointer size. The size and capacity is stored as part of the heap allocation that also holds the vector elements.
+- **clist**: Representation: one pointer size. Each node allocates block storing value and next pointer.
+- **cset**: Representation: 4 pointers size. cset uses one table of keys, and one table of "used/hash-value", which occupies only one byte per bucket.
+- **cmap**: Same as cset, but this uses a table of (key, value) pairs, not only keys.
+- **carray**: carray1, carray2 and carray3. Representation: One pointers, plus 1, 2, or 3 size_t variables to store dimensions. Elements are stored as one block of heap memory.
-CMap, CSet and CVec discussion
+cmap, cset and cvec discussion
----------------------------
-**CMap/CSet** are the most complex of the containers (although, currently only ~370 lines of code). It uses open hashing, but does not rely on power-of-two size table, nor prime number lengths, and it does not have tombstone buckets. It is still among the fastest hash-tables, as shown above. The default max load-factor is 0.85, and it shrinks (and rehashes) when load-factor goes below 0.15, by default (can be set per hash container).
+**cmap/cset** are the most complex of the containers (although, currently only ~370 lines of code). It uses open hashing, but does not rely on power-of-two size table, nor prime number lengths, and it does not have tombstone buckets. It is still among the fastest hash-tables, as shown above. The default max load-factor is 0.85, and it shrinks (and rehashes) when load-factor goes below 0.15, by default (can be set per hash container).
-You may customize the destroy-, hash- and equals- function. It also supports a few other arguments in the declare-statement that allows to define a convertion from a raw/literal type to the key-type specified. This is handy when e.g. having CStr as key, as it enables the usage of string literals as key in *put() and *get() functions, instead of requering a constructed CStr. Without it, you would have to write:
+You may customize the destroy-, hash- and equals- function. It also supports a few other arguments in the declare-statement that allows to define a convertion from a raw/literal type to the key-type specified. This is handy when e.g. having cstr as key, as it enables the usage of string literals as key in *put() and *get() functions, instead of requering a constructed cstr. Without it, you would have to write:
```
-declare_CMap(si, CStr, int);
+declare_cmap(si, cstr, int);
...
cmap_si_put(&map, cstr_make("mykey"), 12);
```
but the main incovenience is with lookup:
```
-CStr lookup = cstr_make("mykey");
+cstr lookup = cstr_make("mykey");
int x = cmap_si_get(&map, lookup)->value;
cstr_destroy(&lookup);
```
-To avoid this, use *declare_CMap_str()*:
+To avoid this, use *declare_cmap_str()*:
```
-declare_CMap_str(si, int);
+declare_cmap_str(si, int);
...
-CMap_si map = cmap_init;
-cmap_si_put(&map, "mykey", 12); // constructs a CStr key from the const char* internally.
+cmap_si map = cmap_init;
+cmap_si_put(&map, "mykey", 12); // constructs a cstr key from the const char* internally.
int x = cmap_si_get(&map, "mykey")->value; // no allocation of string key happens here.
cmap_si_destroy(&map);
```
An alternative would be to use *char* * as key type, but you would have to manage the memory of the hash char* keys yourself.
-Note that this customization is also available for **CVec**, but only affects the *find()* function currently. See *declare_CVec_str()*.
+Note that this customization is also available for **cvec**, but only affects the *find()* function currently. See *declare_cvec_str()*.
Also look at **examples/advanced.c**, it demonstrates how to use a custom struct as a hash map key, using the feature mentioned.
Example usages
--------------
The first example has a very complex nested container type, which demonstrates the power of this library. Look at the simpler examples below to understand it better. The example adds an element into the data structure, and then accesses it. The type used, with c++ template syntax is:
-**CMapMap**< **CStr**, **CMapMap**< *int*, **CList**< **CArray2**< *float* >>>>
+**cmapMap**< **cstr**, **cmapMap**< *int*, **clist**< **carray2**< *float* >>>>
-Note: The *cmap_sm_destroy(&theMap)* call below, will destroy all the nested containers including the memory allocated for CStr keys in theMap object.
+Note: The *cmap_sm_destroy(&theMap)* call below, will destroy all the nested containers including the memory allocated for cstr keys in theMap object.
```
#include <stc/cstr.h>
#include <stc/cmap.h>
@@ -144,41 +144,41 @@ Note: The *cmap_sm_destroy(&theMap)* call below, will destroy all the nested con
void verify_destroy(float* v) {printf("destroy %g\n", *v);}
-declare_CArray(f, float, verify_destroy); // you should omit the last argument - float type need no destroy.
-declare_CList(t2, CArray2_f, carray2_f_destroy, c_noCompare);
-declare_CMap(il, int, CList_t2, clist_t2_destroy);
-declare_CMap_str(sm, CMap_il, cmap_il_destroy);
+declare_carray(f, float, verify_destroy); // you should omit the last argument - float type need no destroy.
+declare_clist(t2, carray2_f, carray2_f_destroy, c_no_compare);
+declare_cmap(il, int, clist_t2, clist_t2_destroy);
+declare_cmap_str(sm, cmap_il, cmap_il_destroy);
int main() {
int xdim = 4, ydim = 6;
int x = 2, y = 5, entry = 42;
- CMap_sm theMap = cmap_init;
+ cmap_sm theMap = cmap_init;
{
// Construct.
- CArray2_f table = carray2_f_make(xdim, ydim, 0.f);
- CList_t2 tableList = clist_init;
- CMap_il listMap = cmap_init;
+ carray2_f table = carray2_f_make(xdim, ydim, 0.f);
+ clist_t2 tableList = clist_init;
+ cmap_il listMap = cmap_init;
// Put in some data.
carray2_f_data(table, x)[y] = 3.1415927; // table[x][y]
- clist_t2_pushBack(&tableList, table);
+ clist_t2_push_back(&tableList, table);
cmap_il_put(&listMap, entry, tableList);
cmap_sm_put(&theMap, "First", listMap);
}
// Access the data entry
- CArray2_f table = clist_back(cmap_il_get(&cmap_sm_get(&theMap, "First")->value, entry)->value);
+ carray2_f table = clist_back(cmap_il_get(&cmap_sm_get(&theMap, "First")->value, entry)->value);
printf("value is: %f\n", carray2_f_value(table, x, y));
cmap_sm_destroy(&theMap); // free up the whole shebang!
}
```
-**CStr**
+**cstr** string example.
```
#include <stc/cstr.h>
int main() {
- CStr s1 = cstr_make("one-nine-three-seven-five");
+ cstr s1 = cstr_make("one-nine-three-seven-five");
printf("%s.\n", s1.str);
cstr_insert(&s1, 3, "-two");
@@ -197,21 +197,21 @@ int main() {
printf("append: %s\n", s1.str);
cstr_destroy(&s1);
- CStr s2 = cstr_from("Index %d: %f", 123, 4.56);
+ cstr s2 = cstr_from("Index %d: %f", 123, 4.56);
cstr_destroy(&s2);
}
```
-**CVec** of *int64_t*
+**cvec** of *int64_t*.
```
#include <stc/cvec.h>
-declare_CVec(ix, int64_t); // ix is just an example tag name, use anything without underscore.
+declare_cvec(ix, int64_t); // ix is just an example tag name, use anything without underscore.
int main() {
- CVec_ix bignums = cvec_init; // = (CVec_ix) cvec_init; if initializing after declaration.
+ cvec_ix bignums = cvec_init; // use cvec_ix_init() if initializing after declaration.
cvec_ix_reserve(&bignums, 100);
for (size_t i = 0; i<100; ++i)
- cvec_ix_pushBack(&bignums, i * i * i);
- cvec_ix_popBack(&bignums); // erase the last
+ cvec_ix_push_back(&bignums, i * i * i);
+ cvec_ix_pop_back(&bignums); // erase the last
uint64_t value;
for (size_t i = 0; i < cvec_size(bignums); ++i)
@@ -219,30 +219,30 @@ int main() {
cvec_ix_destroy(&bignums);
}
```
-**CVec** of *CStr*
+**cvec** of *cstr*.
```
#include <stc/cstr.h>
#include <stc/cvec.h>
-declare_CVec_str();
+declare_cvec_str();
int main() {
- CVec_str names = cvec_init;
- cvec_str_pushBack(&names, cstr_make("Mary"));
- cvec_str_pushBack(&names, cstr_make("Joe"));
+ cvec_str names = cvec_init;
+ cvec_str_push_back(&names, cstr_make("Mary"));
+ cvec_str_push_back(&names, cstr_make("Joe"));
cstr_assign(&names.data[1], cstr_make("Jake")); // replace Joe
printf("%s\n", names.data[1].str); // Access the string char*
cvec_str_destroy(&names);
}
```
-**CMap** of *int -> int*
+**cmap** of *int -> int*.
```
#include <stdio.h>
#include <stc/cmap.h>
-declare_CMap(ii, int, int);
+declare_cmap(ii, int, int);
int main() {
- CMap_ii nums = cmap_init;
+ cmap_ii nums = cmap_init;
cmap_ii_put(&nums, 8, 64);
cmap_ii_put(&nums, 11, 121);
@@ -250,14 +250,14 @@ int main() {
cmap_ii_destroy(&nums);
}
```
-**CSet** of *CStr*
+**cset** of *cstr*.
```
#include <stc/cstr.h>
#include <stc/cmap.h>
-declare_CSet_str(); // CStr set. See the discussion above.
+declare_cset_str(); // cstr set. See the discussion above.
int main() {
- CSet_str words = cset_init;
+ cset_str words = cset_init;
cset_str_put(&words, "Hello");
cset_str_put(&words, "Groovy");
cset_str_erase(&words, "Hello");
@@ -268,37 +268,37 @@ int main() {
cset_str_destroy(&words);
}
```
-**CMap** of *CStr -> CStr*. Temporary CStr values are created by *cstr_make()*, and moved into the container
+**cmap** of *cstr -> cstr*. Temporary cstr values are created by *cstr_make()*, and moved into the container
```
#include <stc/cstr.h>
#include <stc/cmap.h>
-declare_CMap_str(ss, CStr, cstr_destroy);
+declare_cmap_str(ss, cstr, cstr_destroy);
int main() {
- CMap_ss table = cmap_init;
+ cmap_ss table = cmap_init;
cmap_ss_put(&table, "Make", cstr_make("my"));
cmap_ss_put(&table, "Sunny", cstr_make("day"));
printf("Sunny: %s\n", cmap_ss_get(table, "Sunny")->value.str);
cmap_ss_erase(&table, "Make");
printf("size %d\n", cmap_size(table));
- cmap_ss_destroy(&table); // frees key and value CStrs, and hash table (CVec).
+ cmap_ss_destroy(&table); // frees key and value cstrs, and hash table (cvec).
}
```
-**CList** of *int64_t*. Similar to c++ *std::forward_list*, but can do both *pushFront()* and *pushBack()*.
+**clist** of *int64_t*. Similar to c++ *std::forward_list*, but can do both *pushFront()* and *pushBack()*.
```
#include <stdio.h>
#include <time.h>
#include <stc/clist.h>
#include <stc/crandom.h>
-declare_CList(i, uint64_t);
+declare_clist(i, uint64_t);
int main() {
- CList_i list = clist_init;
+ clist_i list = clist_init;
int N = 2000000, n;
- sfc64_t rng = sfc64_seed(time(NULL));
- for (int i=0; i<N; ++i) // one million random numbers
- clist_i_pushBack(&list, sfc64_rand(&rng));
+ crandom64_t rng = crandom64_uniform_engine(time(NULL));
+ for (int i=0; i<N; ++i) // two million random numbers
+ clist_i_push_back(&list, crandom64_uinform_int(&rng));
n = 0;
c_foreach (i, clist_i, list)
if (++n % (N/50) == 0) printf("%10d: %zu\n", n, i.item->value);
@@ -312,27 +312,27 @@ int main() {
clist_i_destroy(&list);
}
```
-**CArray**. 1D, 2D and 3D arrays, heap allocated in one memory block. *CArray3* can have sub-array "views" of *CArray2* and *CArray1* etc., as shown in the following example.
+**carray**. 1d, 2d and 3d arrays, allocated from heap in one memory block. *carray3* may have sub-array "views" of *carray2* and *carray1* etc., as shown in the following example:
```
#include <stdio.h>
#include <stc/carray.h>
-declare_CArray(f, float);
+declare_carray(f, float);
int main()
{
- CArray3_f a3 = carray3_f_make(30, 20, 10, 0.f);
- carray3_f_data(a3, 5, 4)[3] = 10.2f; // a3[5][4][3]
- CArray2_f a2 = carray3_f_at(a3, 5); // sub-array reference (no data copy).
+ carray3f a3 = carray3f_make(30, 20, 10, 0.f);
+ carray3f_data(a3, 5, 4)[3] = 10.2f; // a3[5][4][3]
+ carray2f a2 = carray3f_at(a3, 5); // sub-array reference (no data copy).
- printf("%f\n", carray2_f_value(a2, 4, 3)); // readonly lookup a2[4][3] (=10.2f)
- printf("%f\n", carray2_f_data(a2, 4)[3]); // same, but this is writable.
- printf("%f\n", carray2_f_at(a2, 4).data[3]); // same, via sub-array access.
+ printf("%f\n", carray2f_value(a2, 4, 3)); // readonly lookup a2[4][3] (=10.2f)
+ printf("%f\n", carray2f_data(a2, 4)[3]); // same, but this is writable.
+ printf("%f\n", carray2f_at(a2, 4).data[3]); // same, via sub-array access.
- printf("%f\n", carray3_f_value(a3, 5, 4, 3)); // same data location, via a3 array.
- printf("%f\n", carray3_f_data(a3, 5, 4)[3]);
- printf("%f\n", carray3_f_at2(a3, 5, 4).data[3]);
+ printf("%f\n", carray3f_value(a3, 5, 4, 3)); // same data location, via a3 array.
+ printf("%f\n", carray3f_data(a3, 5, 4)[3]);
+ printf("%f\n", carray3f_at2(a3, 5, 4).data[3]);
- carray2_f_destroy(&a2); // does nothing, since it is a sub-array.
- carray3_f_destroy(&a3); // also invalidates a2.
+ carray2f_destroy(&a2); // does nothing, since it is a sub-array.
+ carray3f_destroy(&a3); // also invalidates a2.
}
```
diff --git a/examples/README.md b/examples/README.md
index 4d43a7d2..ededc828 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -6,7 +6,7 @@ Contains various examples and benchmarks.
advanced.c Example
------------------
-This demonstrates how to customize hash **CMap** with a user-defined key-type. You need to define two things:
+This demonstrates how to customize hash **cmap** with a user-defined key-type. You need to define two things:
1. A hash function; calculates the hash value given an object of the key-type.
@@ -40,8 +40,8 @@ int vikingvw_equals(const VikingVw* x, const VikingVw* y) {
And the Viking data struct:
```
typedef struct Viking {
- CStr name;
- CStr country;
+ cstr name;
+ cstr country;
} Viking;
@@ -58,9 +58,9 @@ Viking viking_fromVw(VikingVw vw) {
}
```
-With this in place, we use the full declare_CMap() macro to define {Viking -> int} hash map type:
+With this in place, we use the full declare_cmap() macro to define {Viking -> int} hash map type:
```
-declare_CMap(vk, Viking, int, c_defaultDestroy, vikingvw_equals, vikingvw_hash,
+declare_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash,
viking_destroy, VikingVw, viking_getVw, viking_fromVw);
```
CMap_vk uses vikingvw_hash() for hash value calculations, and vikingvw_equals() for equality test. cmap_vk_destroy() will free all memory allocated for Viking keys and the hash table values.
@@ -68,13 +68,13 @@ Finally, the demo:
```
int main()
{
- CMap_vk vikings = cmap_init;
+ cmap_vk vikings = cmap_init;
// emplace constructs the keys
cmap_vk_put(&vikings, (VikingVw) {"Einar", "Norway"}, 20);
cmap_vk_put(&vikings, (VikingVw) {"Olaf", "Denmark"}, 24);
cmap_vk_put(&vikings, (VikingVw) {"Harald", "Iceland"}, 12);
- CMapEntry_vk* e = cmap_vk_get(&vikings, (VikingVw) {"Einar", "Norway"});
+ cmapentry_vk* e = cmap_vk_get(&vikings, (VikingVw) {"Einar", "Norway"});
e->value += 5; // update
c_foreach (k, cmap_vk, vikings) {
diff --git a/examples/advanced.c b/examples/advanced.c
index 68ddc9ef..68732544 100644
--- a/examples/advanced.c
+++ b/examples/advanced.c
@@ -1,5 +1,5 @@
/*
- * To be able to use CMap with a user-defined key-type, you need to define two things:
+ * To be able to use cmap with a user-defined key-type, you need to define two things:
* 1. A hash function; this must be a function that calculates the hash value given
* an object of the key-type.
* 2. A comparison function for equality.
@@ -34,8 +34,8 @@ int vikingvw_equals(const VikingVw* x, const VikingVw* y) {
// Viking data struct -----------------------
typedef struct Viking {
- CStr name;
- CStr country;
+ cstr_t name;
+ cstr_t country;
} Viking;
@@ -52,25 +52,25 @@ Viking viking_fromVw(VikingVw vw) {
}
-// Using the full declare_CMap() macro to define [Viking -> int] hash map type:
-declare_CMap(vk, Viking, int, c_defaultDestroy, vikingvw_equals, vikingvw_hash,
+// Using the full declare_cmap() macro to define [Viking -> int] hash map type:
+declare_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash,
viking_destroy, VikingVw, viking_getVw, viking_fromVw);
-// CMap_vk uses vikingvw_hash() for hash value calculations, and vikingvw_equals() for equality test.
+// cmap_vk uses vikingvw_hash() for hash value calculations, and vikingvw_equals() for equality test.
// cmap_vk_destroy() will free all memory allocated for Viking keys and the hash table values.
// Main ----------------------------
int main()
{
- CMap_vk vikings = cmap_init;
+ cmap_vk vikings = cmap_init;
c_push(&vikings, cmap_vk, c_items(
{{"Einar", "Norway"}, 20},
{{"Olaf", "Denmark"}, 24},
{{"Harald", "Iceland"}, 12},
));
- CMapEntry_vk* e = cmap_vk_find(&vikings, (VikingVw) {"Einar", "Norway"});
+ cmapentry_vk* e = cmap_vk_find(&vikings, (VikingVw) {"Einar", "Norway"});
e->value += 5; // update
c_foreach (k, cmap_vk, vikings) {
diff --git a/examples/benchmark.c b/examples/benchmark.c
index 1c69b8ab..f8536a16 100644
--- a/examples/benchmark.c
+++ b/examples/benchmark.c
@@ -1,4 +1,4 @@
-#include <stc/crandom.h>
+#include <stc/crand.h>
#include <stc/cstr.h>
#include <stc/cmap.h>
#include "others/khash.h"
@@ -14,25 +14,25 @@
// Visual Studio: compile with -TP to force C++: cl -TP -EHsc -O2 benchmark.c
-declare_CMap(ii, int64_t, int64_t, c_defaultDestroy, c_defaultEquals, c_fibonacciHash64);
+declare_cmap(ii, int64_t, int64_t, c_default_destroy, c_default_equals, c_fibonacci_hash64);
KHASH_MAP_INIT_INT64(ii, uint64_t)
size_t seed;
-static const float maxLoadFactor = 0.77f;
+static const float max_load_factor = 0.77f;
-CRand64 rng;
-#define SEED(s) rng = crand64_init(seed)
-#define RAND(N) (crand64_gen(&rng) & ((1 << N) - 1))
+crand_eng64_t rng;
+#define SEED(s) rng = crand_eng64_init(seed)
+#define RAND(N) (crand_gen_i64(&rng) & ((1 << N) - 1))
-#define CMAP_SETUP(tag, Key, Value) CMap_##tag map = cmap_init \
- ; cmap_##tag##_setLoadFactors(&map, maxLoadFactor, 0.0)
+#define CMAP_SETUP(tag, Key, Value) cmap_##tag map = cmap_init \
+ ; cmap_##tag##_set_load_factors(&map, max_load_factor, 0.0)
#define CMAP_PUT(tag, key, val) cmap_##tag##_put(&map, key, val)->value
#define CMAP_ERASE(tag, key) cmap_##tag##_erase(&map, key)
#define CMAP_FIND(tag, key) (cmap_##tag##_find(map, key) != NULL)
#define CMAP_SIZE(tag) cmap_size(map)
-#define CMAP_BUCKETS(tag) cmap_bucketCount(map)
+#define CMAP_BUCKETS(tag) (map).bucket_count
#define CMAP_CLEAR(tag) cmap_##tag##_destroy(&map)
#define KMAP_SETUP(tag, Key, Value) khash_t(ii)* map = kh_init(ii); khiter_t ki; int ret
@@ -43,7 +43,7 @@ CRand64 rng;
#define KMAP_BUCKETS(tag) ((size_t) kh_n_buckets(map))
#define KMAP_CLEAR(tag) kh_destroy(ii, map)
-#define UMAP_SETUP(tag, Key, Value) std::unordered_map<Key, Value> map; map.max_load_factor(maxLoadFactor)
+#define UMAP_SETUP(tag, Key, Value) std::unordered_map<Key, Value> map; map.max_load_factor(max_load_factor)
#define UMAP_PUT(tag, key, val) (map[key] = val)
#define UMAP_FIND(tag, key) (map.find(key) != map.end())
#define UMAP_ERASE(tag, key) map.erase(key)
@@ -51,7 +51,7 @@ CRand64 rng;
#define UMAP_BUCKETS(tag) map.bucket_count()
#define UMAP_CLEAR(tag) map.clear()
-#define BMAP_SETUP(tag, Key, Value) ska::bytell_hash_map<Key, Value> map; map.max_load_factor(maxLoadFactor)
+#define BMAP_SETUP(tag, Key, Value) ska::bytell_hash_map<Key, Value> map; map.max_load_factor(max_load_factor)
#define BMAP_PUT(tag, key, val) (map[key] = val)
#define BMAP_FIND(tag, key) (map.find(key) != map.end())
#define BMAP_ERASE(tag, key) map.erase(key)
@@ -59,7 +59,7 @@ CRand64 rng;
#define BMAP_BUCKETS(tag) map.bucket_count()
#define BMAP_CLEAR(tag) map.clear()
-#define FMAP_SETUP(tag, Key, Value) ska::flat_hash_map<Key, Value> map; map.max_load_factor(maxLoadFactor)
+#define FMAP_SETUP(tag, Key, Value) ska::flat_hash_map<Key, Value> map; map.max_load_factor(max_load_factor)
#define FMAP_PUT(tag, key, val) (map[key] = val)
#define FMAP_FIND(tag, key) (map.find(key) != map.end())
#define FMAP_ERASE(tag, key) map.erase(key)
diff --git a/examples/bits.c b/examples/bits.c
index 16adaf96..bf9125de 100644
--- a/examples/bits.c
+++ b/examples/bits.c
@@ -2,7 +2,7 @@
#include <stc/cbitset.h>
int main() {
- CBitset set = cbitset_make(23, true);
+ cbitset_t set = cbitset_make(23, true);
printf("count %zu, %zu\n", cbitset_count(set), set.size);
cbitset_reset(&set, 9);
cbitset_resize(&set, 43, false);
@@ -15,14 +15,14 @@ int main() {
cbitset_resize(&set, 77, true);
cbitset_resize(&set, 93, false);
cbitset_resize(&set, 102, true);
- cbitset_setTo(&set, 99, false);
+ cbitset_set_to(&set, 99, false);
printf("%4zu: ", set.size);
for (int i=0; i<set.size; ++i)
printf("%d", cbitset_test(set, i));
puts("");
- CBitset s2 = cbitset_from(set);
- cbitset_flipAll(&s2);
+ cbitset_t s2 = cbitset_from(set);
+ cbitset_flip_all(&s2);
cbitset_set(&s2, 16);
cbitset_set(&s2, 17);
cbitset_set(&s2, 18);
@@ -32,12 +32,12 @@ int main() {
puts("");
printf(" xor: ");
- cbitset_setXor(&set, s2);
+ cbitset_set_xor(&set, s2);
for (int i=0; i<set.size; ++i)
printf("%d", cbitset_test(set, i));
puts("");
- cbitset_setAll(&set, false);
+ cbitset_set_all(&set, false);
printf("%4zu: ", set.size);
for (int i=0; i<set.size; ++i)
printf("%d", cbitset_test(set, i));
diff --git a/examples/complex.c b/examples/complex.c
index 40dc9722..6a6a268b 100644
--- a/examples/complex.c
+++ b/examples/complex.c
@@ -5,31 +5,31 @@
void check_destroy(float* v) {printf("destroy %g\n", *v);}
-declare_CArray(f, float, check_destroy); // normally omit the last argument - float type need no destroy.
-declare_CList(t2, CArray2f, carray2f_destroy, c_noCompare);
-declare_CMap(il, int, CList_t2, clist_t2_destroy);
-declare_CMap_str(sm, CMap_il, cmap_il_destroy);
+declare_carray(f, float, check_destroy); // normally omit the last argument - float type need no destroy.
+declare_clist(t2, carray2f, carray2f_destroy, c_no_compare);
+declare_cmap(il, int, clist_t2, clist_t2_destroy);
+declare_cmap_str(sm, cmap_il, cmap_il_destroy);
int main() {
int xdim = 4, ydim = 6;
int x = 1, y = 5, tableKey = 42;
const char* strKey = "first";
- CMap_sm theMap = cmap_init;
+ cmap_sm theMap = cmap_init;
{ // Construct.
- CArray2f table = carray2f_make(ydim, xdim, 0.f);
+ carray2f table = carray2f_make(ydim, xdim, 0.f);
printf("table: (%zu, %zu)\n", carray2_ydim(table), carray2_xdim(table));
- CList_t2 tableList = clist_init;
- CMap_il listMap = cmap_init;
+ clist_t2 tableList = clist_init;
+ cmap_il listMap = cmap_init;
// Put in some data.
carray2f_data(table, y)[x] = 3.1415927; // table[y][x]
- clist_t2_pushBack(&tableList, table);
+ clist_t2_push_back(&tableList, table);
cmap_il_put(&listMap, tableKey, tableList);
cmap_sm_put(&theMap, strKey, listMap);
}
{ // Access the data entry
- CArray2f table = clist_back(cmap_il_find(&cmap_sm_find(&theMap, strKey)->value, tableKey)->value);
+ carray2f table = clist_back(cmap_il_find(&cmap_sm_find(&theMap, strKey)->value, tableKey)->value);
printf("value (%d, %d) is: %f\n", y, x, carray2f_value(table, y, x));
}
diff --git a/examples/demos.c b/examples/demos.c
index 03d8a21d..3c7ac5f4 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -8,7 +8,7 @@
void stringdemo1()
{
printf("\nSTRINGDEMO1\n");
- CStr cs = cstr_make("one-nine-three-seven-five");
+ cstr_t cs = cstr_make("one-nine-three-seven-five");
printf("%s.\n", cs.str);
cstr_insert(&cs, 3, "-two");
@@ -33,18 +33,18 @@ void stringdemo1()
}
-declare_CVec(ix, int64_t); // ix is just an example tag name.
+declare_cvec(ix, int64_t); // ix is just an example tag name.
void vectordemo1()
{
printf("\nVECTORDEMO1\n");
- CVec_ix bignums = cvec_init; // = (CVec_ix) cvec_init; if initializing after declaration.
+ cvec_ix bignums = cvec_init; // = (cvec_ix) cvec_init; if initializing after declaration.
cvec_ix_reserve(&bignums, 100);
for (size_t i = 0; i<=100; ++i)
- cvec_ix_pushBack(&bignums, i * i * i);
+ cvec_ix_push_back(&bignums, i * i * i);
printf("erase - %d: %zu\n", 100, bignums.data[100]);
- cvec_ix_popBack(&bignums); // erase the last
+ cvec_ix_pop_back(&bignums); // erase the last
for (size_t i = 0; i < cvec_size(bignums); ++i) {
if (i >= 90) printf("%zu: %zu\n", i, bignums.data[i]);
@@ -54,15 +54,15 @@ void vectordemo1()
-declare_CVec(cs, CStr, cstr_destroy, cstr_compare); // supply inline destructor of values
+declare_cvec(cs, cstr_t, cstr_destroy, cstr_compare); // supply inline destructor of values
void vectordemo2()
{
printf("\nVECTORDEMO2\n");
- CVec_cs names = cvec_init;
- cvec_cs_pushBack(&names, cstr_make("Mary"));
- cvec_cs_pushBack(&names, cstr_make("Joe"));
- cvec_cs_pushBack(&names, cstr_make("Chris"));
+ cvec_cs names = cvec_init;
+ cvec_cs_push_back(&names, cstr_make("Mary"));
+ cvec_cs_push_back(&names, cstr_make("Joe"));
+ cvec_cs_push_back(&names, cstr_make("Chris"));
cstr_assign(&names.data[1], "Jane"); // replace Joe
printf("names[1]: %s\n", names.data[1].str);
@@ -72,39 +72,39 @@ void vectordemo2()
cvec_cs_destroy(&names);
}
-declare_CList(ix, int);
+declare_clist(ix, int);
void listdemo1()
{
printf("\nLISTDEMO1\n");
- CList_ix nums = clist_init, nums2 = clist_init;
+ clist_ix nums = clist_init, nums2 = clist_init;
for (int i = 0; i < 10; ++i)
- clist_ix_pushBack(&nums, i);
+ clist_ix_push_back(&nums, i);
for (int i = 100; i < 110; ++i)
- clist_ix_pushBack(&nums2, i);
+ clist_ix_push_back(&nums2, i);
c_foreach (i, clist_ix, nums)
printf("value: %d\n", i.item->value);
/* merge/append nums2 to nums */
- clist_ix_spliceAfter(&nums, clist_ix_last(&nums), &nums2);
+ clist_ix_splice_after(&nums, clist_ix_last(&nums), &nums2);
c_foreach (i, clist_ix, nums)
printf("spliced: %d\n", i.item->value);
*clist_ix_find(&nums, 100) *= 10;
clist_ix_sort(&nums); // Sort the array
clist_ix_remove(&nums, 105);
- clist_ix_popFront(&nums);
- clist_ix_pushFront(&nums, -99);
+ clist_ix_pop_front(&nums);
+ clist_ix_push_front(&nums, -99);
c_foreach (i, clist_ix, nums)
printf("sorted: %d\n", i.item->value);
clist_ix_destroy(&nums);
}
-declare_CSet(i, int);
+declare_cset(i, int);
void setdemo1()
{
printf("\nSETDEMO1\n");
- CSet_i nums = cset_init;
+ cset_i nums = cset_init;
cset_i_put(&nums, 8);
cset_i_put(&nums, 11);
@@ -114,12 +114,12 @@ void setdemo1()
}
-declare_CMap(ii, int, int);
+declare_cmap(ii, int, int);
void mapdemo1()
{
printf("\nMAPDEMO1\n");
- CMap_ii nums = cmap_init;
+ cmap_ii nums = cmap_init;
cmap_ii_put(&nums, 8, 64);
cmap_ii_put(&nums, 11, 121);
@@ -128,12 +128,12 @@ void mapdemo1()
}
-declare_CMap_str(si, int); // Shorthand macro for the general declare_CMap expansion.
+declare_cmap_str(si, int); // Shorthand macro for the general declare_cmap expansion.
void mapdemo2()
{
printf("\nMAPDEMO2\n");
- CMap_si nums = cmap_init;
+ cmap_si nums = cmap_init;
cmap_si_put(&nums, "Hello", 64);
cmap_si_put(&nums, "Groovy", 121);
cmap_si_put(&nums, "Groovy", 200); // overwrite previous
@@ -150,16 +150,16 @@ void mapdemo2()
}
-declare_CMap_str(ss, CStr, cstr_destroy);
+declare_cmap_str(ss, cstr_t, cstr_destroy);
void mapdemo3()
{
printf("\nMAPDEMO3\n");
- CMap_ss table = cmap_init;
+ cmap_ss table = cmap_init;
cmap_ss_put(&table, "Map", cstr_make("test"));
cmap_ss_put(&table, "Make", cstr_make("my"));
cmap_ss_put(&table, "Sunny", cstr_make("day"));
- CMapEntry_ss *e = cmap_ss_find(&table, "Make");
+ cmapentry_ss *e = cmap_ss_find(&table, "Make");
c_foreach (i, cmap_ss, table)
printf("entry: %s: %s\n", i.item->key.str, i.item->value.str);
printf("size %zu: remove: Make: %s\n", cmap_size(table), e->value.str);
@@ -173,14 +173,14 @@ void mapdemo3()
}
-declare_CArray(f, float);
+declare_carray(f, float);
void arraydemo1()
{
printf("\nARRAYDEMO1\n");
- CArray3f a3 = carray3f_make(30, 20, 10, 0.f);
+ carray3f a3 = carray3f_make(30, 20, 10, 0.f);
carray3f_data(a3, 5, 4)[3] = 10.2f; // a3[5][4][3]
- CArray2f a2 = carray3f_at(a3, 5); // sub-array reference (no data copy).
+ carray2f a2 = carray3f_at(a3, 5); // sub-array reference (no data copy).
printf("a3: %zu: (%zu, %zu, %zu) = %zu\n", sizeof(a3), carray3_xdim(a3), carray3_ydim(a3), carray3_zdim(a3), carray3_size(a3));
printf("a2: %zu: (%zu, %zu) = %zu\n", sizeof(a2), carray2_xdim(a2), carray2_ydim(a2), carray2_size(a2));
diff --git a/examples/geek1.c b/examples/geek1.c
index f8471122..1df74024 100644
--- a/examples/geek1.c
+++ b/examples/geek1.c
@@ -12,14 +12,14 @@ int a[] = { 1, 2, 2, 3, 2, 4, 10 };
#include <stdio.h>
#include <stc/cmap.h>
-declare_CMap(ii, int, int);
+declare_cmap(ii, int, int);
// Function to maximize the number of pairs
int findMaximumPairs(int a[], int n, int k)
{
// Hash-table
- CMap_ii hash = cmap_init;
+ cmap_ii hash = cmap_init;
for (int i = 0; i < n; i++) {
cmap_ii_insert(&hash, a[i] % k, 0)->value++;
}
@@ -38,7 +38,7 @@ int findMaximumPairs(int a[], int n, int k)
int first = it.item->key;
int second = k - it.item->key;
- CMapEntry_ii *hf = cmap_ii_find(&hash, first),
+ cmapentry_ii *hf = cmap_ii_find(&hash, first),
*hs = cmap_ii_insert(&hash, second, 0);
// Check for minimal occurrence
if (hf->value < hs->value) {
diff --git a/examples/geek2.c b/examples/geek2.c
index 1c2f0c05..988f1f98 100644
--- a/examples/geek2.c
+++ b/examples/geek2.c
@@ -3,15 +3,15 @@
#include <stc/cmap.h>
#include <stc/cstr.h>
-declare_CMap_str(ss, CStr, cstr_destroy);
-declare_CSet_str();
+declare_cmap_str(ss, cstr_t, cstr_destroy);
+declare_cset_str();
int main()
{
// Lets use an explicit type signature (which would
- // be `CMap<String, String>` in this example).
- CMap_ss book_reviews = cmap_init;
- CSet_str set = cset_init;
+ // be `cmap<String, String>` in this example).
+ cmap_ss book_reviews = cmap_init;
+ cset_str set = cset_init;
cset_str_put(&set, "Hello");
cset_str_put(&set, "You");
cset_str_put(&set, "Tube");
@@ -50,7 +50,7 @@ int main()
// Look up the values associated with some keys.
const char* to_find[] = {"Pride and Prejudice", "Alice's Adventure in Wonderland", NULL};
for (const char** book = to_find; *book; ++book) {
- CMapEntry_ss* review = cmap_ss_find(&book_reviews, *book);
+ cmapentry_ss* review = cmap_ss_find(&book_reviews, *book);
if (review) printf("%s: %s\n", *book, review->value.str);
else printf("%s is unreviewed.\n", *book);
}
diff --git a/examples/geek3.c b/examples/geek3.c
index f6f8942a..b8de97c8 100644
--- a/examples/geek3.c
+++ b/examples/geek3.c
@@ -3,12 +3,12 @@
#include <stc/cmap.h>
#include <stc/cstr.h>
-declare_CMap_str(si, int);
-declare_CMap_str(ss, CStr, cstr_destroy);
+declare_cmap_str(si, int);
+declare_cmap_str(ss, cstr_t, cstr_destroy);
int main ()
{
- CMap_si mymap = cmap_init;
+ cmap_si mymap = cmap_init;
cmap_si_put(&mymap, "Mars", 3000);
cmap_si_put(&mymap, "Saturn", 60000);
cmap_si_put(&mymap, "Jupiter", 70000);
@@ -28,7 +28,7 @@ int main ()
puts("------------------------");
// Create an unordered_map of three strings (that map to strings)
- CMap_ss u = cmap_init;
+ cmap_ss u = cmap_init;
cmap_ss_put(&u, "RED", cstr_make("#FF0000"));
cmap_ss_put(&u, "GREEN", cstr_make("#00FF00"));
cmap_ss_put(&u, "BLUE", cstr_make("#0000FF"));
diff --git a/examples/geek4.c b/examples/geek4.c
index e7522bb3..c541e805 100644
--- a/examples/geek4.c
+++ b/examples/geek4.c
@@ -37,19 +37,19 @@ Efficient Approach: For all the words of the first sentence, we can check if it
#include <stc/cvec.h>
#include <stc/cstr.h>
-declare_CVec_str();
-declare_CMap_str(sb, bool);
-declare_CVec(sb, CMapEntry_sb, cmapentry_sb_destroy, c_noCompare);
+declare_cvec_str();
+declare_cmap_str(sb, bool);
+declare_cvec(sb, cmapentry_sb, cmapentry_sb_destroy, c_no_compare);
// Function to return the count of common words
// in all the sentences
-int commonWords(CVec_str S)
+int commonWords(cvec_str S)
{
int m, n, i, j;
// To store all the words of first string
- CVec_sb ans = cvec_init;
+ cvec_sb ans = cvec_init;
// m will store number of strings in given vector
m = cvec_size(S);
@@ -59,11 +59,11 @@ int commonWords(CVec_str S)
// Extract all words of first string and store it in ans
while (i < cstr_size(S.data[0])) {
// To store separate words
- CStr word = cstr_init;
- CMapEntry_sb tmp = {cstr_init, false};
+ cstr_t word = cstr_init;
+ cmapentry_sb tmp = {cstr_init, false};
while (i < cstr_size(S.data[0]) && S.data[0].str[i] != ' ') {
- cstr_pushBack(&word, S.data[0].str[i]);
+ cstr_push_back(&word, S.data[0].str[i]);
i++;
}
@@ -75,7 +75,7 @@ int commonWords(CVec_str S)
if (!cstr_empty(word)) {
tmp.key = cstr_move(&word);
tmp.value = true;
- cvec_sb_pushBack(&ans, tmp);
+ cvec_sb_push_back(&ans, tmp);
}
}
@@ -85,13 +85,13 @@ int commonWords(CVec_str S)
for (j = 1; j < m; j++) {
// It will be used to check if a word is present
// in a particuler string
- CMap_sb has = cmap_init;
+ cmap_sb has = cmap_init;
i = 0;
while (i < cstr_size(S.data[j])) {
- CStr word = cstr_init;
+ cstr_t word = cstr_init;
while (i < cstr_size(S.data[j]) && S.data[j].str[i] != ' ') {
- cstr_pushBack(&word, S.data[j].str[i]);
+ cstr_push_back(&word, S.data[j].str[i]);
i++;
}
i++;
@@ -136,11 +136,11 @@ int commonWords(CVec_str S)
// Driver code
int main()
{
- CVec_str S = cvec_init;
- cvec_str_pushBack(&S, cstr_make("there is a cow"));
- cvec_str_pushBack(&S, cstr_make("cow is our mother"));
- cvec_str_pushBack(&S, cstr_make("cow gives us milk and milk is sweet"));
- cvec_str_pushBack(&S, cstr_make("there is a boy who loves cow"));
+ cvec_str S = cvec_init;
+ cvec_str_push_back(&S, cstr_make("there is a cow"));
+ cvec_str_push_back(&S, cstr_make("cow is our mother"));
+ cvec_str_push_back(&S, cstr_make("cow gives us milk and milk is sweet"));
+ cvec_str_push_back(&S, cstr_make("there is a boy who loves cow"));
printf("%d\n", commonWords(S));
cvec_str_destroy(&S);
diff --git a/examples/geek5.c b/examples/geek5.c
index 546cb278..d9885293 100644
--- a/examples/geek5.c
+++ b/examples/geek5.c
@@ -21,33 +21,33 @@ Output: 0
#include <stc/cvec.h>
#include <stc/cstr.h>
-declare_CVec(i, int);
-declare_CMap_str(sv, CVec_i, cvec_i_destroy);
+declare_cvec(i, int);
+declare_cmap_str(sv, cvec_i, cvec_i_destroy);
// Function to return the number of occurrences of
int NumOccurrences(const char* arr[], int n, const char* str, int L, int R)
{
// To store the indices of strings in the array
- CMap_sv M = cmap_init;
+ cmap_sv M = cmap_init;
for (int i = 0; i < n; i++) {
const char* temp = arr[i];
- CMapEntry_sv* it = cmap_sv_find(&M, temp);
+ cmapentry_sv* it = cmap_sv_find(&M, temp);
// If current string doesn't
// have an entry in the map
// then create the entry
if (it == NULL) {
- CVec_i A = cvec_init;
- cvec_i_pushBack(&A, i + 1);
+ cvec_i A = cvec_init;
+ cvec_i_push_back(&A, i + 1);
cmap_sv_put(&M, temp, A);
}
else {
- cvec_i_pushBack(&it->value, i + 1);
+ cvec_i_push_back(&it->value, i + 1);
}
}
- CMapEntry_sv* it = cmap_sv_find(&M, str);
+ cmapentry_sv* it = cmap_sv_find(&M, str);
// If the given string is not
// present in the array
@@ -56,7 +56,7 @@ int NumOccurrences(const char* arr[], int n, const char* str, int L, int R)
// If the given string is present
// in the array
- CVec_i A = it->value;
+ cvec_i A = it->value;
int y = 0, x = 0;
for (; y < cvec_size(A); ++y) if (A.data[y] > R) break;
for (; x < cvec_size(A); ++x) if (A.data[x] > L - 1) break;
diff --git a/examples/geek6.c b/examples/geek6.c
index 6b70dd5e..4689d934 100644
--- a/examples/geek6.c
+++ b/examples/geek6.c
@@ -30,14 +30,14 @@ operation in almost O(1) time complexity.
#include <stdio.h>
#include <stc/cmap.h>
-declare_CSet(i, int);
+declare_cset(i, int);
// Function to find the smallest positive
// missing number
int missingNumber(int a[], int n)
{
// Declaring an unordered_map
- CSet_i mp = cset_init;
+ cset_i mp = cset_init;
// if array value is positive
// store it in map
diff --git a/examples/geek7.c b/examples/geek7.c
index e304718d..36805dce 100644
--- a/examples/geek7.c
+++ b/examples/geek7.c
@@ -26,9 +26,9 @@ After inserting all the elements excluding the ones which are to be deleted, Pop
#include <stc/cmap.h>
#include <stc/cvecpq.h>
-declare_CMap(ii, int, int);
-declare_CVec(i, int);
-declare_CVec_priority_queue(i, >);
+declare_cmap(ii, int, int);
+declare_cvec(i, int);
+declare_cvec_priority_queue(i, >);
// Find k minimum element from arr[0..m-1] after deleting
// elements from del[0..n-1]
@@ -36,19 +36,19 @@ void findElementsAfterDel(int arr[], int m, int del[],
int n, int k)
{
// Hash Map of the numbers to be deleted
- CMap_ii mp = cmap_init;
+ cmap_ii mp = cmap_init;
for (int i = 0; i < n; ++i) {
// Increment the count of del[i]
cmap_ii_insert(&mp, del[i], 0)->value++;
}
- CVec_i heap = cvec_init;
+ cvec_i heap = cvec_init;
for (int i = 0; i < m; ++i) {
// Search if the element is present
- CMapEntry_ii *e = cmap_ii_find(&mp, arr[i]);
+ cmapentry_ii *e = cmap_ii_find(&mp, arr[i]);
if (e != NULL) {
// Decrement its frequency
@@ -57,7 +57,7 @@ void findElementsAfterDel(int arr[], int m, int del[],
// If the frequency becomes 0,
// erase it from the map
if (e->value == 0)
- cmap_ii_eraseEntry(&mp, e);
+ cmap_ii_erase_entry(&mp, e);
}
// Else push it in the min heap
diff --git a/examples/heap.c b/examples/heap.c
index f0d56a46..0f4ab4c2 100644
--- a/examples/heap.c
+++ b/examples/heap.c
@@ -1,20 +1,20 @@
#include <stdio.h>
#include <time.h>
#include "stc/cvecpq.h"
-#include "stc/crandom.h"
+#include "stc/crand.h"
-declare_CVec(f, float);
-declare_CVec_priority_queue(f, >);
+declare_cvec(f, float);
+declare_cvec_priority_queue(f, >);
int main()
{
uint32_t seed = time(NULL);
- CRand32 pcg = crand32_init(seed);
+ crand_eng32_t pcg = crand_eng32_init(seed);
int N = 30000000, M = 100;
- CVec_f vec = cvec_init;
+ cvec_f vec = cvec_init;
clock_t start = clock();
for (int i=0; i<N; ++i)
- cvec_f_pushBack(&vec, crand32_gen(&pcg));
+ cvec_f_push_back(&vec, crand_gen_i32(&pcg));
cvecpq_f_build(&vec);
printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
@@ -25,10 +25,10 @@ int main()
cvecpq_f_pop(&vec);
printf("\n\npopped PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
- pcg = crand32_init(seed);
+ pcg = crand_eng32_init(seed);
start = clock();
for (int i=0; i<N; ++i)
- cvecpq_f_push(&vec, crand32_gen(&pcg));
+ cvecpq_f_push(&vec, crand_gen_i32(&pcg));
printf("pushed PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
for (int i=0; i<M; ++i)
printf("%.0f ", cvecpq_f_top(&vec)), cvecpq_f_pop(&vec);
diff --git a/examples/inits.c b/examples/inits.c
index 240ce5c9..0769ce3c 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -4,17 +4,17 @@
#include <stc/cvec.h>
#include <stc/clist.h>
-declare_CMap(id, int, CStr, cstr_destroy); // Map of int -> CStr
-declare_CMap_str(cnt, int);
+declare_cmap(id, int, cstr_t, cstr_destroy); // Map of int -> cstr_t
+declare_cmap_str(cnt, int);
typedef struct {int x, y;} ipair_t;
-declare_CVec(ip, ipair_t, c_defaultDestroy, c_memCompare);
-declare_CList(ip, ipair_t, c_defaultDestroy, c_memCompare);
+declare_cvec(ip, ipair_t, c_default_destroy, c_mem_compare);
+declare_clist(ip, ipair_t, c_default_destroy, c_mem_compare);
int main(void) {
int year = 2020;
- CMap_id idnames = cmap_init;
+ cmap_id idnames = cmap_init;
c_push(&idnames, cmap_id, c_items(
{100, cstr_make("Hello")},
{110, cstr_make("World")},
@@ -27,7 +27,7 @@ int main(void) {
// ------------------
- CMap_cnt countries = cmap_init;
+ cmap_cnt countries = cmap_init;
cmap_cnt_insert(&countries, "Greenland", 0)->value += 20;
c_push(&countries, cmap_cnt, c_items(
@@ -46,7 +46,7 @@ int main(void) {
// ------------------
- CVec_ip pairs1 = cvec_init;
+ cvec_ip pairs1 = cvec_init;
c_push(&pairs1, cvec_ip, c_items(
{1, 2},
{3, 4},
@@ -61,7 +61,7 @@ int main(void) {
// ------------------
- CList_ip pairs2 = clist_init;
+ clist_ip pairs2 = clist_init;
c_push(&pairs2, clist_ip, c_items(
{1, 2},
{3, 4},
diff --git a/examples/list.c b/examples/list.c
index 09b20b8c..a65a8454 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -1,29 +1,30 @@
#include <stdio.h>
#include <time.h>
#include <stc/clist.h>
-#include <stc/crandom.h>
-declare_CList(ix, uint64_t);
+#include <stc/crand.h>
+declare_clist(fx, double);
int main() {
- CList_ix list = clist_init;
- CRand32 pcg = crand32_init(time(NULL));
+ clist_fx list = clist_init;
+ crand_eng64_t eng = crand_eng64_init(time(NULL));
+ crand_uniform_f64_t dist = crand_uniform_f64_init(1.0, 100.0);
int n;
- for (int i=0; i<10000000; ++i) // ten million
- clist_ix_pushBack(&list, crand32_gen(&pcg));
+ for (int i = 0; i < 10000000; ++i) // ten million
+ clist_fx_push_back(&list, crand_uniform_f64(&eng, dist));
n = 100;
- c_foreach (i, clist_ix, list)
- if (n--) printf("%8d: %10zu\n", 100 - n, i.item->value); else break;
+ c_foreach (i, clist_fx, list)
+ if (n--) printf("%8d: %10f\n", 100 - n, i.item->value); else break;
// Sort them...
- clist_ix_sort(&list); // mergesort O(n*log n)
+ clist_fx_sort(&list); // mergesort O(n*log n)
n = 100;
puts("sorted");
- c_foreach (i, clist_ix, list)
- if (n--) printf("%8d: %10zu\n", 100 - n, i.item->value); else break;
+ c_foreach (i, clist_fx, list)
+ if (n--) printf("%8d: %10f\n", 100 - n, i.item->value); else break;
- clist_ix_clear(&list);
- c_push(&list, clist_ix, c_items(10, 20, 30, 40, 50));
- c_foreach (i, clist_ix, list) printf("%zu ", i.item->value);
+ clist_fx_clear(&list);
+ c_push(&list, clist_fx, c_items(10, 20, 30, 40, 50));
+ c_foreach (i, clist_fx, list) printf("%f ", i.item->value);
puts("");
- clist_ix_destroy(&list);
+ clist_fx_destroy(&list);
} \ No newline at end of file
diff --git a/examples/mapmap.c b/examples/mapmap.c
index 67672dee..b349e8d3 100644
--- a/examples/mapmap.c
+++ b/examples/mapmap.c
@@ -6,12 +6,12 @@ static void test_destr(int* x) {
printf("destroy int: %d\n", *x);
}
-declare_CMap(ii, int, int, test_destr);
-declare_CMap(im, int, CMap_ii, cmap_ii_destroy);
+declare_cmap(ii, int, int, test_destr);
+declare_cmap(im, int, cmap_ii, cmap_ii_destroy);
int main(void) {
- CMap_im m = cmap_init;
- CMap_ii ini = cmap_init;
+ cmap_im m = cmap_init;
+ cmap_ii ini = cmap_init;
cmap_ii_put(&cmap_im_insert(&m, 100, ini)->value, 2000, 200);
cmap_ii_put(&cmap_im_insert(&m, 100, ini)->value, 2001, 201);
cmap_ii_put(&cmap_im_insert(&m, 100, ini)->value, 2000, 400); // update
diff --git a/examples/prime.c b/examples/prime.c
index 05ef4d57..2e6c99ee 100644
--- a/examples/prime.c
+++ b/examples/prime.c
@@ -1,22 +1,13 @@
-#include <stc/cbitset.h>
-
-#if defined(__GNUC__)
-#define cbitset_popcnt64(i) __builtin_popcountll(i)
-#else
-#define cbitset_popcnt64(i) _mm_popcnt_u64(i)
-#endif
-
#include <stdio.h>
+#include <stc/cbitset.h>
static inline void sieveOfEratosthenes(size_t n)
{
- CBitset prime = cbitset_make(n + 1, true);
+ cbitset_t prime = cbitset_make(n + 1, true);
printf("computing prime numbers up to %zu\n", n);
cbitset_reset(&prime, 0);
cbitset_reset(&prime, 1);
- uint64_t m = cbitset_popcnt64(123456);
-
for (size_t i = 2; i <= n; ++i) {
// If prime[i] is not changed, then it is a prime
if (cbitset_test(prime, i) && i*i <= n) {
diff --git a/examples/priority.c b/examples/priority.c
index baab2779..64449304 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -3,18 +3,18 @@
#include <time.h>
#include <stc/cvecpq.h>
#include <stc/cmap.h>
-#include <stc/crandom.h>
+#include <stc/crand.h>
-declare_CVec(i, uint32_t);
-declare_CVec_priority_queue(i, >); // min-heap (increasing values)
+declare_cvec(i, uint32_t);
+declare_cvec_priority_queue(i, >); // min-heap (increasing values)
int main() {
- CRand32 pcg = crand32_init(time(NULL));
- CVec_i heap = cvec_init;
+ crand_eng32_t pcg = crand_eng32_init(time(NULL));
+ cvec_i heap = cvec_init;
// Push ten million random numbers to queue
for (int i=0; i<10000000; ++i)
- cvecpq_i_push(&heap, crand32_gen(&pcg));
+ cvecpq_i_push(&heap, crand_gen_i32(&pcg));
// Extract the hundred smallest.
for (int i=0; i<100; ++i) {
diff --git a/examples/rngbirthday.c b/examples/rngbirthday.c
index 8129497b..c20bac26 100644
--- a/examples/rngbirthday.c
+++ b/examples/rngbirthday.c
@@ -2,12 +2,12 @@
#include <stdio.h>
#include <time.h>
-#include <stc/crandom.h>
+#include <stc/crand.h>
#include <stc/cmap.h>
#include <stc/cvec.h>
#include <stc/cstr.h>
-declare_CMap(ic, uint64_t, uint8_t);
+declare_cmap(ic, uint64_t, uint8_t);
const static uint64_t seed = 1234;
const static uint64_t N = 1ull << 27;
@@ -15,12 +15,12 @@ const static uint64_t mask = (1ull << 52) - 1;
void repeats(void)
{
- CRand64 rng = crand64_init(seed);
- CMap_ic m = cmap_init;
+ crand_eng64_t rng = crand_eng64_init(seed);
+ cmap_ic m = cmap_init;
cmap_ic_reserve(&m, N);
clock_t now = clock();
for (size_t i = 0; i < N; ++i) {
- uint64_t k = crand64_gen(&rng) & mask;
+ uint64_t k = crand_gen_i64(&rng) & mask;
int v = ++cmap_ic_insert(&m, k, 0)->value;
if (v > 1) printf("%zu: %x - %d\n", i, k, v);
}
@@ -29,17 +29,18 @@ void repeats(void)
}
-declare_CMap(x, uint32_t, uint64_t);
-declare_CVec(x, uint64_t);
+declare_cmap(x, uint32_t, uint64_t);
+declare_cvec(x, uint64_t);
void distribution(void)
{
- CRand32 rng = crand32_init(seed); // time(NULL), time(NULL));
+ crand_eng32_t rng = crand_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_make(M);
clock_t now = clock();
+ crand_uniform_i32_t dist = crand_uniform_i32_init(0, M);
for (size_t i = 0; i < N; ++i) {
- ++cmap_x_insert(&map, crand32_genBounded(&rng, M), 0)->value;
+ ++cmap_x_insert(&map, crand_uniform_i32(&rng, dist), 0)->value;
}
float diff = (float) (clock() - now) / CLOCKS_PER_SEC;
diff --git a/examples/rngtest.c b/examples/rngtest.c
index a15a8fcb..f8fe44cf 100644
--- a/examples/rngtest.c
+++ b/examples/rngtest.c
@@ -1,6 +1,6 @@
#include <stdio.h>
#include <time.h>
-#include <stc/crandom.h>
+#include <stc/crand.h>
#ifdef __cplusplus
#include <random>
#endif
@@ -14,26 +14,34 @@ int main(void)
uint64_t v;
printf("start\n");
- CRand32 pcg = crand32_init(time(NULL));
+ crand_eng32_t pcg = crand_eng32_init(time(NULL));
before = clock(); \
v = 0;
for (size_t i=0; i<NN; i++) {
- v += crand32_gen(&pcg);
+ v += crand_gen_i32(&pcg);
}
difference = clock() - before;
printf("pcg32: %.02f, %zu\n", (float) difference / CLOCKS_PER_SEC, v);
- CRand64 sfc = crand64_init(time(NULL));
+ crand_eng64_t sfc = crand_eng64_init(time(NULL));
before = clock(); \
v = 0;
for (size_t i=0; i<NN; i++) {
- v += crand64_gen(&sfc) & 0xffffffff;
+ v += crand_gen_i64(&sfc) & 0xffffffff;
}
difference = clock() - before;
printf("sfc64: %.02f, %zu\n", (float) difference / CLOCKS_PER_SEC, v);
- for (int i=0; i<8; ++i) printf("%f ", crand32_genReal(&pcg));
+ crand_uniform_i32_t i32dist = crand_uniform_i32_init(10, 20);
+ for (int i=0; i<8; ++i) printf("%d ", crand_uniform_i32(&pcg, i32dist));
puts("");
- for (int i=0; i<8; ++i) printf("%f ", crand64_genReal(&sfc));
+
+ crand_uniform_f32_t f32dist = crand_uniform_f32_init(10, 20);
+ for (int i=0; i<8; ++i) printf("%f ", crand_uniform_f32(&pcg, f32dist));
+ puts("");
+
+ crand_uniform_f64_t fdist = crand_uniform_f64_init(10, 20);
+ for (int i=0; i<8; ++i) printf("%f ", crand_uniform_f64(&sfc, fdist));
+ //for (int i=0; i<8; ++i) printf("%zu ", crand_gen_i64(&sfc));
puts("");
} \ No newline at end of file
diff --git a/stc/carray.h b/stc/carray.h
index 6157b7f3..0b0a7751 100644
--- a/stc/carray.h
+++ b/stc/carray.h
@@ -30,13 +30,13 @@
Multi-dimensional generic array allocated as one block of heap-memory.
// demo:
#include <stc/carray.h>
-declare_CArray(f, float);
+declare_carray(f, float);
int main()
{
- CArray3_f a3 = carray3f_make(30, 20, 10, 0.f);
+ carray3f a3 = carray3f_make(30, 20, 10, 0.f);
carray3f_data(a3, 5, 4)[3] = 10.2f; // a3[5][4][3]
- CArray2_f a2 = carray3f_at(a3, 5); // sub-array reference (no data copy).
+ carray2f a2 = carray3f_at(a3, 5); // sub-array reference (no data copy).
printf("%f\n", carray2f_value(a2, 4, 3)); // readonly lookup a2[4][3] (=10.2f)
printf("%f\n", carray2f_data(a2, 4)[3]); // same, but this is writable.
@@ -74,122 +74,122 @@ static inline size_t _carray3_size(const size_t* zdim) {
}
-#define declare_CArray(...) c_MACRO_OVERLOAD(declare_CArray, __VA_ARGS__)
+#define declare_carray(...) c_MACRO_OVERLOAD(declare_carray, __VA_ARGS__)
-#define declare_CArray_2(tag, Value) \
- declare_CArray_3(tag, Value, c_defaultDestroy)
+#define declare_carray_2(tag, Value) \
+ declare_carray_3(tag, Value, c_default_destroy)
-#define declare_CArray_3(tag, Value, valueDestroy) \
+#define declare_carray_3(tag, Value, valueDestroy) \
typedef struct { \
Value *data; \
size_t _xdim; \
- } CArray1##tag; \
+ } carray1##tag; \
\
typedef struct { \
Value *data; \
size_t _xdim, _yxdim; \
- } CArray2##tag; \
+ } carray2##tag; \
\
typedef struct { \
Value *data; \
size_t _xdim, _yxdim, _zdim; \
- } CArray3##tag; \
+ } carray3##tag; \
\
- static inline CArray1##tag \
+ static inline carray1##tag \
carray1##tag##_make(size_t xdim, Value val) { \
- Value* m = c_new_N(Value, xdim); \
+ Value* m = c_new_n(Value, xdim); \
for (size_t i=0; i<xdim; ++i) m[i] = val; \
- CArray1##tag a = {m, xdim | _carray_OWN}; \
+ carray1##tag a = {m, xdim | _carray_OWN}; \
return a; \
} \
- static inline CArray2##tag \
+ static inline carray2##tag \
carray2##tag##_make(size_t ydim, size_t xdim, Value val) { \
const size_t n = ydim * xdim; \
- Value* m = c_new_N(Value, n); \
+ Value* m = c_new_n(Value, n); \
for (size_t i=0; i<n; ++i) m[i] = val; \
- CArray2##tag a = {m, xdim | _carray_OWN, ydim * xdim}; \
+ carray2##tag a = {m, xdim | _carray_OWN, ydim * xdim}; \
return a; \
} \
- static inline CArray3##tag \
+ static inline carray3##tag \
carray3##tag##_make(size_t zdim, size_t ydim, size_t xdim, Value val) { \
const size_t n = zdim * ydim * xdim; \
- Value* m = c_new_N(Value, n); \
+ Value* m = c_new_n(Value, n); \
for (size_t i=0; i<n; ++i) m[i] = val; \
- CArray3##tag a = {m, xdim | _carray_OWN, ydim * xdim, zdim}; \
+ carray3##tag a = {m, xdim | _carray_OWN, ydim * xdim, zdim}; \
return a; \
} \
\
- static inline CArray1##tag \
+ static inline carray1##tag \
carray1##tag##_from(size_t xdim, Value* array, bool own) { \
- CArray1##tag a = {array, xdim | (own ? _carray_OWN : 0)}; \
+ carray1##tag a = {array, xdim | (own ? _carray_OWN : 0)}; \
return a; \
} \
- static inline CArray2##tag \
+ static inline carray2##tag \
carray2##tag##_from(size_t ydim, size_t xdim, Value* array, bool own) { \
- CArray2##tag a = {array, xdim | (own ? _carray_OWN : 0), ydim * xdim}; \
+ carray2##tag a = {array, xdim | (own ? _carray_OWN : 0), ydim * xdim}; \
return a; \
} \
- static inline CArray3##tag \
+ static inline carray3##tag \
carray3##tag##_from(size_t zdim, size_t ydim, size_t xdim, Value* array, bool own) { \
- CArray3##tag a = {array, xdim | (own ? _carray_OWN : 0), ydim * xdim, zdim}; \
+ carray3##tag a = {array, xdim | (own ? _carray_OWN : 0), ydim * xdim, zdim}; \
return a; \
} \
\
static inline void \
- carray1##tag##_destroy(CArray1##tag* self) { \
+ carray1##tag##_destroy(carray1##tag* self) { \
if (self->_xdim & _carray_OWN) { \
size_t n = carray1_size(*self); Value* a = self->data; \
while (n--) valueDestroy(&a[n]); free(a); \
} \
} \
static inline void \
- carray2##tag##_destroy(CArray2##tag* self) { \
+ carray2##tag##_destroy(carray2##tag* self) { \
if (self->_xdim & _carray_OWN) { \
size_t n = carray2_size(*self); Value* a = self->data; \
while (n--) valueDestroy(&a[n]); free(a); \
} \
} \
static inline void \
- carray3##tag##_destroy(CArray3##tag* self) { \
+ carray3##tag##_destroy(carray3##tag* self) { \
if (self->_xdim & _carray_OWN) { \
size_t n = carray3_size(*self); Value* a = self->data; \
while (n--) valueDestroy(&a[n]); free(a); \
} \
} \
\
- static inline CArray1##tag \
- carray2##tag##_at(CArray2##tag a, size_t y) { \
- CArray1##tag sub = {a.data + y*carray2_xdim(a), carray2_xdim(a)}; \
+ static inline carray1##tag \
+ carray2##tag##_at(carray2##tag a, size_t y) { \
+ carray1##tag sub = {a.data + y*carray2_xdim(a), carray2_xdim(a)}; \
return sub; \
} \
static inline Value* \
- carray2##tag##_data(CArray2##tag a, size_t y) { \
+ carray2##tag##_data(carray2##tag a, size_t y) { \
return a.data + y*carray2_xdim(a); \
} \
static inline Value \
- carray2##tag##_value(CArray2##tag a, size_t y, size_t x) { \
+ carray2##tag##_value(carray2##tag a, size_t y, size_t x) { \
return a.data[ y*carray2_xdim(a) + x ]; \
} \
\
- static inline CArray2##tag \
- carray3##tag##_at(CArray3##tag a, size_t z) { \
- CArray2##tag sub = {a.data + z*a._yxdim, carray3_xdim(a), a._yxdim}; \
+ static inline carray2##tag \
+ carray3##tag##_at(carray3##tag a, size_t z) { \
+ carray2##tag sub = {a.data + z*a._yxdim, carray3_xdim(a), a._yxdim}; \
return sub; \
} \
- static inline CArray1##tag \
- carray3##tag##_at2(CArray3##tag a, size_t z, size_t y) { \
- CArray1##tag sub = {a.data + z*a._yxdim + y*carray3_xdim(a), carray3_xdim(a)}; \
+ static inline carray1##tag \
+ carray3##tag##_at2(carray3##tag a, size_t z, size_t y) { \
+ carray1##tag sub = {a.data + z*a._yxdim + y*carray3_xdim(a), carray3_xdim(a)}; \
return sub; \
} \
static inline Value* \
- carray3##tag##_data(CArray3##tag a, size_t z, size_t y) { \
+ carray3##tag##_data(carray3##tag a, size_t z, size_t y) { \
return a.data + z*a._yxdim + y*carray3_xdim(a); \
} \
static inline Value \
- carray3##tag##_value(CArray3##tag a, size_t z, size_t y, size_t x) { \
+ carray3##tag##_value(carray3##tag a, size_t z, size_t y, size_t x) { \
return a.data[ z*a._yxdim + y*carray3_xdim(a) + x ]; \
} \
- typedef Value CArrayValue_##tag
+ typedef Value carrayValue_##tag
#endif
diff --git a/stc/cbitset.h b/stc/cbitset.h
index 4f30c091..aed8b6a7 100644
--- a/stc/cbitset.h
+++ b/stc/cbitset.h
@@ -27,7 +27,7 @@ Similar to boost::dynamic_bitset / std::bitset
#include "cbitset.h"
int main() {
- CBitset set = cbitset_make(23, true);
+ cbitset_t set = cbitset_make(23, true);
cbitset_reset(&set, 9);
cbitset_resize(&set, 43, false);
printf("%4zu: ", set.size);for (int i=0; i<set.size; ++i) printf("%d", cbitset_value(&set, i));puts("");
@@ -55,97 +55,97 @@ int main() {
#define cbitset_popcnt64(i) _mm_popcnt_u64(i)
#endif
-typedef struct { uint64_t* _arr; size_t size; } CBitset;
+typedef struct { uint64_t* _arr; size_t size; } cbitset_t;
#define cbitset_init {NULL, 0}
-STC_API void cbitset_resize(CBitset* self, size_t size, bool value);
-STC_API size_t cbitset_count(CBitset set);
+STC_API void cbitset_resize(cbitset_t* self, size_t size, bool value);
+STC_API size_t cbitset_count(cbitset_t set);
-STC_INLINE void cbitset_setAll(CBitset *self, bool value);
+STC_INLINE void cbitset_set_all(cbitset_t *self, bool value);
-STC_INLINE CBitset cbitset_make(size_t size, bool value) {
- CBitset set = {(uint64_t *) malloc(((size + 63) >> 6) * 8), size};
- cbitset_setAll(&set, value);
+STC_INLINE cbitset_t cbitset_make(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 cbitset_from(CBitset other) {
+STC_INLINE cbitset_t cbitset_from(cbitset_t other) {
size_t n = (other.size + 63) >> 6;
- CBitset set = {(uint64_t *) memcpy(malloc(n * 8), other._arr, n * 8), other.size};
+ cbitset_t set = {(uint64_t *) memcpy(malloc(n * 8), other._arr, n * 8), other.size};
return set;
}
-STC_INLINE void cbitset_destroy(CBitset* self) {
+STC_INLINE void cbitset_destroy(cbitset_t* self) {
free(self->_arr);
}
-STC_INLINE size_t cbitset_size(CBitset set) {return set.size;}
+STC_INLINE size_t cbitset_size(cbitset_t set) {return set.size;}
-STC_INLINE void cbitset_set(CBitset *self, size_t i) {
+STC_INLINE void cbitset_set(cbitset_t *self, size_t i) {
self->_arr[i >> 6] |= 1ull << (i & 63);
}
-STC_INLINE void cbitset_reset(CBitset *self, size_t i) {
+STC_INLINE void cbitset_reset(cbitset_t *self, size_t i) {
self->_arr[i >> 6] &= ~(1ull << (i & 63));
}
-STC_INLINE void cbitset_setTo(CBitset *self, size_t i, bool value) {
+STC_INLINE void cbitset_set_to(cbitset_t *self, size_t i, bool value) {
value ? cbitset_set(self, i) : cbitset_reset(self, i);
}
-STC_INLINE void cbitset_flip(CBitset *self, size_t i) {
+STC_INLINE void cbitset_flip(cbitset_t *self, size_t i) {
self->_arr[i >> 6] ^= 1ull << (i & 63);
}
-STC_INLINE bool cbitset_test(CBitset set, size_t i) {
+STC_INLINE bool cbitset_test(cbitset_t set, size_t i) {
return (set._arr[i >> 6] & (1ull << (i & 63))) != 0;
}
-STC_INLINE void cbitset_setAll(CBitset *self, bool value) {
+STC_INLINE void cbitset_set_all(cbitset_t *self, bool value) {
memset(self->_arr, value ? 0xff : 0x0, ((self->size + 63) >> 6) * 8);
}
-STC_INLINE void cbitset_setAll64(CBitset *self, uint64_t pattern) {
+STC_INLINE void cbitset_set_all_64(cbitset_t *self, uint64_t pattern) {
size_t n = (self->size + 63) >> 6;
for (size_t i=0; i<n; ++i) self->_arr[i] = pattern;
}
-STC_INLINE void cbitset_flipAll(CBitset *self) {
+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;
}
/* Intersection */
-STC_INLINE void cbitset_setAnd(CBitset *self, CBitset other) {
+STC_INLINE void cbitset_set_and(cbitset_t *self, cbitset_t other) {
assert(self->size == other.size);
size_t n = (self->size + 63) >> 6;
for (size_t i=0; i<n; ++i) self->_arr[i] &= other._arr[i];
}
/* Union */
-STC_INLINE void cbitset_setOr(CBitset *self, CBitset other) {
+STC_INLINE void cbitset_set_or(cbitset_t *self, cbitset_t other) {
assert(self->size == other.size);
size_t n = (self->size + 63) >> 6;
for (size_t i=0; i<n; ++i) self->_arr[i] |= other._arr[i];
}
/* Exclusive disjunction */
-STC_INLINE void cbitset_setXor(CBitset *self, CBitset other) {
+STC_INLINE void cbitset_set_xor(cbitset_t *self, cbitset_t other) {
assert(self->size == other.size);
size_t n = (self->size + 63) >> 6;
for (size_t i=0; i<n; ++i) self->_arr[i] ^= other._arr[i];
}
-STC_INLINE CBitset cbitset_and(CBitset s1, CBitset s2) {
- CBitset set = cbitset_from(s1);
- cbitset_setAnd(&set, s2); return set;
+STC_INLINE cbitset_t cbitset_and(cbitset_t s1, cbitset_t s2) {
+ cbitset_t set = cbitset_from(s1);
+ cbitset_set_and(&set, s2); return set;
}
-STC_INLINE CBitset cbitset_or(CBitset s1, CBitset s2) {
- CBitset set = cbitset_from(s1);
- cbitset_setOr(&set, s2); return set;
+STC_INLINE cbitset_t cbitset_or(cbitset_t s1, cbitset_t s2) {
+ cbitset_t set = cbitset_from(s1);
+ cbitset_set_or(&set, s2); return set;
}
-STC_INLINE CBitset cbitset_xor(CBitset s1, CBitset s2) {
- CBitset set = cbitset_from(s1);
- cbitset_setXor(&set, s2); return set;
+STC_INLINE cbitset_t cbitset_xor(cbitset_t s1, cbitset_t s2) {
+ cbitset_t set = cbitset_from(s1);
+ cbitset_set_xor(&set, s2); return set;
}
-STC_INLINE CBitset cbitset_not(CBitset s1) {
- CBitset set = cbitset_from(s1);
- cbitset_flipAll(&set); return set;
+STC_INLINE cbitset_t cbitset_not(cbitset_t s1) {
+ cbitset_t set = cbitset_from(s1);
+ cbitset_flip_all(&set); return set;
}
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
-STC_API void cbitset_resize(CBitset* self, size_t size, bool value) {
+STC_API void cbitset_resize(cbitset_t* self, size_t size, bool value) {
size_t new_n = (size + 63) >> 6, osize = self->size, old_n = (osize + 63) >> 6;
self->_arr = (uint64_t *) realloc(self->_arr, new_n * 8);
self->size = size;
@@ -158,7 +158,7 @@ STC_API void cbitset_resize(CBitset* self, size_t size, bool value) {
}
}
-STC_API size_t cbitset_count(CBitset set) {
+STC_API size_t cbitset_count(cbitset_t set) {
size_t count = 0, n = (set.size + 63) >> 6;
if (set.size > 0) {
--n; for (size_t i=0; i<n; ++i) count += cbitset_popcnt64(set._arr[i]);
diff --git a/stc/cdefs.h b/stc/cdefs.h
index 8e536438..8f5b896f 100644
--- a/stc/cdefs.h
+++ b/stc/cdefs.h
@@ -57,28 +57,28 @@
/*#define FOO(...) c_MACRO_OVERLOAD(FOO, __VA_ARGS__) #define FOO_0() "0" #define FOO_1(x) "1" #define FOO_2(x,y) "2"*/
#define c_new(T) ((T *) malloc(sizeof(T)))
-#define c_new_N(T, n) ((T *) malloc(sizeof(T) * (n)))
+#define c_new_n(T, n) ((T *) malloc(sizeof(T) * (n)))
#define c_max_alloca (1000)
#define c_swap(T, x, y) { T __t = x; x = y; y = __t; }
#define c_defaultInitRaw(x) (x)
#define c_defaultGetRaw(ptr) (*(ptr))
-#define c_noCompare(x, y) (0)
-#define c_memCompare(x, y) memcmp(x, y, sizeof(*(y)))
-#define c_memEquals(x, y) (memCompare(x, y) == 0)
-#define c_defaultEquals(x, y) (*(x) == *(y))
-#define c_defaultLess(x, y) (*(x) < *(y))
+#define c_no_compare(x, y) (0)
+#define c_mem_compare(x, y) memcmp(x, y, sizeof(*(y)))
+#define c_mem_equals(x, y) (memCompare(x, y) == 0)
+#define c_default_equals(x, y) (*(x) == *(y))
+#define c_default_less(x, y) (*(x) < *(y))
#define c_compare(less, x, y) (less(x, y) ? -1 : less(y, x))
-#define c_defaultCompare(x, y) c_compare(c_defaultLess, x, y)
-#define c_defaultDestroy(p) ((void)0)
+#define c_default_compare(x, y) c_compare(c_default_less, x, y)
+#define c_default_destroy(p) ((void)0)
#define c_foreach(it, prefix, container) \
for (prefix##_iter_t it = prefix##_begin(&container); it.item; it = prefix##_next(it))
#define c_items(...) {__VA_ARGS__}
#define c_push(container, prefix, items) do { \
const prefix##_input_t __arr[] = items; \
- prefix##_pushN(container, __arr, sizeof(__arr)/sizeof(prefix##_input_t)); \
+ prefix##_push_n(container, __arr, sizeof(__arr)/sizeof(prefix##_input_t)); \
} while (0)
/* One-byte-at-a-time hash based on Murmur's mix */
@@ -95,13 +95,13 @@ static inline uint32_t c_defaultHash(const void *data, size_t len) {
/* https://programmingpraxis.com/2018/06/19/fibonacci-hash */
/* https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/ */
-static inline uint32_t c_fibonacciHash32(const void* data, size_t len) {
+static inline uint32_t c_fibonacci_hash32(const void* data, size_t len) {
const volatile uint32_t *key = (const uint32_t *) data;
uint32_t x = *key++ * 2654435769u;
while (len -= 4) x ^= *key++ * 2654435769u;
return x;
}
-static inline uint32_t c_fibonacciHash64(const void* data, size_t len) {
+static inline uint32_t c_fibonacci_hash64(const void* data, size_t len) {
const volatile uint64_t *key = (const uint64_t *) data;
uint64_t x = *key++ * 11400714819323198485ull;
while (len -= 8) x ^= *key++ * 11400714819323198485ull;
diff --git a/stc/clist.h b/stc/clist.h
index 026becff..e55880ba 100644
--- a/stc/clist.h
+++ b/stc/clist.h
@@ -34,15 +34,15 @@
#include <stdio.h>
#include <stc/clist.h>
- #include <stc/crandom.h>
- declare_CList(ix, int64_t);
+ #include <stc/crand.h>
+ declare_clist(ix, int64_t);
int main() {
- CList_ix list = clist_init;
- pcg32_random_t pcg = pcg32_seed(123, 0);
+ clist_ix list = clist_init;
+ crand_eng32_t pcg = crand_eng32_init(12345);
int n;
for (int i=0; i<1000000; ++i) // one million
- clist_ix_pushBack(&list, pcg32_random(&pcg));
+ clist_ix_push_back(&list, crand_gen_i32(&pcg));
n = 0;
c_foreach (i, clist_ix, list)
if (++n % 10000 == 0) printf("%8d: %10zd\n", n, i.item->value);
@@ -56,30 +56,30 @@
}
*/
-#define declare_CList(...) c_MACRO_OVERLOAD(declare_CList, __VA_ARGS__)
+#define declare_clist(...) c_MACRO_OVERLOAD(declare_clist, __VA_ARGS__)
-#define declare_CList_2(tag, Value) \
- declare_CList_3(tag, Value, c_defaultDestroy)
-#define declare_CList_3(tag, Value, valueDestroy) \
- declare_CList_4(tag, Value, valueDestroy, c_defaultCompare)
-#define declare_CList_4(tag, Value, valueDestroy, valueCompare) \
- declare_CList_6(tag, Value, valueDestroy, Value, valueCompare, c_defaultGetRaw)
-#define declare_CList_str() \
- declare_CList_6(str, CStr, cstr_destroy, const char*, cstr_compareRaw, cstr_getRaw)
+#define declare_clist_2(tag, Value) \
+ declare_clist_3(tag, Value, c_default_destroy)
+#define declare_clist_3(tag, Value, valueDestroy) \
+ declare_clist_4(tag, Value, valueDestroy, c_default_compare)
+#define declare_clist_4(tag, Value, valueDestroy, valueCompare) \
+ declare_clist_6(tag, Value, valueDestroy, Value, valueCompare, c_defaultGetRaw)
+#define declare_clist_str() \
+ declare_clist_6(str, cstr_t, cstr_destroy, const char*, cstr_compareRaw, cstr_getRaw)
-#define declare_CListTypes(tag, Value) \
- typedef struct CListNode_##tag { \
- struct CListNode_##tag *next; \
+#define declare_clist_types(tag, Value) \
+ typedef struct clistnode_##tag { \
+ struct clistnode_##tag *next; \
Value value; \
- } CListNode_##tag; \
+ } clistnode_##tag; \
\
- typedef struct CList_##tag { \
- CListNode_##tag *last; \
- } CList_##tag; \
+ typedef struct clist_##tag { \
+ clistnode_##tag *last; \
+ } clist_##tag; \
\
typedef struct { \
- CListNode_##tag *item, **_last; \
- } CListIter_##tag, clist_##tag##_iter_t
+ clistnode_##tag *item, **_last; \
+ } clist_##tag##_iter_t
#define clist_init {NULL}
#define clist_front(list) (list).last->next->value
@@ -87,49 +87,49 @@
#define clist_empty(list) ((list).last == NULL)
-#define declare_CList_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
+#define declare_clist_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
\
- declare_CListTypes(tag, Value); \
+ declare_clist_types(tag, Value); \
\
- STC_INLINE CList_##tag \
- clist_##tag##_init(void) {CList_##tag x = clist_init; return x;} \
+ STC_INLINE clist_##tag \
+ clist_##tag##_init(void) {clist_##tag x = clist_init; return x;} \
STC_API void \
- clist_##tag##_destroy(CList_##tag* self); \
+ clist_##tag##_destroy(clist_##tag* self); \
STC_INLINE void \
- clist_##tag##_clear(CList_##tag* self) {clist_##tag##_destroy(self);} \
+ clist_##tag##_clear(clist_##tag* self) {clist_##tag##_destroy(self);} \
STC_API void \
- clist_##tag##_pushBack(CList_##tag* self, Value value); \
+ clist_##tag##_push_back(clist_##tag* self, Value value); \
STC_API void \
- clist_##tag##_pushFront(CList_##tag* self, Value value); \
+ clist_##tag##_push_front(clist_##tag* self, Value value); \
STC_API void \
- clist_##tag##_pushN(CList_##tag *self, const Value in[], size_t size); \
+ clist_##tag##_push_n(clist_##tag *self, const Value in[], size_t size); \
STC_API void \
- clist_##tag##_popFront(CList_##tag* self); \
+ clist_##tag##_pop_front(clist_##tag* self); \
STC_API void \
- clist_##tag##_insertAfter(CList_##tag* self, clist_##tag##_iter_t pos, Value value); \
+ clist_##tag##_insert_after(clist_##tag* self, clist_##tag##_iter_t pos, Value value); \
STC_API void \
- clist_##tag##_eraseAfter(CList_##tag* self, clist_##tag##_iter_t pos); \
+ clist_##tag##_erase_after(clist_##tag* self, clist_##tag##_iter_t pos); \
STC_API void \
- clist_##tag##_spliceFront(CList_##tag* self, CList_##tag* other); \
+ clist_##tag##_splice_front(clist_##tag* self, clist_##tag* other); \
STC_API void \
- clist_##tag##_spliceAfter(CList_##tag* self, clist_##tag##_iter_t pos, CList_##tag* other); \
+ clist_##tag##_splice_after(clist_##tag* self, clist_##tag##_iter_t pos, clist_##tag* other); \
STC_API clist_##tag##_iter_t \
- clist_##tag##_findBefore(CList_##tag* self, RawValue val); \
+ clist_##tag##_find_before(clist_##tag* self, RawValue val); \
STC_API Value* \
- clist_##tag##_find(CList_##tag* self, RawValue val); \
+ clist_##tag##_find(clist_##tag* self, RawValue val); \
STC_API clist_##tag##_iter_t \
- clist_##tag##_remove(CList_##tag* self, RawValue val); \
+ clist_##tag##_remove(clist_##tag* self, RawValue val); \
STC_API void \
- clist_##tag##_sort(CList_##tag* self); \
+ clist_##tag##_sort(clist_##tag* self); \
\
STC_INLINE Value* \
- clist_##tag##_front(CList_##tag* self) {return &self->last->next->value;} \
+ clist_##tag##_front(clist_##tag* self) {return &self->last->next->value;} \
STC_INLINE Value* \
- clist_##tag##_back(CList_##tag* self) {return &self->last->value;} \
+ clist_##tag##_back(clist_##tag* self) {return &self->last->value;} \
\
STC_INLINE clist_##tag##_iter_t \
- clist_##tag##_begin(CList_##tag* self) { \
- CListNode_##tag *head = self->last ? self->last->next : NULL; \
+ clist_##tag##_begin(clist_##tag* self) { \
+ clistnode_##tag *head = self->last ? self->last->next : NULL; \
clist_##tag##_iter_t it = {head, &self->last}; return it; \
} \
STC_INLINE clist_##tag##_iter_t \
@@ -137,61 +137,61 @@
it.item = it.item == *it._last ? NULL : it.item->next; return it; \
} \
STC_INLINE clist_##tag##_iter_t \
- clist_##tag##_last(CList_##tag* self) { \
+ clist_##tag##_last(clist_##tag* self) { \
clist_##tag##_iter_t it = {self->last, &self->last}; return it; \
} \
\
- implement_CList_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
- typedef RawValue CListRawValue_##tag; \
- typedef Value CListValue_##tag, clist_##tag##_input_t
+ implement_clist_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
+ typedef RawValue clist_##tag##_rawvalue_t; \
+ typedef Value clist_##tag##_value_t, clist_##tag##_input_t
/* -------------------------- IMPLEMENTATION ------------------------- */
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
-#define implement_CList_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
+#define implement_clist_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
\
STC_API void \
- clist_##tag##_destroy(CList_##tag* self) { \
+ clist_##tag##_destroy(clist_##tag* self) { \
while (self->last) \
- clist_##tag##_popFront(self); \
+ clist_##tag##_pop_front(self); \
} \
\
STC_API void \
- clist_##tag##_pushBack(CList_##tag* self, Value value) { \
- _clist_insertAfter(self, tag, self->last, value); \
+ clist_##tag##_push_back(clist_##tag* self, Value value) { \
+ _clist_insert_after(self, tag, self->last, value); \
self->last = entry; \
} \
STC_API void \
- clist_##tag##_pushFront(CList_##tag* self, Value value) { \
- _clist_insertAfter(self, tag, self->last, value); \
+ clist_##tag##_push_front(clist_##tag* self, Value value) { \
+ _clist_insert_after(self, tag, self->last, value); \
if (!self->last) self->last = entry; \
} \
STC_API void \
- clist_##tag##_pushN(CList_##tag *self, const Value in[], size_t size) { \
- for (size_t i=0; i<size; ++i) clist_##tag##_pushBack(self, in[i]); \
+ clist_##tag##_push_n(clist_##tag *self, const Value in[], size_t size) { \
+ for (size_t i=0; i<size; ++i) clist_##tag##_push_back(self, in[i]); \
} \
STC_API void \
- clist_##tag##_popFront(CList_##tag* self) { \
- _clist_eraseAfter(self, tag, self->last, valueDestroy); \
+ clist_##tag##_pop_front(clist_##tag* self) { \
+ _clist_erase_after(self, tag, self->last, valueDestroy); \
} \
\
STC_API void \
- clist_##tag##_insertAfter(CList_##tag* self, clist_##tag##_iter_t pos, Value value) { \
- _clist_insertAfter(self, tag, pos.item, value); \
+ clist_##tag##_insert_after(clist_##tag* self, clist_##tag##_iter_t pos, Value value) { \
+ _clist_insert_after(self, tag, pos.item, value); \
if (!self->last || pos.item == self->last) self->last = entry; \
} \
STC_API void \
- clist_##tag##_eraseAfter(CList_##tag* self, clist_##tag##_iter_t pos) { \
- _clist_eraseAfter(self, tag, pos.item, valueDestroy); \
+ clist_##tag##_erase_after(clist_##tag* self, clist_##tag##_iter_t pos) { \
+ _clist_erase_after(self, tag, pos.item, valueDestroy); \
} \
\
static inline void \
- _clist_##tag##_splice(CList_##tag* self, clist_##tag##_iter_t pos, CList_##tag* other, bool bottom) { \
+ _clist_##tag##_splice(clist_##tag* self, clist_##tag##_iter_t pos, clist_##tag* other, bool bottom) { \
if (!pos.item) \
self->last = pos.item = other->last; \
else if (other->last) { \
- CListNode_##tag *next = pos.item->next; \
+ clistnode_##tag *next = pos.item->next; \
pos.item->next = other->last->next; \
other->last->next = next; \
if (bottom && pos.item == self->last) self->last = other->last; \
@@ -199,16 +199,16 @@
other->last = NULL; \
} \
STC_API void \
- clist_##tag##_spliceFront(CList_##tag* self, CList_##tag* other) { \
+ clist_##tag##_splice_front(clist_##tag* self, clist_##tag* other) { \
_clist_##tag##_splice(self, clist_##tag##_last(self), other, false); \
} \
STC_API void \
- clist_##tag##_spliceAfter(CList_##tag* self, clist_##tag##_iter_t pos, CList_##tag* other) { \
+ clist_##tag##_splice_after(clist_##tag* self, clist_##tag##_iter_t pos, clist_##tag* other) { \
_clist_##tag##_splice(self, pos, other, true); \
} \
\
STC_API clist_##tag##_iter_t \
- clist_##tag##_findBefore(CList_##tag* self, RawValue val) { \
+ clist_##tag##_find_before(clist_##tag* self, RawValue val) { \
clist_##tag##_iter_t prev = {self->last, &self->last}; \
c_foreach (i, clist_##tag, *self) { \
RawValue r = valueGetRaw(&i.item->value); \
@@ -221,54 +221,54 @@
} \
\
STC_API Value* \
- clist_##tag##_find(CList_##tag* self, RawValue val) { \
- clist_##tag##_iter_t it = clist_##tag##_findBefore(self, val); \
+ clist_##tag##_find(clist_##tag* self, RawValue val) { \
+ clist_##tag##_iter_t it = clist_##tag##_find_before(self, val); \
return it.item ? &it.item->next->value : NULL; \
} \
\
STC_API clist_##tag##_iter_t \
- clist_##tag##_remove(CList_##tag* self, RawValue val) { \
- clist_##tag##_iter_t it = clist_##tag##_findBefore(self, val); \
- if (it.item) clist_##tag##_eraseAfter(self, it); \
+ clist_##tag##_remove(clist_##tag* self, RawValue val) { \
+ clist_##tag##_iter_t it = clist_##tag##_find_before(self, val); \
+ if (it.item) clist_##tag##_erase_after(self, it); \
return it; \
} \
\
static inline int \
- clist_##tag##_sortCompare(const void* x, const void* y) { \
- RawValue a = valueGetRaw(&((CListNode_##tag *) x)->value); \
- RawValue b = valueGetRaw(&((CListNode_##tag *) y)->value); \
+ clist_##tag##_sort_compare(const void* x, const void* y) { \
+ RawValue a = valueGetRaw(&((clistnode_##tag *) x)->value); \
+ RawValue b = valueGetRaw(&((clistnode_##tag *) y)->value); \
return valueCompareRaw(&a, &b); \
} \
STC_API void \
- clist_##tag##_sort(CList_##tag* self) { \
- CListNode__base* last = _clist_mergesort((CListNode__base *) self->last->next, clist_##tag##_sortCompare); \
- self->last = (CListNode_##tag *) last; \
+ clist_##tag##_sort(clist_##tag* self) { \
+ clistnode__base* last = _clist_mergesort((clistnode__base *) self->last->next, clist_##tag##_sort_compare); \
+ self->last = (clistnode_##tag *) last; \
}
-#define _clist_insertAfter(self, tag, node, val) \
- CListNode_##tag *entry = c_new (CListNode_##tag), \
+#define _clist_insert_after(self, tag, node, val) \
+ clistnode_##tag *entry = c_new (clistnode_##tag), \
*next = self->last ? node->next : entry; \
entry->value = val; \
entry->next = next; \
if (node) node->next = entry
/* +: set self->last based on node */
-#define _clist_eraseAfter(self, tag, node, valueDestroy) \
- CListNode_##tag* del = node->next, *next = del->next; \
+#define _clist_erase_after(self, tag, node, valueDestroy) \
+ clistnode_##tag* del = node->next, *next = del->next; \
node->next = next; \
if (del == next) self->last = NULL; \
else if (self->last == del) self->last = node; \
valueDestroy(&del->value); \
free(del)
-declare_CListTypes(_base, int);
+declare_clist_types(_base, int);
/* Singly linked list Mergesort implementation by Simon Tatham. O(n*log n).
* https://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
*/
-static inline CListNode__base *
-_clist_mergesort(CListNode__base *list, int (*cmp)(const void*, const void*)) {
- CListNode__base *p, *q, *e, *tail, *oldhead;
+static inline clistnode__base *
+_clist_mergesort(clistnode__base *list, int (*cmp)(const void*, const void*)) {
+ clistnode__base *p, *q, *e, *tail, *oldhead;
int insize = 1, nmerges, psize, qsize, i;
if (!list) return NULL;
@@ -321,7 +321,7 @@ _clist_mergesort(CListNode__base *list, int (*cmp)(const void*, const void*)) {
}
#else
-#define implement_CList_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw)
+#define implement_clist_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw)
#endif
#endif
diff --git a/stc/cmap.h b/stc/cmap.h
index 7bf94ab3..7a58506e 100644
--- a/stc/cmap.h
+++ b/stc/cmap.h
@@ -24,21 +24,21 @@
/* // Example:
#include <stdio.h>
#include <stc/cmap.h>
-declare_CSet(sx, int); // Set of int
-declare_CMap(mx, int, char); // Map of int -> char
+declare_cset(sx, int); // Set of int
+declare_cmap(mx, int, char); // Map of int -> char
int main(void) {
- CSet_sx s = cset_init;
+ cset_sx s = cset_init;
cset_sx_put(&s, 5);
cset_sx_put(&s, 8);
c_foreach (i, cset_sx, s) printf("set %d\n", i.item->key);
cset_sx_destroy(&s);
- CMap_mx m = cmap_init;
+ cmap_mx m = cmap_init;
cmap_mx_put(&m, 5, 'a');
cmap_mx_put(&m, 8, 'b');
cmap_mx_put(&m, 12, 'c');
- CMapEntry_mx *e = cmap_mx_find(&m, 10); // = NULL
+ cmapentry_mx *e = cmap_mx_find(&m, 10); // = NULL
char val = cmap_mx_find(&m, 5)->value;
cmap_mx_put(&m, 5, 'd'); // update
cmap_mx_erase(&m, 8);
@@ -55,69 +55,67 @@ int main(void) {
#define cmap_init {NULL, NULL, 0, 0, 0.85f, 0.15f}
#define cmap_size(m) ((size_t) (m).size)
-#define cmap_bucketCount(m) ((size_t) (m).bucketCount)
#define cset_init cmap_init
#define cset_size(s) cmap_size(s)
-#define cset_bucketCount(s) cmap_bucketCount(s)
/* 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))
-#define chash_entryIndex(h, entryPtr) ((entryPtr) - (h).table)
+#define chash_entry_index(h, entryPtr) ((entryPtr) - (h).table)
enum {chash_HASH = 0x7f, chash_USED = 0x80};
-#define declare_CMap(...) \
- c_MACRO_OVERLOAD(declare_CMap, __VA_ARGS__)
+#define declare_cmap(...) \
+ c_MACRO_OVERLOAD(declare_cmap, __VA_ARGS__)
-#define declare_CMap_3(tag, Key, Value) \
- declare_CMap_4(tag, Key, Value, c_defaultDestroy)
+#define declare_cmap_3(tag, Key, Value) \
+ declare_cmap_4(tag, Key, Value, c_default_destroy)
-#define declare_CMap_4(tag, Key, Value, valueDestroy) \
- declare_CMap_6(tag, Key, Value, valueDestroy, c_defaultEquals, c_defaultHash)
+#define declare_cmap_4(tag, Key, Value, valueDestroy) \
+ declare_cmap_6(tag, Key, Value, valueDestroy, c_default_equals, c_defaultHash)
-#define declare_CMap_6(tag, Key, Value, valueDestroy, keyEquals, keyHash) \
- declare_CMap_10(tag, Key, Value, valueDestroy, keyEquals, keyHash, \
- c_defaultDestroy, Key, c_defaultGetRaw, c_defaultInitRaw)
+#define declare_cmap_6(tag, Key, Value, valueDestroy, keyEquals, keyHash) \
+ declare_cmap_10(tag, Key, Value, valueDestroy, keyEquals, keyHash, \
+ c_default_destroy, Key, c_defaultGetRaw, c_defaultInitRaw)
-#define declare_CMap_10(tag, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
+#define declare_cmap_10(tag, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
keyDestroy, RawKey, keyGetRaw, keyInitRaw) \
- declare_CHASH(tag, CMap, cmap, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
+ declare_CHASH(tag, cmap, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
keyDestroy, RawKey, keyGetRaw, keyInitRaw)
-/* CSet: */
-#define declare_CSet(...) \
- c_MACRO_OVERLOAD(declare_CSet, __VA_ARGS__)
+/* cset: */
+#define declare_cset(...) \
+ c_MACRO_OVERLOAD(declare_cset, __VA_ARGS__)
-#define declare_CSet_2(tag, Key) \
- declare_CSet_4(tag, Key, c_defaultEquals, c_defaultHash)
+#define declare_cset_2(tag, Key) \
+ declare_cset_4(tag, Key, c_default_equals, c_defaultHash)
-#define declare_CSet_4(tag, Key, keyEquals, keyHash) \
- declare_CSet_5(tag, Key, keyEquals, keyHash, c_defaultDestroy)
+#define declare_cset_4(tag, Key, keyEquals, keyHash) \
+ declare_cset_5(tag, Key, keyEquals, keyHash, c_default_destroy)
-#define declare_CSet_5(tag, Key, keyEquals, keyHash, keyDestroy) \
- declare_CSet_8(tag, Key, keyEquals, keyHash, keyDestroy, \
+#define declare_cset_5(tag, Key, keyEquals, keyHash, keyDestroy) \
+ declare_cset_8(tag, Key, keyEquals, keyHash, keyDestroy, \
Key, c_defaultGetRaw, c_defaultInitRaw)
-#define declare_CSet_8(tag, Key, keyEqualsRaw, keyHashRaw, keyDestroy, \
+#define declare_cset_8(tag, Key, keyEqualsRaw, keyHashRaw, keyDestroy, \
RawKey, keyGetRaw, keyInitRaw) \
- declare_CHASH(tag, CSet, cset, Key, void, void, keyEqualsRaw, keyHashRaw, \
+ declare_CHASH(tag, cset, Key, void, void, keyEqualsRaw, keyHashRaw, \
keyDestroy, RawKey, keyGetRaw, keyInitRaw)
-/* CSet_str, CMap_str: */
-#define declare_CSet_str() \
- declare_CHASH_STR(str, CSet, cset, void, void)
+/* cset_str, cmap_str: */
+#define declare_cset_str() \
+ declare_CHASH_STR(str, cset, void, void)
-#define declare_CMap_str(...) \
- c_MACRO_OVERLOAD(declare_CMap_str, __VA_ARGS__)
+#define declare_cmap_str(...) \
+ c_MACRO_OVERLOAD(declare_cmap_str, __VA_ARGS__)
-#define declare_CMap_str_2(tag, Value) \
- declare_CHASH_STR(tag, CMap, cmap, Value, c_defaultDestroy)
+#define declare_cmap_str_2(tag, Value) \
+ declare_CHASH_STR(tag, cmap, Value, c_default_destroy)
-#define declare_CMap_str_3(tag, Value, ValueDestroy) \
- declare_CHASH_STR(tag, CMap, cmap, Value, ValueDestroy)
+#define declare_cmap_str_3(tag, Value, ValueDestroy) \
+ declare_CHASH_STR(tag, cmap, Value, ValueDestroy)
-#define declare_CHASH_STR(tag, CType, ctype, Value, valueDestroy) \
- declare_CHASH(tag, CType, ctype, CStr, Value, valueDestroy, cstr_equalsRaw, cstr_hashRaw, \
+#define declare_CHASH_STR(tag, ctype, Value, valueDestroy) \
+ declare_CHASH(tag, ctype, cstr_t, Value, valueDestroy, cstr_equalsRaw, cstr_hashRaw, \
cstr_destroy, const char*, cstr_getRaw, cstr_make)
#define OPT_1_cset(x)
@@ -126,124 +124,126 @@ enum {chash_HASH = 0x7f, chash_USED = 0x80};
#define OPT_2_cmap(x, y) x, y
/* CHASH full: use 'void' for Value if ctype is cset */
-#define declare_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
+#define declare_CHASH(tag, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
keyDestroy, RawKey, keyGetRaw, keyInitRaw) \
typedef struct { \
Key key; \
OPT_1_##ctype(Value value;) \
-} CType##Entry_##tag, ctype##_##tag##_entry_t; \
+} ctype##entry_##tag, ctype##_##tag##_entry_t; \
\
STC_INLINE void \
-ctype##entry_##tag##_destroy(CType##Entry_##tag* e) { \
+ctype##entry_##tag##_destroy(ctype##entry_##tag* e) { \
keyDestroy(&e->key); \
OPT_1_##ctype(valueDestroy(&e->value);) \
} \
typedef struct { \
RawKey key; \
OPT_1_##ctype(Value value;) \
-} CType##Input_##tag, ctype##_##tag##_input_t; \
+} ctype##_##tag##_input_t; \
\
-typedef RawKey CType##RawKey_##tag, ctype##_##tag##_rawkey_t; \
+typedef RawKey ctype##_##tag##_rawkey_t; \
\
typedef struct { \
- CType##Entry_##tag* table; \
+ ctype##entry_##tag* table; \
uint8_t* _hashx; \
- uint32_t size, bucketCount; \
- float maxLoadFactor; \
- float shrinkLimitFactor; \
-} CType##_##tag; \
+ uint32_t size, bucket_count; \
+ float max_load_factor; \
+ float shrink_limit_factor; \
+} ctype##_##tag; \
\
typedef struct { \
- CType##Entry_##tag *item, *_end; \
+ ctype##entry_##tag *item, *_end; \
uint8_t* _hx; \
-} CType##Iter_##tag, ctype##_##tag##_iter_t; \
+} ctype##_##tag##_iter_t; \
\
-STC_INLINE CType##_##tag \
-ctype##_##tag##_init(void) {CType##_##tag x = cmap_init; return x;} \
-STC_API CType##_##tag \
+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;} \
+STC_API ctype##_##tag \
ctype##_##tag##_make(size_t initialSize); \
STC_API void \
-ctype##_##tag##_pushN(CType##_##tag* self, const CType##Input_##tag in[], size_t size); \
+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); \
+ctype##_##tag##_destroy(ctype##_##tag* self); \
STC_API void \
-ctype##_##tag##_clear(CType##_##tag* self); \
+ctype##_##tag##_clear(ctype##_##tag* self); \
STC_INLINE void \
-ctype##_##tag##_setLoadFactors(CType##_##tag* self, float max, float shrink) { \
- self->maxLoadFactor = max; self->shrinkLimitFactor = shrink; \
+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##Entry_##tag* \
-ctype##_##tag##_find(const CType##_##tag* self, CType##RawKey_##tag rawKey); \
-STC_FORCE_INLINE CType##Entry_##tag* /* alias */ \
-ctype##_##tag##_get(const CType##_##tag* self, CType##RawKey_##tag rawKey) \
+STC_API ctype##entry_##tag* \
+ctype##_##tag##_find(const ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey); \
+STC_FORCE_INLINE ctype##entry_##tag* /* alias */ \
+ctype##_##tag##_get(const ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey) \
{return ctype##_##tag##_find(self, rawKey);} \
-STC_API CType##Entry_##tag* /* similar to c++ std::map.insert_or_assign(): */ \
-ctype##_##tag##_put(CType##_##tag* self, OPT_2_##ctype(CType##RawKey_##tag rawKey, Value value)); \
-OPT_1_##ctype(STC_API CType##Entry_##tag* /* similar to c++ std::map.operator[](): */ \
-ctype##_##tag##_insert(CType##_##tag* self, CType##RawKey_##tag rawKey, Value initValue);) \
+STC_API ctype##entry_##tag* /* similar to c++ std::map.insert_or_assign(): */ \
+ctype##_##tag##_put(ctype##_##tag* self, OPT_2_##ctype(ctype##_##tag##_rawkey_t rawKey, Value value)); \
+OPT_1_##ctype(STC_API ctype##entry_##tag* /* similar to c++ std::map.operator[](): */ \
+ctype##_##tag##_insert(ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey, Value initValue);) \
STC_INLINE void \
-ctype##_##tag##_swap(CType##_##tag* a, CType##_##tag* b) { c_swap(CType##_##tag, *a, *b); } \
+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); \
+ctype##_##tag##_reserve(ctype##_##tag* self, size_t size); \
STC_API bool \
-ctype##_##tag##_eraseEntry(CType##_##tag* self, CType##Entry_##tag* entry); \
+ctype##_##tag##_erase_entry(ctype##_##tag* self, ctype##entry_##tag* entry); \
STC_API bool \
-ctype##_##tag##_erase(CType##_##tag* self, CType##RawKey_##tag rawKey); \
+ctype##_##tag##_erase(ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey); \
STC_API ctype##_##tag##_iter_t \
-ctype##_##tag##_begin(CType##_##tag* map); \
+ctype##_##tag##_begin(ctype##_##tag* map); \
STC_API ctype##_##tag##_iter_t \
ctype##_##tag##_next(ctype##_##tag##_iter_t it); \
\
-implement_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
+implement_CHASH(tag, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
keyDestroy, RawKey, keyGetRaw, keyInitRaw) \
-typedef Key CType##Key_##tag, ctype##_##tag##_key_t; \
-typedef Value CType##Value_##tag, ctype##_##tag##_value_t
+typedef Key ctype##_##tag##_key_t; \
+typedef Value ctype##_##tag##_value_t
/* -------------------------- IMPLEMENTATION ------------------------- */
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
-#define implement_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
+#define implement_CHASH(tag, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
keyDestroy, RawKey, keyGetRaw, keyInitRaw) \
- STC_API CType##_##tag \
+ STC_API ctype##_##tag \
ctype##_##tag##_make(size_t initialSize) { \
- CType##_##tag h = ctype##_init; \
+ ctype##_##tag h = ctype##_init; \
ctype##_##tag##_reserve(&h, initialSize); \
return h; \
} \
STC_API void \
-ctype##_##tag##_pushN(CType##_##tag* self, const CType##Input_##tag in[], size_t size) { \
+ctype##_##tag##_push_n(ctype##_##tag* self, const ctype##_##tag##_input_t in[], size_t size) { \
for (size_t i=0; i<size; ++i) ctype##_##tag##_put(self, OPT_2_##ctype(in[i].key, in[i].value)); \
} \
\
-STC_INLINE void ctype##_##tag##_wipe_(CType##_##tag* self) { \
+STC_INLINE void ctype##_##tag##_wipe_(ctype##_##tag* self) { \
if (self->size == 0) return; \
- CType##Entry_##tag* e = self->table, *end = e + self->bucketCount; \
+ ctype##entry_##tag* e = self->table, *end = e + self->bucket_count; \
uint8_t *hx = self->_hashx; \
for (; e != end; ++e) if (*hx++) ctype##entry_##tag##_destroy(e); \
} \
\
-STC_API void ctype##_##tag##_destroy(CType##_##tag* self) { \
+STC_API void ctype##_##tag##_destroy(ctype##_##tag* self) { \
ctype##_##tag##_wipe_(self); \
free(self->_hashx); \
free(self->table); \
} \
\
-STC_API void ctype##_##tag##_clear(CType##_##tag* self) { \
+STC_API void ctype##_##tag##_clear(ctype##_##tag* self) { \
ctype##_##tag##_wipe_(self); \
self->size = 0; \
- memset(self->_hashx, 0, self->bucketCount); \
+ memset(self->_hashx, 0, self->bucket_count); \
} \
\
STC_API size_t \
-ctype##_##tag##_bucket(const CType##_##tag* self, const CType##RawKey_##tag* rawKeyPtr, uint32_t* hxPtr) { \
- uint32_t hash = keyHashRaw(rawKeyPtr, sizeof(CType##RawKey_##tag)); \
+ctype##_##tag##_bucket(const ctype##_##tag* self, const ctype##_##tag##_rawkey_t* rawKeyPtr, uint32_t* hxPtr) { \
+ uint32_t hash = keyHashRaw(rawKeyPtr, sizeof(ctype##_##tag##_rawkey_t)); \
uint32_t sx, hx = (hash & chash_HASH) | chash_USED; \
- size_t cap = self->bucketCount; \
+ size_t cap = self->bucket_count; \
size_t idx = chash_reduce(hash, cap); \
uint8_t* hashx = self->_hashx; \
while ((sx = hashx[idx])) { \
if (sx == hx) { \
- CType##RawKey_##tag r = keyGetRaw(&self->table[idx].key); \
+ ctype##_##tag##_rawkey_t r = keyGetRaw(&self->table[idx].key); \
if (keyEqualsRaw(&r, rawKeyPtr)) break; \
} \
if (++idx == cap) idx = 0; \
@@ -252,25 +252,25 @@ ctype##_##tag##_bucket(const CType##_##tag* self, const CType##RawKey_##tag* raw
return idx; \
} \
\
-STC_API CType##Entry_##tag* \
-ctype##_##tag##_find(const CType##_##tag* self, CType##RawKey_##tag rawKey) { \
+STC_API ctype##entry_##tag* \
+ctype##_##tag##_find(const ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey) { \
if (self->size == 0) return NULL; \
uint32_t hx; \
size_t idx = ctype##_##tag##_bucket(self, &rawKey, &hx); \
return self->_hashx[idx] ? &self->table[idx] : NULL; \
} \
\
-static inline void ctype##_##tag##_reserveExpand_(CType##_##tag* self) { \
- if (self->size + 1 >= self->bucketCount * self->maxLoadFactor) \
+static inline void ctype##_##tag##_reserveExpand_(ctype##_##tag* self) { \
+ if (self->size + 1 >= self->bucket_count * self->max_load_factor) \
ctype##_##tag##_reserve(self, 7 + self->size * 3 / 2); \
} \
\
-STC_API CType##Entry_##tag* \
-ctype##_##tag##_put(CType##_##tag* self, OPT_2_##ctype(CType##RawKey_##tag rawKey, Value value)) { \
+STC_API ctype##entry_##tag* \
+ctype##_##tag##_put(ctype##_##tag* self, OPT_2_##ctype(ctype##_##tag##_rawkey_t rawKey, Value value)) { \
ctype##_##tag##_reserveExpand_(self); \
uint32_t hx; \
size_t idx = ctype##_##tag##_bucket(self, &rawKey, &hx); \
- CType##Entry_##tag* e = &self->table[idx]; \
+ ctype##entry_##tag* e = &self->table[idx]; \
if (self->_hashx[idx]) \
OPT_1_##ctype(valueDestroy(&e->value)) ; \
else { \
@@ -283,12 +283,12 @@ ctype##_##tag##_put(CType##_##tag* self, OPT_2_##ctype(CType##RawKey_##tag rawKe
} \
\
OPT_1_##ctype( \
-STC_API CType##Entry_##tag* \
-ctype##_##tag##_insert(CType##_##tag* self, CType##RawKey_##tag rawKey, Value initValue) { \
+STC_API ctype##entry_##tag* \
+ctype##_##tag##_insert(ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey, Value initValue) { \
ctype##_##tag##_reserveExpand_(self); \
uint32_t hx; \
size_t idx = ctype##_##tag##_bucket(self, &rawKey, &hx); \
- CType##Entry_##tag* e = &self->table[idx]; \
+ ctype##entry_##tag* e = &self->table[idx]; \
if (! self->_hashx[idx]) { \
e->key = keyInitRaw(rawKey); \
self->_hashx[idx] = (uint8_t) hx; \
@@ -299,24 +299,24 @@ ctype##_##tag##_insert(CType##_##tag* self, CType##RawKey_##tag rawKey, Value in
}) \
\
STC_API size_t \
-ctype##_##tag##_reserve(CType##_##tag* self, size_t newcap) { \
- size_t oldcap = self->bucketCount; \
+ctype##_##tag##_reserve(ctype##_##tag* self, size_t newcap) { \
+ size_t oldcap = self->bucket_count; \
if (self->size > newcap) return oldcap; \
- newcap /= self->maxLoadFactor; newcap |= 1; \
- CType##_##tag tmp = { \
- c_new_N(CType##Entry_##tag, newcap), \
+ newcap /= self->max_load_factor; newcap |= 1; \
+ ctype##_##tag tmp = { \
+ c_new_n(ctype##entry_##tag, newcap), \
(uint8_t *) calloc(newcap, sizeof(uint8_t)), \
self->size, (uint32_t) newcap, \
- self->maxLoadFactor, self->shrinkLimitFactor \
+ self->max_load_factor, self->shrink_limit_factor \
}; \
ctype##_##tag##_swap(self, &tmp); \
\
- CType##Entry_##tag* e = tmp.table, *slot = self->table; \
+ ctype##entry_##tag* e = tmp.table, *slot = self->table; \
uint8_t* hashx = self->_hashx; \
uint32_t hx; \
for (size_t i = 0; i < oldcap; ++i, ++e) \
if (tmp._hashx[i]) { \
- CType##RawKey_##tag r = keyGetRaw(&e->key); \
+ ctype##_##tag##_rawkey_t r = keyGetRaw(&e->key); \
size_t idx = ctype##_##tag##_bucket(self, &r, &hx); \
slot[idx] = *e, \
hashx[idx] = (uint8_t) hx; \
@@ -327,11 +327,11 @@ ctype##_##tag##_reserve(CType##_##tag* self, size_t newcap) { \
} \
\
STC_API bool \
-ctype##_##tag##_eraseEntry(CType##_##tag* self, CType##Entry_##tag* entry) { \
- size_t i = chash_entryIndex(*self, entry), j = i, k, cap = self->bucketCount; \
- CType##Entry_##tag* slot = self->table; \
+ctype##_##tag##_erase_entry(ctype##_##tag* self, ctype##entry_##tag* entry) { \
+ size_t i = chash_entry_index(*self, entry), j = i, k, cap = self->bucket_count; \
+ ctype##entry_##tag* slot = self->table; \
uint8_t* hashx = self->_hashx; \
- CType##RawKey_##tag r; \
+ ctype##_##tag##_rawkey_t r; \
if (! hashx[i]) \
return false; \
do { /* deletion from hash table without tombstone */ \
@@ -339,7 +339,7 @@ ctype##_##tag##_eraseEntry(CType##_##tag* self, CType##Entry_##tag* entry) { \
if (! hashx[j]) \
break; \
r = keyGetRaw(&slot[j].key); \
- k = chash_reduce(keyHashRaw(&r, sizeof(CType##RawKey_##tag)), cap); \
+ k = chash_reduce(keyHashRaw(&r, sizeof(ctype##_##tag##_rawkey_t)), cap); \
if ((j < i) ^ (k <= i) ^ (k > j)) /* is k outside (i, j]? */ \
slot[i] = slot[j], hashx[i] = hashx[j], i = j; \
} while (true); \
@@ -350,21 +350,21 @@ ctype##_##tag##_eraseEntry(CType##_##tag* self, CType##Entry_##tag* entry) { \
} \
\
STC_API bool \
-ctype##_##tag##_erase(CType##_##tag* self, CType##RawKey_##tag rawKey) { \
+ctype##_##tag##_erase(ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey) { \
if (self->size == 0) \
return false; \
- size_t cap = self->bucketCount; \
- if (self->size < cap * self->shrinkLimitFactor && cap * sizeof(CType##Entry_##tag) > 1024) \
+ size_t cap = self->bucket_count; \
+ if (self->size < cap * self->shrink_limit_factor && cap * sizeof(ctype##entry_##tag) > 1024) \
ctype##_##tag##_reserve(self, self->size * 6 / 5); \
uint32_t hx; \
size_t i = ctype##_##tag##_bucket(self, &rawKey, &hx); \
- return ctype##_##tag##_eraseEntry(self, self->table + i); \
+ return ctype##_##tag##_erase_entry(self, self->table + i); \
} \
\
STC_API ctype##_##tag##_iter_t \
-ctype##_##tag##_begin(CType##_##tag* map) { \
+ctype##_##tag##_begin(ctype##_##tag* map) { \
uint8_t* hx = map->_hashx; \
- CType##Entry_##tag* e = map->table, *end = e + map->bucketCount; \
+ ctype##entry_##tag* e = map->table, *end = e + map->bucket_count; \
while (e != end && !*hx) ++e, ++hx; \
ctype##_##tag##_iter_t it = {e == end ? NULL : e, end, hx}; return it; \
} \
@@ -377,7 +377,7 @@ ctype##_##tag##_next(ctype##_##tag##_iter_t it) { \
}
#else
-#define implement_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
+#define implement_CHASH(tag, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
keyDestroy, RawKey, keyGetRaw, keyInitRaw)
#endif
diff --git a/stc/coption.h b/stc/copt.h
index 78500210..49c895e0 100644
--- a/stc/coption.h
+++ b/stc/copt.h
@@ -20,21 +20,21 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-#ifndef COPTION__H__
-#define COPTION__H__
+#ifndef COPT__H__
+#define COPT__H__
/* Inspired by https://attractivechaos.wordpress.com/2018/08/31/a-survey-of-argument-parsing-libraries-in-c-c
Fixed major bugs with option arguments (both long and short).
Added arg->faulty output field, and has a more consistent API.
- coption_get() has a very similar interface to GNU's getopt_long(). Each call
- parses one option and returns the option name. opt->arg points to the option
- argument if present. The function returns -1 when all command-line arguments
- are parsed. In this case, opt->ind is the index of the first non-option argument.
+ copt_get() is similar to GNU's getopt_long(). Each call parses one option and
+ returns the option name. opt->arg points to the option argument if present.
+ The function returns -1 when all command-line arguments are parsed. In this case,
+ opt->ind is the index of the first non-option argument.
Example:
int main(int argc, char *argv[])
{
- COptLong longopts[] = {
+ copt_long_t longopts[] = {
{"foo", copt_no_argument, 'f'},
{"bar", copt_required_argument, 'b'},
{"opt", copt_optional_argument, 'o'},
@@ -43,8 +43,8 @@ Example:
const char* optstr = "xy:z::123";
printf("program -x -y ARG -z [ARG] -1 -2 -3 --foo --bar ARG --opt [ARG] [ARGUMENTS]\n");
int c;
- COption opt = coption_init;
- while ((c = coption_get(&opt, argc, argv, optstr, longopts)) != -1) {
+ copt_t opt = copt_init;
+ while ((c = copt_get(&opt, argc, argv, optstr, longopts)) != -1) {
switch (c) {
case '?': printf("error: unknown option: %s\n", opt.faulty); break;
case ':': printf("error: missing argument for %s\n", opt.faulty); break;
@@ -75,17 +75,17 @@ typedef struct {
int longindex; /* idx of long option; or -1 if short */
int _i, _pos, _nargs;
char _faulty[4];
-} COption;
+} copt_t;
typedef struct {
char *name;
int has_arg;
int val;
-} COptLong;
+} copt_long_t;
-static const COption coption_init = {1, 0, NULL, NULL, -1, 1, 0, 0, {'-', '?', '\0'}};
+static const copt_t copt_init = {1, 0, NULL, NULL, -1, 1, 0, 0, {'-', '?', '\0'}};
-static void _coption_permute(char *argv[], int j, int n) { /* move argv[j] over n elements to the left */
+static void _copt_permute(char *argv[], int j, int n) { /* move argv[j] over n elements to the left */
int k;
char *p = argv[j];
for (k = 0; k < n; ++k)
@@ -93,13 +93,13 @@ static void _coption_permute(char *argv[], int j, int n) { /* move argv[j] over
argv[j - k] = p;
}
-/* @param opt output; must be initialized to coption_init on first call
+/* @param opt output; must be initialized to copt_init on first call
* @return ASCII val for a short option; longopt.val for a long option;
* -1 if argv[] is fully processed; '?' for an unknown option or
* an ambiguous long option; ':' if an option argument is missing
*/
-static int coption_get(COption *opt, int argc, char *argv[],
- const char *shortopts, const COptLong *longopts) {
+static int copt_get(copt_t *opt, int argc, char *argv[],
+ const char *shortopts, const copt_long_t *longopts) {
int optc = -1, i0, j, posixly_correct = (shortopts[0] == '+');
if (!posixly_correct) {
while (opt->_i < argc && (argv[opt->_i][0] != '-' || argv[opt->_i][1] == '\0'))
@@ -112,14 +112,14 @@ static int coption_get(COption *opt, int argc, char *argv[],
}
if (argv[opt->_i][0] == '-' && argv[opt->_i][1] == '-') { /* "--" or a long option */
if (argv[opt->_i][2] == '\0') { /* a bare "--" */
- _coption_permute(argv, opt->_i, opt->_nargs);
+ _copt_permute(argv, opt->_i, opt->_nargs);
++opt->_i, opt->ind = opt->_i - opt->_nargs;
return -1;
}
opt->opt = 0, optc = '?', opt->_pos = -1;
if (longopts) { /* parse long options */
int k, n_exact = 0, n_partial = 0;
- const COptLong *o = 0, *o_exact = 0, *o_partial = 0;
+ const copt_long_t *o = 0, *o_exact = 0, *o_partial = 0;
for (j = 2; argv[opt->_i][j] != '\0' && argv[opt->_i][j] != '='; ++j) {} /* find the end of the option name */
for (k = 0; longopts[k].name != 0; ++k)
if (strncmp(&argv[opt->_i][2], longopts[k].name, j - 2) == 0) {
@@ -164,7 +164,7 @@ static int coption_get(COption *opt, int argc, char *argv[],
++opt->_i, opt->_pos = 0;
if (opt->_nargs > 0) /* permute */
for (j = i0; j < opt->_i; ++j)
- _coption_permute(argv, j, opt->_nargs);
+ _copt_permute(argv, j, opt->_nargs);
}
opt->ind = opt->_i - opt->_nargs;
return optc;
diff --git a/stc/crand.h b/stc/crand.h
new file mode 100644
index 00000000..841cb473
--- /dev/null
+++ b/stc/crand.h
@@ -0,0 +1,148 @@
+/* MIT License
+ *
+ * Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef CRAND__H__
+#define CRAND__H__
+
+#include "cdefs.h"
+#include <string.h>
+/*
+ crand_eng32_t eng = crand_eng32_init(seed);
+ crand_uniform_f32_t fdist = crand_uniform_f32_init(1.0f, 6.0f);
+ crand_uniform_i32_t idist = crand_uniform_i32_init(1, 6);
+
+ uint32_t i = crand_gen_i32(&eng);
+ int j = crand_uniform_i32(&eng, idist);
+ float r = crand_uniform_f32(&eng, fdist);
+*/
+
+typedef struct {uint64_t state[2];} crand_eng32_t;
+typedef struct {int32_t min, range;} crand_uniform_i32_t;
+typedef struct {float min, range;} crand_uniform_f32_t;
+
+/* 32 bit random number generator engine */
+STC_API crand_eng32_t crand_eng32_with_seq(uint64_t seed, uint64_t seq);
+STC_INLINE crand_eng32_t crand_eng32_init(uint64_t seed) {
+ return crand_eng32_with_seq(seed, 1);
+}
+
+/* int random number generator, range [0, 2^32) */
+STC_API uint32_t crand_gen_i32(crand_eng32_t* rng);
+
+STC_INLINE float crand_gen_f32(crand_eng32_t* rng) {
+ union {uint32_t i; float f;} u = {0x3F800000u | (crand_gen_i32(rng) >> 9)};
+ return u.f - 1.0f;
+}
+
+/* int random number generator in range [low, high] */
+STC_INLINE crand_uniform_i32_t crand_uniform_i32_init(int32_t low, int32_t high) {
+ crand_uniform_i32_t dist = {low, high - low + 1}; return dist;
+}
+STC_INLINE int32_t crand_uniform_i32(crand_eng32_t* rng, crand_uniform_i32_t dist) {
+ return dist.min + (int32_t) (((uint64_t) crand_gen_i32(rng) * dist.range) >> 32);
+}
+
+/* float random number in range [low, high). Note: 23 bit resolution. */
+STC_INLINE crand_uniform_f32_t crand_uniform_f32_init(float low, float high) {
+ crand_uniform_f32_t dist = {low, high - low}; return dist;
+}
+STC_INLINE float crand_uniform_f32(crand_eng32_t* rng, crand_uniform_f32_t dist) {
+ return dist.min + crand_gen_f32(rng) * dist.range;
+}
+
+
+typedef struct {uint64_t state[4];} crand_eng64_t;
+typedef struct {double min, range;} crand_uniform_f64_t;
+
+/* 64 bit random number generator engine */
+STC_API crand_eng64_t crand_eng64_with_seq(uint64_t seed, uint64_t seq);
+STC_INLINE crand_eng64_t crand_eng64_init(uint64_t seed) {
+ return crand_eng64_with_seq(seed, 1);
+}
+/* int random number generator, range [0, 2^64) */
+STC_API uint64_t crand_gen_i64(crand_eng64_t* rng);
+
+STC_INLINE double crand_gen_f64(crand_eng64_t* rng) {
+ union {uint64_t i; double f;} u = {0x3FF0000000000000ull | (crand_gen_i64(rng) >> 12)};
+ return u.f - 1.0;
+}
+
+/* double random number in range [low, high). 52 bit resolution. */
+STC_INLINE crand_uniform_f64_t crand_uniform_f64_init(float low, float high) {
+ crand_uniform_f64_t dist = {low, high - low}; return dist;
+}
+STC_INLINE double crand_uniform_f64(crand_eng64_t* rng, crand_uniform_f64_t dist) {
+ return dist.min + crand_gen_f64(rng) * dist.range;
+}
+
+
+#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
+
+/* PCG32 random number generator: https://www.pcg-random.org/index.html */
+
+STC_API crand_eng32_t crand_eng32_with_seq(uint64_t seed, uint64_t seq) {
+ crand_eng32_t rng = {0u, (seq << 1u) | 1u}; /* inc must be odd */
+ crand_gen_i32(&rng);
+ rng.state[0] += seed;
+ crand_gen_i32(&rng);
+ return rng;
+}
+STC_API uint32_t crand_gen_i32(crand_eng32_t* rng) {
+ uint64_t old = rng->state[0];
+ rng->state[0] = old * 6364136223846793005ull + rng->state[1];
+ uint32_t xors = ((old >> 18u) ^ old) >> 27u;
+ uint32_t rot = old >> 59u;
+ return (xors >> rot) | (xors << ((-rot) & 31));
+}
+
+/* SFC64 random number generator: http://pracrand.sourceforge.net */
+
+STC_API crand_eng64_t crand_eng64_with_seq(uint64_t seed, uint64_t seq) {
+ crand_eng64_t rng = {seed, seed, seed, (seq << 1u) | 1u}; /* increment must be odd */
+ for (int i = 0; i < 12; ++i) crand_gen_i64(&rng);
+ return rng;
+}
+
+#if USE_SFC64
+STC_API uint64_t crand_gen_i64(crand_eng64_t* rng) { /* original sfc64 */
+ enum {LROT = 24, RSHIFT = 11, LSHIFT = 3};
+ uint64_t *s = rng->state;
+ const uint64_t result = s[0] + s[1] + s[3]++;
+ s[0] = s[1] ^ (s[1] >> RSHIFT);
+ s[1] = s[2] + (s[2] << LSHIFT);
+ s[2] = ((s[2] << LROT) | (s[2] >> (64 - LROT))) + result;
+ return result;
+}
+#else
+STC_API uint64_t crand_gen_i64(crand_eng64_t* rng) { /* copyright: Tyge Løvset */
+ enum {LROT = 24, RSHIFT = 11, LSHIFT = 3};
+ uint64_t *s = rng->state;
+ const uint64_t b = s[1], result = s[0] ^ (s[2] += s[3]|1);
+ s[0] = (b + (b << LSHIFT)) ^ (b >> RSHIFT);
+ s[1] = ((b << LROT) | (b >> (64 - LROT))) + result;
+ return result;
+}
+#endif
+#endif
+
+#endif
diff --git a/stc/crandom.h b/stc/crandom.h
deleted file mode 100644
index bc257f35..00000000
--- a/stc/crandom.h
+++ /dev/null
@@ -1,108 +0,0 @@
-/* MIT License
- *
- * Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-#ifndef CRANDOM__H__
-#define CRANDOM__H__
-
-#include "cdefs.h"
-#include <string.h>
-
-/* ------------- CRand32: PCG32 -------------- */
-
-typedef struct {uint64_t state, inc;} CRand32;
-
-STC_API CRand32 crand32_init2(uint64_t seed, uint64_t seq);
-
-STC_INLINE CRand32 crand32_init(uint64_t seed) {
- return crand32_init2(seed, seed);
-}
-STC_API uint32_t crand32_gen(CRand32* rng);
-
-/* Uniform random number in range [0, bound) */
-STC_INLINE uint32_t crand32_genBounded(CRand32* rng, uint32_t bound) {
- return (uint32_t) (((uint64_t) crand32_gen(rng) * bound) >> 32);
-}
-
-/* float random int number in range [0, 1). Note: 23 bit resolution. */
-STC_INLINE float crand32_genReal(CRand32* rng) {
- union {uint32_t i; float f;} u = {0x3F800000u | (crand32_gen(rng) >> 9)};
- return u.f - 1.0f;
-}
-
-/* ------------- CRand64: SFC64 -------------- */
-
-typedef struct {uint64_t state[4];} CRand64;
-
-STC_API CRand64 crand64_init(const uint64_t seed);
-
-STC_API uint64_t crand64_gen(CRand64* rng);
-
-/* float64 random int number in range [0, 1), 52 bit resolution */
-STC_INLINE double crand64_genReal(CRand64* rng) {
- union {uint64_t i; double f;} u = {0x3FF0000000000000ull | (crand64_gen(rng) >> 12)};
- return u.f - 1.0;
-}
-
-
-#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
-
-/* PCG32 random number generator: https://www.pcg-random.org/index.html */
-
-STC_API CRand32 crand32_init2(uint64_t seed, uint64_t seq) {
- CRand32 rng = {0u, (seq << 1u) | 1u}; /* inc must be odd */
- crand32_gen(&rng);
- rng.state += seed;
- crand32_gen(&rng);
- return rng;
-}
-
-STC_API uint32_t crand32_gen(CRand32* rng) {
- uint64_t old = rng->state;
- rng->state = old * 6364136223846793005ull + rng->inc;
- uint32_t xos = ((old >> 18u) ^ old) >> 27u;
- uint32_t rot = old >> 59u;
- return (xos >> rot) | (xos << ((-rot) & 31));
-}
-
-
-/* SFC64 random number generator: http://pracrand.sourceforge.net */
-
-STC_API CRand64 crand64_init(const uint64_t seed) {
- CRand64 state = {{seed, seed, seed, 1}};
- for (int i = 0; i < 12; ++i) crand64_gen(&state);
- return state;
-}
-
-STC_API uint64_t crand64_gen(CRand64* rng) {
- enum {LR=24, RS=11, LS=3};
- uint64_t *s = rng->state;
- const uint64_t result = s[0] + s[1] + s[3]++;
- s[0] = s[1] ^ (s[1] >> RS);
- s[1] = s[2] + (s[2] << LS);
- s[2] = (s[2] << LR) | (s[2] >> (64 - LR)) + result;
- return result;
-}
-
-#endif
-
-#endif
diff --git a/stc/cstr.h b/stc/cstr.h
index e1eebb7d..35049094 100644
--- a/stc/cstr.h
+++ b/stc/cstr.h
@@ -31,159 +31,159 @@
#include "cdefs.h"
-typedef struct CStr {
+typedef struct cstr_t {
char* str;
-} CStr;
+} cstr_t;
#define _cstr_rep(self) (((size_t *) (self)->str) - 2)
#define _cstr_size(s) ((size_t *) (s).str)[-2]
#define _cstr_mem(cap) (sizeof(size_t) * (3 + (cap)/sizeof(size_t)))
static size_t _cstr_nullrep[] = {0, 0, 0};
-static CStr cstr_init = {(char* ) &_cstr_nullrep[2]};
+static cstr_t cstr_init = {(char* ) &_cstr_nullrep[2]};
#define cstr_size(s) ((const size_t *) (s).str)[-2]
#define cstr_capacity(s) ((const size_t *) (s).str)[-1]
#define cstr_npos ((size_t) (-1))
-STC_API CStr
-cstr_makeN(const char* str, size_t len);
-STC_API CStr
+STC_API cstr_t
+cstr_make_n(const char* str, size_t len);
+STC_API cstr_t
cstr_from(const char* fmt, ...);
STC_API void
-cstr_reserve(CStr* self, size_t cap);
+cstr_reserve(cstr_t* self, size_t cap);
STC_API void
-cstr_resize(CStr* self, size_t len, char fill);
-STC_API CStr
-cstr_makeReserved(size_t cap);
-STC_API CStr*
-cstr_assignN(CStr* self, const char* str, size_t len);
-STC_API CStr*
-cstr_appendN(CStr* self, const char* str, size_t len);
+cstr_resize(cstr_t* self, size_t len, char fill);
+STC_API cstr_t
+cstr_make_reserved(size_t cap);
+STC_API cstr_t*
+cstr_assign_n(cstr_t* self, const char* str, size_t len);
+STC_API cstr_t*
+cstr_append_n(cstr_t* self, const char* str, size_t len);
STC_API void
-cstr_insertN(CStr* self, size_t pos, const char* str, size_t n);
+cstr_insert_n(cstr_t* self, size_t pos, const char* str, size_t n);
STC_API size_t
-cstr_replaceN(CStr* self, size_t pos, const char* str1, size_t n1, const char* str2, size_t n2);
+cstr_replace_n(cstr_t* self, size_t pos, const char* str1, size_t n1, const char* str2, size_t n2);
STC_API void
-cstr_erase(CStr* self, size_t pos, size_t n);
+cstr_erase(cstr_t* self, size_t pos, size_t n);
STC_API char*
-cstr_strnstr(CStr s, size_t pos, const char* needle, size_t n);
+cstr_strnstr(cstr_t s, size_t pos, const char* needle, size_t n);
STC_INLINE void
-cstr_destroy(CStr* self) {
+cstr_destroy(cstr_t* self) {
if (cstr_capacity(*self)) {
free(_cstr_rep(self));
}
}
-STC_INLINE CStr
-cstr_makeFilled(size_t len, char fill) {
- CStr s = cstr_init;
+STC_INLINE cstr_t
+cstr_make_filled(size_t len, char fill) {
+ cstr_t s = cstr_init;
if (len) cstr_resize(&s, len, fill);
return s;
}
-STC_INLINE CStr
+STC_INLINE cstr_t
cstr_make(const char* str) {
- return cstr_makeN(str, strlen(str));
+ return cstr_make_n(str, strlen(str));
}
-STC_INLINE CStr
-cstr_makeCopy(CStr s) {
- return cstr_makeN(s.str, cstr_size(s));
+STC_INLINE cstr_t
+cstr_make_copy(cstr_t s) {
+ return cstr_make_n(s.str, cstr_size(s));
}
STC_INLINE void
-cstr_clear(CStr* self) {
+cstr_clear(cstr_t* self) {
cstr_destroy(self);
*self = cstr_init;
}
-STC_INLINE CStr*
-cstr_assign(CStr* self, const char* str) {
- return cstr_assignN(self, str, strlen(str));
+STC_INLINE cstr_t*
+cstr_assign(cstr_t* self, const char* str) {
+ return cstr_assign_n(self, str, strlen(str));
}
-STC_INLINE CStr*
-cstr_copy(CStr* self, CStr s) {
- return cstr_assignN(self, s.str, cstr_size(s));
+STC_INLINE cstr_t*
+cstr_copy(cstr_t* self, cstr_t s) {
+ return cstr_assign_n(self, s.str, cstr_size(s));
}
-STC_INLINE CStr*
-cstr_take(CStr* self, CStr s) {
+STC_INLINE cstr_t*
+cstr_take(cstr_t* self, cstr_t s) {
if (self->str != s.str && cstr_capacity(*self))
free(_cstr_rep(self));
self->str = s.str;
return self;
}
-STC_INLINE CStr
-cstr_move(CStr* self) {
- CStr tmp = *self;
+STC_INLINE cstr_t
+cstr_move(cstr_t* self) {
+ cstr_t tmp = *self;
*self = cstr_init;
return tmp;
}
-STC_INLINE CStr*
-cstr_append(CStr* self, const char* str) {
- return cstr_appendN(self, str, strlen(str));
+STC_INLINE cstr_t*
+cstr_append(cstr_t* self, const char* str) {
+ return cstr_append_n(self, str, strlen(str));
}
-STC_INLINE CStr*
-cstr_appendS(CStr* self, CStr s) {
- return cstr_appendN(self, s.str, cstr_size(s));
+STC_INLINE cstr_t*
+cstr_appendS(cstr_t* self, cstr_t s) {
+ return cstr_append_n(self, s.str, cstr_size(s));
}
-STC_INLINE CStr*
-cstr_pushBack(CStr* self, char value) {
- return cstr_appendN(self, &value, 1);
+STC_INLINE cstr_t*
+cstr_push_back(cstr_t* self, char value) {
+ return cstr_append_n(self, &value, 1);
}
STC_INLINE void
-cstr_popBack(CStr* self) {
+cstr_pop_back(cstr_t* self) {
--_cstr_size(*self);
}
STC_INLINE char
-cstr_back(CStr s) {
+cstr_back(cstr_t s) {
return s.str[cstr_size(s) - 1];
}
STC_INLINE void
-cstr_insert(CStr* self, size_t pos, const char* str) {
- cstr_insertN(self, pos, str, strlen(str));
+cstr_insert(cstr_t* self, size_t pos, const char* str) {
+ cstr_insert_n(self, pos, str, strlen(str));
}
STC_INLINE size_t
-cstr_replace(CStr* self, size_t pos, const char* str1, const char* str2) {
- return cstr_replaceN(self, pos, str1, strlen(str1), str2, strlen(str2));
+cstr_replace(cstr_t* self, size_t pos, const char* str1, const char* str2) {
+ return cstr_replace_n(self, pos, str1, strlen(str1), str2, strlen(str2));
}
/* readonly */
STC_INLINE bool
-cstr_empty(CStr s) {
+cstr_empty(cstr_t s) {
return cstr_size(s) == 0;
}
STC_INLINE bool
-cstr_equals(CStr s1, const char* str) {
+cstr_equals(cstr_t s1, const char* str) {
return strcmp(s1.str, str) == 0;
}
STC_INLINE bool
-cstr_equalsS(CStr s1, CStr s2) {
+cstr_equalsS(cstr_t s1, cstr_t s2) {
return strcmp(s1.str, s2.str) == 0;
}
STC_INLINE int
cstr_compare(const void* s1, const void* s2) {
- return strcmp(((const CStr*)s1)->str, ((const CStr*)s2)->str);
+ return strcmp(((const cstr_t*)s1)->str, ((const cstr_t*)s2)->str);
}
STC_INLINE size_t
-cstr_findN(CStr s, size_t pos, const char* needle, size_t n) {
+cstr_findN(cstr_t s, size_t pos, const char* needle, size_t n) {
char* res = cstr_strnstr(s, pos, needle, n);
return res ? res - s.str : cstr_npos;
}
STC_INLINE size_t
-cstr_find(CStr s, size_t pos, const char* needle) {
+cstr_find(cstr_t s, size_t pos, const char* needle) {
char* res = strstr(s.str + pos, needle);
return res ? res - s.str : cstr_npos;
}
-/* CVec / CMap API functions: */
+/* CVec / cmap API functions: */
#define cstr_getRaw(x) ((x)->str)
#define cstr_compareRaw(x, y) strcmp(*(x), *(y))
@@ -200,7 +200,7 @@ STC_INLINE uint32_t cstr_hashRaw(const char* const* sPtr, size_t ignored) {
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
STC_API void
-cstr_reserve(CStr* self, size_t cap) {
+cstr_reserve(cstr_t* self, size_t cap) {
size_t len = cstr_size(*self), oldcap = cstr_capacity(*self);
if (cap > oldcap) {
size_t* rep = (size_t *) realloc(oldcap ? _cstr_rep(self) : NULL, _cstr_mem(cap));
@@ -211,40 +211,40 @@ cstr_reserve(CStr* self, size_t cap) {
}
STC_API void
-cstr_resize(CStr* self, size_t len, char fill) {
+cstr_resize(cstr_t* self, size_t len, char fill) {
size_t n = cstr_size(*self);
cstr_reserve(self, len);
if (len > n) memset(self->str + n, fill, len - n);
self->str[_cstr_size(*self) = len] = '\0';
}
-STC_API CStr
-cstr_makeReserved(size_t cap) {
+STC_API cstr_t
+cstr_make_reserved(size_t cap) {
if (cap == 0) return cstr_init;
size_t *rep = (size_t *) malloc(_cstr_mem(cap));
- CStr s = {(char *) (rep + 2)};
+ cstr_t s = {(char *) (rep + 2)};
rep[0] = 0, rep[1] = cap, s.str[0] = '\0';
return s;
}
-STC_API CStr
-cstr_makeN(const char* str, size_t len) {
+STC_API cstr_t
+cstr_make_n(const char* str, size_t len) {
if (len == 0) return cstr_init;
size_t *rep = (size_t *) malloc(_cstr_mem(len));
- CStr s = {(char *) (rep + 2)};
+ cstr_t s = {(char *) (rep + 2)};
memcpy(s.str, str, len);
s.str[rep[0] = rep[1] = len] = '\0';
return s;
}
-STC_API CStr
+STC_API cstr_t
cstr_from(const char* fmt, ...) {
- CStr tmp = cstr_init;
+ cstr_t tmp = cstr_init;
va_list args;
va_start(args, fmt);
int len = vsnprintf(NULL, (size_t)0, fmt, args);
if (len > 0) {
- tmp = cstr_makeReserved(len);
+ tmp = cstr_make_reserved(len);
vsprintf(tmp.str, fmt, args);
_cstr_size(tmp) = len;
}
@@ -252,8 +252,8 @@ cstr_from(const char* fmt, ...) {
return tmp;
}
-STC_API CStr*
-cstr_assignN(CStr* self, const char* str, size_t len) {
+STC_API cstr_t*
+cstr_assign_n(cstr_t* self, const char* str, size_t len) {
if (len || cstr_capacity(*self)) {
cstr_reserve(self, len);
memmove(self->str, str, len);
@@ -262,8 +262,8 @@ cstr_assignN(CStr* self, const char* str, size_t len) {
return self;
}
-STC_API CStr*
-cstr_appendN(CStr* self, const char* str, size_t len) {
+STC_API cstr_t*
+cstr_append_n(cstr_t* self, const char* str, size_t len) {
if (len) {
size_t oldlen = cstr_size(*self), newlen = oldlen + len;
if (newlen > cstr_capacity(*self))
@@ -274,7 +274,7 @@ cstr_appendN(CStr* self, const char* str, size_t len) {
return self;
}
-STC_INLINE void _cstr_internalMove(CStr* self, size_t pos1, size_t pos2) {
+STC_INLINE void _cstr_internalMove(cstr_t* self, size_t pos1, size_t pos2) {
if (pos1 == pos2)
return;
size_t len = cstr_size(*self), newlen = len + pos2 - pos1;
@@ -285,7 +285,7 @@ STC_INLINE void _cstr_internalMove(CStr* self, size_t pos1, size_t pos2) {
}
STC_API void
-cstr_insertN(CStr* self, size_t pos, const char* str, size_t n) {
+cstr_insert_n(cstr_t* self, size_t pos, const char* str, size_t n) {
char* xstr = (char *) memcpy(n > c_max_alloca ? malloc(n) : alloca(n), str, n);
_cstr_internalMove(self, pos, pos + n);
memcpy(&self->str[pos], xstr, n);
@@ -293,7 +293,7 @@ cstr_insertN(CStr* self, size_t pos, const char* str, size_t n) {
}
STC_API size_t
-cstr_replaceN(CStr* self, size_t pos, const char* str1, size_t n1, const char* str2, size_t n2) {
+cstr_replace_n(cstr_t* self, size_t pos, const char* str1, size_t n1, const char* str2, size_t n2) {
size_t pos2 = cstr_findN(*self, pos, str1, n1);
if (pos2 == cstr_npos) return cstr_npos;
char* xstr2 = (char *) memcpy(n2 > c_max_alloca ? malloc(n2) : alloca(n2), str2, n2);
@@ -304,7 +304,7 @@ cstr_replaceN(CStr* self, size_t pos, const char* str1, size_t n1, const char* s
}
STC_API void
-cstr_erase(CStr* self, size_t pos, size_t n) {
+cstr_erase(cstr_t* self, size_t pos, size_t n) {
size_t len = cstr_size(*self);
if (len) {
memmove(&self->str[pos], &self->str[pos + n], len - (pos + n));
@@ -313,7 +313,7 @@ cstr_erase(CStr* self, size_t pos, size_t n) {
}
STC_API char*
-cstr_strnstr(CStr s, size_t pos, const char* needle, size_t n) {
+cstr_strnstr(cstr_t s, size_t pos, const char* needle, size_t n) {
char *x = s.str + pos, /* haystack */
*z = s.str + cstr_size(s) - n + 1;
if (x >= z)
diff --git a/stc/cvec.h b/stc/cvec.h
index 74153455..d76c001b 100644
--- a/stc/cvec.h
+++ b/stc/cvec.h
@@ -34,67 +34,67 @@
#define cvec_front(cv) (cv).data[0]
#define cvec_back(cv) (cv).data[_cvec_size(cv) - 1] /* may have side effect */
-#define declare_CVec(...) c_MACRO_OVERLOAD(declare_CVec, __VA_ARGS__)
-#define declare_CVec_2(tag, Value) \
- declare_CVec_3(tag, Value, c_defaultDestroy)
-#define declare_CVec_3(tag, Value, valueDestroy) \
- declare_CVec_4(tag, Value, valueDestroy, c_defaultCompare)
-#define declare_CVec_4(tag, Value, valueDestroy, valueCompare) \
- declare_CVec_6(tag, Value, valueDestroy, valueCompare, Value, c_defaultGetRaw)
-#define declare_CVec_str() \
- declare_CVec_6(str, CStr, cstr_destroy, cstr_compareRaw, const char*, cstr_getRaw)
+#define declare_cvec(...) c_MACRO_OVERLOAD(declare_cvec, __VA_ARGS__)
+#define declare_cvec_2(tag, Value) \
+ declare_cvec_3(tag, Value, c_default_destroy)
+#define declare_cvec_3(tag, Value, valueDestroy) \
+ declare_cvec_4(tag, Value, valueDestroy, c_default_compare)
+#define declare_cvec_4(tag, Value, valueDestroy, valueCompare) \
+ declare_cvec_6(tag, Value, valueDestroy, valueCompare, Value, c_defaultGetRaw)
+#define declare_cvec_str() \
+ declare_cvec_6(str, cstr_t, cstr_destroy, cstr_compareRaw, const char*, cstr_getRaw)
-#define declare_CVec_6(tag, Value, valueDestroy, valueCompareRaw, RawValue, valueGetRaw) \
+#define declare_cvec_6(tag, Value, valueDestroy, valueCompareRaw, RawValue, valueGetRaw) \
\
-typedef struct CVec_##tag { \
+typedef struct cvec_##tag { \
Value* data; \
-} CVec_##tag; \
+} cvec_##tag; \
\
-STC_INLINE CVec_##tag \
-cvec_##tag##_init(void) {CVec_##tag x = cvec_init; return x;} \
-STC_API CVec_##tag \
+STC_INLINE cvec_##tag \
+cvec_##tag##_init(void) {cvec_##tag x = cvec_init; return x;} \
+STC_API cvec_##tag \
cvec_##tag##_make(size_t size, Value null); \
STC_API void \
-cvec_##tag##_pushN(CVec_##tag *self, const Value in[], size_t size); \
+cvec_##tag##_push_n(cvec_##tag *self, const Value in[], size_t size); \
STC_API void \
-cvec_##tag##_destroy(CVec_##tag* self); \
+cvec_##tag##_destroy(cvec_##tag* self); \
STC_API void \
-cvec_##tag##_reserve(CVec_##tag* self, size_t cap); \
+cvec_##tag##_reserve(cvec_##tag* self, size_t cap); \
STC_API void \
-cvec_##tag##_clear(CVec_##tag* self); \
+cvec_##tag##_clear(cvec_##tag* self); \
STC_API void \
-cvec_##tag##_pushBack(CVec_##tag* self, Value value); \
+cvec_##tag##_push_back(cvec_##tag* self, Value value); \
STC_INLINE void \
-cvec_##tag##_popBack(CVec_##tag* self) { \
+cvec_##tag##_pop_back(cvec_##tag* self) { \
valueDestroy(&self->data[_cvec_size(*self) - 1]); \
--_cvec_size(*self); \
} \
STC_INLINE Value* \
-cvec_##tag##_front(CVec_##tag* self) {return self->data;} \
+cvec_##tag##_front(cvec_##tag* self) {return self->data;} \
STC_INLINE Value* \
-cvec_##tag##_back(CVec_##tag* self) {return self->data + _cvec_size(*self) - 1;} \
+cvec_##tag##_back(cvec_##tag* self) {return self->data + _cvec_size(*self) - 1;} \
STC_INLINE Value* \
-cvec_##tag##_at(CVec_##tag* self, size_t i) {return self->data + i;} \
+cvec_##tag##_at(cvec_##tag* self, size_t i) {return self->data + i;} \
STC_API void \
-cvec_##tag##_insert(CVec_##tag* self, size_t pos, Value value); \
+cvec_##tag##_insert(cvec_##tag* self, size_t pos, Value value); \
STC_API void \
-cvec_##tag##_erase(CVec_##tag* self, size_t pos, size_t size); \
+cvec_##tag##_erase(cvec_##tag* self, size_t pos, size_t size); \
STC_API void \
-cvec_##tag##_sort(CVec_##tag* self); \
+cvec_##tag##_sort(cvec_##tag* self); \
STC_API size_t \
-cvec_##tag##_find(const CVec_##tag* self, RawValue rawValue); \
+cvec_##tag##_find(const cvec_##tag* self, RawValue rawValue); \
STC_INLINE void \
-cvec_##tag##_swap(CVec_##tag* a, CVec_##tag* b) { \
+cvec_##tag##_swap(cvec_##tag* a, cvec_##tag* b) { \
c_swap(Value*, a->data, b->data); \
} \
\
typedef struct { \
Value *item, *end; \
-} CVecIter_##tag, cvec_##tag##_iter_t; \
+} cvec_##tag##_iter_t; \
\
STC_INLINE cvec_##tag##_iter_t \
-cvec_##tag##_begin(CVec_##tag* vec) { \
+cvec_##tag##_begin(cvec_##tag* vec) { \
const size_t n = cvec_size(*vec); \
cvec_##tag##_iter_t it = {n ? vec->data : NULL, vec->data + n}; \
return it; \
@@ -105,32 +105,32 @@ cvec_##tag##_next(cvec_##tag##_iter_t it) { \
return it; \
} \
\
-implement_CVec_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
-typedef Value CVecValue_##tag, cvec_##tag##_input_t; \
-typedef RawValue CVecRawValue_##tag
+implement_cvec_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
+typedef Value cvec_##tag##_value_t, cvec_##tag##_input_t; \
+typedef RawValue cvec_##tag##_rawvalue_t
/* -------------------------- IMPLEMENTATION ------------------------- */
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
-#define implement_CVec_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
+#define implement_cvec_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
\
-STC_API CVec_##tag \
+STC_API cvec_##tag \
cvec_##tag##_make(size_t size, Value null) { \
- CVec_##tag vec = cvec_init; \
+ cvec_##tag vec = cvec_init; \
cvec_##tag##_reserve(&vec, size); \
_cvec_size(vec) = size; \
for (size_t i=0; i<size; ++i) vec.data[i] = null; \
return vec; \
} \
STC_API void \
-cvec_##tag##_pushN(CVec_##tag *self, const Value in[], size_t size) { \
+cvec_##tag##_push_n(cvec_##tag *self, const Value in[], size_t size) { \
cvec_##tag##_reserve(self, cvec_size(*self) + size); \
_cvec_size(*self) += size; \
for (size_t i=0; i<size; ++i) self->data[i] = in[i]; \
} \
\
STC_API void \
-cvec_##tag##_destroy(CVec_##tag* self) { \
+cvec_##tag##_destroy(cvec_##tag* self) { \
Value* p = self->data; \
size_t i = 0, n = cvec_size(*self); \
for (; i < n; ++p, ++i) valueDestroy(p); \
@@ -138,7 +138,7 @@ cvec_##tag##_destroy(CVec_##tag* self) { \
} \
\
STC_API void \
-cvec_##tag##_reserve(CVec_##tag* self, size_t cap) { \
+cvec_##tag##_reserve(cvec_##tag* self, size_t cap) { \
size_t len = cvec_size(*self); \
if (cap >= len) { \
size_t* rep = (size_t *) realloc(_cvec_alloced(self->data), 2 * sizeof(size_t) + cap * sizeof(Value)); \
@@ -149,14 +149,14 @@ cvec_##tag##_reserve(CVec_##tag* self, size_t cap) { \
} \
\
STC_API void \
-cvec_##tag##_clear(CVec_##tag* self) { \
- CVec_##tag cv = cvec_init; \
+cvec_##tag##_clear(cvec_##tag* self) { \
+ cvec_##tag cv = cvec_init; \
cvec_##tag##_destroy(self); \
*self = cv; \
} \
\
STC_API void \
-cvec_##tag##_pushBack(CVec_##tag* self, Value value) { \
+cvec_##tag##_push_back(cvec_##tag* self, Value value) { \
size_t len = cvec_size(*self); \
if (len == cvec_capacity(*self)) \
cvec_##tag##_reserve(self, 7 + len * 5 / 3); \
@@ -165,7 +165,7 @@ cvec_##tag##_pushBack(CVec_##tag* self, Value value) { \
} \
\
STC_API void \
-cvec_##tag##_insert(CVec_##tag* self, size_t pos, Value value) { \
+cvec_##tag##_insert(cvec_##tag* self, size_t pos, Value value) { \
size_t len = cvec_size(*self); \
if (len == cvec_capacity(*self)) \
cvec_##tag##_reserve(self, 7 + len * 5 / 3); \
@@ -175,7 +175,7 @@ cvec_##tag##_insert(CVec_##tag* self, size_t pos, Value value) { \
} \
\
STC_API void \
-cvec_##tag##_erase(CVec_##tag* self, size_t pos, size_t size) { \
+cvec_##tag##_erase(cvec_##tag* self, size_t pos, size_t size) { \
size_t len = cvec_size(*self); \
if (len) { \
Value* p = &self->data[pos], *start = p, *end = p + size; \
@@ -186,7 +186,7 @@ cvec_##tag##_erase(CVec_##tag* self, size_t pos, size_t size) { \
} \
\
STC_API size_t \
-cvec_##tag##_find(const CVec_##tag* self, RawValue rawValue) { \
+cvec_##tag##_find(const cvec_##tag* self, RawValue rawValue) { \
const Value *p = self->data, *end = p + cvec_size(*self); \
for (; p != end; ++p) { \
RawValue r = valueGetRaw(p); \
@@ -196,20 +196,20 @@ cvec_##tag##_find(const CVec_##tag* self, RawValue rawValue) { \
} \
\
STC_API int \
-cvec_##tag##_sortCompare(const void* x, const void* y) { \
+cvec_##tag##_sort_compare(const void* x, const void* y) { \
RawValue rx = valueGetRaw((const Value *) x); \
RawValue ry = valueGetRaw((const Value *) y); \
return valueCompareRaw(&rx, &ry); \
} \
STC_EXTERN_IMPORT void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)); \
STC_API void \
-cvec_##tag##_sort(CVec_##tag* self) { \
+cvec_##tag##_sort(cvec_##tag* self) { \
size_t len = cvec_size(*self); \
- if (len) qsort(self->data, len, sizeof(Value), cvec_##tag##_sortCompare); \
+ if (len) qsort(self->data, len, sizeof(Value), cvec_##tag##_sort_compare); \
}
#else
-#define implement_CVec_6(tag, Value, valueDestroy, valueCompareRaw, RawValue, valueGetRaw)
+#define implement_cvec_6(tag, Value, valueDestroy, valueCompareRaw, RawValue, valueGetRaw)
#endif
#if defined(_WIN32) && defined(_DLL)
diff --git a/stc/cvecpq.h b/stc/cvecpq.h
index 1d0f390b..2e6f2d09 100644
--- a/stc/cvecpq.h
+++ b/stc/cvecpq.h
@@ -24,15 +24,15 @@
/* Priority Queue using CVec as heap.
#include <stc/cvecpq.h>
- #include <stc/crandom.h>
- declare_CVec(i, int);
- declare_CVec_priority_queue(i, >); // min-heap (increasing values)
+ #include <stc/crand.h>
+ declare_cvec(i, int);
+ declare_cvec_priority_queue(i, >); // min-heap (increasing values)
int main() {
- pcg32_random_t pcg = pcg32_seed(1234, 0);
- CVec_i heap = cvec_init;
+ crand_eng32_t pcg = crand_eng32_init(1234);
+ cvec_i heap = cvec_init;
// Push one million random numbers onto the queue.
for (int i=0; i<1000000; ++i)
- cvecpq_i_push(&heap, pcg32_random(&pcg));
+ cvecpq_i_push(&heap, crand_gen_i32(&pcg));
// Extract the 100 smallest.
for (int i=0; i<100; ++i) {
printf("%d ", cvecpq_i_top(&heap));
@@ -47,37 +47,37 @@
#include "cvec.h"
-#define declare_CVec_priority_queue(tag, cmpOpr) /* < or > */ \
+#define declare_cvec_priority_queue(tag, cmpOpr) /* < or > */ \
\
STC_API void \
-cvecpq_##tag##_build(CVec_##tag* self); \
+cvecpq_##tag##_build(cvec_##tag* self); \
STC_API void \
-cvecpq_##tag##_erase(CVec_##tag* self, size_t i); \
-STC_INLINE CVecValue_##tag \
-cvecpq_##tag##_top(CVec_##tag* self) {return self->data[0];} \
+cvecpq_##tag##_erase(cvec_##tag* self, size_t i); \
+STC_INLINE cvec_##tag##_value_t \
+cvecpq_##tag##_top(cvec_##tag* self) {return self->data[0];} \
STC_INLINE void \
-cvecpq_##tag##_pop(CVec_##tag* self) {cvecpq_##tag##_erase(self, 0);} \
+cvecpq_##tag##_pop(cvec_##tag* self) {cvecpq_##tag##_erase(self, 0);} \
STC_API void \
-cvecpq_##tag##_push(CVec_##tag* self, CVecValue_##tag value); \
+cvecpq_##tag##_push(cvec_##tag* self, cvec_##tag##_value_t value); \
STC_API void \
-cvecpq_##tag##_pushN(CVec_##tag *self, const CVecValue_##tag in[], size_t size); \
+cvecpq_##tag##_push_n(cvec_##tag *self, const cvec_##tag##_value_t in[], size_t size); \
\
-implement_CVec_priority_queue(tag, cmpOpr) \
-typedef CVecValue_##tag cvecpq_##tag##_input_t
+implement_cvec_priority_queue(tag, cmpOpr) \
+typedef cvec_##tag##_value_t cvecpq_##tag##_input_t
/* -------------------------- IMPLEMENTATION ------------------------- */
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
-#define implement_CVec_priority_queue(tag, cmpOpr) \
+#define implement_cvec_priority_queue(tag, cmpOpr) \
\
STC_INLINE void \
-_cvecpq_##tag##_siftDown(CVecValue_##tag* arr, size_t i, size_t n) { \
+_cvecpq_##tag##_siftDown(cvec_##tag##_value_t* arr, size_t i, size_t n) { \
size_t r = i, c = i << 1; \
while (c <= n) { \
- if (c < n && cvec_##tag##_sortCompare(&arr[c], &arr[c + 1]) cmpOpr 0) \
+ if (c < n && cvec_##tag##_sort_compare(&arr[c], &arr[c + 1]) cmpOpr 0) \
++c; \
- if (cvec_##tag##_sortCompare(&arr[r], &arr[c]) cmpOpr 0) { \
- CVecValue_##tag t = arr[r]; arr[r] = arr[c]; arr[r = c] = t; \
+ if (cvec_##tag##_sort_compare(&arr[r], &arr[c]) cmpOpr 0) { \
+ cvec_##tag##_value_t t = arr[r]; arr[r] = arr[c]; arr[r = c] = t; \
} else \
return; \
c <<= 1; \
@@ -85,38 +85,38 @@ _cvecpq_##tag##_siftDown(CVecValue_##tag* arr, size_t i, size_t n) { \
} \
\
STC_API void \
-cvecpq_##tag##_erase(CVec_##tag* self, size_t i) { \
+cvecpq_##tag##_erase(cvec_##tag* self, size_t i) { \
size_t n = cvec_size(*self) - 1; \
self->data[i] = self->data[n]; \
- cvec_##tag##_popBack(self); \
+ cvec_##tag##_pop_back(self); \
_cvecpq_##tag##_siftDown(self->data - 1, i + 1, n); \
} \
\
STC_API void \
-cvecpq_##tag##_push(CVec_##tag* self, CVecValue_##tag value) { \
- cvec_##tag##_pushBack(self, value); /* sift-up the value */ \
+cvecpq_##tag##_push(cvec_##tag* self, cvec_##tag##_value_t value) { \
+ cvec_##tag##_push_back(self, value); /* sift-up the value */ \
size_t n = cvec_size(*self), c = n; \
- CVecValue_##tag *arr = self->data - 1; \
- for (; c > 1 && cvec_##tag##_sortCompare(&arr[c >> 1], &value) cmpOpr 0; c >>= 1) \
+ cvec_##tag##_value_t *arr = self->data - 1; \
+ for (; c > 1 && cvec_##tag##_sort_compare(&arr[c >> 1], &value) cmpOpr 0; c >>= 1) \
arr[c] = arr[c >> 1]; \
if (c != n) arr[c] = value; \
} \
STC_API void \
-cvecpq_##tag##_pushN(CVec_##tag *self, const CVecValue_##tag in[], size_t size) { \
+cvecpq_##tag##_push_n(cvec_##tag *self, const cvec_##tag##_value_t in[], size_t size) { \
cvec_##tag##_reserve(self, cvec_size(*self) + size); \
for (size_t i=0; i<size; ++i) cvecpq_##tag##_push(self, in[i]); \
} \
\
STC_API void \
-cvecpq_##tag##_build(CVec_##tag* self) { \
+cvecpq_##tag##_build(cvec_##tag* self) { \
size_t n = cvec_size(*self); \
- CVecValue_##tag *arr = self->data - 1; \
+ cvec_##tag##_value_t *arr = self->data - 1; \
for (size_t k = n >> 1; k != 0; --k) \
_cvecpq_##tag##_siftDown(arr, k, n); \
}
#else
-#define implement_CVec_priority_queue(tag, cmpOpr)
+#define implement_cvec_priority_queue(tag, cmpOpr)
#endif
#endif