aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tucker@tuckerevans.com>2020-06-10 12:34:11 -0400
committerTucker Evans <tucker@tuckerevans.com>2020-06-10 12:34:11 -0400
commitfb65c9cc91fe5bcf75e1519bd73d31242fbccb9d (patch)
treeadd11fd6cd09b44095c15acb1a8b0ac6558c48f0
parent92b0bac22ab3f3feeb48feb013753961581c7615 (diff)
Add bounds checking macro for double ended queue.
-rw-r--r--collections/double_ended_queue.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/collections/double_ended_queue.c b/collections/double_ended_queue.c
index e06ba36..f8f59e4 100644
--- a/collections/double_ended_queue.c
+++ b/collections/double_ended_queue.c
@@ -5,10 +5,15 @@
#include <stdio.h>
+/*Checks bounds for index (y) in queue*/
+#define DEQ_IN_BOUNDS(x, y) (y < deq_size(x)) \
+
#define START_SIZE 64;
/*TODO add returned error codes for current void functions
* (resize, push, etc.)
+ *
+ * Should all functions return a status code and take in a dest argument?
*/
/* Double ended queue as a circular buffer
@@ -184,6 +189,9 @@ void* deq_swap_rm_front(root, index)
deq *root;
int index;
{
+ if (!root || !DEQ_IN_BOUNDS(root,index))
+ return NULL;
+
deq_swap(root, 0, index);
return deq_pop_front(root);
}
@@ -192,6 +200,9 @@ void* deq_swap_rm_back(root, index)
deq *root;
int index;
{
+ if (!root || !DEQ_IN_BOUNDS(root,index))
+ return NULL;
+
deq_swap(root,deq_size(root) - 1,index);
return deq_pop_back(root);
}
@@ -200,16 +211,11 @@ void deq_remove(root, index)
deq *root;
int index;
{
- if (!root || index > root->limit)
+ if (!root || !DEQ_IN_BOUNDS(root,index));
return;
index = (root->beg + index) % root->limit;
- /*Bounds check*/
- if ((root->beg <= root->end && index < root->beg || index >= root->end)
- || (index >= root->end && index < root->beg))
- return;
-
root->base[index] = NULL;
if (root->beg < root->end || index >= root->beg) {