diff options
| author | Tyge Lovset <[email protected]> | 2022-05-01 06:25:31 +0200 |
|---|---|---|
| committer | Tyge Lovset <[email protected]> | 2022-05-01 06:25:31 +0200 |
| commit | 82d139295e3488656855270a420447af65088ae4 (patch) | |
| tree | 9d04a9be997637c7c88e19ae09720c743a168b53 /include/stc/cvec.h | |
| parent | f46f405d4c28de4e087bc275edee1409e6596000 (diff) | |
| download | STC-modified-82d139295e3488656855270a420447af65088ae4.tar.gz STC-modified-82d139295e3488656855270a420447af65088ae4.zip | |
- Array expansions (insert, reserve, clone) are now checked for malloc nomem, and result can be checked by user in cvec, cdeq, cmap, csmap (cvec/cdeq insert functions now returns pointer, not iter).
- Refactoring.
Diffstat (limited to 'include/stc/cvec.h')
| -rw-r--r-- | include/stc/cvec.h | 75 |
1 files changed, 38 insertions, 37 deletions
diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 7b032ff0..24cf8249 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -84,8 +84,9 @@ 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_key null);
STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_key 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,
- const _cx_value* p1, const _cx_value* p2);
+STC_API _cx_value* _cx_memb(_insert_range_p)(_cx_self* self, _cx_value* pos,
+ const _cx_value* p1, const _cx_value* p2);
+STC_API _cx_value* _cx_memb(_expand_uninitialized_at_p)(_cx_self* self, _cx_value* pos, const size_t n);
#if !c_option(c_no_cmp)
STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y);
STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_keyraw raw);
@@ -94,7 +95,7 @@ STC_API _cx_iter _cx_memb(_binary_search_in)(_cx_iter it1, _cx_iter it2, #if !defined _i_no_clone
STC_API _cx_self _cx_memb(_clone)(_cx_self cx);
-STC_API _cx_iter _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos,
+STC_API _cx_value* _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos,
const _cx_value* p1, const _cx_value* p2);
STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val)
{ return i_keyclone(val); }
@@ -105,21 +106,21 @@ STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { *self = _cx_memb(_clone)(other);
}
#if !defined _i_no_emplace
-STC_API _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos,
+STC_API _cx_value* _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos,
const _cx_raw* p1, const _cx_raw* p2);
STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, i_keyraw raw)
{ return _cx_memb(_push)(self, i_keyfrom(raw)); }
STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_keyraw raw)
{ return _cx_memb(_push)(self, i_keyfrom(raw)); }
-STC_INLINE _cx_iter
+STC_INLINE _cx_value*
_cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], const size_t n) {
return _cx_memb(_emplace_range_p)(self, self->data + idx, arr, arr + n);
}
-STC_INLINE _cx_iter
+STC_INLINE _cx_value*
_cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_keyraw raw) {
return _cx_memb(_emplace_range_p)(self, it.ref, &raw, &raw + 1);
}
-STC_INLINE _cx_iter
+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);
}
@@ -168,22 +169,19 @@ _cx_memb(_shrink_to_fit)(_cx_self *self) { }
STC_INLINE _cx_value*
-_cx_memb(_expand_uninitialized)(_cx_self *self, size_t n) {
- size_t len = _cx_memb(_size)(*self);
- if (!_cx_memb(_reserve)(self, len + n)) return NULL;
- cvec_rep_(self)->size += n;
- return self->data + len;
+_cx_memb(_expand_uninitialized)(_cx_self *self, const size_t n) {
+ return _cx_memb(_expand_uninitialized_at_p)(self, self->data + _cx_memb(_size)(*self), n);
}
-STC_INLINE _cx_iter
+STC_INLINE _cx_value*
_cx_memb(_insert)(_cx_self* self, const size_t idx, i_key value) {
return _cx_memb(_insert_range_p)(self, self->data + idx, &value, &value + 1);
}
-STC_INLINE _cx_iter
+STC_INLINE _cx_value*
_cx_memb(_insert_n)(_cx_self* self, const size_t idx, const _cx_value arr[], const size_t n) {
return _cx_memb(_insert_range_p)(self, self->data + idx, arr, arr + n);
}
-STC_INLINE _cx_iter
+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);
}
@@ -314,35 +312,36 @@ STC_DEF _cx_value* _cx_memb(_push)(_cx_self* self, i_key value) {
struct cvec_rep *r = cvec_rep_(self);
if (r->size == r->cap) {
- _cx_memb(_reserve)(self, (r->size*3 >> 1) + 4);
+ if (!_cx_memb(_reserve)(self, (r->size*3 >> 1) + 4))
+ return NULL;
r = cvec_rep_(self);
}
_cx_value *v = self->data + r->size++;
*v = value; return v;
}
-static _cx_value*
-_cx_memb(_insert_space_)(_cx_self* self, _cx_value* pos, const size_t len) {
+STC_DEF _cx_value*
+_cx_memb(_expand_uninitialized_at_p)(_cx_self* self, _cx_value* pos, const size_t n) {
const size_t idx = pos - self->data;
struct cvec_rep* r = cvec_rep_(self);
- if (!len) return pos;
- if (r->size + len > r->cap) {
- _cx_memb(_reserve)(self, (r->size*3 >> 1) + len);
+ if (!n) return pos;
+ if (r->size + n > r->cap) {
+ if (!_cx_memb(_reserve)(self, r->size*3/2 + n))
+ return NULL;
r = cvec_rep_(self);
pos = self->data + idx;
}
- memmove(pos + len, pos, (r->size - idx) * sizeof *pos);
- r->size += len;
+ memmove(pos + n, pos, (r->size - idx)*sizeof *pos);
+ r->size += n;
return pos;
}
-STC_DEF _cx_iter
+STC_DEF _cx_value*
_cx_memb(_insert_range_p)(_cx_self* self, _cx_value* pos,
const _cx_value* p1, const _cx_value* p2) {
- pos = _cx_memb(_insert_space_)(self, pos, p2 - p1);
- _cx_iter it = {pos};
- memcpy(pos, p1, (p2 - p1)*sizeof *p1);
- return it;
+ pos = _cx_memb(_expand_uninitialized_at_p)(self, pos, p2 - p1);
+ if (pos) memcpy(pos, p1, (p2 - p1)*sizeof *p1);
+ return pos;
}
STC_DEF _cx_iter
@@ -362,27 +361,29 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self cx) {
const size_t len = cvec_rep_(&cx)->size;
_cx_self out = _cx_memb(_with_capacity)(len);
- _cx_memb(_clone_range_p)(&out, out.data, cx.data, cx.data + len);
+ if (cvec_rep_(&out)->cap)
+ _cx_memb(_clone_range_p)(&out, out.data, cx.data, cx.data + len);
return out;
}
-STC_DEF _cx_iter
+STC_DEF _cx_value*
_cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos,
const _cx_value* p1, const _cx_value* p2) {
- pos = _cx_memb(_insert_space_)(self, pos, p2 - p1);
- _cx_iter it = {pos};
- for (; p1 != p2; ++p1)
+ pos = _cx_memb(_expand_uninitialized_at_p)(self, pos, p2 - p1);
+ _cx_value* it = pos;
+ if (pos) for (; p1 != p2; ++p1)
*pos++ = i_keyclone((*p1));
return it;
}
#if !defined _i_no_emplace
-STC_DEF _cx_iter
+STC_DEF _cx_value*
_cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos,
const _cx_raw* p1, const _cx_raw* p2) {
- pos = _cx_memb(_insert_space_)(self, pos, p2 - p1);
- _cx_iter it = {pos};
- for (; p1 != p2; ++p1) *pos++ = i_keyfrom((*p1));
+ pos = _cx_memb(_expand_uninitialized_at_p)(self, pos, p2 - p1);
+ _cx_value* it = pos;
+ if (pos) for (; p1 != p2; ++p1)
+ *pos++ = i_keyfrom((*p1));
return it;
}
#endif // !_i_no_emplace
|
