-
Notifications
You must be signed in to change notification settings - Fork 3
/
map_str.go
361 lines (312 loc) · 9.99 KB
/
map_str.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
package frozen
import (
"encoding/json"
"fmt"
"github.com/arr-ai/hash"
"github.com/arr-ai/frozen/errors"
"github.com/arr-ai/frozen/internal/depth"
"github.com/arr-ai/frozen/internal/fu"
"github.com/arr-ai/frozen/internal/iterator/skvi"
"github.com/arr-ai/frozen/internal/tree/skvt"
"github.com/arr-ai/frozen/internal/value"
"github.com/arr-ai/frozen/pkg/kv/skv"
)
// StringMapBuilder provides a more efficient way to build Maps incrementally.
type StringMapBuilder struct {
tb skvt.Builder
}
func NewStringMapBuilder(capacity int) *StringMapBuilder {
return &StringMapBuilder{tb: *skvt.NewBuilder(capacity)}
}
// Count returns the number of entries in the StringMap under construction.
func (b *StringMapBuilder) Count() int {
return b.tb.Count()
}
// Put adds or changes an entry into the StringMap under construction.
func (b *StringMapBuilder) Put(key string, value interface{}) {
b.tb.Add(skvt.DefaultNPKeyCombineArgs, skv.KV(key, value))
}
// Remove removes an entry from the StringMap under construction.
func (b *StringMapBuilder) Remove(key string) {
b.tb.Remove(skvt.DefaultNPKeyEqArgs, skv.KV(key, nil))
}
func (b *StringMapBuilder) Has(v string) bool {
_, has := b.Get(v)
return has
}
// Get returns the value for key from the StringMap under construction or false if
// not found.
func (b *StringMapBuilder) Get(key string) (interface{}, bool) {
if entry := b.tb.Get(skvt.DefaultNPKeyEqArgs, skv.KV(key, nil)); entry != nil {
return entry.Value, true
}
return nil, false
}
// Finish returns a StringMap containing all entries added since the StringMapBuilder was
// initialised or the last call to Finish.
func (b *StringMapBuilder) Finish() StringMap {
return newStringMap(b.tb.Finish())
}
func StringKeyEqual(a, b interface{}) bool {
return value.Equal(a.(KeyValue).Key, b.(KeyValue).Key)
}
// StringMap maps keys to values. The zero value is the empty StringMap.
type StringMap struct {
tree skvt.Tree
}
var _ value.Key = StringMap{}
func newStringMap(tree skvt.Tree) StringMap {
return StringMap{tree: tree}
}
// NewStringMap creates a new StringMap with kvs as keys and values.
func NewStringMap(kvs ...skv.KeyValue) StringMap {
var b StringMapBuilder
for _, skv := range kvs {
b.Put(skv.Key, skv.Value)
}
return b.Finish()
}
// NewStringMapFromKeys creates a new StringMap in which values are computed from keys.
func NewStringMapFromKeys(keys Set, f func(key string) interface{}) StringMap {
var b StringMapBuilder
for i := keys.Range(); i.Next(); {
val := i.Value().(string)
b.Put(val, f(val))
}
return b.Finish()
}
// NewStringMapFromGoMap takes a map[interface{}]interface{} and returns a frozen StringMap from it.
func NewStringMapFromGoMap(m map[string]interface{}) StringMap {
mb := NewStringMapBuilder(len(m))
for k, v := range m {
mb.Put(k, v)
}
return mb.Finish()
}
// IsEmpty returns true if the StringMap has no entries.
func (m StringMap) IsEmpty() bool {
return m.tree.Count() == 0
}
// Count returns the number of entries in the StringMap.
func (m StringMap) Count() int {
return m.tree.Count()
}
// Any returns an arbitrary entry from the StringMap.
func (m StringMap) Any() (key, value interface{}) {
for i := m.Range(); i.Next(); {
return i.Entry()
}
panic("empty map")
}
// With returns a new StringMap with key associated with val and all other keys
// retained from m.
func (m StringMap) With(key string, val interface{}) StringMap {
kv := skv.KV(key, val)
return newStringMap(m.tree.With(skvt.DefaultNPKeyCombineArgs, kv))
}
// Without returns a new StringMap with all keys retained from m except the elements
// of keys.
func (m StringMap) Without(keys Set) StringMap {
args := skvt.NewEqArgs(
m.tree.Gauge(), skvt.KeyEqual, skvt.KeyHash, skvt.KeyHash)
for i := keys.Range(); i.Next(); {
m.tree = m.tree.Without(args, skv.KV(i.Value().(string), nil))
}
return m
// TODO: Reinstate parallelisable implementation below.
// return newStringMap(m.tree.Difference(args, keys.tree))
}
// Without2 shoves keys into a Set and calls m.Without.
func (m StringMap) Without2(keys ...string) StringMap {
var sb SetBuilder
for _, key := range keys {
sb.Add(key)
}
return m.Without(sb.Finish())
}
// Has returns true iff the key exists in the map.
func (m StringMap) Has(key string) bool {
return m.tree.Get(skvt.DefaultNPKeyEqArgs, skv.KV(key, nil)) != nil
}
// Get returns the value associated with key in m and true iff the key is found.
func (m StringMap) Get(key string) (interface{}, bool) {
if skv := m.tree.Get(skvt.DefaultNPKeyEqArgs, skv.KV(key, nil)); skv != nil {
return skv.Value, true
}
return nil, false
}
// MustGet returns the value associated with key in m or panics if the key is
// not found.
func (m StringMap) MustGet(key string) interface{} {
if val, has := m.Get(key); has {
return val
}
panic(fmt.Sprintf("key not found: %v", key))
}
// GetElse returns the value associated with key in m or deflt if the key is not
// found.
func (m StringMap) GetElse(key string, deflt interface{}) interface{} {
if val, has := m.Get(key); has {
return val
}
return deflt
}
// GetElseFunc returns the value associated with key in m or the result of
// calling deflt if the key is not found.
func (m StringMap) GetElseFunc(key string, deflt func() interface{}) interface{} {
if val, has := m.Get(key); has {
return val
}
return deflt()
}
// Keys returns a Set with all the keys in the StringMap.
func (m StringMap) Keys() Set {
var b SetBuilder
for i := m.Range(); i.Next(); {
b.Add(i.Key())
}
return b.Finish()
}
// Values returns a Set with all the Values in the StringMap.
func (m StringMap) Values() Set {
var b SetBuilder
for i := m.Range(); i.Next(); {
b.Add(i.Value())
}
return b.Finish()
}
// Project returns a StringMap with only keys included from this StringMap.
func (m StringMap) Project(keys Set) StringMap {
return m.Where(func(key string, val interface{}) bool {
return keys.Has(key)
})
}
// Where returns a StringMap with only key-value pairs satisfying pred.
func (m StringMap) Where(pred func(key string, val interface{}) bool) StringMap {
var b StringMapBuilder
for i := m.Range(); i.Next(); {
if key, val := i.Entry(); pred(key, val) {
b.Put(key, val)
}
}
return b.Finish()
}
// StringMap returns a StringMap with keys from this StringMap, but the values replaced by the
// result of calling f.
func (m StringMap) StringMap(f func(key, val interface{}) interface{}) StringMap {
var b StringMapBuilder
for i := m.Range(); i.Next(); {
key, val := i.Entry()
b.Put(key, f(key, val))
}
return b.Finish()
}
// Reduce returns the result of applying f to each key-value pair on the StringMap.
// The result of each call is used as the acc argument for the next element.
func (m StringMap) Reduce(f func(acc, key, val interface{}) interface{}, acc interface{}) interface{} {
for i := m.Range(); i.Next(); {
acc = f(acc, i.Key(), i.Value())
}
return acc
}
func (m StringMap) EqArgs() *skvt.EqArgs {
return skvt.NewEqArgs(depth.NewGauge(m.Count()), skvt.KeyEqual, skvt.KeyHash, skvt.KeyHash)
}
// Merge returns a map from the merging between two maps, should there be a key overlap,
// the value that corresponds to key will be replaced by the value resulted from the
// provided resolve function.
func (m StringMap) Merge(n StringMap, resolve func(key string, a, b interface{}) interface{}) StringMap {
extractAndResolve := func(a, b skv.KeyValue) skv.KeyValue {
return skv.KV(a.Key, resolve(a.Key, a.Value, b.Value))
}
args := skvt.NewCombineArgs(m.EqArgs(), extractAndResolve)
return newStringMap(m.tree.Combine(args, n.tree))
}
// Update returns a StringMap with key-value pairs from n added or replacing existing
// keys.
func (m StringMap) Update(n StringMap) StringMap {
f := skvt.UseRHS
if m.Count() > n.Count() {
m, n = n, m
f = skvt.UseLHS
}
args := skvt.NewCombineArgs(m.EqArgs(), f)
return newStringMap(m.tree.Combine(args, n.tree))
}
// Hash computes a hash val for s.
func (m StringMap) Hash(seed uintptr) uintptr {
h := hash.Uintptr(uintptr(3167960924819262823&uint64(^uintptr(0))), seed)
for i := m.Range(); i.Next(); {
h ^= hash.Interface(i.Value(), hash.Interface(i.Key(), seed))
}
return h
}
// Equal returns true iff i is a StringMap with all the same key-value pairs as this
// StringMap.
func (m StringMap) Equal(i interface{}) bool {
if n, ok := i.(StringMap); ok {
args := skvt.NewEqArgs(
depth.NewGauge(m.Count()),
skv.KeyValueEqual,
skvt.KeyHash,
skvt.KeyHash,
)
return m.tree.Equal(args, n.tree)
}
return false
}
// String returns a string representatio of the StringMap.
func (m StringMap) String() string {
return fmt.Sprintf("%v", m)
}
// Format writes a string representation of the StringMap into state.
func (m StringMap) Format(f fmt.State, verb rune) {
fu.WriteString(f, "(")
for i, n := m.Range(), 0; i.Next(); n++ {
if n > 0 {
fu.WriteString(f, ", ")
}
fu.Format(i.Key(), f, verb)
fu.WriteString(f, ": ")
fu.Format(i.Value(), f, verb)
}
fu.WriteString(f, ")")
}
// Range returns a StringMapIterator over the StringMap.
func (m StringMap) Range() *StringMapIterator {
return &StringMapIterator{i: m.tree.Iterator()}
}
// MarshalJSON implements json.Marshaler.
func (m StringMap) MarshalJSON() ([]byte, error) {
proxy := map[string]interface{}{}
for i := m.Range(); i.Next(); {
proxy[i.Key()] = i.Value()
}
data, err := json.Marshal(proxy)
return data, errors.Wrap(err, 0)
}
// StringMapIterator provides for iterating over a StringMap.
type StringMapIterator struct {
i skvi.Iterator
skv skv.KeyValue
}
// Next moves to the next key-value pair or returns false if there are no more.
func (i *StringMapIterator) Next() bool {
if i.i.Next() {
i.skv = i.i.Value()
return true
}
return false
}
// Key returns the key for the current entry.
func (i *StringMapIterator) Key() string {
return i.skv.Key
}
// Value returns the value for the current entry.
func (i *StringMapIterator) Value() interface{} {
return i.skv.Value
}
// Entry returns the current key-value pair as two return values.
func (i *StringMapIterator) Entry() (key string, value interface{}) {
return i.skv.Key, i.skv.Value
}