From: David Given Date: Thu, 2 Feb 2017 21:57:17 +0000 (+0100) Subject: Tuples can be removed from a map by index. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=708662baf7d4a65c79fbc5785f67441c6ea59ce3;p=ack.git Tuples can be removed from a map by index. --- diff --git a/modules/src/data/pmap.c b/modules/src/data/pmap.c index 5eaadc2dd..bbc41e7ea 100644 --- a/modules/src/data/pmap.c +++ b/modules/src/data/pmap.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "pmap.h" static void append(void* mapp, void* left, void* right) @@ -73,6 +74,16 @@ void pmap_add_bi(void* mapp, void* left, void* right) append(map, left, right); } +void pmap_remove_index(void* mapp, int index) +{ + struct pmap* map = mapp; + struct pmap_node* node = &map->item[index]; + + assert((index >= 0) && (index < map->count)); + memmove(node, node+1, sizeof(*node) * (map->count - index - 1)); + map->count--; +} + void pmap_remove(void* mapp, void* left, void* right) { struct pmap* map = mapp; @@ -83,8 +94,7 @@ void pmap_remove(void* mapp, void* left, void* right) struct pmap_node* node = &map->item[i]; if ((node->left == left) && (node->right == right)) { - memmove(node, node+1, sizeof(*node) * (map->count - i - 1)); - map->count--; + pmap_remove_index(map, i); return; } } diff --git a/modules/src/data/pmap.h b/modules/src/data/pmap.h index 9d4ec3326..3921143c0 100644 --- a/modules/src/data/pmap.h +++ b/modules/src/data/pmap.h @@ -26,6 +26,7 @@ struct pmap extern void pmap_put(void* map, void* left, void* right); extern void pmap_add(void* map, void* left, void* right); extern void pmap_add_bi(void* map, void* left, void* right); +extern void pmap_remove_index(void* map, int index); extern void pmap_remove(void* map, void* left, void* right); extern void pmap_remove_bi(void* map, void* left, void* right); extern void pmap_remove_either(void* map, void* either);