-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinternal.go
166 lines (134 loc) · 4.47 KB
/
internal.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
// Copyright(c) 2016 Ethan Zhuang <[email protected]>.
// Package emap implements an enhanced map in golang.
// The main enhancement of emap over original golang map is the support of searching index.
// Values in the emap can have one or more indices which can be used to search or delete.
// Key in the emap must be unique as same as the key in the golang map.
// Index in the emap is an N to M relation which mean a value can have multi indices and multi values can have one same index.
package emap
import (
"errors"
)
func insert(valueStore map[interface{}]interface{}, keyStore map[interface{}][]interface{}, indexStore map[interface{}][]interface{}, key interface{}, value interface{}, indices ...interface{}) error {
if _, exist := keyStore[key]; exist {
return errors.New("key duplicte")
}
keyStore[key] = indices
valueStore[key] = value
for _, index := range indices {
if keys, exist := indexStore[index]; exist {
indexStore[index] = append(keys, key)
} else {
indexStore[index] = []interface{}{key}
}
}
return nil
}
func fetchByKey(valueStore map[interface{}]interface{}, key interface{}) (interface{}, error) {
if value, exist := valueStore[key]; exist {
return value, nil
}
return nil, errors.New("key not exist")
}
func fetchByIndex(valueStore map[interface{}]interface{}, indexStore map[interface{}][]interface{}, index interface{}) ([]interface{}, error) {
if keys, exist := indexStore[index]; exist {
values := make([]interface{}, len(keys))
for i, key := range keys {
values[i] = valueStore[key]
}
return values, nil
}
return nil, errors.New("index not exist")
}
func deleteByKey(valueStore map[interface{}]interface{}, keyStore map[interface{}][]interface{}, indexStore map[interface{}][]interface{}, key interface{}) error {
if _, exist := keyStore[key]; !exist {
return errors.New("key not exist")
}
indices := make([]interface{}, len(keyStore[key]))
for i := range indices {
indices[i] = keyStore[key][i]
}
for _, index := range indices {
removeIndex(keyStore, indexStore, key, index)
}
delete(keyStore, key)
delete(valueStore, key)
return nil
}
func deleteByIndex(valueStore map[interface{}]interface{}, keyStore map[interface{}][]interface{}, indexStore map[interface{}][]interface{}, index interface{}) error {
if _, exist := indexStore[index]; !exist {
return errors.New("index not exist")
}
keys := make([]interface{}, len(indexStore[index]))
for i := range keys {
keys[i] = indexStore[index][i]
}
for _, key := range keys {
deleteByKey(valueStore, keyStore, indexStore, key)
}
return nil
}
func addIndex(keyStore map[interface{}][]interface{}, indexStore map[interface{}][]interface{}, key interface{}, index interface{}) error {
if _, exist := keyStore[key]; !exist {
return errors.New("key not exist")
}
for _, each := range keyStore[key] {
if each == index {
return errors.New("index duplicte")
}
}
keyStore[key] = append(keyStore[key], index)
if keys, exist := indexStore[index]; exist {
indexStore[index] = append(keys, key)
} else {
indexStore[index] = []interface{}{key}
}
return nil
}
func removeIndex(keyStore map[interface{}][]interface{}, indexStore map[interface{}][]interface{}, key interface{}, index interface{}) error {
if _, exist := keyStore[key]; !exist {
return errors.New("key not exist")
}
if _, exist := indexStore[index]; !exist {
return errors.New("index not exist")
}
for i, each := range keyStore[key] {
if each == index {
if i == len(keyStore[key])-1 {
keyStore[key] = keyStore[key][:i]
} else {
keyStore[key] = append(keyStore[key][:i], keyStore[key][i+1:]...)
}
break
}
}
for i, each := range indexStore[index] {
if each == key {
if i == len(indexStore[index])-1 {
indexStore[index] = indexStore[index][:i]
} else {
indexStore[index] = append(indexStore[index][:i], indexStore[index][i+1:]...)
}
break
}
}
if len(indexStore[index]) == 0 {
delete(indexStore, index)
}
return nil
}
func transform(valueStore map[interface{}]interface{}, callback func(interface{}, interface{}) (interface{}, error)) (map[interface{}]interface{}, error) {
var err error
targets := make(map[interface{}]interface{}, len(valueStore))
for key, value := range valueStore {
targets[key], err = callback(key, value)
if err != nil {
return nil, err
}
}
return targets, nil
}
func foreach(valueStore map[interface{}]interface{}, callback func(interface{}, interface{})) {
for key, value := range valueStore {
callback(key, value)
}
}