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

addrset source pure

Package addrset provides a specialized set data structure for managing unique Gno addresses.

Overview

Package addrset provides a specialized set data structure for managing unique Gno addresses.

It is built on top of an AVL tree for efficient operations and maintains addresses in sorted order. This package is particularly useful when you need to:

  • Track a collection of unique addresses (e.g., for whitelists, participants, etc.)
  • Efficiently check address membership
  • Support pagination when displaying addresses

Example usage:

Example
 1import (
 2    "gno.land/p/moul/addrset"
 3)
 4
 5func MyHandler() {
 6    // Create a new address set
 7    var set addrset.Set
 8
 9    // Add some addresses
10    addr1 := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
11    addr2 := address("g1sss5g0rkqr88k4u648yd5d3l9t4d8vvqwszqth")
12
13    set.Add(addr1)  // returns true (newly added)
14    set.Add(addr2)  // returns true (newly added)
15    set.Add(addr1)  // returns false (already exists)
16
17    // Check membership
18    if set.Has(addr1) {
19        // addr1 is in the set
20    }
21
22    // Get size
23    size := set.Size()  // returns 2
24
25    // Iterate with pagination (10 items per page, starting at offset 0)
26    set.IterateByOffset(0, 10, func(addr address) bool {
27        // Process addr
28        return false  // continue iteration
29    })
30
31    // Remove an address
32    set.Remove(addr1)  // returns true (was present)
33    set.Remove(addr1)  // returns false (not present)
34}

Functions 1

func NewReadonlySet

1func NewReadonlySet(s *Set) *ReadonlySet
source

NewReadonlySet returns a read-only view of s.

Types 2

type ReadonlySet

struct
1type ReadonlySet struct {
2	set *Set
3}
source

ReadonlySet is a read-only view of a *Set. Cross-package callers cannot mutate the underlying set through this type: it exposes no mutator methods and holds the *Set in an unexported field, so a foreign realm can neither reach the set nor invoke Add/Remove on it.

A ReadonlySet is a thin handle over the live Set (it does not copy or snapshot), so reads through it always reflect the Set's current contents.

Methods on ReadonlySet

func Has

method on ReadonlySet
1func (r ReadonlySet) Has(addr address) bool
source

Has reports whether addr is in the underlying set.

func IterateByOffset

method on ReadonlySet
1func (r ReadonlySet) IterateByOffset(offset, count int, fn func(addr address) bool) (stopped bool)
source

IterateByOffset walks the underlying set in sorted order, starting at offset and visiting up to count addresses. fn returns true to stop early; IterateByOffset returns true if iteration was stopped that way.

The wrapped Set.IterateByOffset has no return value, so the "stopped" result is synthesized from the last callback return via a closure-captured local.

func ReverseIterateByOffset

method on ReadonlySet
1func (r ReadonlySet) ReverseIterateByOffset(offset, count int, fn func(addr address) bool) (stopped bool)
source

ReverseIterateByOffset is IterateByOffset in reverse (descending) order.

func Size

method on ReadonlySet
1func (r ReadonlySet) Size() int
source

Size returns the number of addresses in the underlying set.

type Set

struct
1type Set struct {
2	tree avl.Tree
3}
source

Methods on Set

func Add

method on Set
1func (s *Set) Add(addr address) bool
source

Add inserts an address into the set. Returns true if the address was newly added, false if it already existed.

func Has

method on Set
1func (s *Set) Has(addr address) bool
source

Has checks if an address exists in the set.

func IterateByOffset

method on Set
1func (s *Set) IterateByOffset(offset int, count int, cb func(addr address) bool)
source

IterateByOffset walks through addresses starting at the given offset. The callback should return true to stop iteration.

func Readonly

method on Set
1func (s *Set) Readonly() *ReadonlySet
source

Readonly returns a read-only view of the set.

func Remove

method on Set
1func (s *Set) Remove(addr address) bool
source

Remove deletes an address from the set. Returns true if the address was found and removed, false if it didn't exist.

func ReverseIterateByOffset

method on Set
1func (s *Set) ReverseIterateByOffset(offset int, count int, cb func(addr address) bool)
source

ReverseIterateByOffset walks through addresses in reverse order starting at the given offset. The callback should return true to stop iteration.

func Size

method on Set
1func (s *Set) Size() int
source

Size returns the number of addresses in the set.

func Tree

method on Set
1func (s *Set) Tree() avl.ITree
source

Tree returns the underlying AVL tree for advanced usage.

Imports 1

Source Files 5