aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Liodden <[email protected]>2018-01-03 00:29:57 +0100
committerErik Liodden <[email protected]>2018-01-03 00:29:57 +0100
commitf8218b7db8de9004c68e1c21b3ca8ef99e83179d (patch)
treee237b0704a302890ac445def514bd930d28c669e
parent314fcabf2f6bb0f0e851ce4d28e78c9628a674ec (diff)
downloadalgdat-f8218b7db8de9004c68e1c21b3ca8ef99e83179d.tar.gz
queue: add queue peek
reveal the next element in the queue without removing it from the queue itself. similar to dequeue.
-rw-r--r--queue.c7
-rw-r--r--structure.h1
2 files changed, 7 insertions, 1 deletions
diff --git a/queue.c b/queue.c
index b34dd56..b9e5eec 100644
--- a/queue.c
+++ b/queue.c
@@ -56,11 +56,16 @@ void queue_enqueue(struct queue *q, void *elem_addr)
q->log_length++;
}
-void queue_dequeue(struct queue *q, void *elem_addr)
+void queue_peek(struct queue *q, void *elem_addr)
{
assert(q->log_length != 0);
void *source = (char *)q->elems + (q->head * q->elem_size);
memcpy(elem_addr, source, q->elem_size);
+}
+
+void queue_dequeue(struct queue *q, void *elem_addr)
+{
+ queue_peek(q, elem_addr);
q->head = (q->head + 1) % q->alloc_length;
q->log_length--;
}
diff --git a/structure.h b/structure.h
index c8e6743..d73eb22 100644
--- a/structure.h
+++ b/structure.h
@@ -50,6 +50,7 @@ struct queue {
void queue_init(struct queue *q, size_t elem_size, void (*freefn)(void *));
void queue_dispose(struct queue *q);
void queue_enqueue(struct queue *q, void *elem_addr);
+void queue_peek(struct queue *q, void *elem_addr);
void queue_dequeue(struct queue *q, void *elem_addr);
int queue_length(struct queue *q);