From 86083e3a9257367d444751771c01f496dc40fc39 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 19 May 2018 11:21:18 +0200 Subject: [PATCH] Add methods push_move & pop_move. Rewrite _pop_back to be a little bit faster for complex structure as it used DO_MOVE instead of plain _set. --- m-list.h | 31 ++++++++++++++++++++++++++++--- tests/test-mlist.c | 11 ++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/m-list.h b/m-list.h index 2b94824b..67afc736 100644 --- a/m-list.h +++ b/m-list.h @@ -76,7 +76,9 @@ IT_REMOVE(M_C(name,_remove)), \ CLEAN(M_C(name,_clean)), \ PUSH(M_C(name,_push_back)), \ - POP(M_C(name,_pop_back)) \ + POP(M_C(name,_pop_back)), \ + PUSH_MOVE(M_C(name,_push_move)), \ + POP_MOVE(M_C(name,_pop_move)) \ ,REVERSE(M_C(name,_reverse)) \ ,OPLIST(oplist) \ ,M_IF_METHOD(GET_STR, oplist)(GET_STR(M_C(name, _get_str)),) \ @@ -217,9 +219,32 @@ LISTI_CONTRACT(v); \ assert(*v != NULL); \ if (data != NULL) { \ - M_GET_SET oplist(*data, (*v)->data); \ + M_DO_MOVE (oplist, *data, (*v)->data); \ + } else { \ + M_GET_CLEAR oplist((*v)->data); \ } \ - M_GET_CLEAR oplist((*v)->data); \ + struct M_C(name, _s) *tofree = *v; \ + *v = (*v)->next; \ + M_C(name,_int_del)(tofree); \ + LISTI_CONTRACT(v); \ + } \ + \ + static inline void \ + M_C(name, _push_move)(list_t v, type *x) \ + { \ + assert (x != NULL); \ + type *data = M_C(name, _push_raw)(v); \ + if (M_UNLIKELY (data == NULL)) \ + return; \ + M_DO_INIT_MOVE (oplist, *data, *x); \ + } \ + \ + static inline void \ + M_C(name, _pop_move)(type *data, list_t v) \ + { \ + LISTI_CONTRACT(v); \ + assert(*v != NULL && data != NULL); \ + M_DO_INIT_MOVE (oplist, *data, (*v)->data); \ struct M_C(name, _s) *tofree = *v; \ *v = (*v)->next; \ M_C(name,_int_del)(tofree); \ diff --git a/tests/test-mlist.c b/tests/test-mlist.c index 7cecec4e..4da44c59 100644 --- a/tests/test-mlist.c +++ b/tests/test-mlist.c @@ -180,7 +180,16 @@ static void test_uint(void) assert (!list_uint_equal_p(v,v2)); assert (list_uint_hash(v) != 0); - + + x = 1478963; + s = *list_uint_back(v); + list_uint_push_move(v, &x); + assert (*list_uint_back(v) == 1478963); + x = 0; + list_uint_pop_move(&x, v); + assert (x == 1478963); + assert (s == *list_uint_back(v)); + list_uint_clear(v); list_uint_clear(v2); }