-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
371 lines (341 loc) · 9.49 KB
/
map.go
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package treemap // import "jsouthworth.net/go/immutable/treemap"
import (
"errors"
"fmt"
"reflect"
"strings"
"jsouthworth.net/go/dyn"
"jsouthworth.net/go/seq"
)
var errOddElements = errors.New("must supply an even number elements")
var errRangeSig = errors.New("Range requires a function: func(k kT, v vT) bool or func(k kT, v vT)")
// Entry is a map entry. Each entry consists of a key and value.
type Entry interface {
Key() interface{}
Value() interface{}
}
// EntryNew returns an Entry
func EntryNew(key, value interface{}) Entry {
return entry{key, value}
}
// Map is a persistent immutable map based on Red/Black
// trees. Operations on map returns a new map that shares much of the
// structure with the original map.
type Map struct {
compare cmpFunc
root tree
count int
}
var empty = Map{
compare: dyn.Compare,
root: &leaf{cmp: dyn.Compare},
count: 0,
}
type mapOptions struct {
compare cmpFunc
}
// Option is a type that allows changes to pluggable parts of the
// Map implementation.
type Option func(*mapOptions)
// Compare is an option to the Empty function that will allow
// one to specify a different comparison operator instead
// of the default which is from the dyn library.
func Compare(cmp func(k1, k2 interface{}) int) Option {
return func(o *mapOptions) {
o.compare = cmp
}
}
// Empty returns a new empty persistent map, one may supply options
// for the map by using one of the option generating functions and
// providing that to Empty.
func Empty(options ...Option) *Map {
var opts mapOptions
for _, opt := range options {
opt(&opts)
}
if opts.compare == nil {
return &empty
}
return &Map{
compare: opts.compare,
root: &leaf{cmp: opts.compare},
count: 0,
}
}
// New converts a list of elements to a persistent map
// by associating them pairwise. New will panic if the
// number of elements is not even.
func New(elems ...interface{}) *Map {
return newWithOptions(elems)
}
func newWithOptions(elems []interface{}, options ...Option) *Map {
if len(elems)%2 != 0 {
panic(errOddElements)
}
out := Empty(options...)
for i := 0; i < len(elems); i += 2 {
out = out.Assoc(elems[i], elems[i+1])
}
return out
}
// From will convert many different go types to an immutable map.
// Converting some types is more efficient than others and the mechanisms
// are described below.
//
// *Map:
// Returned directly as it is already immutable.
// map[interface{}]interface{}:
// Converted directly by looping over the map and calling Assoc starting with an empty transient map. The transient map is the converted to a persistent one and returned.
// []Entry:
// The entries are looped over and Assoc is called on an empty transient map. The transient map is converted to a persistent map and then returned.
// []interface{}:
// The elements are passed to New.
// map[kT]vT:
// Reflection is used to loop over the entries of the map and associate them with an empty transient map. The transient map is converted to a persistent map and then returned.
// []T:
// Reflection is used to convert the slice to []interface{} and then passed to New.
func From(value interface{}, options ...Option) *Map {
switch v := value.(type) {
case *Map:
return v
case map[interface{}]interface{}:
out := Empty(options...)
for key, val := range v {
out = out.Assoc(key, val)
}
return out
case []Entry:
out := Empty(options...)
for _, entry := range v {
out = out.Assoc(entry.Key(), entry.Value())
}
return out
case []interface{}:
return newWithOptions(v, options...)
default:
return mapFromReflection(value)
}
}
func mapFromReflection(value interface{}, options ...Option) *Map {
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Map:
out := Empty(options...)
for _, key := range v.MapKeys() {
val := v.MapIndex(key)
out = out.Assoc(key.Interface(), val.Interface())
}
return out
case reflect.Slice:
sl := make([]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
elem := v.Index(i)
sl[i] = elem.Interface()
}
return newWithOptions(sl, options...)
default:
return Empty(options...)
}
}
// At returns the value associated with the key.
// If one is not found, nil is returned.
func (m *Map) At(key interface{}) interface{} {
ent, ok := get(m.root, key)
if !ok {
return nil
}
return ent.value
}
// EntryAt returns the entry (key, value pair) of the key.
// If one is not found, nil is returned.
func (m *Map) EntryAt(key interface{}) Entry {
v, ok := get(m.root, key)
if !ok {
return nil
}
return v
}
// Contains will test if the key exists in the map.
func (m *Map) Contains(key interface{}) bool {
_, ok := get(m.root, key)
return ok
}
// Find will return the value for a key if it exists in the map and
// whether the key exists in the map. For non-nil values, exists will
// always be true.
func (m *Map) Find(key interface{}) (value interface{}, exists bool) {
return get(m.root, key)
}
// Assoc associates a value with a key in the map.
// A new persistent map is returned if the key and value
// are different from one already in the map, if the entry
// is already in the map the original map is returned.
func (m *Map) Assoc(key, value interface{}) *Map {
root, added := insert(m.root, key, value)
switch {
case root == m.root:
return m
case added:
return &Map{
compare: m.compare,
root: root,
count: m.count + 1,
}
default:
return &Map{
compare: m.compare,
root: root,
count: m.count,
}
}
}
// Conj associates a value with a key in the map.
func (m *Map) Conj(elem interface{}) interface{} {
entry := elem.(Entry)
return m.Assoc(entry.Key(), entry.Value())
}
// Delete removes a key and associated value from the map.
func (m *Map) Delete(key interface{}) *Map {
root := _delete(m.root, key)
if root == m.root {
return m
}
return &Map{
compare: m.compare,
root: root,
count: m.count - 1,
}
}
// Length returns the number of entries in the map.
func (m *Map) Length() int {
return m.count
}
// Range will loop over the entries in the Map and call 'do' on each entry.
// The 'do' function may be of many types:
//
// func(key, value interface{}) bool:
// Takes empty interfaces and returns if the loop should continue.
// Useful to avoid reflection or for hetrogenous maps.
// func(key, value interface{}):
// Takes empty interfaces.
// Useful to avoid reflection or for hetrogenous maps.
// func(entry Entry) bool:
// Takes the Entry type and returns if the loop should continue
// Is called directly and avoids entry unpacking if not necessary.
// func(entry Entry):
// Takes the Entry type.
// Is called directly and avoids entry unpacking if not necessary.
// func(k kT, v vT) bool
// Takes a key of key type and a value of value type and returns if the loop should contiune.
// Is called with reflection and will panic if the kT and vT types are incorrect.
// func(k kT, v vT)
// Takes a key of key type and a value of value type.
// Is called with reflection and will panic if the kT and vT types are incorrect.
// Range will panic if passed anything not matching these signatures.
func (m *Map) Range(do interface{}) {
s := seq.Seq(m)
if s == nil {
return
}
var cont = true
fn := genRangeFunc(do)
for s != nil && cont {
entry := seq.First(s).(Entry)
cont = fn(entry)
s = seq.Seq(seq.Next(s))
}
}
func genRangeFunc(do interface{}) func(Entry) bool {
switch fn := do.(type) {
case func(key, value interface{}) bool:
return func(entry Entry) bool {
return fn(entry.Key(), entry.Value())
}
case func(key, value interface{}):
return func(entry Entry) bool {
fn(entry.Key(), entry.Value())
return true
}
case func(e Entry) bool:
return fn
case func(e Entry):
return func(entry Entry) bool {
fn(entry)
return true
}
default:
rv := reflect.ValueOf(do)
if rv.Kind() != reflect.Func {
panic(errRangeSig)
}
rt := rv.Type()
if rt.NumIn() != 2 || rt.NumOut() > 1 {
panic(errRangeSig)
}
if rt.NumOut() == 1 &&
rt.Out(0).Kind() != reflect.Bool {
panic(errRangeSig)
}
return func(entry Entry) bool {
out := dyn.Apply(do, entry.Key(), entry.Value())
if out != nil {
return out.(bool)
}
return true
}
}
}
// Seq returns a seralized sequence of Entry
// corresponding to the maps entries.
func (m *Map) Seq() seq.Sequence {
if _, isLeaf := m.root.(*leaf); isLeaf {
return nil
}
return sequenceNew(m.root)
}
// String returns a string representation of the map.
func (m *Map) String() string {
var b strings.Builder
fmt.Fprint(&b, "{ ")
m.Range(func(entry Entry) {
fmt.Fprintf(&b, "%s ", entry)
})
fmt.Fprint(&b, "}")
return b.String()
}
// AsNative returns the map converted to a go native map type.
func (m *Map) AsNative() map[interface{}]interface{} {
out := make(map[interface{}]interface{})
m.Range(func(key, val interface{}) {
out[key] = val
})
return out
}
// Equal tests if two maps are Equal by comparing the entries of each.
// Equal implements the Equaler which allows for deep
// comparisons when there are maps of maps
func (m *Map) Equal(o interface{}) bool {
other, ok := o.(*Map)
if !ok {
return ok
}
if m.Length() != other.Length() {
return false
}
foundAll := true
m.Range(func(key, value interface{}) bool {
if !dyn.Equal(other.At(key), value) {
foundAll = false
return false
}
return true
})
return foundAll
}
// Apply takes an arbitrary number of arguments and returns the
// value At the first argument. Apply allows map to be called
// as a function by the 'dyn' library.
func (m *Map) Apply(args ...interface{}) interface{} {
k := args[0]
return m.At(k)
}