summaryrefslogtreecommitdiffhomepage
path: root/include/stc/cvec.h
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-08-15 16:54:56 +0200
committerTyge Løvset <[email protected]>2022-08-15 16:54:56 +0200
commitbf3c50da1a346b56b6846c0f7b9e7a222f602c2f (patch)
tree3284252adec60cce3bd4d7c5876fe2d8a3d9506e /include/stc/cvec.h
parente9e405dbe1ec3eb51ce2e3398b3c24583293892e (diff)
downloadSTC-modified-bf3c50da1a346b56b6846c0f7b9e7a222f602c2f.tar.gz
STC-modified-bf3c50da1a346b56b6846c0f7b9e7a222f602c2f.zip
More iterator fixes. Make sure cvec/cdeq find_in() return end() iterator if item not found. Small cstr API change to u8_replace*.
Diffstat (limited to 'include/stc/cvec.h')
-rw-r--r--include/stc/cvec.h23
1 files changed, 12 insertions, 11 deletions
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index 92065619..1be7211d 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -66,7 +66,8 @@ int main() {
struct cvec_rep { size_t size, cap; unsigned data[1]; };
#define cvec_rep_(self) c_unchecked_container_of((self)->data, struct cvec_rep, data)
-#define it2_ref_(it1, it2) (it1.ref && !it2.ref ? it2.end : it2.ref)
+#define _it2_ptr(it1, it2) (it1.ref && !it2.ref ? it2.end : it2.ref)
+#define _it_ptr(it) (it.ref ? it.ref : it.end)
#endif // CVEC_H_INCLUDED
#ifndef _i_prefix
@@ -107,7 +108,7 @@ _cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], cons
}
STC_INLINE _cx_iter
_cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_raw raw) {
- return _cx_memb(_emplace_range)(self, (it.ref ? it.ref : it.end), &raw, &raw + 1);
+ return _cx_memb(_emplace_range)(self, _it_ptr(it), &raw, &raw + 1);
}
#endif // !_i_no_emplace
@@ -169,7 +170,7 @@ _cx_memb(_insert_n)(_cx_self* self, const size_t idx, const _cx_value arr[], con
}
STC_INLINE _cx_iter
_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) {
- return _cx_memb(_insert_range)(self, (it.ref ? it.ref : it.end), &value, &value + 1);
+ return _cx_memb(_insert_range)(self, _it_ptr(it), &value, &value + 1);
}
STC_INLINE _cx_iter
@@ -182,7 +183,7 @@ _cx_memb(_erase_at)(_cx_self* self, _cx_iter it) {
}
STC_INLINE _cx_iter
_cx_memb(_erase_range)(_cx_self* self, _cx_iter i1, _cx_iter i2) {
- return _cx_memb(_erase_range_p)(self, i1.ref, it2_ref_(i1, i2));
+ return _cx_memb(_erase_range_p)(self, i1.ref, _it2_ptr(i1, i2));
}
STC_INLINE const _cx_value*
@@ -243,7 +244,7 @@ _cx_memb(_lower_bound)(const _cx_self* self, _cx_raw raw) {
STC_INLINE void
_cx_memb(_sort_range)(_cx_iter i1, _cx_iter i2, int(*cmp)(const _cx_value*, const _cx_value*)) {
- qsort(i1.ref, it2_ref_(i1, i2) - i1.ref, sizeof(_cx_value),
+ qsort(i1.ref, _it2_ptr(i1, i2) - i1.ref, sizeof(_cx_value),
(int(*)(const void*, const void*)) cmp);
}
@@ -396,8 +397,8 @@ _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos,
const _cx_raw* p1, const _cx_raw* p2) {
_cx_iter it = _cx_memb(_insert_uninit)(self, pos, p2 - p1);
if (it.ref)
- for (_cx_iter j = it; p1 != p2; ++p1)
- *j.ref++ = i_keyfrom((*p1));
+ for (_cx_value* p = it.ref; p1 != p2; ++p1)
+ *p++ = i_keyfrom((*p1));
return it;
}
#endif // !_i_no_emplace
@@ -405,19 +406,19 @@ _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos,
#if !c_option(c_no_cmp)
STC_DEF _cx_iter
_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, _cx_raw raw) {
- const _cx_value* p2 = it2_ref_(i1, i2);
+ const _cx_value* p2 = _it2_ptr(i1, i2);
for (; i1.ref != p2; ++i1.ref) {
const _cx_raw r = i_keyto(i1.ref);
if (i_eq((&raw), (&r)))
return i1;
}
- return i2;
+ i2.ref = NULL; return i2; // NB!
}
STC_DEF _cx_iter
_cx_memb(_binary_search_in)(_cx_iter i1, _cx_iter i2, const _cx_raw raw,
_cx_iter* lower_bound) {
- const _cx_value* p2 = it2_ref_(i1, i2);
+ const _cx_value* p2 = _it2_ptr(i1, i2);
_cx_iter mid = i1;
while (i1.ref != p2) {
mid.ref = i1.ref + (p2 - i1.ref)/2;
@@ -429,7 +430,7 @@ _cx_memb(_binary_search_in)(_cx_iter i1, _cx_iter i2, const _cx_raw raw,
else i1.ref = mid.ref + 1;
}
*lower_bound = i1.ref == i2.end ? i2 : i1;
- return i2;
+ i2.ref = NULL; return i2; // NB!
}
STC_DEF int