diff --git a/deque.go b/deque.go index aa9d34e..7194962 100644 --- a/deque.go +++ b/deque.go @@ -117,6 +117,17 @@ func (q *Deque) At(i int) interface{} { return q.buf[(q.head+i)&(len(q.buf)-1)] } +// Set puts the element at index i in the queue. Set shares the same purpose +// than At() but perform the opposite operation. The index i is the same +// index defined by At(). If the index is invalid, the call panics. +func (q *Deque) Set(i int, elem interface{}) { + if i < 0 || i >= q.count { + panic("deque: Set() called with index out of range") + } + // bitwise modulus + q.buf[(q.head+i)&(len(q.buf)-1)] = elem +} + // Clear removes all elements from the queue, but retains the current capacity. // This is useful when repeatedly reusing the queue at high frequency to avoid // GC during reuse. The queue will not be resized smaller as long as items are