aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tucker@tuckerevans.com>2020-05-25 00:47:06 -0400
committerTucker Evans <tuckerevans24@gmail.com>2020-05-26 01:50:10 -0400
commit56f138f1de82d4bf7e21282f115fbcb63a6fe4d4 (patch)
tree499013dc6b5fd9f593a21188de9d25be48426648
parent8860413709853bf1daede5e2c95d502791952b7b (diff)
Fix index function for double ended queue.
Double ended queue's did not account for pushing in-front of the base i.e. a truly circular buffer, this is the start to fixing that across all functions.
-rw-r--r--collections/double_ended_queue.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/collections/double_ended_queue.c b/collections/double_ended_queue.c
index 967a716..88d4641 100644
--- a/collections/double_ended_queue.c
+++ b/collections/double_ended_queue.c
@@ -117,11 +117,18 @@ void* deq_index(root, index)
deq *root;
int index;
{
- if (!root || root->beg + index >= root->end) {
+ void *tmp;
+
+ if (!root) {
+ return NULL;
+ }
+
+ tmp = root->base + (root->beg + index - root->base) % root->limit);
+ if (tmp > root->end) {
return NULL;
}
- return root->beg[index];
+ return *tmp;
}
void* deq_pop_back(root)