Documentation ¶
Index ¶
- type Deque
- func (dq *Deque[V]) All() iter.Seq[V]
- func (dq *Deque[V]) Backwards() iter.Seq[V]
- func (dq *Deque[V]) Enumerate(start int) iter.Seq2[int, V]
- func (dq *Deque[V]) EnumerateBackwards(start int) iter.Seq2[int, V]
- func (dq *Deque[V]) IsEmpty() bool
- func (dq *Deque[V]) Len() int
- func (dq *Deque[V]) PeekBack() V
- func (dq *Deque[V]) PeekFront() V
- func (dq *Deque[V]) PopBack() V
- func (dq *Deque[V]) PopFront() V
- func (dq *Deque[V]) PushBack(val V)
- func (dq *Deque[V]) PushFront(val V)
- func (dq *Deque[V]) String() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Deque ¶
type Deque[V any] struct { // contains filtered or unexported fields }
Deque is a double-ended queue implementation
func NewDeque ¶
NewDeque creates a new deque. If vals are provided, they are added to the deque, and the deque is initialized.
Example ¶
package main import ( "fmt" dq "github.com/elordeiro/go/container/deque" ) func main() { d := dq.NewDeque(1, 2, 3) d.PushFront(0) d.PushBack(4) fmt.Println(d) }
Output: <->[0 1 2 3 4]
func (*Deque[V]) All ¶
All returns an iter.Seq[V] that yields all elements in the deque It returns a single use iterator
Example ¶
package main import ( "fmt" dq "github.com/elordeiro/go/container/deque" ) func main() { d := dq.NewDeque(1, 2, 3, 4) for v := range d.All() { fmt.Println(v) } }
Output: 1 2 3 4
func (*Deque[V]) Backwards ¶
Backwards returns an iter.Seq[V] that yields all elements in the deque in reverse order as if the deque was a stack. It returns a single use iterator
Example ¶
package main import ( "fmt" dq "github.com/elordeiro/go/container/deque" ) func main() { d := dq.NewDeque(1, 2, 3, 4) for v := range d.Backwards() { fmt.Println(v) } }
Output: 4 3 2 1
func (*Deque[V]) Enumerate ¶
Enumerate returns an iter.Seq2[int, V] that yields all elements in the deque along with their index starting from the provided start index. It returns a single use iterator
Example ¶
package main import ( "fmt" dq "github.com/elordeiro/go/container/deque" ) func main() { d := dq.NewDeque(1, 2, 3, 4) for i, v := range d.Enumerate(0) { fmt.Println(i, v) } }
Output: 0 1 1 2 2 3 3 4
func (*Deque[V]) EnumerateBackwards ¶
EnumerateBackwards returns an iter.Seq2[int, V] that yields all elements in the deque along with their index starting from the provided start index in reverse order as if the deque was a stack. It returns a single use iterator
Example ¶
package main import ( "fmt" dq "github.com/elordeiro/go/container/deque" ) func main() { d := dq.NewDeque(1, 2, 3, 4) for i, v := range d.EnumerateBackwards(0) { fmt.Println(i, v) } }
Output: 0 4 1 3 2 2 3 1
func (*Deque[V]) IsEmpty ¶
IsEmpty returns true if the deque is empty, false otherwise
Example ¶
package main import ( "fmt" dq "github.com/elordeiro/go/container/deque" ) func main() { d := dq.NewDeque[int]() fmt.Println(d.IsEmpty()) d.PushBack(1) fmt.Println(d.IsEmpty()) }
Output: true false
func (*Deque[V]) Len ¶
Len returns the number of elements in the deque
Example ¶
package main import ( "fmt" dq "github.com/elordeiro/go/container/deque" ) func main() { d := dq.NewDeque(1, 2, 3) fmt.Println(d.Len()) }
Output: 3
func (*Deque[V]) PeekBack ¶
func (dq *Deque[V]) PeekBack() V
PeekBack returns the element at the back of the deque without removing it.
Example ¶
package main import ( "fmt" dq "github.com/elordeiro/go/container/deque" ) func main() { d := dq.NewDeque(1, 2, 3) fmt.Println(d.PeekBack()) fmt.Println(d) }
Output: 3 <->[1 2 3]
func (*Deque[V]) PeekFront ¶
func (dq *Deque[V]) PeekFront() V
PeekFront returns the element at the front of the deque without removing it.
Example ¶
package main import ( "fmt" dq "github.com/elordeiro/go/container/deque" ) func main() { d := dq.NewDeque(1, 2, 3) fmt.Println(d.PeekFront()) fmt.Println(d) }
Output: 1 <->[1 2 3]
func (*Deque[V]) PopBack ¶
func (dq *Deque[V]) PopBack() V
PopBack removes and returns the element at the back of the deque. Panics if the deque is empty.
Example ¶
package main import ( "fmt" dq "github.com/elordeiro/go/container/deque" ) func main() { d := dq.NewDeque(1, 2, 3) fmt.Println(d.PopBack()) }
Output: 3
func (*Deque[V]) PopFront ¶
func (dq *Deque[V]) PopFront() V
PopFront removes and returns the element at the front of the deque. Panics if the deque is empty.
Example ¶
package main import ( "fmt" dq "github.com/elordeiro/go/container/deque" ) func main() { d := dq.NewDeque(1, 2, 3) fmt.Println(d.PopFront()) }
Output: 1
func (*Deque[V]) PushBack ¶
func (dq *Deque[V]) PushBack(val V)
PushBack adds an element to the back of the deque
Example ¶
package main import ( "fmt" dq "github.com/elordeiro/go/container/deque" ) func main() { d := dq.NewDeque(1, 2, 3) d.PushBack(4) fmt.Println(d) }
Output: <->[1 2 3 4]