aboutsummaryrefslogtreecommitdiff
path: root/stmutil/containers.go
blob: c7a4a4916e16995b947d85e61030d1feb2e6382a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package stmutil

import (
	"unsafe"

	"github.com/benbjohnson/immutable"

	"github.com/anacrolix/missinggo/v2/iter"
)

type Settish interface {
	Add(any) Settish
	Delete(any) Settish
	Contains(any) bool
	Range(func(any) bool)
	iter.Iterable
	Len() int
}

type mapToSet struct {
	m Mappish
}

type interhash struct{}

func (interhash) Hash(x any) uint32 {
	return uint32(nilinterhash(unsafe.Pointer(&x), 0))
}

func (interhash) Equal(i, j any) bool {
	return i == j
}

func NewSet() Settish {
	return mapToSet{NewMap()}
}

func NewSortedSet(lesser lessFunc) Settish {
	return mapToSet{NewSortedMap(lesser)}
}

func (s mapToSet) Add(x any) Settish {
	s.m = s.m.Set(x, nil)
	return s
}

func (s mapToSet) Delete(x any) Settish {
	s.m = s.m.Delete(x)
	return s
}

func (s mapToSet) Len() int {
	return s.m.Len()
}

func (s mapToSet) Contains(x any) bool {
	_, ok := s.m.Get(x)
	return ok
}

func (s mapToSet) Range(f func(any) bool) {
	s.m.Range(func(k, _ any) bool {
		return f(k)
	})
}

func (s mapToSet) Iter(cb iter.Callback) {
	s.Range(cb)
}

type Map struct {
	*immutable.Map
}

func NewMap() Mappish {
	return Map{immutable.NewMap(interhash{})}
}

var _ Mappish = Map{}

func (m Map) Delete(x any) Mappish {
	m.Map = m.Map.Delete(x)
	return m
}

func (m Map) Set(key, value any) Mappish {
	m.Map = m.Map.Set(key, value)
	return m
}

func (sm Map) Range(f func(key, value any) bool) {
	iter := sm.Map.Iterator()
	for !iter.Done() {
		if !f(iter.Next()) {
			return
		}
	}
}

func (sm Map) Iter(cb iter.Callback) {
	sm.Range(func(key, _ any) bool {
		return cb(key)
	})
}

type SortedMap struct {
	*immutable.SortedMap
}

func (sm SortedMap) Set(key, value any) Mappish {
	sm.SortedMap = sm.SortedMap.Set(key, value)
	return sm
}

func (sm SortedMap) Delete(key any) Mappish {
	sm.SortedMap = sm.SortedMap.Delete(key)
	return sm
}

func (sm SortedMap) Range(f func(key, value any) bool) {
	iter := sm.SortedMap.Iterator()
	for !iter.Done() {
		if !f(iter.Next()) {
			return
		}
	}
}

func (sm SortedMap) Iter(cb iter.Callback) {
	sm.Range(func(key, _ any) bool {
		return cb(key)
	})
}

type lessFunc func(l, r any) bool

type comparer struct {
	less lessFunc
}

func (me comparer) Compare(i, j any) int {
	if me.less(i, j) {
		return -1
	} else if me.less(j, i) {
		return 1
	} else {
		return 0
	}
}

func NewSortedMap(less lessFunc) Mappish {
	return SortedMap{
		SortedMap: immutable.NewSortedMap(comparer{less}),
	}
}

type Mappish interface {
	Set(key, value any) Mappish
	Delete(key any) Mappish
	Get(key any) (any, bool)
	Range(func(_, _ any) bool)
	Len() int
	iter.Iterable
}

func GetLeft(l, _ any) any {
	return l
}

//go:noescape
//go:linkname nilinterhash runtime.nilinterhash
func nilinterhash(p unsafe.Pointer, h uintptr) uintptr

func interfaceHash(x any) uint32 {
	return uint32(nilinterhash(unsafe.Pointer(&x), 0))
}

type Lenner interface {
	Len() int
}

type List = *immutable.List