summaryrefslogtreecommitdiffhomepage
path: root/include/stc/ccommon.h
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-20 14:44:25 +0100
committerTyge Løvset <[email protected]>2023-02-20 14:44:25 +0100
commita8fc8ac4e8c1481300e0d46bbd376f32ebeb4635 (patch)
tree68dad18a7cf235916b5a1a2257bb80e69bb5d23e /include/stc/ccommon.h
parent0e3d07dbd991c1f1a691b24655c37ddab660a9d9 (diff)
downloadSTC-modified-a8fc8ac4e8c1481300e0d46bbd376f32ebeb4635.tar.gz
STC-modified-a8fc8ac4e8c1481300e0d46bbd376f32ebeb4635.zip
Added c_eraseremove_if() for cvec, cdeq, cstack, cqueue in ccommon.h. Some cleanup.
Diffstat (limited to 'include/stc/ccommon.h')
-rw-r--r--include/stc/ccommon.h18
1 files changed, 15 insertions, 3 deletions
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index dae991ed..711a9c6d 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -244,14 +244,26 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle,
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##_iter it = C##_begin(&cnt); \
- for (intptr_t index = 0; it.ref; ++index) { \
- if (pred) it = C##_erase_at(&cnt, it); \
+ 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 _first = C##_begin(_cnt), it = _first; \
+ for (; it.ref; C##_next(&it)) \
+ if (pred) ++_n; \
+ else C##_value_drop(_first.ref), *_first.ref = *it.ref, C##_next(&_first); \
+ _cnt->_len -= _n; \
+} while (0)
+
#endif // CCOMMON_H_INCLUDED
#undef STC_API