-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintToStringHashmap.go
182 lines (163 loc) · 4.46 KB
/
intToStringHashmap.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
package immutable
import (
"errors"
"unsafe"
"github.com/object88/immutable/core"
"github.com/object88/immutable/handlers/integers"
"github.com/object88/immutable/handlers/strings"
)
type IntToStringHashmap struct {
h *core.InternalHashmap
c *core.HashmapConfig
}
func NewIntToStringHashmap(contents map[int]string, options ...core.HashmapOption) *IntToStringHashmap {
opts := core.DefaultHashmapOptions()
for _, fn := range options {
fn(opts)
}
c := &core.HashmapConfig{
KeyConfig: integers.GetHandler(),
Options: opts,
ValueConfig: strings.GetHandler(),
}
hash := core.CreateEmptyInternalHashmap(opts.PackedBucket, len(contents))
for k, v := range contents {
key, value := k, v
hash.InternalSet(c, unsafe.Pointer(&key), unsafe.Pointer(&value))
}
return &IntToStringHashmap{hash, c}
}
// Filter returns a subset of the collection, based on the predicate supplied
func (hm *IntToStringHashmap) Filter(predicate func(key int, value string) (bool, error)) (*IntToStringHashmap, error) {
if hm == nil {
return nil, errors.New("Pointer receiver is nil")
}
newHashmap, err := hm.h.Filter(hm.c, func(kp, vp unsafe.Pointer) (bool, error) {
key, value := *(*int)(kp), *(*string)(vp)
return predicate(key, value)
})
if err != nil {
return nil, err
}
return &IntToStringHashmap{newHashmap, hm.c}, nil
}
// ForEach iterates over each key-value pair in this collection
func (hm *IntToStringHashmap) ForEach(predicate func(key int, value string)) {
if hm == nil {
return
}
hm.h.ForEach(hm.c, func(kp, vp unsafe.Pointer) {
key, value := *(*int)(kp), *(*string)(vp)
predicate(key, value)
})
}
func (hm *IntToStringHashmap) Get(key int) (value string, ok bool, err error) {
if hm == nil {
return "", false, errors.New("Pointer receiver is nil")
}
k := unsafe.Pointer(&key)
r, ok, err := hm.h.Get(hm.c, k)
if err != nil {
return "", false, err
}
if !ok {
return "", false, nil
}
v := *(*string)(r)
return v, true, nil
}
func (hm *IntToStringHashmap) GetKeys() (results []int, err error) {
if hm == nil {
return nil, errors.New("Pointer receiver is nil")
}
var a []unsafe.Pointer
a, err = hm.h.GetKeys(hm.c)
if err != nil {
return nil, err
}
results = make([]int, len(a))
for k, v := range a {
results[k] = *(*int)(v)
}
return results, nil
}
func (hm *IntToStringHashmap) GoString() string {
if hm == nil {
return "(nil)"
}
return hm.h.GoString(hm.c)
}
func (hm *IntToStringHashmap) Insert(key int, value string) (result *IntToStringHashmap, err error) {
if hm == nil {
return nil, errors.New("Pointer receiver is nil")
}
kp, vp := unsafe.Pointer(&key), unsafe.Pointer(&value)
newHashmap, err := hm.h.Insert(hm.c, kp, vp)
if err != nil {
return nil, err
}
if newHashmap == hm.h {
return hm, nil
}
return &IntToStringHashmap{newHashmap, hm.c}, nil
}
func (hm *IntToStringHashmap) Map(predicate func(key int, value string) (result string, err error)) (*IntToStringHashmap, error) {
if hm == nil {
return nil, errors.New("Pointer receiver is nil")
}
newHashmap, err := hm.h.Map(hm.c, func(kp, vp unsafe.Pointer) (newv unsafe.Pointer, err error) {
key, value := *(*int)(kp), *(*string)(vp)
newS, err := predicate(key, value)
if err != nil {
return nil, err
}
return unsafe.Pointer(&newS), nil
})
if err != nil {
return nil, err
}
return &IntToStringHashmap{newHashmap, hm.c}, nil
}
func (hm *IntToStringHashmap) Reduce(accumulator interface{}, predicate func(accumulator interface{}, key int, value string) (interface{}, error)) (result interface{}, err error) {
if hm == nil {
return nil, errors.New("Pointer receiver is nil")
}
acc := accumulator
err = hm.h.Reduce(hm.c, func(kp, vp unsafe.Pointer) error {
key, value := *(*int)(kp), *(*string)(vp)
acc, err = predicate(acc, key, value)
return err
})
if err != nil {
return nil, err
}
return acc, nil
}
// Remove returns a copy of the provided InternalHashmap with the specified element
// removed.
func (hm *IntToStringHashmap) Remove(key int) (*IntToStringHashmap, error) {
if hm == nil {
return nil, errors.New("Pointer receiver is nil")
}
kp := unsafe.Pointer(&key)
newHashmap, err := hm.h.Remove(hm.c, kp)
if err != nil {
return nil, err
}
if newHashmap == hm.h {
return hm, nil
}
return &IntToStringHashmap{newHashmap, hm.c}, nil
}
func (hm *IntToStringHashmap) Size() int {
if hm == nil {
return 0
}
return hm.h.Size()
}
func (hm *IntToStringHashmap) String() string {
if hm == nil {
return "(nil)"
}
return hm.h.String(hm.c)
}