summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-03-22 17:32:21 +0100
committerTyge Løvset <[email protected]>2023-03-22 17:32:21 +0100
commite6a25eaaf4687ff99d0d719d0b32ad512156039e (patch)
tree6bc1730f6882eb054c6472361487a0be8b85c7aa /include
parent66429475ed6ac0deaba85989174e8880762d7888 (diff)
downloadSTC-modified-e6a25eaaf4687ff99d0d719d0b32ad512156039e.tar.gz
STC-modified-e6a25eaaf4687ff99d0d719d0b32ad512156039e.zip
Added c_foreach_r() macro for reverse iter of cvec, cdeq, cstack.
Moved c_find_if, c_erase_if, c_eraseremove_if to algo/filter.h Internals.
Diffstat (limited to 'include')
-rw-r--r--include/stc/algo/filter.h44
-rw-r--r--include/stc/ccommon.h48
-rw-r--r--include/stc/cmap.h4
-rw-r--r--include/stc/forward.h2
4 files changed, 55 insertions, 43 deletions
diff --git a/include/stc/algo/filter.h b/include/stc/algo/filter.h
index 5e3125b1..1978ea43 100644
--- a/include/stc/algo/filter.h
+++ b/include/stc/algo/filter.h
@@ -49,6 +49,8 @@ int main()
#include <stc/ccommon.h>
+// c_forfilter:
+
#define c_flt_skip(i, n) (c_flt_count(i) > (n))
#define c_flt_skipwhile(i, pred) ((i).b.s2[(i).b.s2top++] |= !(pred))
#define c_flt_take(i, n) _flt_take(&(i).b, n)
@@ -65,6 +67,48 @@ int main()
C##_next(&i.it), i.ref = i.it.ref, i.b.s1top=0, i.b.s2top=0) \
if (!(filter)) ; else
+
+// c_find_if, c_erase_if, c_eraseremove_if:
+
+#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; \
+ for (it = C##_begin(&cnt); it.ref && !(pred); C##_next(&it)) \
+ ++index; \
+} while (0)
+
+#define c_find_if_5(it, C, start, end, pred) do { \
+ intptr_t index = 0; \
+ const C##_value* _endref = (end).ref; \
+ for (it = start; it.ref != _endref && !(pred); C##_next(&it)) \
+ ++index; \
+ if (it.ref == _endref) it.ref = NULL; \
+} while (0)
+
+
+// Use with: clist, cmap, cset, csmap, csset, cstr:
+#define c_erase_if(it, C, cnt, pred) do { \
+ C* _cnt = &cnt; \
+ for (C##_iter it = C##_begin(_cnt); it.ref;) { \
+ if (pred) it = C##_erase_at(_cnt, it); \
+ else C##_next(&it); \
+ } \
+} while (0)
+
+
+// Use with: cstack, cvec, cdeq, cqueue:
+#define c_eraseremove_if(it, C, cnt, pred) do { \
+ C* _cnt = &cnt; \
+ intptr_t _n = 0; \
+ C##_iter it = C##_begin(_cnt), _i; \
+ while (it.ref && !(pred)) \
+ C##_next(&it); \
+ for (_i = it; it.ref; C##_next(&it)) \
+ if (pred) C##_value_drop(it.ref), ++_n; \
+ else *_i.ref = *it.ref, C##_next(&_i); \
+ _cnt->_len -= _n; \
+} while (0)
+
// ------------------------ private -------------------------
#ifndef c_NFILTERS
#define c_NFILTERS 32
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index ad6063e4..b822b9f8 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -178,6 +178,10 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle,
for (C##_iter it = start, *_endref = (C##_iter*)(finish).ref \
; it.ref != (C##_value*)_endref; C##_next(&it))
+#define c_foreach_r(it, C, cnt) \
+ for (C##_iter it = {.ref=C##_end(&cnt).end - 1, .end=(cnt).data - 1} \
+ ; it.ref != it.end; --it.ref)
+
#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) \
@@ -193,8 +197,8 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle,
; (_inc > 0) ^ (i > _end); i += _inc)
#ifndef __cplusplus
#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))} \
+ for (struct {T* ref; int size, index;} \
+ it = {.ref=(T[])__VA_ARGS__, .size=(int)(sizeof((T[])__VA_ARGS__)/sizeof(T))} \
; it.index < it.size; ++it.ref, ++it.index)
#else
#include <initializer_list>
@@ -224,44 +228,8 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle,
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_find_if(...) c_MACRO_OVERLOAD(c_find_if, __VA_ARGS__)
-#define c_find_if_4(it, C, cnt, pred) do { \
- intptr_t index = 0; \
- for (it = C##_begin(&cnt); it.ref && !(pred); C##_next(&it)) \
- ++index; \
-} while (0)
-#define c_find_if_5(it, C, start, end, pred) do { \
- intptr_t index = 0; \
- const C##_value* _endref = (end).ref; \
- for (it = start; it.ref != _endref && !(pred); C##_next(&it)) \
- ++index; \
- if (it.ref == _endref) it.ref = NULL; \
-} while (0)
-
-// use with: clist, cmap, cset, csmap, csset, cstr:
-#define c_erase_if(it, C, cnt, pred) do { \
- C* _cnt = &cnt; \
- for (C##_iter it = C##_begin(_cnt); it.ref;) { \
- if (pred) it = C##_erase_at(_cnt, it); \
- else C##_next(&it); \
- } \
-} while (0)
-
-// use with: cstack, cvec, cdeq, cqueue:
-#define c_eraseremove_if(it, C, cnt, pred) do { \
- C* _cnt = &cnt; \
- intptr_t _n = 0; \
- C##_iter it = C##_begin(_cnt), _i; \
- while (it.ref && !(pred)) \
- C##_next(&it); \
- for (_i = it; it.ref; C##_next(&it)) \
- if (pred) C##_value_drop(it.ref), ++_n; \
- else *_i.ref = *it.ref, C##_next(&_i); \
- _cnt->_len -= _n; \
-} while (0)
+#define c_drop(C, ...) \
+ do { c_forlist (_i, C*, {__VA_ARGS__}) C##_drop(*_i.ref); } while(0)
#endif // CCOMMON_H_INCLUDED
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index bc3b5546..d9081ae0 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -224,7 +224,7 @@ STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) {
if (it._hx)
while (*it._hx == 0)
++it.ref, ++it._hx;
- if (it.ref == it.end) it.ref = NULL;
+ if (it.ref == it._end) it.ref = NULL;
return it;
}
@@ -235,7 +235,7 @@ _cx_memb(_end)(const _cx_self* self)
STC_INLINE void
_cx_memb(_next)(_cx_iter* it) {
while ((++it->ref, *++it->_hx == 0)) ;
- if (it->ref == it->end) it->ref = NULL;
+ if (it->ref == it->_end) it->ref = NULL;
}
STC_INLINE _cx_iter
diff --git a/include/stc/forward.h b/include/stc/forward.h
index 00c531fe..31e67e7d 100644
--- a/include/stc/forward.h
+++ b/include/stc/forward.h
@@ -118,7 +118,7 @@ typedef union {
} SELF##_result; \
\
typedef struct { \
- SELF##_value *ref, *end; \
+ SELF##_value *ref, *_end; \
uint8_t* _hx; \
} SELF##_iter; \
\