Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

v2 source pure

Package deque provides a doubly-linked deque with optional size limits, optimized for minimal storage updates and O(1...

Readme View source

gno.land/p/moul/deque/v1

TODO: describe this package.


Part of moul/gno-contracts — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage.

⚠️ Disclaimer: provided as-is, without warranty; not security-audited. Full disclaimer: DISCLAIMER.

Overview

Package deque provides a doubly-linked deque with optional size limits, optimized for minimal storage updates and O(1) operations in Gno environments.

This implementation uses a doubly-linked list structure that provides: - O(1) push back operations - O(1) pop front operations - O(1) access to first/last elements - No array resizing or slice shifting - Minimal allocations (one node per element) - Minimal storage updates (typically 1-2 pointer updates per operation) - Optional max-size eviction policy

Optimized for Gno blockchain storage: each operation updates only specific nodes rather than entire data structures, making it ideal for storage-efficient lists that need simple read operations like first/last access, size checks, list enumeration, and iteration with minimal storage update overhead.

Example usage:

Example
 1// Unbounded deque
 2d := deque.New()
 3d.PushBack("a", "b", "c")
 4
 5// Bounded deque with automatic eviction
 6d = deque.NewBounded(3)
 7d.PushBack("a", "b", "c", "d")  // "a" gets evicted
 8
 9first := d.PopFront()  // Returns "b"
10last := d.Last()       // Returns "d"
11
12// Iterate forward
13iter := d.All()
14iter(func(val any) bool {
15    println(val)
16    return true // continue
17})
18
19// Iterate backward
20iter = d.Backward()
21iter(func(val any) bool {
22    println(val)
23    return true // continue
24})

Functions 2

func New

1func New() *Deque
source

New creates a new unbounded deque.

func NewBounded

1func NewBounded(maxSize int) *Deque
source

NewBounded creates a new bounded deque with the specified maximum size. When the deque exceeds maxSize, elements are removed from the front.

Types 2

type Deque

struct
1type Deque struct {
2	head *Node
3	tail *Node
4	size int
5	max  int // 0 = unlimited
6}
source

Deque represents a doubly-linked deque with optional size limits

Methods on Deque

func All

method on Deque
1func (d *Deque) All() func(yield func(any) bool)
source

All returns an iterator function for forward traversal (Go 1.23+ compatible) Note: range-over-func syntax is not yet supported in Gno, use iter() directly

func Backward

method on Deque
1func (d *Deque) Backward() func(yield func(any) bool)
source

Backward returns an iterator function for backward traversal (Go 1.23+ compatible) Note: range-over-func syntax is not yet supported in Gno, use iter() directly

func Clear

method on Deque
1func (d *Deque) Clear()
source

Clear removes all elements from the deque

func Enumerate

method on Deque
1func (d *Deque) Enumerate(fn func(index int, value any) bool)
source

Enumerate calls fn for each element with its index (0-based from front)

func First

method on Deque
1func (d *Deque) First() any
source

First returns the front element without removing it. Returns nil if the deque is empty.

func Get

method on Deque
1func (d *Deque) Get(index int) any
source

Get returns the element at the specified index (0 = front). Returns nil if index is out of bounds.

func IsBounded

method on Deque
1func (d *Deque) IsBounded() bool
source

IsBounded returns true if the deque has a size limit

func IsEmpty

method on Deque
1func (d *Deque) IsEmpty() bool
source

IsEmpty returns true if the deque is empty

func Last

method on Deque
1func (d *Deque) Last() any
source

Last returns the back element without removing it. Returns nil if the deque is empty.

func List

method on Deque
1func (d *Deque) List() []any
source

List returns all elements as a slice, ordered from front to back

func MaxSize

method on Deque
1func (d *Deque) MaxSize() int
source

MaxSize returns the maximum size limit (0 = unlimited)

func PopBack

method on Deque
1func (d *Deque) PopBack() any
source

PopBack removes and returns the back element. Returns nil if the deque is empty.

func PopFront

method on Deque
1func (d *Deque) PopFront() any
source

PopFront removes and returns the front element. Returns nil if the deque is empty.

func PushBack

method on Deque
1func (d *Deque) PushBack(items ...any)
source

PushBack adds one or more elements to the back of the deque. If the deque exceeds maxSize, elements are removed from the front.

func PushFront

method on Deque
1func (d *Deque) PushFront(items ...any)
source

PushFront adds one or more elements to the front of the deque. If bounded and size limit is exceeded, elements are removed from the back.

func SetMaxSize

method on Deque
1func (d *Deque) SetMaxSize(maxSize int)
source

SetMaxSize changes the maximum size limit. If the new limit is smaller than current size, elements are removed from front.

func Size

method on Deque
1func (d *Deque) Size() int
source

Size returns the current number of elements

type Node

struct
1type Node struct {
2	value any
3	prev  *Node
4	next  *Node
5}
source

Node represents a single element in the doubly-linked list

Source Files 3