From 10c06664445843e7b516cdd041220a1f5e916a62 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 6 May 2021 08:25:07 +0200 Subject: Added erase() for cvec, cdeq and clist for consistency. --- docs/cdeq_api.md | 3 ++- docs/clist_api.md | 3 ++- docs/cvec_api.md | 17 +++++++++------ stc/cdeq.h | 10 ++++++--- stc/clist.h | 65 ++++++++++++++++++++++++++++--------------------------- stc/cmap.h | 35 +++++++++++++++--------------- stc/cvec.h | 4 ++++ 7 files changed, 75 insertions(+), 62 deletions(-) diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index 49cae7b7..555976b5 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -57,14 +57,15 @@ void cdeq_X_emplace_back(cdeq_X* self, RawValue raw); void cdeq_X_pop_back(cdeq_X* self); cdeq_X_iter_t cdeq_X_insert(cdeq_X* self, size_t idx, Value value); // move value -cdeq_X_iter_t cdeq_X_insert_at(cdeq_X* self, cdeq_X_iter_t it, Value value); // move value cdeq_X_iter_t cdeq_X_insert_n(cdeq_X* self, size_t idx, const Value[] arr, size_t n); // move arr values +cdeq_X_iter_t cdeq_X_insert_at(cdeq_X* self, cdeq_X_iter_t it, Value value); // move value cdeq_X_iter_t cdeq_X_emplace(cdeq_X* self, size_t idx, RawValue raw); cdeq_X_iter_t cdeq_X_emplace_n(cdeq_X* self, size_t idx, const RawValue[] arr, size_t n); cdeq_X_iter_t cdeq_X_emplace_at(cdeq_X* self, cdeq_X_iter_t it, RawValue raw); cdeq_X_iter_t cdeq_X_emplace_range(cdeq_X* self, cdeq_X_iter_t it1, cdeq_X_iter_t it2); // will clone +cdeq_X_iter_t cdeq_X_erase(cdeq_X* self, size_t idx); cdeq_X_iter_t cdeq_X_erase_n(cdeq_X* self, size_t idx, size_t n); cdeq_X_iter_t cdeq_X_erase_at(cdeq_X* self, cdeq_X_iter_t it); cdeq_X_iter_t cdeq_X_erase_range(cdeq_X* self, cdeq_X_iter_t it1, cdeq_X_iter_t it2); diff --git a/docs/clist_api.md b/docs/clist_api.md index 27b87c23..7807b677 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -67,7 +67,8 @@ void clist_X_emplace_items(clist_X *self, const clist_X_rawvalue_ clist_X_iter_t clist_X_insert(clist_X* self, clist_X_iter_t it, Value value); // return iter to new elem clist_X_iter_t clist_X_emplace(clist_X* self, clist_X_iter_t it, RawValue raw); -clist_X_iter_t clist_X_erase_at(clist_X* self, clist_X_iter_t it); // return iter after it +clist_X_iter_t clist_X_erase(clist_X* self, clist_X_iter_t it); // return iter after it +clist_X_iter_t clist_X_erase_at(clist_X* self, clist_X_iter_t it); // alias for erase() clist_X_iter_t clist_X_erase_range(clist_X* self, clist_X_iter_t it1, clist_X_iter_t it2); size_t clist_X_remove(clist_X* self, RawValue raw); // removes all elements equal to raw diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 264242c2..0efd7a62 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -58,14 +58,15 @@ void cvec_X_emplace_back(cvec_X* self, RawValue raw); void cvec_X_pop_back(cvec_X* self); cvec_X_iter_t cvec_X_insert(cvec_X* self, size_t idx, Value value); // move value -cvec_X_iter_t cvec_X_insert_at(cvec_X* self, cvec_X_iter_t it, Value value); // move value cvec_X_iter_t cvec_X_insert_n(cvec_X* self, size_t idx, const Value[] arr, size_t n); // move arr values +cvec_X_iter_t cvec_X_insert_at(cvec_X* self, cvec_X_iter_t it, Value value); // move value cvec_X_iter_t cvec_X_emplace(cvec_X* self, size_t idx, RawValue raw); cvec_X_iter_t cvec_X_emplace_n(cvec_X* self, size_t idx, const RawValue[] arr, size_t n); cvec_X_iter_t cvec_X_emplace_at(cvec_X* self, cvec_X_iter_t it, RawValue raw); cvec_X_iter_t cvec_X_emplace_range(cvec_X* self, cvec_X_iter_t it1, cvec_X_iter_t it2); // will clone +cvec_X_iter_t cvec_X_erase(cvec_X* self, size_t idx); cvec_X_iter_t cvec_X_erase_n(cvec_X* self, size_t idx, size_t n); cvec_X_iter_t cvec_X_erase_at(cvec_X* self, cvec_X_iter_t it); cvec_X_iter_t cvec_X_erase_range(cvec_X* self, cvec_X_iter_t it1, cvec_X_iter_t it2); @@ -107,23 +108,25 @@ int main() { // Create a vector containing integers cvec_i vec = cvec_i_init(); - c_emplace(cvec_i, vec, {7, 5, 16, 8}); - // Add two more integers to vector + // Add two integers to vector cvec_i_push_back(&vec, 25); cvec_i_push_back(&vec, 13); + // Append a set of numbers + c_emplace(cvec_i, vec, {7, 5, 16, 8}); + printf("initial: "); - c_foreach (n, cvec_i, vec) { - printf(" %d", *n.ref); + c_foreach (k, cvec_i, vec) { + printf(" %d", *k.ref); } // Sort the vector cvec_i_sort(&vec); printf("\nsorted: "); - c_foreach (n, cvec_i, vec) { - printf(" %d", *n.ref); + c_foreach (k, cvec_i, vec) { + printf(" %d", *k.ref); } cvec_i_del(&vec); diff --git a/stc/cdeq.h b/stc/cdeq.h index d5abcdba..6bd85cf3 100644 --- a/stc/cdeq.h +++ b/stc/cdeq.h @@ -147,6 +147,10 @@ struct cdeq_rep { size_t size, cap; void* base[]; }; CX##_emplace_range_p(self, self->data + _cdeq_rep(self)->size, arr, arr + n); \ } \ \ + STC_INLINE CX##_iter_t \ + CX##_erase(CX* self, size_t idx) { \ + return CX##_erase_range_p(self, self->data + idx, self->data + idx + 1); \ + } \ STC_INLINE CX##_iter_t \ CX##_erase_n(CX* self, size_t idx, size_t n) { \ return CX##_erase_range_p(self, self->data + idx, self->data + idx + n); \ @@ -260,8 +264,8 @@ static struct cdeq_rep _cdeq_inits = {0, 0}; STC_DEF void \ CX##_expand_right_(CX* self, size_t idx, size_t n) { \ struct cdeq_rep* rep = _cdeq_rep(self); \ - size_t sz = rep->size, cap = rep->cap, nfront = _cdeq_nfront(self); \ - size_t nback = cap - sz - nfront; \ + size_t sz = rep->size, cap = rep->cap; \ + size_t nfront = _cdeq_nfront(self), nback = cap - sz - nfront; \ if (nback >= n || sz*1.3 + n > cap && CX##_realloc_(self, n)) { \ memmove(self->data + idx + n, self->data + idx, (sz - idx)*sizeof(Value)); \ } else { \ @@ -278,7 +282,7 @@ static struct cdeq_rep _cdeq_inits = {0, 0}; size_t idx = pos - self->data; \ if (idx*2 < _cdeq_rep(self)->size) CX##_expand_left_(self, idx, n); \ else CX##_expand_right_(self, idx, n); \ - if (n) _cdeq_rep(self)->size += n; \ + if (n) _cdeq_rep(self)->size += n; /* do only if size > 0 */ \ return self->data + idx; \ } \ \ diff --git a/stc/clist.h b/stc/clist.h index 02278cc3..2bebf58e 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -99,39 +99,40 @@ STC_API size_t _clist_count(const clist_VOID* self); _c_using_clist_types(CX, Value); \ typedef RawValue CX##_rawvalue_t; \ \ - STC_API CX CX##_clone(CX list); \ - STC_API void CX##_del(CX* self); \ - STC_API void CX##_push_back(CX* self, Value value); \ - STC_API void CX##_push_front(CX* self, Value value); \ - STC_API CX##_iter_t CX##_insert(CX* self, CX##_iter_t it, Value value); \ - STC_API void CX##_emplace_items(CX *self, const CX##_rawvalue_t arr[], size_t n); \ - STC_API CX##_iter_t CX##_erase_at(CX* self, CX##_iter_t it); \ - STC_API CX##_iter_t CX##_erase_range(CX* self, CX##_iter_t it1, CX##_iter_t it2); \ - STC_API size_t CX##_remove(CX* self, RawValue val); \ - STC_API CX##_iter_t CX##_splice(CX* self, CX##_iter_t it, CX* other); \ - STC_API CX CX##_split(CX* self, CX##_iter_t it1, CX##_iter_t it2); \ - STC_API void CX##_sort(CX* self); \ - STC_API CX##_iter_t CX##_find_in(CX##_iter_t it1, CX##_iter_t it2, RawValue val); \ - STC_API CX##_node_t* CX##_erase_after_(CX* self, CX##_node_t* node); \ + STC_API CX CX##_clone(CX list); \ + STC_API void CX##_del(CX* self); \ + STC_API void CX##_push_back(CX* self, Value value); \ + STC_API void CX##_push_front(CX* self, Value value); \ + STC_API CX##_iter_t CX##_insert(CX* self, CX##_iter_t it, Value value); \ + STC_API void CX##_emplace_items(CX *self, const CX##_rawvalue_t arr[], size_t n); \ + STC_API CX##_iter_t CX##_erase_at(CX* self, CX##_iter_t it); \ + STC_API CX##_iter_t CX##_erase_range(CX* self, CX##_iter_t it1, CX##_iter_t it2); \ + STC_API size_t CX##_remove(CX* self, RawValue val); \ + STC_API CX##_iter_t CX##_splice(CX* self, CX##_iter_t it, CX* other); \ + STC_API CX CX##_split(CX* self, CX##_iter_t it1, CX##_iter_t it2); \ + STC_API void CX##_sort(CX* self); \ + STC_API CX##_iter_t CX##_find_in(CX##_iter_t it1, CX##_iter_t it2, RawValue val); \ + STC_API CX##_node_t* CX##_erase_after_(CX* self, CX##_node_t* node); \ \ - STC_INLINE CX CX##_init(void) {CX lst = {NULL}; return lst;} \ - STC_INLINE bool CX##_empty(CX lst) {return lst.last == NULL;} \ - STC_INLINE size_t CX##_count(CX lst) \ - {return _clist_count((const clist_VOID*) &lst);} \ - STC_INLINE void CX##_clear(CX* self) {CX##_del(self);} \ - STC_INLINE Value CX##_value_clone(Value val) \ - {return valueFromRaw(valueToRaw(&val));} \ - STC_INLINE Value CX##_value_fromraw(RawValue raw) \ - {return valueFromRaw(raw);} \ - STC_INLINE void CX##_pop_front(CX* self) \ - {CX##_erase_after_(self, self->last);} \ - STC_INLINE void CX##_emplace_back(CX* self, RawValue raw) \ - {CX##_push_back(self, valueFromRaw(raw));} \ - STC_INLINE void CX##_emplace_front(CX* self, RawValue raw) \ - {CX##_push_front(self, valueFromRaw(raw));} \ - STC_INLINE \ - CX##_iter_t CX##_emplace(CX* self, CX##_iter_t it, RawValue raw) \ - {return CX##_insert(self, it, valueFromRaw(raw));} \ + STC_INLINE CX CX##_init(void) {CX lst = {NULL}; return lst;} \ + STC_INLINE bool CX##_empty(CX lst) {return lst.last == NULL;} \ + STC_INLINE size_t CX##_count(CX lst) \ + {return _clist_count((const clist_VOID*) &lst);} \ + STC_INLINE void CX##_clear(CX* self) {CX##_del(self);} \ + STC_INLINE Value CX##_value_clone(Value val) \ + {return valueFromRaw(valueToRaw(&val));} \ + STC_INLINE Value CX##_value_fromraw(RawValue raw) \ + {return valueFromRaw(raw);} \ + STC_INLINE void CX##_pop_front(CX* self) \ + {CX##_erase_after_(self, self->last);} \ + STC_INLINE CX##_iter_t CX##_erase(CX* self, CX##_iter_t it) \ + {return CX##_erase_at(self, it);} \ + STC_INLINE void CX##_emplace_back(CX* self, RawValue raw) \ + {CX##_push_back(self, valueFromRaw(raw));} \ + STC_INLINE void CX##_emplace_front(CX* self, RawValue raw) \ + {CX##_push_front(self, valueFromRaw(raw));} \ + STC_INLINE CX##_iter_t CX##_emplace(CX* self, CX##_iter_t it, RawValue raw) \ + {return CX##_insert(self, it, valueFromRaw(raw));} \ STC_INLINE CX##_value_t* CX##_front(const CX* self) {return &self->last->next->value;} \ STC_INLINE CX##_value_t* CX##_back(const CX* self) {return &self->last->value;} \ \ diff --git a/stc/cmap.h b/stc/cmap.h index 1da2d980..e0e86d5c 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -187,7 +187,6 @@ STC_INLINE uint64_t c_default_hash64(const void* data, size_t ignored) STC_API void CX##_reserve(CX* self, size_t capacity); \ STC_API chash_bucket_t CX##_bucket_(const CX* self, const CX##_rawkey_t* rkeyptr); \ STC_API CX##_result_t CX##_insert_entry_(CX* self, RawKey rkey); \ - STC_API CX##_iter_t CX##_find(const CX* self, RawKey rkey); \ STC_API void CX##_erase_entry(CX* self, CX##_value_t* val); \ \ STC_INLINE CX CX##_init(void) {CX m = _cmap_inits; return m;} \ @@ -201,8 +200,6 @@ STC_INLINE uint64_t c_default_hash64(const void* data, size_t ignored) STC_INLINE void CX##_swap(CX *map1, CX *map2) {c_swap(CX, *map1, *map2);} \ STC_INLINE bool CX##_contains(const CX* self, RawKey rkey) \ {return self->size && self->_hashx[CX##_bucket_(self, &rkey).idx];} \ - STC_INLINE CX##_value_t* CX##_get(const CX* self, RawKey rkey) \ - {return CX##_find(self, rkey).ref;} \ \ STC_INLINE void \ CX##_value_clone(CX##_value_t* _dst, CX##_value_t* _val) { \ @@ -267,6 +264,19 @@ STC_INLINE uint64_t c_default_hash64(const void* data, size_t ignored) chash_bucket_t b = CX##_bucket_(self, &rkey); \ return &self->table[b.idx].second; \ }) \ +\ + STC_INLINE CX##_iter_t \ + CX##_find(const CX* self, RawKey rkey) { \ + CX##_iter_t it = {NULL}; \ + if (self->size == 0) return it; \ + chash_bucket_t b = CX##_bucket_(self, &rkey); \ + if (*(it._hx = self->_hashx+b.idx)) it.ref = self->table+b.idx; \ + return it; \ + } \ +\ + STC_INLINE CX##_value_t* \ + CX##_get(const CX* self, RawKey rkey) \ + {return CX##_find(self, rkey).ref;} \ \ STC_INLINE CX##_iter_t \ CX##_begin(const CX* self) { \ @@ -276,14 +286,12 @@ STC_INLINE uint64_t c_default_hash64(const void* data, size_t ignored) } \ \ STC_INLINE CX##_iter_t \ - CX##_end(const CX* self) {\ - CX##_iter_t it = {self->table + self->bucket_count}; return it; \ - } \ + CX##_end(const CX* self) \ + {CX##_iter_t it = {self->table + self->bucket_count}; return it;} \ \ STC_INLINE void \ - CX##_next(CX##_iter_t* it) { \ - while ((++it->ref, *++it->_hx == 0)) ; \ - } \ + CX##_next(CX##_iter_t* it) \ + {while ((++it->ref, *++it->_hx == 0)) ;} \ \ STC_INLINE size_t \ CX##_erase(CX* self, RawKey rkey) { \ @@ -360,15 +368,6 @@ STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) \ } \ return b; \ } \ -\ - STC_DEF CX##_iter_t \ - CX##_find(const CX* self, RawKey rkey) { \ - CX##_iter_t it = {NULL}; \ - if (self->size == 0) return it; \ - chash_bucket_t b = CX##_bucket_(self, &rkey); \ - if (*(it._hx = self->_hashx+b.idx)) it.ref = self->table+b.idx; \ - return it; \ - } \ \ STC_DEF CX##_result_t \ CX##_insert_entry_(CX* self, RawKey rkey) { \ diff --git a/stc/cvec.h b/stc/cvec.h index cc50a951..3856ec9b 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -146,6 +146,10 @@ struct cvec_rep { size_t size, cap; void* data[]; }; CX##_emplace_range_p(self, self->data + _cvec_rep(self)->size, arr, arr + n); \ } \ \ + STC_INLINE CX##_iter_t \ + CX##_erase(CX* self, size_t idx) { \ + return CX##_erase_range_p(self, self->data + idx, self->data + idx + 1); \ + } \ STC_INLINE CX##_iter_t \ CX##_erase_n(CX* self, size_t idx, size_t n) { \ return CX##_erase_range_p(self, self->data + idx, self->data + idx + n); \ -- cgit v1.2.3