summaryrefslogtreecommitdiffhomepage
path: root/include/stc
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2022-08-15 08:17:01 +0200
committerTyge Lovset <[email protected]>2022-08-15 08:17:01 +0200
commit2cfad29db0fda313873f2b4a809ff60aca897785 (patch)
tree2af00342f8fde6f71876937fabd6852be69b5890 /include/stc
parentdd6e6a60b8d1af9127f2694efc314e810b71c9b1 (diff)
downloadSTC-modified-2cfad29db0fda313873f2b4a809ff60aca897785.tar.gz
STC-modified-2cfad29db0fda313873f2b4a809ff60aca897785.zip
Improved docs/ex. Fix range bug when container is empty cvec/cdeq.
Diffstat (limited to 'include/stc')
-rw-r--r--include/stc/cdeq.h7
-rw-r--r--include/stc/cvec.h9
2 files changed, 9 insertions, 7 deletions
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h
index 4bef2f38..1d75408c 100644
--- a/include/stc/cdeq.h
+++ b/include/stc/cdeq.h
@@ -29,6 +29,7 @@
struct cdeq_rep { size_t size, cap; unsigned base[1]; };
#define cdeq_rep_(self) c_unchecked_container_of((self)->_base, struct cdeq_rep, base)
+#define it2_ref_(it1, it2) (it1.ref && !it2.ref ? it2.end : it2.ref)
#endif // CDEQ_H_INCLUDED
#ifndef _i_prefix
@@ -137,7 +138,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, (i2.ref ? i2.ref : i2.end));
+ return _cx_memb(_erase_range_p)(self, i1.ref, it2_ref_(i1, i2));
}
@@ -193,7 +194,7 @@ _cx_memb(_get_mut)(_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, (i2.ref ? i2.ref : i2.end) - i1.ref, sizeof *i1.ref,
+ qsort(i1.ref, it2_ref_(i1, i2) - i1.ref, sizeof *i1.ref,
(int(*)(const void*, const void*)) cmp);
}
@@ -427,7 +428,7 @@ _cx_memb(_copy_range)(_cx_self* self, _cx_value* pos,
STC_DEF _cx_iter
_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, _cx_raw raw) {
- const _cx_value* p2 = i2.ref ? i2.ref : i2.end;
+ const _cx_value* p2 = it2_ref_(i1, i2);
for (; i1.ref != p2; ++i1.ref) {
const _cx_raw r = i_keyto(i1.ref);
if (i_eq((&raw), (&r)))
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index c734abb6..170bf741 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -66,6 +66,7 @@ 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)
#endif // CVEC_H_INCLUDED
#ifndef _i_prefix
@@ -181,7 +182,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, (i2.ref ? i2.ref : i2.end));
+ return _cx_memb(_erase_range_p)(self, i1.ref, it2_ref_(i1, i2));
}
STC_INLINE const _cx_value*
@@ -242,7 +243,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, (i2.ref ? i2.ref : i2.end) - i1.ref, sizeof(_cx_value),
+ qsort(i1.ref, it2_ref_(i1, i2) - i1.ref, sizeof(_cx_value),
(int(*)(const void*, const void*)) cmp);
}
@@ -404,7 +405,7 @@ _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 = i2.ref ? i2.ref : i2.end;
+ const _cx_value* p2 = it2_ref_(i1, i2);
for (; i1.ref != p2; ++i1.ref) {
const _cx_raw r = i_keyto(i1.ref);
if (i_eq((&raw), (&r)))
@@ -416,7 +417,7 @@ _cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, _cx_raw raw) {
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 = i2.ref ? i2.ref : i2.end;
+ const _cx_value* p2 = it2_ref_(i1, i2);
_cx_iter mid = i1;
while (i1.ref != p2) {
mid.ref = i1.ref + (p2 - i1.ref)/2;