Removed At/Set, added Reset
This commit is contained in:
parent
8e6da63be2
commit
ef54e74e1e
38
deque.go
38
deque.go
|
@ -141,37 +141,6 @@ func (q *Deque) Back() int {
|
|||
return q.buf[q.prev(q.tail)]
|
||||
}
|
||||
|
||||
// At returns the element at index i in the queue without removing the element
|
||||
// from the queue. This method accepts only non-negative index values. At(0)
|
||||
// refers to the first element and is the same as Front(). At(Len()-1) refers
|
||||
// to the last element and is the same as Back(). If the index is invalid, the
|
||||
// call panics.
|
||||
//
|
||||
// The purpose of At is to allow Deque to serve as a more general purpose
|
||||
// circular buffer, where items are only added to and removed from the ends of
|
||||
// the deque, but may be read from any place within the deque. Consider the
|
||||
// case of a fixed-size circular log buffer: A new entry is pushed onto one end
|
||||
// and when full the oldest is popped from the other end. All the log entries
|
||||
// in the buffer must be readable without altering the buffer contents.
|
||||
func (q *Deque) At(i int) int {
|
||||
if i < 0 || i >= q.count {
|
||||
panic("deque: At() called with index out of range")
|
||||
}
|
||||
// bitwise modulus
|
||||
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 int) {
|
||||
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
|
||||
|
@ -188,6 +157,13 @@ func (q *Deque) Clear() {
|
|||
q.count = 0
|
||||
}
|
||||
|
||||
// Reset sets properties to empty, but does not rewrite the buffer
|
||||
func (q *Deque) Reset() {
|
||||
q.head = 0
|
||||
q.tail = 0
|
||||
q.count = 0
|
||||
}
|
||||
|
||||
// Rotate rotates the deque n steps front-to-back. If n is negative, rotates
|
||||
// back-to-front. Having Deque provide Rotate() avoids resizing that could
|
||||
// happen if implementing rotation using only Pop and Push methods.
|
||||
|
|
Loading…
Reference in a new issue