-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashmap.go
145 lines (112 loc) · 3.37 KB
/
hashmap.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
package immutable
import (
"bytes"
"encoding/gob"
"github.com/barweiss/go-tuple"
"github.com/imunhatep/gocollection/dict"
"github.com/samber/mo"
)
// HashMap is a container for an optional Values of type V. If Values exists, HashMap is
// of type Some. If the Values is absent, HashMap is of type None.
type HashMap[K comparable, V any] struct {
items map[K]V
}
// NewMap Builds a HashMap
func NewMap[K comparable, V any]() HashMap[K, V] {
mapVal := HashMap[K, V]{}
mapVal.items = make(map[K]V)
return mapVal
}
func ToMap[K comparable, V any](data map[K]V) HashMap[K, V] {
return HashMap[K, V]{items: data}
}
func (o HashMap[K, V]) GobEncode() ([]byte, error) {
store := bytes.NewBuffer([]byte{})
encoder := gob.NewEncoder(store)
err := encoder.Encode(o.items)
return store.Bytes(), err
}
func (o *HashMap[K, V]) GobDecode(data []byte) error {
o.items = map[K]V{}
err := gob.NewDecoder(bytes.NewBuffer(data)).Decode(&o.items)
return err
}
func (o HashMap[K, V]) Get(k K) mo.Option[tuple.T2[K, V]] {
return dict.Get(o.items, k)
}
func (o HashMap[K, V]) GetOrElse(k K, def V) V {
return dict.GetOrElse(o.items, k, def)
}
func (o HashMap[K, V]) Join(m HashMap[K, V]) HashMap[K, V] {
return ToMap(dict.Merge(o.items, m.ToMap()))
}
func (o HashMap[K, V]) Update(key K, value V) HashMap[K, V] {
return ToMap(dict.Set(o.items, key, value))
}
func (o HashMap[K, V]) Remove(key K) HashMap[K, V] {
return ToMap(dict.Remove(o.items, key))
}
func (o HashMap[K, V]) Contains(s V) bool {
return dict.Contains(o.items, s)
}
func (o HashMap[K, V]) Find(f func(K, V) bool) mo.Option[tuple.T2[K, V]] {
return dict.Find(o.items, f)
}
func (o HashMap[K, V]) HasKey(k K) bool {
return dict.ContainsKey(o.items, k)
}
// Size returns 1 when Values is present or 0 instead.
func (o HashMap[K, V]) Size() int {
return dict.Size(o.items)
}
// Limit returns first N elements.
func (o HashMap[K, V]) Limit(c int) HashMap[K, V] {
return ToMap(dict.Limit(o.items, c))
}
func (o HashMap[K, V]) IsEmpty() bool {
return dict.IsEmpty(o.items)
}
func (o HashMap[K, V]) Keys() Sequence[K] {
return NewSequence(dict.Keys(o.items)...)
}
func (o HashMap[K, V]) Values() Sequence[V] {
return NewSequence(dict.Values(o.items)...)
}
func (o HashMap[K, V]) Sort(f func(V, V) bool) SortedMap[K, V] {
return ToSortedMap(o.items).Sort(f)
}
func (o HashMap[K, V]) SortByKey(f func(K, K) bool) SortedMap[K, V] {
return ToSortedMap(o.items).SortByKey(f)
}
func (o HashMap[K, V]) Filter(f func(K, V) bool) HashMap[K, V] {
return ToMap(dict.Filter(o.items, f))
}
func (o HashMap[K, V]) FilterNot(f func(K, V) bool) HashMap[K, V] {
return ToMap(dict.FilterNot(o.items, f))
}
func (o HashMap[K, V]) FoldLeft(z HashMap[K, V], f func(HashMap[K, V], K, V) HashMap[K, V]) HashMap[K, V] {
return dict.Fold(o.items, z, f)
}
// HashMap executes the mapper function if Values is present or returns None if absent.
func (o HashMap[K, V]) Map(f func(K, V) V) HashMap[K, V] {
return ToMap(dict.Map(o.items, f))
}
func (o HashMap[K, V]) ToMap() map[K]V {
return dict.Copy(o.items)
}
func (o HashMap[K, V]) ToSlice() []tuple.T2[K, V] {
return dict.ToSlice(o.items)
}
func (o HashMap[K, V]) ToSequence() Sequence[tuple.T2[K, V]] {
return NewSequence(o.ToSlice()...)
}
func HashMapFoldLeft[K comparable, V any, T any](
src HashMap[K, V],
dst T,
p func(T, K, V) T,
) T {
for k, v := range src.ToMap() {
dst = p(dst, k, v)
}
return dst
}