Playground
deque.gno
// 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:
//
// // Unbounded deque
// d := deque.New()
// d.PushBack("a", "b", "c")
//
// // Bounded deque with automatic eviction
// d = deque.NewBounded(3)
// d.PushBack("a", "b", "c", "d") // "a" gets evicted
//
// first := d.PopFront() // Returns "b"
// last := d.Last() // Returns "d"
//
// // Iterate forward
// iter := d.All()
// iter(func(val any) bool {
// println(val)
// return true // continue
// })
//
// // Iterate backward
// iter = d.Backward()
// iter(func(val any) bool {
// println(val)
// return true // continue
// })
package deque
// Node represents a single element in the doubly-linked list
type Node struct {
value any
prev *Node
next *Node
}
// Deque represents a doubly-linked deque with optional size limits
type Deque struct {
head *Node
tail *Node
size int
max int // 0 = unlimited
}
// New creates a new unbounded deque.
func New() *Deque {
return &Deque{
max: 0, // unlimited
}
}
// NewBounded creates a new bounded deque with the specified maximum size.
// When the deque exceeds maxSize, elements are removed from the front.
func NewBounded(maxSize int) *Deque {
if maxSize <= 0 {
maxSize = 1
}
return &Deque{
max: maxSize,
}
}
// PushBack adds one or more elements to the back of the deque.
// If the deque exceeds maxSize, elements are removed from the front.
func (d *Deque) PushBack(items ...any) {
for _, item := range items {
d.pushBackSingle(item)
}
}
// pushBackSingle adds a single element to the back
func (d *Deque) pushBackSingle(item any) {
node := &Node{value: item}
if d.tail == nil {
// First element
d.head = node
d.tail = node
} else {
// Link to existing tail
d.tail.next = node
node.prev = d.tail
d.tail = node
}
d.size++
// Handle size limit by removing from front
if d.max > 0 && d.size > d.max {
d.popFrontNode()
}
}
// PopFront removes and returns the front element.
// Returns nil if the deque is empty.
func (d *Deque) PopFront() any {
if d.head == nil {
return nil
}
value := d.head.value
d.popFrontNode()
return value
}
// popFrontNode removes the front node
func (d *Deque) popFrontNode() {
if d.head == nil {
return
}
if d.head == d.tail {
// Only one element
d.head = nil
d.tail = nil
} else {
// Move head forward
d.head = d.head.next
d.head.prev = nil
}
d.size--
}
// PopBack removes and returns the back element.
// Returns nil if the deque is empty.
func (d *Deque) PopBack() any {
if d.tail == nil {
return nil
}
value := d.tail.value
d.popBackNode()
return value
}
// popBackNode removes the back node
func (d *Deque) popBackNode() {
if d.tail == nil {
return
}
if d.head == d.tail {
// Only one element
d.head = nil
d.tail = nil
} else {
// Move tail backward
d.tail = d.tail.prev
d.tail.next = nil
}
d.size--
}
// 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 (d *Deque) PushFront(items ...any) {
for _, item := range items {
d.pushFrontSingle(item)
}
}
// pushFrontSingle adds a single element to the front
func (d *Deque) pushFrontSingle(item any) {
node := &Node{value: item}
if d.head == nil {
// First element
d.head = node
d.tail = node
} else {
// Link to existing head
d.head.prev = node
node.next = d.head
d.head = node
}
d.size++
// Handle size limit by removing from back
if d.max > 0 && d.size > d.max {
d.popBackNode()
}
}
// Size returns the current number of elements
func (d *Deque) Size() int {
return d.size
}
// MaxSize returns the maximum size limit (0 = unlimited)
func (d *Deque) MaxSize() int {
return d.max
}
// IsEmpty returns true if the deque is empty
func (d *Deque) IsEmpty() bool {
return d.size == 0
}
// IsBounded returns true if the deque has a size limit
func (d *Deque) IsBounded() bool {
return d.max > 0
}
// First returns the front element without removing it.
// Returns nil if the deque is empty.
func (d *Deque) First() any {
if d.head == nil {
return nil
}
return d.head.value
}
// Last returns the back element without removing it.
// Returns nil if the deque is empty.
func (d *Deque) Last() any {
if d.tail == nil {
return nil
}
return d.tail.value
}
// Get returns the element at the specified index (0 = front).
// Returns nil if index is out of bounds.
func (d *Deque) Get(index int) any {
if index < 0 || index >= d.size {
return nil
}
node := d.head
for i := 0; i < index; i++ {
node = node.next
}
return node.value
}
// List returns all elements as a slice, ordered from front to back
func (d *Deque) List() []any {
if d.size == 0 {
return nil
}
result := make([]any, d.size)
node := d.head
for i := 0; i < d.size; i++ {
result[i] = node.value
node = node.next
}
return result
}
// Enumerate calls fn for each element with its index (0-based from front)
func (d *Deque) Enumerate(fn func(index int, value any) bool) {
node := d.head
for i := 0; i < d.size; i++ {
if fn(i, node.value) {
break
}
node = node.next
}
}
// Clear removes all elements from the deque
func (d *Deque) Clear() {
d.head = nil
d.tail = nil
d.size = 0
}
// SetMaxSize changes the maximum size limit.
// If the new limit is smaller than current size, elements are removed from front.
func (d *Deque) SetMaxSize(maxSize int) {
d.max = maxSize
if maxSize > 0 {
for d.size > maxSize {
d.popFrontNode()
}
}
}
// 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 (d *Deque) All() func(yield func(any) bool) {
return func(yield func(any) bool) {
current := d.head
for current != nil {
if !yield(current.value) {
return
}
current = current.next
}
}
}
// 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 (d *Deque) Backward() func(yield func(any) bool) {
return func(yield func(any) bool) {
current := d.tail
for current != nil {
if !yield(current.value) {
return
}
current = current.prev
}
}
}
deque_test.gno
package deque
import "testing"
func TestNew(t *testing.T) {
d := New()
if d.Size() != 0 {
t.Errorf("Expected size 0, got %d", d.Size())
}
if d.MaxSize() != 0 {
t.Errorf("Expected max size 0 (unlimited), got %d", d.MaxSize())
}
if d.IsBounded() {
t.Error("Expected unbounded deque")
}
}
func TestNewBounded(t *testing.T) {
d := NewBounded(5)
if d.MaxSize() != 5 {
t.Errorf("Expected max size 5, got %d", d.MaxSize())
}
if d.Size() != 0 {
t.Errorf("Expected size 0, got %d", d.Size())
}
if !d.IsBounded() {
t.Error("Expected bounded deque")
}
}
func TestPushBackPopFront(t *testing.T) {
d := New() // unbounded
// Test empty pop
if d.PopFront() != nil {
t.Error("Expected nil when popping from empty deque")
}
d.PushBack("a", "b", "c")
if d.Size() != 3 {
t.Errorf("Expected size 3, got %d", d.Size())
}
item := d.PopFront()
if item != "a" {
t.Errorf("Expected 'a', got %v", item)
}
if d.Size() != 2 {
t.Errorf("Expected size 2, got %d", d.Size())
}
}
func TestPushFrontPopBack(t *testing.T) {
d := New() // unbounded
d.PushFront("a", "b", "c")
if d.Size() != 3 {
t.Errorf("Expected size 3, got %d", d.Size())
}
// Elements should be in reverse order: [c, b, a]
if d.First() != "c" {
t.Errorf("Expected 'c' as first, got %v", d.First())
}
if d.Last() != "a" {
t.Errorf("Expected 'a' as last, got %v", d.Last())
}
item := d.PopBack()
if item != "a" {
t.Errorf("Expected 'a', got %v", item)
}
if d.Size() != 2 {
t.Errorf("Expected size 2, got %d", d.Size())
}
}
func TestBoundedEvictionBack(t *testing.T) {
d := NewBounded(3)
// Fill to capacity
d.PushBack("a", "b", "c")
if d.Size() != 3 {
t.Errorf("Expected size 3, got %d", d.Size())
}
// Push beyond capacity - should evict "a" from front
d.PushBack("d")
if d.Size() != 3 {
t.Errorf("Expected size 3, got %d", d.Size())
}
if d.First() != "b" {
t.Errorf("Expected 'b' (oldest after eviction), got %v", d.First())
}
if d.Last() != "d" {
t.Errorf("Expected 'd' (newest), got %v", d.Last())
}
}
func TestBoundedEvictionFront(t *testing.T) {
d := NewBounded(3)
// Fill to capacity
d.PushBack("a", "b", "c")
// Push to front beyond capacity - should evict "c" from back
d.PushFront("x")
if d.Size() != 3 {
t.Errorf("Expected size 3, got %d", d.Size())
}
if d.First() != "x" {
t.Errorf("Expected 'x' (newest at front), got %v", d.First())
}
if d.Last() != "b" {
t.Errorf("Expected 'b' (oldest after back eviction), got %v", d.Last())
}
}
func TestFirstLast(t *testing.T) {
d := New()
// Empty deque
if d.First() != nil {
t.Error("Expected nil for empty deque first")
}
if d.Last() != nil {
t.Error("Expected nil for empty deque last")
}
d.PushBack("x", "y", "z")
if d.First() != "x" {
t.Errorf("Expected 'x', got %v", d.First())
}
if d.Last() != "z" {
t.Errorf("Expected 'z', got %v", d.Last())
}
}
func TestGet(t *testing.T) {
d := New()
d.PushBack("a", "b", "c")
if d.Get(0) != "a" {
t.Errorf("Expected 'a' at index 0, got %v", d.Get(0))
}
if d.Get(1) != "b" {
t.Errorf("Expected 'b' at index 1, got %v", d.Get(1))
}
if d.Get(2) != "c" {
t.Errorf("Expected 'c' at index 2, got %v", d.Get(2))
}
if d.Get(3) != nil {
t.Error("Expected nil for out-of-bounds index")
}
if d.Get(-1) != nil {
t.Error("Expected nil for negative index")
}
}
func TestList(t *testing.T) {
d := New()
// Empty deque
list := d.List()
if list != nil {
t.Errorf("Expected nil for empty deque, got %v", list)
}
d.PushBack("x", "y", "z")
list = d.List()
if len(list) != 3 {
t.Errorf("Expected length 3, got %d", len(list))
}
if list[0] != "x" || list[1] != "y" || list[2] != "z" {
t.Errorf("Expected [x, y, z], got %v", list)
}
}
func TestEnumerate(t *testing.T) {
d := New()
d.PushBack("a", "b", "c")
var enumerated []string
var indices []int
d.Enumerate(func(index int, item any) bool {
indices = append(indices, index)
enumerated = append(enumerated, item.(string))
return false // continue
})
if len(enumerated) != 3 {
t.Errorf("Expected 3 enumerated items, got %d", len(enumerated))
}
if enumerated[0] != "a" || enumerated[1] != "b" || enumerated[2] != "c" {
t.Errorf("Expected [a, b, c], got %v", enumerated)
}
if indices[0] != 0 || indices[1] != 1 || indices[2] != 2 {
t.Errorf("Expected [0, 1, 2], got %v", indices)
}
}
func TestEnumerateEarlyStop(t *testing.T) {
d := New()
d.PushBack("a", "b", "c")
var enumerated []string
d.Enumerate(func(index int, item any) bool {
enumerated = append(enumerated, item.(string))
return item == "b" // stop after b
})
if len(enumerated) != 2 {
t.Errorf("Expected 2 enumerated items, got %d", len(enumerated))
}
if enumerated[0] != "a" || enumerated[1] != "b" {
t.Errorf("Expected [a, b], got %v", enumerated)
}
}
func TestSetMaxSize(t *testing.T) {
d := New()
d.PushBack("a", "b", "c", "d", "e")
// Reduce max size - should trim from front
d.SetMaxSize(3)
if d.Size() != 3 {
t.Errorf("Expected size 3 after reducing max size, got %d", d.Size())
}
if d.First() != "c" {
t.Errorf("Expected 'c' as first after trim, got %v", d.First())
}
// Increase max size
d.SetMaxSize(10)
if d.MaxSize() != 10 {
t.Errorf("Expected max size 10, got %d", d.MaxSize())
}
}
func TestClear(t *testing.T) {
d := New()
d.PushBack("a", "b", "c")
d.Clear()
if d.Size() != 0 {
t.Errorf("Expected size 0 after clear, got %d", d.Size())
}
if d.First() != nil {
t.Error("Expected nil first after clear")
}
if d.Last() != nil {
t.Error("Expected nil last after clear")
}
if !d.IsEmpty() {
t.Error("Expected deque to be empty after clear")
}
}
func TestUnboundedGrowth(t *testing.T) {
d := New() // unbounded
// Add many items
for i := 0; i < 100; i++ {
d.PushBack(i)
}
if d.Size() != 100 {
t.Errorf("Expected size 100, got %d", d.Size())
}
if d.First() != 0 {
t.Errorf("Expected 0 as first, got %v", d.First())
}
if d.Last() != 99 {
t.Errorf("Expected 99 as last, got %v", d.Last())
}
}
func TestMixedOperations(t *testing.T) {
d := NewBounded(4)
// Test mixed push front and back operations
d.PushBack("b", "c")
d.PushFront("a")
d.PushBack("d")
// Should have [a, b, c, d]
list := d.List()
if len(list) != 4 || list[0] != "a" || list[1] != "b" || list[2] != "c" || list[3] != "d" {
t.Errorf("Expected [a, b, c, d], got %v", list)
}
// Push beyond capacity
d.PushFront("x") // Should evict "d" from back
// Should have [x, a, b, c]
list = d.List()
if len(list) != 4 || list[0] != "x" || list[1] != "a" || list[2] != "b" || list[3] != "c" {
t.Errorf("Expected [x, a, b, c], got %v", list)
}
// Mixed pop operations
front := d.PopFront() // "x"
back := d.PopBack() // "c"
if front != "x" {
t.Errorf("Expected 'x' from front, got %v", front)
}
if back != "c" {
t.Errorf("Expected 'c' from back, got %v", back)
}
// Should have [a, b]
list = d.List()
if len(list) != 2 || list[0] != "a" || list[1] != "b" {
t.Errorf("Expected [a, b], got %v", list)
}
}
func TestAllIterator(t *testing.T) {
d := New()
d.PushBack("x", "y", "z")
var items []any
iter := d.All()
iter(func(value any) bool {
items = append(items, value)
return true // continue
})
if len(items) != 3 {
t.Errorf("Expected 3 items, got %d", len(items))
}
if items[0] != "x" || items[1] != "y" || items[2] != "z" {
t.Errorf("Expected [x, y, z], got %v", items)
}
}
func TestBackwardIterator(t *testing.T) {
d := New()
d.PushBack("x", "y", "z")
var items []any
iter := d.Backward()
iter(func(value any) bool {
items = append(items, value)
return true // continue
})
if len(items) != 3 {
t.Errorf("Expected 3 items, got %d", len(items))
}
if items[0] != "z" || items[1] != "y" || items[2] != "x" {
t.Errorf("Expected [z, y, x] (reverse), got %v", items)
}
}
func TestAllIteratorEarlyBreak(t *testing.T) {
d := New()
d.PushBack("a", "b", "c", "d", "e")
var items []any
iter := d.All()
iter(func(value any) bool {
items = append(items, value)
if value == "c" {
return false // stop
}
return true // continue
})
if len(items) != 3 {
t.Errorf("Expected 3 items after break, got %d", len(items))
}
if items[0] != "a" || items[1] != "b" || items[2] != "c" {
t.Errorf("Expected [a, b, c], got %v", items)
}
}
gnomod.toml
module = "gno.land/p/nym-moultest123/deque/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186404 max_deposit = "8000000ugnot"