From 5d45cd9e073a0bc62435b7057a270463b706051c Mon Sep 17 00:00:00 2001 From: Valient Gough Date: Sun, 9 Jun 2024 18:20:52 -0700 Subject: [PATCH] zero removed elements --- Taskfile.yml | 4 ++++ ring.go | 11 ++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index 3174a3a..3412f1a 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -11,3 +11,7 @@ tasks: cmds: - go test -fuzz=Fuzz -fuzztime 30s . + bench: + desc: Run benchmarks. + cmds: + - go test -bench=. -benchmem diff --git a/ring.go b/ring.go index 26b573d..fe44724 100644 --- a/ring.go +++ b/ring.go @@ -41,13 +41,14 @@ func (r *Ring[T]) PushBack(e T) bool { // PopFront removes and returns the first element in the ring. // If the ring is empty, it returns false. func (r *Ring[T]) PopFront() (T, bool) { + var zero T // right-hand side always contains the first element. if len(r.right) == 0 { - var zero T return zero, false } el := r.right[0] + r.right[0] = zero r.right = r.right[1:] if cap(r.right) == 0 { // right side is exhausted, so what was the left is now the right. @@ -68,8 +69,8 @@ func (r *Ring[T]) PopIndex(i int) (T, bool) { if i == 0 { return r.PopFront() } + var zero T if i < 0 || i >= r.Len() { - var zero T return zero, false } @@ -79,6 +80,7 @@ func (r *Ring[T]) PopIndex(i int) (T, bool) { // and the start of the left are adjacent (modulo ring size). el := r.left[idx] copy(r.left[idx:], r.left[idx+1:]) + r.left[len(r.left)-1] = zero r.left = r.left[:len(r.left)-1] return el, true } @@ -89,6 +91,7 @@ func (r *Ring[T]) PopIndex(i int) (T, bool) { el := r.right[i] updated := r.right[1:] copy(updated, r.right[:i]) + r.right[0] = zero r.right = updated return el, true } @@ -136,10 +139,8 @@ func (r *Ring[T]) Cap() int { // It returns the number of elements copied. // This does not consume elements from the ring. func (r *Ring[T]) Copy(out []T) int { - n := min(len(out), r.Len()) idx := copy(out, r.right) - copy(out[idx:], r.left) - return n + return idx + copy(out[idx:], r.left) } // Reset removes all elements from the ring.