From 7f264be520123c71211967a56d2826d272694edf Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 15 Dec 2021 10:45:22 +0100 Subject: Some improvements in cvec and cpque. --- include/stc/cpque.h | 34 +++++++++++++++++----------------- include/stc/cvec.h | 15 +++++++-------- 2 files changed, 24 insertions(+), 25 deletions(-) (limited to 'include') diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 6b09e1d5..49aa2406 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -46,9 +46,19 @@ STC_INLINE _cx_self _cx_memb(_init)(void) { return c_make(_cx_self){0}; } STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { - if (n != self->size && n < self->capacity) return true; - _cx_value *t = (_cx_value *)c_realloc(self->data, n*sizeof *t); - return t ? (self->data = t, self->capacity = n) : 0; + if (n != self->size && n <= self->capacity) return true; + _cx_value *d = (_cx_value *)c_realloc(self->data, n*sizeof *d); + return d ? (self->data = d, self->capacity = n, true) : false; +} + +STC_INLINE bool +_cx_memb(_resize)(_cx_self* self, const size_t len, i_val null) { + if (!_cx_memb(_reserve)(self, len)) return false; + const size_t n = self->size; + for (size_t i = len; i < n; ++i) i_valdel(&self->data[i]); + for (size_t i = n; i < len; ++i) self->data[i] = null; + self->size = len; + return true; } STC_INLINE void _cx_memb(_shrink_to_fit)(_cx_self* self) @@ -90,23 +100,13 @@ STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { _cx_memb(_del)(self); *self = _cx_memb(_clone)(other); } -STC_INLINE void _cx_memb(_emplace)(_cx_self* self, _cx_rawvalue raw) - { _cx_memb(_push)(self, i_valfrom(raw)); } +STC_INLINE void _cx_memb(_emplace)(_cx_self* self, _cx_value val) + { _cx_memb(_push)(self, i_valfrom(val)); } STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val) - { return i_valfrom(i_valto(&val)); } + { return i_valfrom(val); } #endif -STC_INLINE void -_cx_memb(_push_back)(_cx_self* self, _cx_value value) { - if (self->size == self->capacity) _cx_memb(_reserve)(self, self->size*3/2 + 4); - self->data[ self->size++ ] = value; -} - -STC_INLINE void -_cx_memb(_pop_back)(_cx_self* self) - { _cx_value* p = &self->data[--self->size]; i_valdel(p); } - /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp) @@ -132,7 +132,7 @@ _cx_memb(_make_heap)(_cx_self* self) { STC_DEF _cx_self _cx_memb(_clone)(_cx_self q) { _cx_self out = _cx_memb(_with_capacity)(q.size); for (; out.size < out.capacity; ++out.size, ++q.data) - out.data[out.size] = i_valfrom(i_valto(q.data)); + out.data[out.size] = i_valfrom(*q.data); return out; } #endif diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 626ddad5..f64f7840 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -82,7 +82,7 @@ STC_API _cx_self _cx_memb(_init)(void); STC_API void _cx_memb(_del)(_cx_self* self); STC_API void _cx_memb(_clear)(_cx_self* self); STC_API bool _cx_memb(_reserve)(_cx_self* self, size_t cap); -STC_API bool _cx_memb(_resize)(_cx_self* self, size_t size, i_val fill_val); +STC_API bool _cx_memb(_resize)(_cx_self* self, size_t size, i_val null); STC_API _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value); STC_API _cx_iter _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2); STC_API _cx_iter _cx_memb(_insert_range_p)(_cx_self* self, _cx_value* pos, @@ -147,9 +147,9 @@ STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t offs) STC_INLINE size_t _cx_memb(_index)(_cx_self cx, _cx_iter it) { return it.ref - cx.data; } STC_INLINE _cx_self -_cx_memb(_with_size)(const size_t size, i_val null_val) { +_cx_memb(_with_size)(const size_t size, i_val null) { _cx_self cx = _cx_memb(_init)(); - _cx_memb(_resize)(&cx, size, null_val); + _cx_memb(_resize)(&cx, size, null); return cx; } @@ -263,7 +263,7 @@ STC_DEF bool _cx_memb(_reserve)(_cx_self* self, const size_t cap) { struct cvec_rep* rep = cvec_rep_(self); const size_t len = rep->size; - if (cap >= len) { + if (cap > rep->cap || cap && cap == len) { rep = (struct cvec_rep*) c_realloc(rep->cap ? rep : NULL, offsetof(struct cvec_rep, data) + cap*sizeof(i_val)); if (!rep) return false; @@ -275,13 +275,12 @@ _cx_memb(_reserve)(_cx_self* self, const size_t cap) { } STC_DEF bool -_cx_memb(_resize)(_cx_self* self, const size_t len, i_val fill) { - if (len > _cx_memb(_capacity)(*self)) - if (!_cx_memb(_reserve)(self, len)) return false; +_cx_memb(_resize)(_cx_self* self, const size_t len, i_val null) { + if (!_cx_memb(_reserve)(self, len)) return false; struct cvec_rep *rep = cvec_rep_(self); const size_t n = rep->size; for (size_t i = len; i < n; ++i) i_valdel(&self->data[i]); - for (size_t i = n; i < len; ++i) self->data[i] = fill; + for (size_t i = n; i < len; ++i) self->data[i] = null; if (rep->cap) rep->size = len; return true; } -- cgit v1.2.3