From 9eafcffc28cc8cc0ee273888c1af043ff4d685a4 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 31 Dec 2021 08:48:58 +0100 Subject: Added alias functions push_back and pop_back to cstack + docs update. --- docs/cstack_api.md | 29 ++++++++++++++++------------- include/stc/cstack.h | 24 +++++++++++++++--------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/docs/cstack_api.md b/docs/cstack_api.md index 672b3168..417e8d80 100644 --- a/docs/cstack_api.md +++ b/docs/cstack_api.md @@ -23,27 +23,30 @@ See the c++ class [std::stack](https://en.cppreference.com/w/cpp/container/stack ```c cstack_X cstack_X_init(void); -cstack_X cstack_with_capacity(size_t cap); -cstack_X cstack_with_size(size_t size, i_val fill); +cstack_X cstack_X_with_capacity(size_t cap); +cstack_X cstack_X_with_size(size_t size, i_val fill); cstack_X cstack_X_clone(cstack_X st); void cstack_X_clear(cstack_X* self); bool cstack_X_reserve(cstack_X* self, size_t n); void cstack_X_shrink_to_fit(cstack_X* self); void cstack_X_copy(cstack_X* self, cstack_X other); -void cstack_X_drop(cstack_X* self); // destructor +void cstack_X_drop(cstack_X* self); // destructor size_t cstack_X_size(cstack_X st); size_t cstack_X_capacity(cstack_X st); bool cstack_X_empty(cstack_X st); -cstack_X_value* cstack_X_top(const cstack_X* self); -const cstack_X_value* cstack_X_at(const cstack_X* self, size_t idx); +i_val* cstack_X_top(const cstack_X* self); +const i_val* cstack_X_at(const cstack_X* self, size_t idx); -cstack_X_value* cstack_X_push(cstack_X* self, i_val value); -cstack_X_value* cstack_X_emplace(cstack_X* self, i_valraw raw); +i_val* cstack_X_push(cstack_X* self, i_val value); +i_val* cstack_X_push_back(cstack_X* self, i_val value); // same as push +i_val* cstack_X_emplace(cstack_X* self, i_valraw raw); +i_val* cstack_X_emplace_back(cstack_X* self, i_valraw raw); // same as emplace void cstack_X_pop(cstack_X* self); +void cstack_X_pop_back(cstack_X* self); // same as pop cstack_X_iter cstack_X_begin(const cstack_X* self); cstack_X_iter cstack_X_end(const cstack_X* self); @@ -64,24 +67,24 @@ i_val cstack_X_value_clone(i_val value); ## Example ```c +#define i_type IStack #define i_val int -#define i_tag i #include #include int main() { - cstack_i S = cstack_i_init(); + IStack stk = IStack_init(); for (int i=0; i < 100; ++i) - cstack_i_push(&S, i*i); + IStack_push(&stk, i*i); for (int i=0; i < 90; ++i) - cstack_i_pop(&S); + IStack_pop(&stk); - printf("top: %d\n", *cstack_i_top(&S)); + printf("top: %d\n", *IStack_top(&stk)); - cstack_i_drop(&S); + IStack_drop(&stk); } ``` Output: diff --git a/include/stc/cstack.h b/include/stc/cstack.h index 1ceec0cb..5999441e 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -46,9 +46,9 @@ STC_INLINE _cx_self _cx_memb(_with_capacity)(size_t cap) { return out; } -STC_INLINE _cx_self _cx_memb(_with_size)(size_t size, i_val fill) { - _cx_self out = {(_cx_value *) c_malloc(size*sizeof fill), size, size}; - while (size) out.data[--size] = fill; +STC_INLINE _cx_self _cx_memb(_with_size)(size_t size, i_val null) { + _cx_self out = {(_cx_value *) c_malloc(size*sizeof null), size, size}; + while (size) out.data[--size] = null; return out; } @@ -70,12 +70,6 @@ STC_INLINE bool _cx_memb(_empty)(_cx_self v) STC_INLINE size_t _cx_memb(_capacity)(_cx_self v) { return v.capacity; } -STC_INLINE _cx_value* _cx_memb(_top)(const _cx_self* self) - { return &self->data[self->size - 1]; } - -STC_INLINE void _cx_memb(_pop)(_cx_self* self) - { _cx_value* p = &self->data[--self->size]; i_valdrop(p); } - STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { if (n < self->size) return true; _cx_value *t = (_cx_value *)c_realloc(self->data, n*sizeof *t); @@ -85,11 +79,21 @@ STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { STC_INLINE void _cx_memb(_shrink_to_fit)(_cx_self* self) { _cx_memb(_reserve)(self, self->size); } +STC_INLINE _cx_value* _cx_memb(_top)(const _cx_self* self) + { return &self->data[self->size - 1]; } + STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value val) { if (self->size == self->capacity) _cx_memb(_reserve)(self, self->size*3/2 + 4); _cx_value* vp = self->data + self->size++; *vp = val; return vp; } +STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, _cx_value val) + { return _cx_memb(_push)(self, val); } + +STC_INLINE void _cx_memb(_pop)(_cx_self* self) + { _cx_value* p = &self->data[--self->size]; i_valdrop(p); } +STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) + { _cx_memb(_pop)(self); } STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx) { assert(idx < self->size); return self->data + idx; } @@ -98,6 +102,8 @@ STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx) #if !defined _i_no_raw STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) { return _cx_memb(_push)(self, i_valfrom(raw)); } +STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, _cx_raw raw) + { return _cx_memb(_push)(self, i_valfrom(raw)); } #endif STC_INLINE _cx_self _cx_memb(_clone)(_cx_self v) { _cx_self out = {(_cx_value *) c_malloc(v.size*sizeof(_cx_value)), v.size, v.size}; -- cgit v1.2.3