diff options
| author | Tyge Løvset <[email protected]> | 2022-08-10 12:59:20 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-08-10 12:59:20 +0200 |
| commit | 1756309078f4c09765bde898e50b8a3078cebc7d (patch) | |
| tree | 592e82f3ee6f064e2f889d3fde065761f5308bf1 /include/stc/cvec.h | |
| parent | 90624d6d398ff1d0f79df3dd656c4ad0c9c498a9 (diff) | |
| download | STC-modified-1756309078f4c09765bde898e50b8a3078cebc7d.tar.gz STC-modified-1756309078f4c09765bde898e50b8a3078cebc7d.zip | |
Fixed cdeq, cvec, cmap and cstack: iterators .ref is now NULL when it is end(). clist and csmap/cset already has this property.
Diffstat (limited to 'include/stc/cvec.h')
| -rw-r--r-- | include/stc/cvec.h | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/include/stc/cvec.h b/include/stc/cvec.h index a4308628..99df920c 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -106,7 +106,7 @@ _cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], cons } STC_INLINE _cx_value* _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_raw raw) { - return _cx_memb(_emplace_range_p)(self, it.ref, &raw, &raw + 1); + return _cx_memb(_emplace_range_p)(self, (it.ref ? it.ref : it._end), &raw, &raw + 1); } #endif // !_i_no_emplace @@ -121,10 +121,6 @@ STC_INLINE void _cx_memb(_copy)(_cx_self* self, const _cx_self* other) { _cx_memb(_drop)(self); *self = _cx_memb(_clone)(*other); } -STC_INLINE _cx_value* -_cx_memb(_emplace_range)(_cx_self* self, _cx_iter it, _cx_iter it1, _cx_iter it2) { - return _cx_memb(_clone_range_p)(self, it.ref, it1.ref, it2.ref); -} #endif // !_i_no_clone STC_INLINE size_t _cx_memb(_size)(const _cx_self* cx) { return cvec_rep_(cx)->size; } @@ -142,7 +138,8 @@ STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value) STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_memb(_pop)(self); } STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { size_t n = cvec_rep_(self)->size; return c_make(_cx_iter){n ? self->data : NULL, self->data + n}; } -STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) { return c_make(_cx_iter){NULL}; } +STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) + { return c_make(_cx_iter){NULL, self->data + cvec_rep_(self)->size}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->_end) it->ref = NULL; } STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t offs) { if ((it.ref += offs) >= it._end) it.ref = NULL; return it; } @@ -182,7 +179,7 @@ _cx_memb(_insert_n)(_cx_self* self, const size_t idx, const _cx_value arr[], con } STC_INLINE _cx_value* _cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) { - return _cx_memb(_insert_range_p)(self, it.ref, &value, &value + 1); + return _cx_memb(_insert_range_p)(self, (it.ref ? it.ref : it._end), &value, &value + 1); } STC_INLINE _cx_iter @@ -194,8 +191,8 @@ _cx_memb(_erase_at)(_cx_self* self, _cx_iter it) { return _cx_memb(_erase_range_p)(self, it.ref, it.ref + 1); } STC_INLINE _cx_iter -_cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2) { - return _cx_memb(_erase_range_p)(self, it1.ref, it2.ref); +_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)); } STC_INLINE const _cx_value* @@ -237,10 +234,11 @@ _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 - i1.ref, sizeof(_cx_value), (int(*)(const void*, const void*)) _cmp_); +_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), + (int(*)(const void*, const void*)) cmp); } + STC_INLINE void _cx_memb(_sort)(_cx_self* self) { _cx_memb(_sort_range)(_cx_memb(_begin)(self), _cx_memb(_end)(self), _cx_memb(_value_cmp)); @@ -356,7 +354,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { { i_keydrop(p); } memmove(p1, p2, (end - p2) * sizeof *p1); cvec_rep_(self)->size -= len; - return c_make(_cx_iter){.ref = p2 == end ? NULL : p1, end - len}; + return c_make(_cx_iter){p2 == end ? NULL : p1, end - len}; } #if !defined _i_no_clone @@ -395,7 +393,8 @@ _cx_memb(_emplace_range_p)(_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) { - for (; i1.ref != i2.ref; ++i1.ref) { + const _cx_value* p2 = i2.ref ? i2.ref : i2._end; + for (; i1.ref != p2; ++i1.ref) { const _cx_raw r = i_keyto(i1.ref); if (i_eq((&raw), (&r))) return i1; @@ -405,18 +404,19 @@ _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) { - _cx_iter mid, last = i2; - while (i1.ref != i2.ref) { - mid.ref = i1.ref + (i2.ref - i1.ref)/2; + const _cx_value* p2 = i2.ref ? i2.ref : i2._end; + _cx_iter mid = i1; + while (i1.ref != p2) { + mid.ref = i1.ref + (p2 - i1.ref)/2; const _cx_raw m = i_keyto(mid.ref); const int c = i_cmp((&raw), (&m)); if (!c) return *lower_bound = mid; - else if (c < 0) i2.ref = mid.ref; + else if (c < 0) p2 = mid.ref; else i1.ref = mid.ref + 1; } - *lower_bound = i1; - return last; + *lower_bound = i1.ref == i2._end ? i2 : i1; + return i2; } STC_DEF int |
