From 511663aafac77e04c9f20bdb0ea2142e34474a66 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 8 Apr 2021 11:14:57 +0200 Subject: Fix: csmap_X_erase_at() now returns an iter to next element. --- docs/clist_api.md | 6 +++--- docs/csmap_api.md | 2 +- stc/csmap.h | 15 +++++++++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/docs/clist_api.md b/docs/clist_api.md index cd8c5ed8..4adfdf29 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -14,10 +14,10 @@ will invalidate other iterators currently refering to these elements and their i However, an iterator to a succesive element can both be dereferenced and advanced. After advancing (using *clist_X_next(&it)* or *it = cslist_X_fwd(it, n)*), the iterator is in a valid state. This implies: -- `clist_X_insert(&L, clist_X_fwd(it,1))` (insert_after) is well formed if element at `it` exists. -- `clist_X_erase_at(&L, clist_X_fwd(it,1))` (erase_after) is well formed if element at `it` exists and is not last in list. +- `clist_X_insert(&L, clist_X_fwd(it,1), x)` is identical to *std::forward_list* L.insert_after(it, x)*. +- `clist_X_erase_at(&L, clist_X_fwd(it,1))` is identical to *std::forward_list* L.erase_after(it)*. - Iterators returned from *clist_X_insert()* and *clist_X_erase_at()* are always valid or `end`. -- Elements can be safely removed from a list via multiple iterators if done in back to front order. +- Elements can be safely removed from a list via multiple iterators if done back to front order. See the c++ class [std::list](https://en.cppreference.com/w/cpp/container/list) for similar API and [std::forward_list](https://en.cppreference.com/w/cpp/container/forward_list) for a functional description. diff --git a/docs/csmap_api.md b/docs/csmap_api.md index bb023c64..dcde885f 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -65,7 +65,7 @@ csmap_X_result_t csmap_X_emplace_or_assign(csmap_X* self, RawKey rkey, RawMap void csmap_X_emplace_n(csmap_X* self, const csmap_X_rawvalue_t arr[], size_t size); size_t csmap_X_erase(csmap_X* self, RawKey rkey); -csmap_X_iter_t csmap_X_erase_at(csmap_X* self, csmap_X_iter_t pos); +csmap_X_iter_t csmap_X_erase_at(csmap_X* self, csmap_X_iter_t pos); // returns iter to next element csmap_X_iter_t csmap_X_begin(const csmap_X* self); csmap_X_iter_t csmap_X_end(const csmap_X* self); diff --git a/stc/csmap.h b/stc/csmap.h index 39f1c0a9..caa9406a 100644 --- a/stc/csmap.h +++ b/stc/csmap.h @@ -299,10 +299,8 @@ struct csmap_rep { size_t root, disp, head, size, cap; void* nodes[]; }; STC_API int \ C##X##_erase(C##X* self, RawKey rkey); \ \ - STC_INLINE int \ - C##X##_erase_at(C##X* self, C##X##_iter_t pos) { \ - return C##X##_erase(self, keyToRaw(KEY_REF_##C(pos.ref))); \ - } \ + STC_API C##X##_iter_t \ + C##X##_erase_at(C##X* self, C##X##_iter_t pos); \ \ _implement_AATREE(X, C, Key, Mapped, keyCompareRaw, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ @@ -512,6 +510,15 @@ static struct csmap_rep _smap_inits = {0, 0, 0, 0}; if (erased) {_csmap_rep(self)->root = root; --_csmap_rep(self)->size;} \ return erased; \ } \ +\ + STC_DEF C##X##_iter_t \ + C##X##_erase_at(C##X* self, C##X##_iter_t pos) { \ + C##X##_rawkey_t raw = keyToRaw(KEY_REF_##C(pos.ref)); C##X##_next(&pos); \ + C##X##_rawkey_t nxt = keyToRaw(KEY_REF_##C(pos.ref)); \ + C##X##_erase(self, raw); \ + C##X##_find_it(self, nxt, &pos); \ + return pos; \ + } \ \ static C##X##_size_t \ C##X##_clone_r_(C##X* self, const C##X##_node_t* src, C##X##_size_t sn) { \ -- cgit v1.2.3