Merge pull request #4723 from sworleys/List-Del-API

List API add const and return object on `*_del`
This commit is contained in:
David Lamparter 2019-08-01 16:31:09 +02:00 committed by GitHub
commit 2e2094b7d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 45 deletions

View File

@ -135,8 +135,10 @@ macro_inline void prefix ## _add_tail(struct prefix##_head *h, type *item) \
macro_inline void prefix ## _del_hint(struct prefix##_head *h, type *item, \ macro_inline void prefix ## _del_hint(struct prefix##_head *h, type *item, \
_Atomic atomptr_t *hint) \ _Atomic atomptr_t *hint) \
{ atomlist_del_hint(&h->ah, &item->field.ai, hint); } \ { atomlist_del_hint(&h->ah, &item->field.ai, hint); } \
macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \ macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
{ atomlist_del_hint(&h->ah, &item->field.ai, NULL); } \ { atomlist_del_hint(&h->ah, &item->field.ai, NULL); \
/* TODO: Return NULL if not found */ \
return item; } \
macro_inline type *prefix ## _pop(struct prefix##_head *h) \ macro_inline type *prefix ## _pop(struct prefix##_head *h) \
{ char *p = (char *)atomlist_pop(&h->ah); \ { char *p = (char *)atomlist_pop(&h->ah); \
return p ? (type *)(p - offsetof(type, field)) : NULL; } \ return p ? (type *)(p - offsetof(type, field)) : NULL; } \
@ -273,9 +275,11 @@ macro_inline void prefix ## _del_hint(struct prefix##_head *h, type *item, \
{ \ { \
atomsort_del_hint(&h->ah, &item->field.ai, hint); \ atomsort_del_hint(&h->ah, &item->field.ai, hint); \
} \ } \
macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \ macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
{ \ { \
atomsort_del_hint(&h->ah, &item->field.ai, NULL); \ atomsort_del_hint(&h->ah, &item->field.ai, NULL); \
/* TODO: Return NULL if not found */ \
return item; \
} \ } \
macro_inline size_t prefix ## _count(struct prefix##_head *h) \ macro_inline size_t prefix ## _count(struct prefix##_head *h) \
{ \ { \

View File

@ -333,9 +333,10 @@ color:
return (old); return (old);
} }
void typed_rb_remove(struct rbt_tree *rbt, struct rb_entry *rbe) struct typed_rb_entry *typed_rb_remove(struct rbt_tree *rbt,
struct rb_entry *rbe)
{ {
rbe_remove(rbt, rbe); return rbe_remove(rbt, rbe);
} }
struct typed_rb_entry *typed_rb_insert(struct rbt_tree *rbt, struct typed_rb_entry *typed_rb_insert(struct rbt_tree *rbt,

View File

@ -38,29 +38,30 @@ struct typed_rb_root {
size_t count; size_t count;
}; };
struct typed_rb_entry *typed_rb_insert(struct typed_rb_root *, struct typed_rb_entry *typed_rb_insert(struct typed_rb_root *rbt,
struct typed_rb_entry *rbe, struct typed_rb_entry *rbe,
int (*cmpfn)( int (*cmpfn)(
const struct typed_rb_entry *a, const struct typed_rb_entry *a,
const struct typed_rb_entry *b)); const struct typed_rb_entry *b));
void typed_rb_remove(struct typed_rb_root *, struct typed_rb_entry *rbe); struct typed_rb_entry *typed_rb_remove(struct typed_rb_root *rbt,
struct typed_rb_entry *typed_rb_find(struct typed_rb_root *, struct typed_rb_entry *rbe);
struct typed_rb_entry *typed_rb_find(struct typed_rb_root *rbt,
const struct typed_rb_entry *rbe, const struct typed_rb_entry *rbe,
int (*cmpfn)( int (*cmpfn)(
const struct typed_rb_entry *a, const struct typed_rb_entry *a,
const struct typed_rb_entry *b)); const struct typed_rb_entry *b));
struct typed_rb_entry *typed_rb_find_gteq(struct typed_rb_root *, struct typed_rb_entry *typed_rb_find_gteq(struct typed_rb_root *rbt,
const struct typed_rb_entry *rbe, const struct typed_rb_entry *rbe,
int (*cmpfn)( int (*cmpfn)(
const struct typed_rb_entry *a, const struct typed_rb_entry *a,
const struct typed_rb_entry *b)); const struct typed_rb_entry *b));
struct typed_rb_entry *typed_rb_find_lt(struct typed_rb_root *, struct typed_rb_entry *typed_rb_find_lt(struct typed_rb_root *rbt,
const struct typed_rb_entry *rbe, const struct typed_rb_entry *rbe,
int (*cmpfn)( int (*cmpfn)(
const struct typed_rb_entry *a, const struct typed_rb_entry *a,
const struct typed_rb_entry *b)); const struct typed_rb_entry *b));
struct typed_rb_entry *typed_rb_min(struct typed_rb_root *); struct typed_rb_entry *typed_rb_min(struct typed_rb_root *rbt);
struct typed_rb_entry *typed_rb_next(struct typed_rb_entry *); struct typed_rb_entry *typed_rb_next(struct typed_rb_entry *rbe);
#define _PREDECL_RBTREE(prefix) \ #define _PREDECL_RBTREE(prefix) \
struct prefix ## _head { struct typed_rb_root rr; }; \ struct prefix ## _head { struct typed_rb_root rr; }; \
@ -99,9 +100,11 @@ macro_inline type *prefix ## _find_lt(struct prefix##_head *h, \
re = typed_rb_find_lt(&h->rr, &item->field.re, cmpfn_nuq); \ re = typed_rb_find_lt(&h->rr, &item->field.re, cmpfn_nuq); \
return container_of_null(re, type, field.re); \ return container_of_null(re, type, field.re); \
} \ } \
macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \ macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
{ \ { \
typed_rb_remove(&h->rr, &item->field.re); \ struct typed_rb_entry *re; \
re = typed_rb_remove(&h->rr, &item->field.re); \
return container_of_null(re, type, field.re); \
} \ } \
macro_inline type *prefix ## _pop(struct prefix##_head *h) \ macro_inline type *prefix ## _pop(struct prefix##_head *h) \
{ \ { \
@ -130,7 +133,7 @@ macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
re = item ? typed_rb_next(&item->field.re) : NULL; \ re = item ? typed_rb_next(&item->field.re) : NULL; \
return container_of_null(re, type, field.re); \ return container_of_null(re, type, field.re); \
} \ } \
macro_pure size_t prefix ## _count(struct prefix##_head *h) \ macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
{ \ { \
return h->rr.count; \ return h->rr.count; \
} \ } \

View File

@ -341,13 +341,14 @@ struct sskip_item *typesafe_skiplist_find_lt(struct sskip_head *head,
return best; return best;
} }
void typesafe_skiplist_del(struct sskip_head *head, struct sskip_item *item, struct sskip_item *typesafe_skiplist_del(
int (*cmpfn)(const struct sskip_item *a, struct sskip_head *head, struct sskip_item *item,
const struct sskip_item *b)) int (*cmpfn)(const struct sskip_item *a, const struct sskip_item *b))
{ {
size_t level = SKIPLIST_MAXDEPTH; size_t level = SKIPLIST_MAXDEPTH;
struct sskip_item *prev = &head->hitem, *next; struct sskip_item *prev = &head->hitem, *next;
int cmpval; int cmpval;
bool found = false;
while (level) { while (level) {
next = sl_level_get(prev, level - 1); next = sl_level_get(prev, level - 1);
@ -359,6 +360,7 @@ void typesafe_skiplist_del(struct sskip_head *head, struct sskip_item *item,
sl_level_set(prev, level - 1, sl_level_set(prev, level - 1,
sl_level_get(item, level - 1)); sl_level_get(item, level - 1));
level--; level--;
found = true;
continue; continue;
} }
cmpval = cmpfn(next, item); cmpval = cmpfn(next, item);
@ -369,6 +371,9 @@ void typesafe_skiplist_del(struct sskip_head *head, struct sskip_item *item,
level--; level--;
} }
if (!found)
return NULL;
/* TBD: assert when trying to remove non-existing item? */ /* TBD: assert when trying to remove non-existing item? */
head->count--; head->count--;
@ -379,6 +384,8 @@ void typesafe_skiplist_del(struct sskip_head *head, struct sskip_item *item,
XFREE(MTYPE_SKIPLIST_OFLOW, oflow); XFREE(MTYPE_SKIPLIST_OFLOW, oflow);
} }
memset(item, 0, sizeof(*item)); memset(item, 0, sizeof(*item));
return item;
} }
struct sskip_item *typesafe_skiplist_pop(struct sskip_head *head) struct sskip_item *typesafe_skiplist_pop(struct sskip_head *head)

View File

@ -109,17 +109,18 @@ macro_inline void prefix ## _add_after(struct prefix##_head *h, \
typesafe_list_add(&h->sh, nextp, &item->field.si); \ typesafe_list_add(&h->sh, nextp, &item->field.si); \
} \ } \
/* TODO: del_hint */ \ /* TODO: del_hint */ \
macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \ macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
{ \ { \
struct slist_item **iter = &h->sh.first; \ struct slist_item **iter = &h->sh.first; \
while (*iter && *iter != &item->field.si) \ while (*iter && *iter != &item->field.si) \
iter = &(*iter)->next; \ iter = &(*iter)->next; \
if (!*iter) \ if (!*iter) \
return; \ return NULL; \
h->sh.count--; \ h->sh.count--; \
*iter = item->field.si.next; \ *iter = item->field.si.next; \
if (!item->field.si.next) \ if (!item->field.si.next) \
h->sh.last_next = iter; \ h->sh.last_next = iter; \
return item; \
} \ } \
macro_inline type *prefix ## _pop(struct prefix##_head *h) \ macro_inline type *prefix ## _pop(struct prefix##_head *h) \
{ \ { \
@ -149,7 +150,7 @@ macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
sitem = &item->field.si; \ sitem = &item->field.si; \
return container_of_null(sitem->next, type, field.si); \ return container_of_null(sitem->next, type, field.si); \
} \ } \
macro_pure size_t prefix ## _count(struct prefix##_head *h) \ macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
{ \ { \
return h->sh.count; \ return h->sh.count; \
} \ } \
@ -212,13 +213,14 @@ macro_inline void prefix ## _add_after(struct prefix##_head *h, \
prev = after ? &after->field.di : &h->dh.hitem; \ prev = after ? &after->field.di : &h->dh.hitem; \
typesafe_dlist_add(&h->dh, prev, &item->field.di); \ typesafe_dlist_add(&h->dh, prev, &item->field.di); \
} \ } \
macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \ macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
{ \ { \
struct dlist_item *ditem = &item->field.di; \ struct dlist_item *ditem = &item->field.di; \
ditem->prev->next = ditem->next; \ ditem->prev->next = ditem->next; \
ditem->next->prev = ditem->prev; \ ditem->next->prev = ditem->prev; \
h->dh.count--; \ h->dh.count--; \
ditem->prev = ditem->next = NULL; \ ditem->prev = ditem->next = NULL; \
return item; \
} \ } \
macro_inline type *prefix ## _pop(struct prefix##_head *h) \ macro_inline type *prefix ## _pop(struct prefix##_head *h) \
{ \ { \
@ -250,7 +252,7 @@ macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
return NULL; \ return NULL; \
return prefix ## _next(h, item); \ return prefix ## _next(h, item); \
} \ } \
macro_pure size_t prefix ## _count(struct prefix##_head *h) \ macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
{ \ { \
return h->dh.count; \ return h->dh.count; \
} \ } \
@ -308,7 +310,7 @@ macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
h->hh.count++; \ h->hh.count++; \
return NULL; \ return NULL; \
} \ } \
macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \ macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
{ \ { \
struct heap_item *other; \ struct heap_item *other; \
uint32_t index = item->field.hi.index; \ uint32_t index = item->field.hi.index; \
@ -321,6 +323,7 @@ macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \
typesafe_heap_pushdown(&h->hh, index, other, prefix ## __cmp); \ typesafe_heap_pushdown(&h->hh, index, other, prefix ## __cmp); \
if (HEAP_RESIZE_TRESH_DN(h)) \ if (HEAP_RESIZE_TRESH_DN(h)) \
typesafe_heap_resize(&h->hh, false); \ typesafe_heap_resize(&h->hh, false); \
return item; \
} \ } \
macro_inline type *prefix ## _pop(struct prefix##_head *h) \ macro_inline type *prefix ## _pop(struct prefix##_head *h) \
{ \ { \
@ -354,7 +357,7 @@ macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
return NULL; \ return NULL; \
return prefix ## _next(h, item); \ return prefix ## _next(h, item); \
} \ } \
macro_pure size_t prefix ## _count(struct prefix##_head *h) \ macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
{ \ { \
return h->hh.count; \ return h->hh.count; \
} \ } \
@ -449,15 +452,16 @@ macro_inline type *prefix ## _find_lt(struct prefix##_head *h, \
return container_of_null(prev, type, field.si); \ return container_of_null(prev, type, field.si); \
} \ } \
/* TODO: del_hint */ \ /* TODO: del_hint */ \
macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \ macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
{ \ { \
struct ssort_item **iter = &h->sh.first; \ struct ssort_item **iter = &h->sh.first; \
while (*iter && *iter != &item->field.si) \ while (*iter && *iter != &item->field.si) \
iter = &(*iter)->next; \ iter = &(*iter)->next; \
if (!*iter) \ if (!*iter) \
return; \ return NULL; \
h->sh.count--; \ h->sh.count--; \
*iter = item->field.si.next; \ *iter = item->field.si.next; \
return item; \
} \ } \
macro_inline type *prefix ## _pop(struct prefix##_head *h) \ macro_inline type *prefix ## _pop(struct prefix##_head *h) \
{ \ { \
@ -485,7 +489,7 @@ macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
sitem = &item->field.si; \ sitem = &item->field.si; \
return container_of_null(sitem->next, type, field.si); \ return container_of_null(sitem->next, type, field.si); \
} \ } \
macro_pure size_t prefix ## _count(struct prefix##_head *h) \ macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
{ \ { \
return h->sh.count; \ return h->sh.count; \
} \ } \
@ -617,10 +621,10 @@ macro_inline type *prefix ## _find(struct prefix##_head *h, const type *item) \
} \ } \
return NULL; \ return NULL; \
} \ } \
macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \ macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
{ \ { \
if (!h->hh.tabshift) \ if (!h->hh.tabshift) \
return; \ return NULL; \
uint32_t hval = item->field.hi.hashval, hbits = HASH_KEY(h->hh, hval); \ uint32_t hval = item->field.hi.hashval, hbits = HASH_KEY(h->hh, hval); \
struct thash_item **np = &h->hh.entries[hbits]; \ struct thash_item **np = &h->hh.entries[hbits]; \
while (*np && (*np)->hashval < hval) \ while (*np && (*np)->hashval < hval) \
@ -628,12 +632,13 @@ macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \
while (*np && *np != &item->field.hi && (*np)->hashval == hval) \ while (*np && *np != &item->field.hi && (*np)->hashval == hval) \
np = &(*np)->next; \ np = &(*np)->next; \
if (*np != &item->field.hi) \ if (*np != &item->field.hi) \
return; \ return NULL; \
*np = item->field.hi.next; \ *np = item->field.hi.next; \
item->field.hi.next = NULL; \ item->field.hi.next = NULL; \
h->hh.count--; \ h->hh.count--; \
if (HASH_SHRINK_THRESHOLD(h->hh)) \ if (HASH_SHRINK_THRESHOLD(h->hh)) \
typesafe_hash_shrink(&h->hh); \ typesafe_hash_shrink(&h->hh); \
return item; \
} \ } \
macro_inline type *prefix ## _pop(struct prefix##_head *h) \ macro_inline type *prefix ## _pop(struct prefix##_head *h) \
{ \ { \
@ -675,7 +680,7 @@ macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
return NULL; \ return NULL; \
return prefix ## _next(h, item); \ return prefix ## _next(h, item); \
} \ } \
macro_pure size_t prefix ## _count(struct prefix##_head *h) \ macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
{ \ { \
return h->hh.count; \ return h->hh.count; \
} \ } \
@ -751,9 +756,11 @@ macro_inline type *prefix ## _find_lt(struct prefix##_head *h, \
&item->field.si, cmpfn_nuq); \ &item->field.si, cmpfn_nuq); \
return container_of_null(sitem, type, field.si); \ return container_of_null(sitem, type, field.si); \
} \ } \
macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \ macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
{ \ { \
typesafe_skiplist_del(&h->sh, &item->field.si, cmpfn_uq); \ struct sskip_item *sitem = typesafe_skiplist_del(&h->sh, \
&item->field.si, cmpfn_uq); \
return container_of_null(sitem, type, field.si); \
} \ } \
macro_inline type *prefix ## _pop(struct prefix##_head *h) \ macro_inline type *prefix ## _pop(struct prefix##_head *h) \
{ \ { \
@ -776,7 +783,7 @@ macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
next = item ? item->field.si.next[0] : NULL; \ next = item ? item->field.si.next[0] : NULL; \
return container_of_null(next, type, field.si); \ return container_of_null(next, type, field.si); \
} \ } \
macro_pure size_t prefix ## _count(struct prefix##_head *h) \ macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
{ \ { \
return h->sh.count; \ return h->sh.count; \
} \ } \
@ -848,8 +855,8 @@ extern struct sskip_item *typesafe_skiplist_find_lt(struct sskip_head *head,
const struct sskip_item *item, int (*cmpfn)( const struct sskip_item *item, int (*cmpfn)(
const struct sskip_item *a, const struct sskip_item *a,
const struct sskip_item *b)); const struct sskip_item *b));
extern void typesafe_skiplist_del(struct sskip_head *head, extern struct sskip_item *typesafe_skiplist_del(
struct sskip_item *item, int (*cmpfn)( struct sskip_head *head, struct sskip_item *item, int (*cmpfn)(
const struct sskip_item *a, const struct sskip_item *a,
const struct sskip_item *b)); const struct sskip_item *b));
extern struct sskip_item *typesafe_skiplist_pop(struct sskip_head *head); extern struct sskip_item *typesafe_skiplist_pop(struct sskip_head *head);

View File

@ -209,7 +209,7 @@ static void concat(test_, TYPE)(void)
assert(list_add(&head, &dummy) == &itm[j]); assert(list_add(&head, &dummy) == &itm[j]);
else { else {
assert(list_add(&head, &dummy) == NULL); assert(list_add(&head, &dummy) == NULL);
list_del(&head, &dummy); assert(list_del(&head, &dummy) != NULL);
} }
} }
ts_hashx("add-dup", "a538546a6e6ab0484e925940aa8dd02fd934408bbaed8cb66a0721841584d838"); ts_hashx("add-dup", "a538546a6e6ab0484e925940aa8dd02fd934408bbaed8cb66a0721841584d838");
@ -255,7 +255,7 @@ static void concat(test_, TYPE)(void)
list_first(&head) == &dummy); list_first(&head) == &dummy);
} else if (list_next(&head, &dummy)) } else if (list_next(&head, &dummy))
assert(list_next(&head, &dummy)->val > j); assert(list_next(&head, &dummy)->val > j);
list_del(&head, &dummy); assert(list_del(&head, &dummy) != NULL);
} }
ts_hash("add-dup+find_{lt,gteq}", "a538546a6e6ab0484e925940aa8dd02fd934408bbaed8cb66a0721841584d838"); ts_hash("add-dup+find_{lt,gteq}", "a538546a6e6ab0484e925940aa8dd02fd934408bbaed8cb66a0721841584d838");
#endif #endif
@ -295,7 +295,7 @@ static void concat(test_, TYPE)(void)
(void)prng_rand(prng); (void)prng_rand(prng);
j = prng_rand(prng) % NITEM; j = prng_rand(prng) % NITEM;
if (itm[j].scratchpad == 1) { if (itm[j].scratchpad == 1) {
list_del(&head, &itm[j]); assert(list_del(&head, &itm[j]) != NULL);
itm[j].scratchpad = 0; itm[j].scratchpad = 0;
l++; l++;
} }
@ -307,7 +307,7 @@ static void concat(test_, TYPE)(void)
assert(item->scratchpad != 0); assert(item->scratchpad != 0);
if (item->val & 1) { if (item->val & 1) {
list_del(&head, item); assert(list_del(&head, item) != NULL);
item->scratchpad = 0; item->scratchpad = 0;
l++; l++;
} }
@ -333,7 +333,7 @@ static void concat(test_, TYPE)(void)
for (i = 0; i < NITEM / 2; i++) { for (i = 0; i < NITEM / 2; i++) {
j = prng_rand(prng) % NITEM; j = prng_rand(prng) % NITEM;
if (itm[j].scratchpad == 1) { if (itm[j].scratchpad == 1) {
list_del(&head, &itm[j]); assert(list_del(&head, &itm[j]) != NULL);
itm[j].scratchpad = 0; itm[j].scratchpad = 0;
k--; k--;
} }
@ -371,7 +371,7 @@ static void concat(test_, TYPE)(void)
for (i = 0; i < NITEM / 2; i++) { for (i = 0; i < NITEM / 2; i++) {
j = prng_rand(prng) % NITEM; j = prng_rand(prng) % NITEM;
if (itm[j].scratchpad == 1) { if (itm[j].scratchpad == 1) {
list_del(&head, &itm[j]); assert(list_del(&head, &itm[j]) != NULL);
itm[j].scratchpad = 0; itm[j].scratchpad = 0;
k--; k--;
} }
@ -424,7 +424,7 @@ static void concat(test_, TYPE)(void)
item = &itm[j]; item = &itm[j];
if (item->scratchpad == 0) if (item->scratchpad == 0)
continue; continue;
list_del(&head, item); assert(list_del(&head, item) != NULL);
} }
item->scratchpad = 0; item->scratchpad = 0;
k--; k--;
@ -469,7 +469,7 @@ static void concat(test_, TYPE)(void)
item = &itm[j]; item = &itm[j];
if (item->scratchpad == 0) if (item->scratchpad == 0)
continue; continue;
list_del(&head, item); assert(list_del(&head, item) != NULL);
} }
item->scratchpad = 0; item->scratchpad = 0;
k--; k--;