瀏覽代碼

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 5 年之前
父節點
當前提交
213864a50c
共有 1 個文件被更改,包括 3 次插入0 次删除
  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);
 	item := queue_get(q^, 0);
 	q.offset = (q.offset + 1) % array_len(q.data);
 	q.offset = (q.offset + 1) % array_len(q.data);
 	q.len -= 1;
 	q.len -= 1;
+	if q.len == 0 {
+		q.offset = 0;
+	}
 	return item;
 	return item;
 }
 }