diff options
| author | Tyge Løvset <[email protected]> | 2021-12-15 10:45:22 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-12-15 10:45:22 +0100 |
| commit | 7f264be520123c71211967a56d2826d272694edf (patch) | |
| tree | 6afcd2ccf5ffb24fcaa2ef0be46434721b567549 /include/stc/cpque.h | |
| parent | a2671650430bf12304bead0bc9f23903f03db8a6 (diff) | |
| download | STC-modified-7f264be520123c71211967a56d2826d272694edf.tar.gz STC-modified-7f264be520123c71211967a56d2826d272694edf.zip | |
Some improvements in cvec and cpque.
Diffstat (limited to 'include/stc/cpque.h')
| -rw-r--r-- | include/stc/cpque.h | 34 |
1 files changed, 17 insertions, 17 deletions
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
|
