From 16f77d53e3d5c7d04aa5bb5c88b410b7b1f7e011 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 16 Sep 2021 10:46:09 +0200 Subject: Added return value pointer from push_front/emplace_front and push_back/emplace_back.c --- include/stc/cdeq.h | 22 ++++++++++++---------- include/stc/clist.h | 18 ++++++++++-------- include/stc/cvec.h | 11 ++++++----- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 79ea7959..8fe81845 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -45,13 +45,13 @@ STC_API Self cx_memb(_init)(void); STC_API Self cx_memb(_clone)(Self cx); STC_API void cx_memb(_clear)(Self* self); STC_API void cx_memb(_del)(Self* self); -STC_API void cx_memb(_push_back)(Self* self, i_val value); +STC_API cx_value_t* cx_memb(_push_back)(Self* self, i_val value); STC_API void cx_memb(_expand_right_half_)(Self* self, size_t idx, size_t n); #ifndef i_queue STC_API cx_iter_t cx_memb(_find_in)(cx_iter_t p1, cx_iter_t p2, i_valraw raw); STC_API int cx_memb(_value_compare)(const cx_value_t* x, const cx_value_t* y); -STC_API void cx_memb(_push_front)(Self* self, i_val value); +STC_API cx_value_t* cx_memb(_push_front)(Self* self, i_val value); STC_API cx_iter_t cx_memb(_erase_range_p)(Self* self, cx_value_t* p1, cx_value_t* p2); STC_API cx_iter_t cx_memb(_insert_range_p)(Self* self, cx_value_t* pos, const cx_value_t* p1, const cx_value_t* p2, bool clone); @@ -67,8 +67,8 @@ STC_INLINE i_val cx_memb(_value_fromraw)(i_valraw raw) { return i_valfrom STC_INLINE i_valraw cx_memb(_value_toraw)(cx_value_t* pval) { return i_valto(pval); } STC_INLINE i_val cx_memb(_value_clone)(i_val val) { return i_valfrom(i_valto(&val)); } -STC_INLINE void cx_memb(_emplace_back)(Self* self, i_valraw raw) - { cx_memb(_push_back)(self, i_valfrom(raw)); } +STC_INLINE cx_value_t* cx_memb(_emplace_back)(Self* self, i_valraw raw) + { return cx_memb(_push_back)(self, i_valfrom(raw)); } STC_INLINE void cx_memb(_pop_front)(Self* self) { i_valdel(self->data++); --cdeq_rep_(self)->size; } STC_INLINE cx_value_t* cx_memb(_back)(const Self* self) @@ -103,8 +103,8 @@ cx_memb(_emplace_items)(Self *self, const cx_rawvalue_t arr[], size_t n) { #ifndef i_queue -STC_INLINE void cx_memb(_emplace_front)(Self* self, i_valraw raw) { - cx_memb(_push_front)(self, i_valfrom(raw)); +STC_INLINE cx_value_t* cx_memb(_emplace_front)(Self* self, i_valraw raw) { + return cx_memb(_push_front)(self, i_valfrom(raw)); } STC_INLINE void cx_memb(_pop_back)(Self* self) { @@ -260,12 +260,13 @@ cx_memb(_expand_right_half_)(Self* self, size_t idx, size_t n) { } } -STC_DEF void +STC_DEF cx_value_t* cx_memb(_push_back)(Self* self, i_val value) { struct cdeq_rep* rep = cdeq_rep_(self); if (_cdeq_nfront(self) + rep->size == rep->cap) cx_memb(_expand_right_half_)(self, rep->size, 1); - self->data[cdeq_rep_(self)->size++] = value; + cx_value_t *v = self->data + cdeq_rep_(self)->size++; + *v = value; return v; } STC_DEF Self @@ -304,14 +305,15 @@ cx_memb(_insert_space_)(Self* self, cx_value_t* pos, size_t n) { return self->data + idx; } -STC_DEF void +STC_DEF cx_value_t* cx_memb(_push_front)(Self* self, i_val value) { if (self->data == self->_base) cx_memb(_expand_left_half_)(self, 0, 1); else --self->data; - *self->data = value; ++cdeq_rep_(self)->size; + *self->data = value; + return self->data; } STC_DEF cx_iter_t diff --git a/include/stc/clist.h b/include/stc/clist.h index 18685d9e..f7d09c74 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -95,8 +95,8 @@ STC_API size_t _clist_count(const clist_VOID* self); STC_API Self cx_memb(_clone)(Self cx); STC_API void cx_memb(_del)(Self* self); -STC_API void cx_memb(_push_back)(Self* self, i_val value); -STC_API void cx_memb(_push_front)(Self* self, i_val value); +STC_API cx_value_t* cx_memb(_push_back)(Self* self, i_val value); +STC_API cx_value_t* cx_memb(_push_front)(Self* self, i_val value); STC_API cx_iter_t cx_memb(_insert)(Self* self, cx_iter_t it, i_val value); STC_API void cx_memb(_emplace_items)(Self *self, const cx_rawvalue_t arr[], size_t n); STC_API cx_iter_t cx_memb(_erase_at)(Self* self, cx_iter_t it); @@ -121,10 +121,10 @@ STC_INLINE void cx_memb(_pop_front)(Self* self) { cx_memb(_erase_after_)(self, self->last); } STC_INLINE cx_iter_t cx_memb(_erase)(Self* self, cx_iter_t it) { return cx_memb(_erase_at)(self, it); } -STC_INLINE void cx_memb(_emplace_back)(Self* self, i_valraw raw) - { cx_memb(_push_back)(self, i_valfrom(raw)); } -STC_INLINE void cx_memb(_emplace_front)(Self* self, i_valraw raw) - { cx_memb(_push_front)(self, i_valfrom(raw)); } +STC_INLINE cx_value_t* cx_memb(_emplace_back)(Self* self, i_valraw raw) + { return cx_memb(_push_back)(self, i_valfrom(raw)); } +STC_INLINE cx_value_t* cx_memb(_emplace_front)(Self* self, i_valraw raw) + { return cx_memb(_push_front)(self, i_valfrom(raw)); } STC_INLINE cx_iter_t cx_memb(_emplace)(Self* self, cx_iter_t it, i_valraw raw) { return cx_memb(_insert)(self, it, i_valfrom(raw)); } STC_INLINE cx_value_t* cx_memb(_front)(const Self* self) { return &self->last->next->value; } @@ -191,16 +191,18 @@ cx_memb(_del)(Self* self) { while (self->last) cx_memb(_erase_after_)(self, self->last); } -STC_DEF void +STC_DEF cx_value_t* cx_memb(_push_back)(Self* self, i_val value) { _c_clist_insert_after(self, Self, self->last, value); self->last = entry; + return &entry->value; } -STC_DEF void +STC_DEF cx_value_t* cx_memb(_push_front)(Self* self, i_val value) { _c_clist_insert_after(self, Self, self->last, value); if (!self->last) self->last = entry; + return &entry->value; } STC_DEF void diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 607175d6..03e04290 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -86,7 +86,7 @@ STC_API void cx_memb(_resize)(Self* self, size_t size, i_val fill_val STC_API int cx_memb(_value_compare)(const cx_value_t* x, const cx_value_t* y); STC_API cx_iter_t cx_memb(_find_in)(cx_iter_t it1, cx_iter_t it2, i_valraw raw); STC_API cx_iter_t cx_memb(_bsearch_in)(cx_iter_t it1, cx_iter_t it2, i_valraw raw); -STC_API void cx_memb(_push_back)(Self* self, i_val value); +STC_API cx_value_t* cx_memb(_push_back)(Self* self, i_val value); STC_API cx_iter_t cx_memb(_erase_range_p)(Self* self, cx_value_t* p1, cx_value_t* p2); STC_API cx_iter_t cx_memb(_insert_range_p)(Self* self, cx_value_t* pos, const cx_value_t* p1, const cx_value_t* p2, bool clone); @@ -104,8 +104,8 @@ STC_INLINE void cx_memb(_swap)(Self* a, Self* b) { c_swap(Self, *a, *b); STC_INLINE cx_value_t* cx_memb(_front)(const Self* self) { return self->data; } STC_INLINE cx_value_t* cx_memb(_back)(const Self* self) { return self->data + cvec_rep_(self)->size - 1; } -STC_INLINE void cx_memb(_emplace_back)(Self* self, i_valraw raw) - { cx_memb(_push_back)(self, i_valfrom(raw)); } +STC_INLINE cx_value_t* cx_memb(_emplace_back)(Self* self, i_valraw raw) + { return cx_memb(_push_back)(self, i_valfrom(raw)); } STC_INLINE void cx_memb(_pop_back)(Self* self) { i_valdel(&self->data[--cvec_rep_(self)->size]); } STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self) @@ -272,12 +272,13 @@ cx_memb(_resize)(Self* self, size_t len, i_val null_val) { if (rep->cap) rep->size = len; } -STC_DEF void +STC_DEF cx_value_t* cx_memb(_push_back)(Self* self, i_val value) { size_t len = cvec_rep_(self)->size; if (len == cx_memb(_capacity)(*self)) cx_memb(_reserve)(self, (len*13 >> 3) + 4); - self->data[cvec_rep_(self)->size++] = value; + cx_value_t *v = self->data + cvec_rep_(self)->size++; + *v = value; return v; } STC_DEF Self -- cgit v1.2.3