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

readonly.gno

1.84 Kb · 56 lines
 1package addrset
 2
 3// ReadonlySet is a read-only view of a *Set. Cross-package callers cannot
 4// mutate the underlying set through this type: it exposes no mutator
 5// methods and holds the *Set in an unexported field, so a foreign realm
 6// can neither reach the set nor invoke Add/Remove on it.
 7//
 8// A ReadonlySet is a thin handle over the live Set (it does not copy or
 9// snapshot), so reads through it always reflect the Set's current contents.
10type ReadonlySet struct {
11	set *Set
12}
13
14// NewReadonlySet returns a read-only view of s.
15func NewReadonlySet(s *Set) *ReadonlySet {
16	return &ReadonlySet{set: s}
17}
18
19// Readonly returns a read-only view of the set.
20func (s *Set) Readonly() *ReadonlySet {
21	return NewReadonlySet(s)
22}
23
24// Has reports whether addr is in the underlying set.
25func (r ReadonlySet) Has(addr address) bool {
26	return r.set.Has(addr)
27}
28
29// Size returns the number of addresses in the underlying set.
30func (r ReadonlySet) Size() int {
31	return r.set.Size()
32}
33
34// IterateByOffset walks the underlying set in sorted order, starting at
35// offset and visiting up to count addresses. fn returns true to stop early;
36// IterateByOffset returns true if iteration was stopped that way.
37//
38// The wrapped Set.IterateByOffset has no return value, so the "stopped"
39// result is synthesized from the last callback return via a
40// closure-captured local.
41func (r ReadonlySet) IterateByOffset(offset, count int, fn func(addr address) bool) (stopped bool) {
42	r.set.IterateByOffset(offset, count, func(a address) bool {
43		stopped = fn(a)
44		return stopped
45	})
46	return stopped
47}
48
49// ReverseIterateByOffset is IterateByOffset in reverse (descending) order.
50func (r ReadonlySet) ReverseIterateByOffset(offset, count int, fn func(addr address) bool) (stopped bool) {
51	r.set.ReverseIterateByOffset(offset, count, func(a address) bool {
52		stopped = fn(a)
53		return stopped
54	})
55	return stopped
56}