Documentation ¶
Overview ¶
The deque package implements an efficient double-ended queue data structure called Deque.
Usage:
d := deque.New() d.PushFront("foo") d.PushBack("bar") d.PushBack("123") l := d.Len() // l == 3 v, ok := d.PopFront() // v.(string) == "foo", ok == true v, ok = d.PopFront() // v.(string) == "bar", ok == true v, ok = d.PopBack() // v.(string) == "123", ok == true v, ok = d.PopBack() // v == nil, ok == false v, ok = d.PopFront() // v == nil, ok == false l = d.Len() // l == 0
A discussion of the internals can be found at the top of deque.go.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Deque ¶
type Deque struct {
// contains filtered or unexported fields
}
Deque implements an efficient double-ended queue.
Internally it is composed of a doubly-linked list (list.List) of blocks. Each block is a slice that holds 0 to blockLen items. The Deque starts with one block. Blocks are added to the front and back when the edge blocks fill up as items are pushed onto the deque. Edge blocks are removed when blocks become empty as items are popped off the Deque.
Only the front and back blocks may contain less than blockLen items. The first element in the Deque is d.blocks.Front().Value[d.frontIdx]. The last element in the Deque is d.blocks.Back().Value[d.backIdx].
This approach is more efficient than using a standard doubly-linked list for a queue because memory allocation is only required every blockLen items, instead of for each pushed item. Conversely, fewer memory deallocations are required when popping items. Bookkeeping overhead per item is also reduced.
func NewWithMaxLen ¶
New returns a new Deque instance which is limited to a certain length. Pushes which cause the length to exceed the specified size will cause an item to be dropped from the opposing side.
A maxLen of 0 means that there is no maximum length limit in place.
func (*Deque) PopBack ¶
PopBack removes an item from the back of the queue and returns it. The returned flag is true unless there were no items left in the queue.
func (*Deque) PopFront ¶
PopFront removes an item from the front of the queue and returns it. The returned flag is true unless there were no items left in the queue.