diff options
author | Tucker Evans <tucker@tuckerevans.com> | 2020-06-10 23:37:15 -0400 |
---|---|---|
committer | Tucker Evans <tucker@tuckerevans.com> | 2020-06-10 23:46:57 -0400 |
commit | 277e14127bbe1a5080a2a88b6ee3ef94859bc3a5 (patch) | |
tree | a5d35861acfa3efa9ef29911529c4fd9bb6d14ba /collections/double_ended_queue.c | |
parent | 0f7085dedd2f60f2ccc0b1f49558f5be89de97b2 (diff) |
Add insert function to double ended queue.
Diffstat (limited to 'collections/double_ended_queue.c')
-rw-r--r-- | collections/double_ended_queue.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/collections/double_ended_queue.c b/collections/double_ended_queue.c index bf329ca..4de6a27 100644 --- a/collections/double_ended_queue.c +++ b/collections/double_ended_queue.c @@ -173,6 +173,44 @@ void *item; root->base[(root->beg + index) % root->limit] = item; } +void deq_insert(root, index, item) +deq *root; +int index; +void *item; +{ + if (!root) + return; + + if (index > deq_size(root)) + return; + + if (root->end >= root->limit) + deq_resize(root); + + index = (root->beg + index) % root->limit; + + if (root->beg < root->end) { + memmove(root->base + index + 1, root->base + index, + (root->end - index) * sizeof(void*)); + ++root->end; + root->end = root->end % root->limit; + } else if (index < root->end) { + memmove(root->base + index + 1, + root->base + index, + (root->end - index) * sizeof(void*)); + ++root->end; + } else { + memmove(root->base + root->beg - 1, root->base + root->beg, + (index - root->beg) * sizeof(void*)); + --root->beg; + } + + if (root->end == root->beg) + root->end = root->limit; + + root->base[index] = item; +} + void* deq_pop_front(root) deq *root; { |