func New
New creates a new unbounded deque.
Package deque provides a doubly-linked deque with optional size limits, optimized for minimal storage updates and O(1...
gno.land/p/moul/deque/v1TODO: 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.
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:
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})
Deque represents a doubly-linked deque with optional size limits
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
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
Clear removes all elements from the deque
Enumerate calls fn for each element with its index (0-based from front)
First returns the front element without removing it. Returns nil if the deque is empty.
Get returns the element at the specified index (0 = front). Returns nil if index is out of bounds.
IsBounded returns true if the deque has a size limit
IsEmpty returns true if the deque is empty
Last returns the back element without removing it. Returns nil if the deque is empty.
List returns all elements as a slice, ordered from front to back
MaxSize returns the maximum size limit (0 = unlimited)
PopBack removes and returns the back element. Returns nil if the deque is empty.
PopFront removes and returns the front element. Returns nil if the deque is empty.
PushBack adds one or more elements to the back of the deque. If the deque exceeds maxSize, elements are removed from the front.
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.
SetMaxSize changes the maximum size limit. If the new limit is smaller than current size, elements are removed from front.
Size returns the current number of elements
Node represents a single element in the doubly-linked list