Browse Source

Reuse container.Queue capacity when calling pop_front()

Currently, the Queue will never reuse it's full capacity if you call `pop_front`, even if you empty it before pushing more items.

With this change, if you empty the Queue with `pop_front`, then the offset will be set back to the start of the underlying array when you pop the last item.
Future pushes will then reuse the already-allocated--but now empty--space.
Tetralux 4 years ago
parent
commit
213864a50c
1 changed files with 3 additions and 0 deletions
  1. 3 0
      core/container/queue.odin

+ 3 - 0
core/container/queue.odin

@@ -115,6 +115,9 @@ queue_pop_front :: proc(q: ^$Q/Queue($T)) -> T {
 	item := queue_get(q^, 0);
 	q.offset = (q.offset + 1) % array_len(q.data);
 	q.len -= 1;
+	if q.len == 0 {
+		q.offset = 0;
+	}
 	return item;
 }