deque

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Index

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

func NewDeque[V any](vals ...V) *Deque[V]

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

func (dq *Deque[V]) All() iter.Seq[V]

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

func (dq *Deque[V]) Backwards() iter.Seq[V]

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

func (dq *Deque[V]) Enumerate(start int) iter.Seq2[int, V]

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

func (dq *Deque[V]) EnumerateBackwards(start int) iter.Seq2[int, V]

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

func (dq *Deque[V]) IsEmpty() bool

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

func (dq *Deque[V]) Len() int

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]

func (*Deque[V]) PushFront

func (dq *Deque[V]) PushFront(val V)

PushFront adds an element to the front of the deque

Example
package main

import (
	"fmt"

	dq "github.com/elordeiro/go/container/deque"
)

func main() {
	d := dq.NewDeque(1, 2, 3)
	d.PushFront(0)
	fmt.Println(d)
}
Output:

<->[0 1 2 3]

func (*Deque[V]) String

func (dq *Deque[V]) String() string

String returns a string representation of the deque

Example
package main

import (
	"fmt"

	dq "github.com/elordeiro/go/container/deque"
)

func main() {
	d := dq.NewDeque(1, 2, 3, 4)
	fmt.Println(d)
}
Output:

<->[1 2 3 4]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL