diff options
author | Tucker Evans <tucker@tuckerevans.com> | 2020-05-30 20:35:05 -0400 |
---|---|---|
committer | Tucker Evans <tucker@tuckerevans.com> | 2020-05-30 20:35:05 -0400 |
commit | b93a7b87b55a42ca864325ae91328a5627885471 (patch) | |
tree | 78d1c79fca3de9a431e957cc5efa4aa1f0c64d6e /collections | |
parent | 10cd9eadf3f14d4b25ef85ea563938a784fcf20c (diff) |
Add remove index function for double ended queue.
Diffstat (limited to 'collections')
-rw-r--r-- | collections/double_ended_queue.c | 26 | ||||
-rw-r--r-- | collections/double_ended_queue.h | 3 |
2 files changed, 28 insertions, 1 deletions
diff --git a/collections/double_ended_queue.c b/collections/double_ended_queue.c index 7938307..0348cfa 100644 --- a/collections/double_ended_queue.c +++ b/collections/double_ended_queue.c @@ -184,6 +184,32 @@ deq *root; return root->base[root->end]; } +void deq_remove(root, index) +deq *root; +int index; +{ + if (!root || index > root->limit + || (index >= root->end && index < root->beg)) + return; + + index = (root->beg + index) % root->limit; + if (index >= root->end) + return NULL; + + root->base[index] = NULL; + + if (root->beg < root->end || index >= root->beg) { + root->beg = memmove(root->base + root->beg + 1, + root->base + root->beg, + (index - root->beg) * sizeof(void*)); + ++root->beg; + } else { + memmove(root->base + index, root->base + index + 1, + (--root->end - index) * sizeof(void*)); + } + +} + /* Note: Elements are not freed * deq_clear should be called before if they are no longer needed. */ diff --git a/collections/double_ended_queue.h b/collections/double_ended_queue.h index 7f11f66..bfc9d20 100644 --- a/collections/double_ended_queue.h +++ b/collections/double_ended_queue.h @@ -33,12 +33,13 @@ void deq_truncate(deq*, int); void* deq_front(deq*); void* deq_back(deq*); +void remove(deq*, int); + /* * resevee * push back * swap_rm_front/back * insert - * remove */ #endif |