-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_map.go
515 lines (417 loc) · 10.2 KB
/
hash_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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package gost
import (
"fmt"
"strings"
)
// A hash map
type HashMap[K comparable, V any] struct {
data map[K]V
}
// Creates an empty HashMap.
func HashMapNew[K comparable, V any]() HashMap[K, V] {
return HashMap[K, V]{data: map[K]V{}}
}
// Creates an empty HashMap with at least the specified capacity.
func HashMapWithCapacity[K comparable, V any](capacity USize) HashMap[K, V] {
return HashMap[K, V]{data: make(map[K]V, capacity)}
}
// As Raw Map
func (self HashMap[K, V]) AsMap() map[K]V {
return self.data
}
// Returns the number of elements in the map.
//
// map := gost.HashMapNew[Int, Int]()
// gost.AssertEq(map.Len(), gost.USize(0))
//
// map.Insert(gost.I32(1), gost.I32(2))
// gost.AssertEq(map.Len(), gost.USize(1))
func (self HashMap[K, V]) Len() USize {
return USize(len(self.data))
}
// Returns true if the map contains no elements.
//
// map := gost.HashMapNew[Int, Int]()
// gost.Assert(map.IsEmpty())
//
// map.Insert(gost.I32(1), gost.I32(2))
// gost.Assert(!map.IsEmpty())
func (self HashMap[K, V]) IsEmpty() Bool {
return self.Len() == 0
}
// Inserts a key-value pair into the map.
// If the map did not have this key present, None is returned.
//
// map := gost.HashMapNew[Int, Int]()
// gost.Assert(map.Insert(gost.I32(1), gost.I32(2)).IsNone())
// gost.AssertEq(map.Insert(gost.I32(1), gost.I32(3)), gost.Some[gost.I32](gost.I32(2)))
func (self *HashMap[K, V]) Insert(key K, value V) Option[V] {
old, ok := self.data[key]
self.data[key] = value
if ok {
return Some(old)
} else {
return None[V]()
}
}
// Removes a key from the map, returning the value at the key if the key was previously in the map.
//
// map := gost.HashMapNew[Int, Int]()
// gost.Assert(map.Insert(gost.I32(1), gost.I32(2)).IsNone())
// gost.AssertEq(map.Insert(gost.I32(1), gost.I32(3)), gost.Some[gost.I32](gost.I32(2)))
func (self *HashMap[K, V]) Remove(key K) Option[V] {
old, ok := self.data[key]
delete(self.data, key)
if ok {
return Some(old)
} else {
return None[V]()
}
}
// Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
//
// map := gost.HashMapNew[Int, Int]()
// map.Insert(gost.I32(1), gost.I32(2))
// map.Clear()
// gost.Assert(map.IsEmpty())
func (self *HashMap[K, V]) Clear() {
self.data = map[K]V{}
}
// Returns value corresponding to the key.
//
// map := gost.HashMapNew[Int, Int]()
// map.Insert(gost.I32(1), gost.I32(2))
// gost.AssertEq(map.Get(gost.I32(1)), gost.Some[gost.I32](gost.I32(2)))
func (self HashMap[K, V]) Get(key K) Option[V] {
value, ok := self.data[key]
if ok {
return Some(value)
} else {
return None[V]()
}
}
// Returns true if the map contains a value for the specified key.
//
// map := gost.HashMapNew[Int, Int]()
// map.Insert(gost.I32(1), gost.I32(2))
// gost.Assert(map.ContainsKey(gost.I32(1)))
// gost.Assert(!map.ContainsKey(gost.I32(3)))
func (self HashMap[K, V]) ContainsKey(key K) Bool {
_, ok := self.data[key]
return Bool(ok)
}
// Returns true if the map contains a value for the specified value.
type HashMapIter[K comparable, V any] struct {
vec Vec[Pair[K, V]]
position USize
}
// into_iter
func (self HashMap[K, V]) IntoIter() Iterator[Pair[K, V]] {
vec := Vec[Pair[K, V]]{}
for key, value := range self.data {
vec.Push(Pair[K, V]{Key: key, Value: value})
}
return &HashMapIter[K, V]{vec: vec, position: 0}
}
// next
func (self *HashMapIter[K, V]) Next() Option[Pair[K, V]] {
if self.position >= self.vec.Len() {
return None[Pair[K, V]]()
}
value := self.vec.GetUnchecked(self.position)
self.position++
return Some[Pair[K, V]](value)
}
// map
func (self HashMapIter[K, V]) Map(f func(Pair[K, V]) Pair[K, V]) Iterator[Pair[K, V]] {
newVec := VecNew[Pair[K, V]]()
for {
value := self.Next()
if value.IsNone() {
return newVec.IntoIter()
}
newVec.Push(f(value.Unwrap()))
}
}
// filter
func (self HashMapIter[K, V]) Filter(f func(Pair[K, V]) Bool) Iterator[Pair[K, V]] {
newVec := VecNew[Pair[K, V]]()
for {
value := self.Next()
if value.IsNone() {
return newVec.IntoIter()
}
unwraped := value.Unwrap()
if f(unwraped) {
newVec.Push(unwraped)
}
}
}
// fold
func (self HashMapIter[K, V]) Fold(init Pair[K, V], f func(Pair[K, V], Pair[K, V]) Pair[K, V]) Pair[K, V] {
for {
value := self.Next()
if value.IsNone() {
return init
}
init = f(init, value.Unwrap())
}
}
// rev
func (self HashMapIter[K, V]) Rev() Iterator[Pair[K, V]] {
newVec := VecWithLen[Pair[K, V]](self.vec.Len())
i := self.vec.Len() - 1
for {
value := self.Next()
if value.IsNone() {
return newVec.IntoIter()
}
newVec.AsSlice()[i] = value.Unwrap()
i--
}
}
// Collect to Vec
func (self HashMapIter[K, V]) CollectToVec() Vec[Pair[K, V]] {
return self.vec
}
// Collect to LinkedList
func (self HashMapIter[K, V]) CollectToLinkedList() LinkedList[Pair[K, V]] {
list := LinkedListNew[Pair[K, V]]()
for {
value := self.Next()
if value.IsNone() {
return list
}
list.PushBack(value.Unwrap())
}
}
// An iterator visiting all keys in arbitrary order. The iterator element type is K.
type HashMapKeys[K any] struct {
vec Vec[K]
position USize
}
// An iterator visiting all keys in arbitrary order. The iterator element type is K.
//
// map := gost.HashMapNew[Int, Int]()
// map.Insert(gost.I32(1), gost.I32(2))
// map.Insert(gost.I32(3), gost.I32(4))
// map.Insert(gost.I32(5), gost.I32(6))
// keys := map.Keys()
// gost.AssertEq(keys.Next(), gost.Some[gost.I32](gost.I32(1)))
func (self HashMap[K, V]) Keys() Iterator[K] {
vec := Vec[K]{}
for key := range self.data {
vec.Push(key)
}
return &HashMapKeys[K]{vec: vec, position: 0}
}
// next
func (self *HashMapKeys[K]) Next() Option[K] {
if self.position >= self.vec.Len() {
return None[K]()
}
value := self.vec.GetUnchecked(self.position)
self.position++
return Some[K](value)
}
// map
func (self HashMapKeys[K]) Map(f func(K) K) Iterator[K] {
newVec := VecNew[K]()
for {
value := self.Next()
if value.IsNone() {
return newVec.IntoIter()
}
newVec.Push(f(value.Unwrap()))
}
}
// filter
func (self HashMapKeys[K]) Filter(f func(K) Bool) Iterator[K] {
newVec := VecNew[K]()
for {
value := self.Next()
if value.IsNone() {
return newVec.IntoIter()
}
unwraped := value.Unwrap()
if f(unwraped) {
newVec.Push(unwraped)
}
}
}
// fold
func (self HashMapKeys[K]) Fold(init K, f func(K, K) K) K {
for {
value := self.Next()
if value.IsNone() {
return init
}
init = f(init, value.Unwrap())
}
}
// rev
func (self HashMapKeys[K]) Rev() Iterator[K] {
newVec := VecWithLen[K](self.vec.Len())
i := self.vec.Len() - 1
for {
value := self.Next()
if value.IsNone() {
return newVec.IntoIter()
}
newVec.AsSlice()[i] = value.Unwrap()
i--
}
}
// Collect to Vec
func (self HashMapKeys[K]) CollectToVec() Vec[K] {
return self.vec
}
// Collect to LinkedList
func (self HashMapKeys[K]) CollectToLinkedList() LinkedList[K] {
list := LinkedListNew[K]()
for {
value := self.Next()
if value.IsNone() {
return list
}
list.PushBack(value.Unwrap())
}
}
// An iterator visiting all values in arbitrary order. The iterator element type is V.
type HashMapValues[V any] struct {
vec Vec[V]
position USize
}
// An iterator visiting all values in arbitrary order. The iterator element type is V.
func (self HashMap[K, V]) Values() Iterator[V] {
vec := Vec[V]{}
for _, value := range self.data {
vec.Push(value)
}
return &HashMapValues[V]{vec: vec, position: 0}
}
// next
func (self *HashMapValues[V]) Next() Option[V] {
if self.position >= self.vec.Len() {
return None[V]()
}
value := self.vec.GetUnchecked(self.position)
self.position++
return Some[V](value)
}
// map
func (self HashMapValues[V]) Map(f func(V) V) Iterator[V] {
newVec := VecNew[V]()
for {
value := self.Next()
if value.IsNone() {
return newVec.IntoIter()
}
newVec.Push(f(value.Unwrap()))
}
}
// filter
func (self HashMapValues[V]) Filter(f func(V) Bool) Iterator[V] {
newVec := VecNew[V]()
for {
value := self.Next()
if value.IsNone() {
return newVec.IntoIter()
}
unwraped := value.Unwrap()
if f(unwraped) {
newVec.Push(unwraped)
}
}
}
// fold
func (self HashMapValues[V]) Fold(init V, f func(V, V) V) V {
for {
value := self.Next()
if value.IsNone() {
return init
}
init = f(init, value.Unwrap())
}
}
// rev
func (self HashMapValues[V]) Rev() Iterator[V] {
newVec := VecWithLen[V](self.vec.Len())
i := self.vec.Len() - 1
for {
value := self.Next()
if value.IsNone() {
return newVec.IntoIter()
}
newVec.AsSlice()[i] = value.Unwrap()
i--
}
}
// Collect to Vec
func (self HashMapValues[V]) CollectToVec() Vec[V] {
return self.vec
}
// Collect to LinkedList
func (self HashMapValues[V]) CollectToLinkedList() LinkedList[V] {
list := LinkedListNew[V]()
for {
value := self.Next()
if value.IsNone() {
return list
}
list.PushBack(value.Unwrap())
}
}
// impl Display for HashMap
func (self HashMap[K, V]) Display() String {
buffer := String("")
buffer += "HashMap{"
fields := []string{}
for key, value := range self.data {
fields = append(fields, string(Format("{}: {}", key, value)))
}
buffer += String(strings.Join(fields, ", "))
buffer += "}"
return buffer
}
// impl Debug for HashMap
func (self HashMap[K, V]) Debug() String {
return self.Display()
}
// impl AsRef for HashMap
func (self HashMap[K, V]) AsRef() *HashMap[K, V] {
return &self
}
// impl Clone for HashMap
func (self HashMap[K, V]) Clone() HashMap[K, V] {
newMap := HashMapNew[K, V]()
for key, value := range self.data {
cloneKey := castToClone[K](key)
cloneValue := castToClone[V](value)
if cloneKey.IsNone() {
typeName := getTypeName(key)
panic(fmt.Sprintf("'%s' does not implement Clone[%s]", typeName, typeName))
} else if cloneValue.IsNone() {
typeName := getTypeName(value)
panic(fmt.Sprintf("'%s' does not implement Clone[%s]", typeName, typeName))
}
newMap.Insert(cloneKey.Unwrap().Clone(), cloneValue.Unwrap().Clone())
}
return newMap
}
// impl Eq for HashMap
func (self HashMap[K, V]) Eq(rhs HashMap[K, V]) Bool {
if self.Len() != rhs.Len() {
return false
}
for key, value := range self.data {
rhsValue, ok := rhs.data[key]
if !ok {
return false
}
if !castToEq[V](value).Unwrap().Eq(rhsValue) {
return false
}
}
return true
}