func NewReadonlySet
NewReadonlySet returns a read-only view of s.
Package addrset provides a specialized set data structure for managing unique Gno addresses.
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:
Example usage:
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}
NewReadonlySet returns a read-only view of s.
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.
Has reports whether addr is in the underlying set.
1func (r ReadonlySet) IterateByOffset(offset, count int, fn func(addr address) bool) (stopped bool)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.
1func (r ReadonlySet) ReverseIterateByOffset(offset, count int, fn func(addr address) bool) (stopped bool)ReverseIterateByOffset is IterateByOffset in reverse (descending) order.
Size returns the number of addresses in the underlying set.
Add inserts an address into the set. Returns true if the address was newly added, false if it already existed.
Has checks if an address exists in the set.
IterateByOffset walks through addresses starting at the given offset. The callback should return true to stop iteration.
Readonly returns a read-only view of the set.
Remove deletes an address from the set. Returns true if the address was found and removed, false if it didn't exist.
ReverseIterateByOffset walks through addresses in reverse order starting at the given offset. The callback should return true to stop iteration.
Size returns the number of addresses in the set.
Tree returns the underlying AVL tree for advanced usage.