aboutsummaryrefslogtreecommitdiff
path: root/stmutil/containers.go
blob: 0cc592dd07fb0241631d11f70e671c28eab5a117 (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
183
184
185
186
187
188
189
190
191
192
package stmutil

import (
	"unsafe"

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

// This is the type constraint for keys passed through from github.com/benbjohnson/immutable.
type KeyConstraint interface {
	comparable
}

type Settish[K KeyConstraint] interface {
	Add(K) Settish[K]
	Delete(K) Settish[K]
	Contains(K) bool
	Range(func(K) bool)
	iter.Iterable
	Len() int
}

type mapToSet[K KeyConstraint] struct {
	m Mappish[K, struct{}]
}

type interhash[K KeyConstraint] struct{}

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

func (interhash[K]) Equal(i, j K) bool {
	return i == j
}

func NewSet[K KeyConstraint]() Settish[K] {
	return mapToSet[K]{NewMap[K, struct{}]()}
}

func NewSortedSet[K KeyConstraint](lesser lessFunc[K]) Settish[K] {
	return mapToSet[K]{NewSortedMap[K, struct{}](lesser)}
}

func (s mapToSet[K]) Add(x K) Settish[K] {
	s.m = s.m.Set(x, struct{}{})
	return s
}

func (s mapToSet[K]) Delete(x K) Settish[K] {
	s.m = s.m.Delete(x)
	return s
}

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

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

func (s mapToSet[K]) Range(f func(K) bool) {
	s.m.Range(func(k K, _ struct{}) bool {
		return f(k)
	})
}

func (s mapToSet[K]) Iter(cb iter.Callback) {
	s.Range(func(k K) bool {
		return cb(k)
	})
}

type Map[K KeyConstraint, V any] struct {
	*immutable.Map[K, V]
}

func NewMap[K KeyConstraint, V any]() Mappish[K, V] {
	return Map[K, V]{immutable.NewMap[K, V](interhash[K]{})}
}

func (m Map[K, V]) Delete(x K) Mappish[K, V] {
	m.Map = m.Map.Delete(x)
	return m
}

func (m Map[K, V]) Set(key K, value V) Mappish[K, V] {
	m.Map = m.Map.Set(key, value)
	return m
}

func (sm Map[K, V]) Range(f func(K, V) bool) {
	iter := sm.Map.Iterator()
	for {
		k, v, ok := iter.Next()
		if !ok {
			break
		}
		if !f(k, v) {
			return
		}
	}
}

func (sm Map[K, V]) Iter(cb iter.Callback) {
	sm.Range(func(key K, _ V) bool {
		return cb(key)
	})
}

type SortedMap[K KeyConstraint, V any] struct {
	*immutable.SortedMap[K, V]
}

func (sm SortedMap[K, V]) Set(key K, value V) Mappish[K, V] {
	sm.SortedMap = sm.SortedMap.Set(key, value)
	return sm
}

func (sm SortedMap[K, V]) Delete(key K) Mappish[K, V] {
	sm.SortedMap = sm.SortedMap.Delete(key)
	return sm
}

func (sm SortedMap[K, V]) Range(f func(key K, value V) bool) {
	iter := sm.SortedMap.Iterator()
	for {
		k, v, ok := iter.Next()
		if !ok {
			break
		}
		if !f(k, v) {
			return
		}
	}
}

func (sm SortedMap[K, V]) Iter(cb iter.Callback) {
	sm.Range(func(key K, _ V) bool {
		return cb(key)
	})
}

type lessFunc[T KeyConstraint] func(l, r T) bool

type comparer[K KeyConstraint] struct {
	less lessFunc[K]
}

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

func NewSortedMap[K KeyConstraint, V any](less lessFunc[K]) Mappish[K, V] {
	return SortedMap[K, V]{
		SortedMap: immutable.NewSortedMap[K, V](comparer[K]{less}),
	}
}

type Mappish[K, V any] interface {
	Set(K, V) Mappish[K, V]
	Delete(key K) Mappish[K, V]
	Get(key K) (V, bool)
	Range(func(K, V) 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
}