summaryrefslogtreecommitdiffhomepage
path: root/include/stc
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
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')
-rw-r--r--include/stc/carc.h12
-rw-r--r--include/stc/cbox.h14
-rw-r--r--include/stc/ccommon.h18
-rw-r--r--include/stc/cdeq.h1
-rw-r--r--include/stc/cstack.h3
-rw-r--r--include/stc/cvec.h4
6 files changed, 33 insertions, 19 deletions
diff --git a/include/stc/carc.h b/include/stc/carc.h
index d606ac6e..02bbaf52 100644
--- a/include/stc/carc.h
+++ b/include/stc/carc.h
@@ -160,6 +160,12 @@ STC_INLINE _cx_self _cx_memb(_clone)(_cx_self ptr) {
return ptr;
}
+// take ownership of unowned
+STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self unowned) {
+ _cx_memb(_drop)(self);
+ *self = unowned;
+}
+// share ownership with ptr
STC_INLINE void _cx_memb(_assign)(_cx_self* self, _cx_self ptr) {
if (ptr.use_count)
_i_atomic_inc(ptr.use_count);
@@ -167,12 +173,6 @@ STC_INLINE void _cx_memb(_assign)(_cx_self* self, _cx_self ptr) {
*self = ptr;
}
-STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self unowned) {
- if (self->get != unowned.get)
- _cx_memb(_drop)(self);
- *self = unowned;
-}
-
#ifndef i_no_cmp
STC_INLINE int _cx_memb(_raw_cmp)(const _cx_raw* rx, const _cx_raw* ry)
{ return i_cmp(rx, ry); }
diff --git a/include/stc/cbox.h b/include/stc/cbox.h
index 44c819dd..641fcbfc 100644
--- a/include/stc/cbox.h
+++ b/include/stc/cbox.h
@@ -145,18 +145,18 @@ STC_INLINE _cx_self _cx_memb(_from)(_cx_value val)
}
#endif // !i_no_clone
+// take ownership of unowned
STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self unowned) {
- if (unowned.get != self->get)
- _cx_memb(_drop)(self);
+ _cx_memb(_drop)(self);
*self = unowned;
}
-/* transfer ownership; set dying to NULL */
-STC_INLINE void _cx_memb(_assign)(_cx_self* self, _cx_self* dying) {
- if (dying->get == self->get)
+// transfer ownership from moved; set moved to NULL
+STC_INLINE void _cx_memb(_assign)(_cx_self* self, _cx_self* moved) {
+ if (moved->get == self->get)
return;
_cx_memb(_drop)(self);
- *self = *dying;
- dying->get = NULL;
+ *self = *moved;
+ moved->get = NULL;
}
#ifndef i_no_cmp
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
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h
index fd508bf6..8ca06ada 100644
--- a/include/stc/cdeq.h
+++ b/include/stc/cdeq.h
@@ -52,6 +52,7 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int
{ while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); }
STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n)
{ _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; }
+STC_INLINE void _cx_memb(_value_drop)(_cx_value* val) { i_keydrop(val); }
#if !defined _i_queue
#if !defined i_no_emplace
STC_API _cx_iter _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos,
diff --git a/include/stc/cstack.h b/include/stc/cstack.h
index 54bf7850..f0c930e5 100644
--- a/include/stc/cstack.h
+++ b/include/stc/cstack.h
@@ -82,7 +82,6 @@ STC_INLINE void _cx_memb(_drop)(_cx_self* self) {
i_free(self->data);
#endif
}
-
STC_INLINE intptr_t _cx_memb(_size)(const _cx_self* self)
{ return self->_len; }
@@ -96,6 +95,8 @@ STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* self) {
return i_capacity;
#endif
}
+STC_INLINE void _cx_memb(_value_drop)(_cx_value* val)
+ { i_keydrop(val); }
STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, intptr_t n) {
if (n < self->_len) return true;
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index 84c91228..88153912 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -92,6 +92,7 @@ STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value
STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw raw);
STC_API _cx_iter _cx_memb(_binary_search_in)(_cx_iter it1, _cx_iter it2, _cx_raw raw, _cx_iter* lower_bound);
#endif
+STC_INLINE void _cx_memb(_value_drop)(_cx_value* val) { i_keydrop(val); }
#if !defined i_no_emplace
STC_API _cx_iter _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos,
@@ -413,8 +414,7 @@ _cx_memb(_binary_search_in)(_cx_iter i1, _cx_iter i2, const _cx_raw raw,
i1.ref = NULL; return i1;
}
-STC_DEF int
-_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
+STC_DEF int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
const _cx_raw rx = i_keyto(x);
const _cx_raw ry = i_keyto(y);
return i_cmp((&rx), (&ry));