diff --git a/container/deque/deque.go b/container/deque/deque.go index cbad94a..ce20b3d 100644 --- a/container/deque/deque.go +++ b/container/deque/deque.go @@ -175,6 +175,15 @@ func (d *Deque[T]) Item(i int) T { return d.a[idx] } +// Set sets the ith item in the deque. 0 is the front and d.Len()-1 is the back. +func (d *Deque[T]) Set(i int, t T) { + if i < 0 || i >= d.Len() { + panic("deque index out of range") + } + idx := (d.front + i) % len(d.a) + d.a[idx] = t +} + func positiveMod(l, d int) int { x := l % d if x < 0 { diff --git a/container/deque/deque_test.go b/container/deque/deque_test.go index b49ccbf..e3d83c1 100644 --- a/container/deque/deque_test.go +++ b/container/deque/deque_test.go @@ -55,9 +55,12 @@ func FuzzDeque(f *testing.F) { func() { if len(oracle) == 0 { t.Log("Front() should panic") - defer func() { recover() }() - deque.Front() - t.FailNow() + func() { + defer func() { recover() }() + deque.Front() + t.FailNow() + }() + return } oracleItem := oracle[0] t.Logf("Front() -> %#v", oracleItem) @@ -67,9 +70,12 @@ func FuzzDeque(f *testing.F) { func() { if len(oracle) == 0 { t.Log("Back() should panic") - defer func() { recover() }() - deque.Back() - t.FailNow() + func() { + defer func() { recover() }() + deque.Back() + t.FailNow() + }() + return } oracleItem := oracle[len(oracle)-1] t.Logf("Back() -> %#v", oracleItem) @@ -79,15 +85,32 @@ func FuzzDeque(f *testing.F) { func(i int) { if i < 0 || i >= len(oracle) { t.Logf("Item(%d) should panic", i) - defer func() { recover() }() - deque.Item(i) - t.FailNow() + func() { + defer func() { recover() }() + deque.Item(i) + t.FailNow() + }() + return } oracleItem := oracle[i] t.Logf("Item(%d) -> %#v", i, oracleItem) dequeItem := deque.Item(i) require2.Equal(t, oracleItem, dequeItem) }, + func(i int, x byte) { + if i < 0 || i >= len(oracle) { + t.Logf("Set(%d, x) should panic", i) + func() { + defer func() { recover() }() + deque.Item(i) + t.FailNow() + }() + return + } + t.Logf("Set(%d, %d)", i, x) + oracle[i] = x + deque.Set(i, x) + }, func() { t.Log("Iterate()") oracleAll := oracle