summaryrefslogtreecommitdiffhomepage
path: root/include/stc
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-08 16:16:49 +0100
committerTyge Løvset <[email protected]>2023-02-08 17:18:24 +0100
commitc4441f5fc665194fbd7a894a67a64a08c3beac42 (patch)
tree82f231b6e8fcb75625166f98aa785baaa265a3d6 /include/stc
parent673dd5319a488d4b702b94dd9aeda4e497ae4fbc (diff)
downloadSTC-modified-c4441f5fc665194fbd7a894a67a64a08c3beac42.tar.gz
STC-modified-c4441f5fc665194fbd7a894a67a64a08c3beac42.zip
Changed to use lowercase flow-control macros in examples (uppercase will still be supported). Improved many examples to use c_make() to init containers.
Diffstat (limited to 'include/stc')
-rw-r--r--include/stc/algo/crange.h4
-rw-r--r--include/stc/algo/filter.h23
-rw-r--r--include/stc/cbits.h6
-rw-r--r--include/stc/cbox.h2
-rw-r--r--include/stc/ccommon.h54
-rw-r--r--include/stc/clist.h10
-rw-r--r--include/stc/cmap.h4
-rw-r--r--include/stc/cqueue.h2
-rw-r--r--include/stc/cregex.h2
-rw-r--r--include/stc/cset.h2
-rw-r--r--include/stc/csmap.h4
-rw-r--r--include/stc/cspan.h4
-rw-r--r--include/stc/csset.h2
-rw-r--r--include/stc/csview.h6
-rw-r--r--include/stc/priv/altnames.h30
15 files changed, 77 insertions, 78 deletions
diff --git a/include/stc/algo/crange.h b/include/stc/algo/crange.h
index 3993e615..1b840516 100644
--- a/include/stc/algo/crange.h
+++ b/include/stc/algo/crange.h
@@ -28,13 +28,13 @@
int main()
{
crange r1 = crange_make(80, 90);
- c_FOREACH (i, crange, r1)
+ c_foreach (i, crange, r1)
printf(" %lld", *i.ref);
puts("");
// use a temporary crange object.
int a = 100, b = INT32_MAX;
- c_FORFILTER (i, crange, crange_obj(a, b, 8)
+ c_forfilter (i, crange, crange_obj(a, b, 8)
, i.index > 10
, c_flt_take(i, 3))
printf(" %lld", *i.ref);
diff --git a/include/stc/algo/filter.h b/include/stc/algo/filter.h
index 9037c984..b2b59fa6 100644
--- a/include/stc/algo/filter.h
+++ b/include/stc/algo/filter.h
@@ -28,15 +28,14 @@
int main()
{
- c_WITH (cstack_int stk = {0}, cstack_int_drop(&stk)) {
- c_FORLIST (i, int, {1, 2, 3, 4, 5, 6, 7, 8, 9})
- cstack_int_push(&stk, i);
-
- c_FOREACH (i, cstack_int, stk)
+ c_with (cstack_int stk = c_make(cstack_int, {1, 2, 3, 4, 5, 6, 7, 8, 9}),
+ cstack_int_drop(&stk))
+ {
+ c_foreach (i, cstack_int, stk)
printf(" %d", *i.ref);
puts("");
- c_FORFILTER (i, cstack_int, stk
+ c_forfilter (i, cstack_int, stk
, c_flt_skipwhile(i, *i.ref < 3)
&& (*i.ref & 1) == 0 // even only
, c_flt_take(i, 2)) // break after 2
@@ -59,15 +58,15 @@ int main()
#define c_flt_skipwhile(i, pred) ((i).s2[(i).s2top++] |= !(pred))
#define c_flt_takewhile(i, pred) !c_flt_skipwhile(i, pred)
-#define c_FORFILTER(...) c_MACRO_OVERLOAD(c_FORFILTER, __VA_ARGS__)
+#define c_forfilter(...) c_MACRO_OVERLOAD(c_forfilter, __VA_ARGS__)
-#define c_FORFILTER_4(i, C, cnt, filter) \
- c_FORFILTER_B(i, C, C##_begin(&cnt), filter)
+#define c_forfilter_4(i, C, cnt, filter) \
+ c_forfilter_B(i, C, C##_begin(&cnt), filter)
-#define c_FORFILTER_5(i, C, cnt, filter, cond) \
- c_FORFILTER_B(i, C, C##_begin(&cnt), filter) if (!(cond)) break; else
+#define c_forfilter_5(i, C, cnt, filter, cond) \
+ c_forfilter_B(i, C, C##_begin(&cnt), filter) if (!(cond)) break; else
-#define c_FORFILTER_B(i, C, start, filter) \
+#define c_forfilter_B(i, C, start, filter) \
for (struct {C##_iter it; C##_value *ref; \
uint32_t s1[c_NFILTERS], index, count; \
bool s2[c_NFILTERS]; uint8_t s1top, s2top;} \
diff --git a/include/stc/cbits.h b/include/stc/cbits.h
index d72be230..0c169b88 100644
--- a/include/stc/cbits.h
+++ b/include/stc/cbits.h
@@ -27,13 +27,13 @@ Similar to boost::dynamic_bitset / std::bitset
#include "cbits.h"
int main() {
- c_WITH (cbits bset = cbits_with_size(23, true), cbits_drop(&bset))
+ c_with (cbits bset = cbits_with_size(23, true), cbits_drop(&bset))
{
cbits_reset(&bset, 9);
cbits_resize(&bset, 43, false);
printf("%4zu: ", cbits_size(&bset));
- c_FORRANGE (i, cbits_size(&bset))
+ c_forrange (i, cbits_size(&bset))
printf("%d", cbits_at(&bset, i));
puts("");
cbits_set(&bset, 28);
@@ -43,7 +43,7 @@ int main() {
cbits_set_value(&bset, 99, false);
printf("%4zu: ", cbits_size(&bset));
- c_FORRANGE (i, cbits_size(&bset))
+ c_forrange (i, cbits_size(&bset))
printf("%d", cbits_at(&bset, i));
puts("");
}
diff --git a/include/stc/cbox.h b/include/stc/cbox.h
index c914152c..08449031 100644
--- a/include/stc/cbox.h
+++ b/include/stc/cbox.h
@@ -45,7 +45,7 @@ void Person_drop(Person* p) {
#include <stc/cbox.h>
int main() {
- c_AUTO (PBox, p, q)
+ c_auto (PBox, p, q)
{
p = PBox_from(Person_from("John Smiths", "[email protected]"));
q = PBox_clone(p);
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index d7d2d804..0b3083ef 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -173,65 +173,65 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle,
/* Control block macros */
-#define c_FOREACH(...) c_MACRO_OVERLOAD(c_FOREACH, __VA_ARGS__)
-#define c_FOREACH_3(it, C, cnt) \
+#define c_foreach(...) c_MACRO_OVERLOAD(c_foreach, __VA_ARGS__)
+#define c_foreach_3(it, C, cnt) \
for (C##_iter it = C##_begin(&cnt); it.ref; C##_next(&it))
-#define c_FOREACH_4(it, C, start, finish) \
+#define c_foreach_4(it, C, start, finish) \
for (C##_iter it = start, *_endref = (C##_iter*)(finish).ref \
; it.ref != (C##_value*)_endref; C##_next(&it))
-#define c_FORWHILE(i, C, start, cond) \
+#define c_forwhile(i, C, start, cond) \
for (struct {C##_iter it; C##_value *ref; intptr_t index;} \
i = {.it=start, .ref=i.it.ref}; i.it.ref && (cond) \
; C##_next(&i.it), i.ref = i.it.ref, ++i.index)
-#define c_FORPAIR(key, val, C, cnt) /* structured binding */ \
+#define c_forpair(key, val, C, cnt) /* structured binding */ \
for (struct {C##_iter it; const C##_key* key; C##_mapped* val;} _ = {.it=C##_begin(&cnt)} \
; _.it.ref && (_.key = &_.it.ref->first, _.val = &_.it.ref->second) \
; C##_next(&_.it))
-#define c_FORRANGE(...) c_MACRO_OVERLOAD(c_FORRANGE, __VA_ARGS__)
-#define c_FORRANGE_1(stop) c_FORRANGE_3(_c_i, 0, stop)
-#define c_FORRANGE_2(i, stop) c_FORRANGE_3(i, 0, stop)
-#define c_FORRANGE_3(i, start, stop) \
+#define c_forrange(...) c_MACRO_OVERLOAD(c_forrange, __VA_ARGS__)
+#define c_forrange_1(stop) c_forrange_3(_c_i, 0, stop)
+#define c_forrange_2(i, stop) c_forrange_3(i, 0, stop)
+#define c_forrange_3(i, start, stop) \
for (long long i=start, _end=(long long)(stop); i < _end; ++i)
-#define c_FORRANGE_4(i, start, stop, step) \
+#define c_forrange_4(i, start, stop, step) \
for (long long i=start, _inc=step, _end=(long long)(stop) - (_inc > 0) \
; (_inc > 0) ^ (i > _end); i += _inc)
#ifndef __cplusplus
- #define c_FORLIST(it, T, ...) \
+ #define c_forlist(it, T, ...) \
for (struct {T* data; T* ref; int size, index;} \
it = {.data=(T[])__VA_ARGS__, .ref=it.data, .size=(int)(sizeof((T[])__VA_ARGS__)/sizeof(T))} \
; it.index < it.size; ++it.ref, ++it.index)
#else
#include <initializer_list>
- #define c_FORLIST(it, T, ...) \
+ #define c_forlist(it, T, ...) \
for (struct {std::initializer_list<T> _il; std::initializer_list<T>::iterator data, ref; size_t size, index;} \
it = {._il=__VA_ARGS__, .data=it._il.begin(), .ref=it.data, .size=it._il.size()} \
; it.index < it.size; ++it.ref, ++it.index)
#endif
-#define c_WITH(...) c_MACRO_OVERLOAD(c_WITH, __VA_ARGS__)
-#define c_WITH_2(declvar, drop) for (declvar, **_c_i = NULL; !_c_i; ++_c_i, drop)
-#define c_WITH_3(declvar, pred, drop) for (declvar, **_c_i = NULL; !_c_i && (pred); ++_c_i, drop)
-#define c_SCOPE(init, drop) for (int _c_i = (init, 1); _c_i; --_c_i, drop)
-#define c_DEFER(...) for (int _c_i = 1; _c_i; --_c_i, __VA_ARGS__)
+#define c_with(...) c_MACRO_OVERLOAD(c_with, __VA_ARGS__)
+#define c_with_2(declvar, drop) for (declvar, **_c_i = NULL; !_c_i; ++_c_i, drop)
+#define c_with_3(declvar, pred, drop) for (declvar, **_c_i = NULL; !_c_i && (pred); ++_c_i, drop)
+#define c_scope(init, drop) for (int _c_i = (init, 1); _c_i; --_c_i, drop)
+#define c_defer(...) for (int _c_i = 1; _c_i; --_c_i, __VA_ARGS__)
-#define c_AUTO(...) c_MACRO_OVERLOAD(c_AUTO, __VA_ARGS__)
-#define c_AUTO_2(C, a) \
- c_WITH_2(C a = C##_init(), C##_drop(&a))
-#define c_AUTO_3(C, a, b) \
- c_WITH_2(c_EXPAND(C a = C##_init(), b = C##_init()), \
+#define c_auto(...) c_MACRO_OVERLOAD(c_auto, __VA_ARGS__)
+#define c_auto_2(C, a) \
+ c_with_2(C a = C##_init(), C##_drop(&a))
+#define c_auto_3(C, a, b) \
+ c_with_2(c_EXPAND(C a = C##_init(), b = C##_init()), \
(C##_drop(&b), C##_drop(&a)))
-#define c_AUTO_4(C, a, b, c) \
- c_WITH_2(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init()), \
+#define c_auto_4(C, a, b, c) \
+ c_with_2(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init()), \
(C##_drop(&c), C##_drop(&b), C##_drop(&a)))
-#define c_AUTO_5(C, a, b, c, d) \
- c_WITH_2(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init(), d = C##_init()), \
+#define c_auto_5(C, a, b, c, d) \
+ c_with_2(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init(), d = C##_init()), \
(C##_drop(&d), C##_drop(&c), C##_drop(&b), C##_drop(&a)))
/* Generic functions */
-#define c_drop(C, ...) do { c_FORLIST (_i, C*, {__VA_ARGS__}) C##_drop(*_i.ref); } while(0)
+#define c_drop(C, ...) do { c_forlist (_i, C*, {__VA_ARGS__}) C##_drop(*_i.ref); } while(0)
#define c_find_if(...) c_MACRO_OVERLOAD(c_find_if, __VA_ARGS__)
#define c_find_if_4(it, C, cnt, pred) do { \
intptr_t index = 0; \
diff --git a/include/stc/clist.h b/include/stc/clist.h
index de874927..f257bc19 100644
--- a/include/stc/clist.h
+++ b/include/stc/clist.h
@@ -34,19 +34,19 @@
int main()
{
- c_AUTO (clist_ix, list)
+ c_auto (clist_ix, list)
{
int n;
for (int i = 0; i < 1000000; ++i) // one million
clist_ix_push_back(&list, crandom() >> 32);
n = 0;
- c_FOREACH (i, clist_ix, list)
+ c_foreach (i, clist_ix, list)
if (++n % 10000 == 0) printf("%8d: %10zu\n", n, *i.ref);
// Sort them...
clist_ix_sort(&list); // mergesort O(n*log n)
n = 0;
puts("sorted");
- c_FOREACH (i, clist_ix, list)
+ c_foreach (i, clist_ix, list)
if (++n % 10000 == 0) printf("%8d: %10zu\n", n, *i.ref);
}
}
@@ -273,7 +273,7 @@ _clist_mergesort(clist_VOID_node *list, int (*cmp)(const clist_VOID_node*, const
STC_DEF _cx_self
_cx_memb(_clone)(_cx_self cx) {
_cx_self out = _cx_memb(_init)();
- c_FOREACH (it, _cx_self, cx)
+ c_foreach (it, _cx_self, cx)
_cx_memb(_push_back)(&out, i_keyclone((*it.ref)));
return out;
}
@@ -406,7 +406,7 @@ _cx_memb(_split_off)(_cx_self* self, _cx_iter it1, _cx_iter it2) {
STC_DEF _cx_iter
_cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw val) {
- c_FOREACH (it, _cx_self, it1, it2) {
+ c_foreach (it, _cx_self, it1, it2) {
_cx_raw r = i_keyto(it.ref);
if (i_eq((&r), (&val)))
return it;
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index f3c82469..0553823b 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -31,7 +31,7 @@
#include <stc/cmap.h>
int main(void) {
- c_WITH (cmap_ichar m = cmap_ichar_init(), cmap_ichar_drop(&m))
+ c_with (cmap_ichar m = cmap_ichar_init(), cmap_ichar_drop(&m))
{
cmap_ichar_emplace(&m, 5, 'a');
cmap_ichar_emplace(&m, 8, 'b');
@@ -42,7 +42,7 @@ int main(void) {
cmap_ichar_emplace_or_assign(&m, 5, 'd'); // update
cmap_ichar_erase(&m, 8);
- c_FOREACH (i, cmap_ichar, m)
+ c_foreach (i, cmap_ichar, m)
printf("map %d: %c\n", i.ref->first, i.ref->second);
}
}
diff --git a/include/stc/cqueue.h b/include/stc/cqueue.h
index 1f90e00e..67909f8e 100644
--- a/include/stc/cqueue.h
+++ b/include/stc/cqueue.h
@@ -33,7 +33,7 @@ int main() {
stc64_t rng = stc64_new(1234);
stc64_uniform_t dist = stc64_uniform_new(0, n);
- c_AUTO (cqueue_int, Q)
+ c_auto (cqueue_int, Q)
{
// Push ten million random numbers onto the queue.
for (int i=0; i<n; ++i)
diff --git a/include/stc/cregex.h b/include/stc/cregex.h
index fa136f16..a48b4c49 100644
--- a/include/stc/cregex.h
+++ b/include/stc/cregex.h
@@ -80,7 +80,7 @@ typedef struct {
csview match[CREG_MAX_CAPTURES];
} cregex_iter;
-#define c_FORMATCH(it, Re, Input) \
+#define c_formatch(it, Re, Input) \
for (cregex_iter it = {Re, Input}; \
cregex_find_4(it.re, it.input, it.match, CREG_M_NEXT) == CREG_OK; )
diff --git a/include/stc/cset.h b/include/stc/cset.h
index c7ade49a..58cbeb3e 100644
--- a/include/stc/cset.h
+++ b/include/stc/cset.h
@@ -33,7 +33,7 @@ int main(void) {
cset_sx_insert(&s, 5);
cset_sx_insert(&s, 8);
- c_FOREACH (i, cset_sx, s)
+ c_foreach (i, cset_sx, s)
printf("set %d\n", *i.ref);
cset_sx_drop(&s);
}
diff --git a/include/stc/csmap.h b/include/stc/csmap.h
index 6a1899b9..6a359fbb 100644
--- a/include/stc/csmap.h
+++ b/include/stc/csmap.h
@@ -32,7 +32,7 @@
#include <stc/csmap.h>
int main(void) {
- c_WITH (csmap_sx m = csmap_sx_init(), csmap_sx_drop(&m))
+ c_with (csmap_sx m = csmap_sx_init(), csmap_sx_drop(&m))
{
csmap_sx_emplace(&m, "Testing one", 1.234);
csmap_sx_emplace(&m, "Testing two", 12.34);
@@ -43,7 +43,7 @@ int main(void) {
csmap_sx_emplace_or_assign(&m, "Testing three", 1000.0); // update
csmap_sx_erase(&m, "Testing two");
- c_FOREACH (i, csmap_sx, m)
+ c_foreach (i, csmap_sx, m)
printf("map %s: %g\n", cstr_str(&i.ref->first), i.ref->second);
}
}
diff --git a/include/stc/cspan.h b/include/stc/cspan.h
index 62de174a..bbaaf6c0 100644
--- a/include/stc/cspan.h
+++ b/include/stc/cspan.h
@@ -43,11 +43,11 @@ int demo2() {
int array[] = {10, 20, 30, 23, 22, 21};
Intspan span = cspan_from_array(array);
- c_FOREACH (i, Intspan, span)
+ c_foreach (i, Intspan, span)
printf(" %d", *i.ref);
puts("");
- c_FORFILTER (i, Intspan, span,
+ c_forfilter (i, Intspan, span,
, c_flt_skipwhile(i, *i.ref < 25)
&& (*i.ref & 1) == 0 // even only
, c_flt_take(i, 2)) // break after 2
diff --git a/include/stc/csset.h b/include/stc/csset.h
index 392d258c..c14d2a6a 100644
--- a/include/stc/csset.h
+++ b/include/stc/csset.h
@@ -36,7 +36,7 @@ int main(void) {
csset_i_insert(&s, 3);
csset_i_insert(&s, 5);
- c_FOREACH (k, csset_i, s)
+ c_foreach (k, csset_i, s)
printf("set %d\n", *k.ref);
csset_i_drop(&s);
}
diff --git a/include/stc/csview.h b/include/stc/csview.h
index a30672cd..748f7d30 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -115,13 +115,13 @@ STC_API csview csview_substr_ex(csview sv, intptr_t pos, intptr_t n);
STC_API csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2);
STC_API csview csview_token(csview sv, const char* sep, intptr_t* start);
-#define c_FORTOKEN_SV(it, inputsv, sep) \
+#define c_fortoken_sv(it, inputsv, sep) \
for (struct { csview _inp, token, *ref; const char *_sep; intptr_t pos; } \
it = {._inp=inputsv, .token=it._inp, .ref=&it.token, ._sep=sep} \
; it.pos <= it._inp.size && (it.token = csview_token(it._inp, it._sep, &it.pos)).str ; )
-#define c_FORTOKEN(it, input, sep) \
- c_FORTOKEN_SV(it, csview_from(input), sep)
+#define c_fortoken(it, input, sep) \
+ c_fortoken_sv(it, csview_from(input), sep)
/* csview interaction with cstr: */
#ifdef CSTR_H_INCLUDED
diff --git a/include/stc/priv/altnames.h b/include/stc/priv/altnames.h
index 0b01d251..bfda05ca 100644
--- a/include/stc/priv/altnames.h
+++ b/include/stc/priv/altnames.h
@@ -20,20 +20,20 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-#define c_forlist c_FORLIST
-#define c_forrange c_FORRANGE
-#define c_foreach c_FOREACH
-#define c_forwhile c_FORWHILE
-#define c_forpair c_FORPAIR
-#define c_forfilter c_FORFILTER
-#define c_formatch c_FORMATCH
-#define c_fortoken c_FORTOKEN
-#define c_fortoken_sv c_FORTOKEN_SV
-#define c_auto c_AUTO
-#define c_with c_WITH
-#define c_scope c_SCOPE
-#define c_defer c_DEFER
-#define c_sv c_SV
-#define c_ARGSV c_SVARG
+#define c_FORLIST c_forlist
+#define c_FORRANGE c_forrange
+#define c_FOREACH c_foreach
+#define c_FORWHILE c_forwhile
+#define c_FORPAIR c_forpair
+#define c_FORFILTER c_forfilter
+#define c_FORMATCH c_formatch
+#define c_FORTOKEN c_fortoken
+#define c_FORTOKEN_SV c_fortoken_sv
+#define c_AUTO c_auto
+#define c_WITH c_with
+#define c_SCOPE c_scope
+#define c_DEFER c_defer
#define c_NEW c_new
#define c_ARRAYLEN c_arraylen
+#define c_ARGSV c_SVARG
+#define c_sv c_SV