-
Notifications
You must be signed in to change notification settings - Fork 15
/
treemap.go
221 lines (187 loc) · 5.45 KB
/
treemap.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
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package treemap implements a map backed by red-black Tree.
//
// Elements are ordered by key in the map.
//
// Structure is not thread safe.
//
// Reference: http://en.wikipedia.org/wiki/Associative_array
package treemap
import (
"encoding/json"
"fmt"
"github.com/eosspark/eos-go/common"
"github.com/eosspark/eos-go/crypto/rlp"
"github.com/eosspark/eos-go/libraries/container"
rbt "github.com/eosspark/eos-go/libraries/container/redblacktree"
"strings"
)
// template type Map(K,V,Compare,Multi)
type K int
type V int
var Compare = func(a, b interface{}) int { return container.IntComparator(a.(int), b.(int)) }
var Multi = false
func assertMapImplementation() {
var _ container.Map = (*Map)(nil)
}
// Map holds the elements in a red-black Tree
type Map struct {
*rbt.Tree
}
// NewWith instantiates a Tree map with the custom comparator.
func New() *Map {
return &Map{Tree: rbt.NewWith(Compare, Multi)}
}
func CopyFrom(tm *Map) *Map {
return &Map{Tree: rbt.CopyFrom(tm.Tree)}
}
// Put inserts key-value pair into the map.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (m *Map) Put(key K, value V) {
m.Tree.Put(key, value)
}
func (m *Map) Insert(key K, value V) Iterator {
return Iterator{m.Tree.Insert(key, value)}
}
// Get searches the element in the map by key and returns its value or nil if key is not found in Tree.
// Second return parameter is true if key was found, otherwise false.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (m *Map) Get(key K) Iterator {
return Iterator{m.Tree.Get(key)}
}
// Remove removes the element from the map by key.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (m *Map) Remove(key K) {
m.Tree.Remove(key)
}
// Keys returns all keys in-order
func (m *Map) Keys() []K {
keys := make([]K, m.Tree.Size())
it := m.Tree.Iterator()
for i := 0; it.Next(); i++ {
keys[i] = it.Key().(K)
}
return keys
}
// Values returns all values in-order based on the key.
func (m *Map) Values() []V {
values := make([]V, m.Tree.Size())
it := m.Tree.Iterator()
for i := 0; it.Next(); i++ {
values[i] = it.Value().(V)
}
return values
}
// Each calls the given function once for each element, passing that element's key and value.
func (m *Map) Each(f func(key K, value V)) {
Iterator := m.Iterator()
for Iterator.Next() {
f(Iterator.Key(), Iterator.Value())
}
}
// Find passes each element of the container to the given function and returns
// the first (key,value) for which the function is true or nil,nil otherwise if no element
// matches the criteria.
func (m *Map) Find(f func(key K, value V) bool) (k K, v V) {
Iterator := m.Iterator()
for Iterator.Next() {
if f(Iterator.Key(), Iterator.Value()) {
return Iterator.Key(), Iterator.Value()
}
}
return
}
// String returns a string representation of container
func (m Map) String() string {
str := "TreeMap\nmap["
it := m.Iterator()
for it.Next() {
str += fmt.Sprintf("%v:%v ", it.Key(), it.Value())
}
return strings.TrimRight(str, " ") + "]"
}
// Iterator holding the Iterator's state
type Iterator struct {
rbt.Iterator
}
// Iterator returns a stateful Iterator whose elements are key/value pairs.
func (m *Map) Iterator() Iterator {
return Iterator{Iterator: m.Tree.Iterator()}
}
// Begin returns First Iterator whose position points to the first element
// Return End Iterator when the map is empty
func (m *Map) Begin() Iterator {
return Iterator{m.Tree.Begin()}
}
// End returns End Iterator
func (m *Map) End() Iterator {
return Iterator{m.Tree.End()}
}
// Value returns the current element's value.
// Does not modify the state of the Iterator.
func (iterator Iterator) Value() V {
return iterator.Iterator.Value().(V)
}
// Key returns the current element's key.
// Does not modify the state of the Iterator.
func (iterator Iterator) Key() K {
return iterator.Iterator.Key().(K)
}
func (m *Map) LowerBound(key K) Iterator {
return Iterator{m.Tree.LowerBound(key)}
}
func (m *Map) UpperBound(key K) Iterator {
return Iterator{m.Tree.UpperBound(key)}
}
// ToJSON outputs the JSON representation of the map.
type pair struct {
Key K `json:"key"`
Val V `json:"val"`
}
func (m Map) MarshalJSON() ([]byte, error) {
elements := make([]pair, 0, m.Size())
it := m.Iterator()
for it.Next() {
elements = append(elements, pair{it.Key(), it.Value()})
}
return json.Marshal(&elements)
}
// FromJSON populates the map from the input JSON representation.
func (m *Map) UnmarshalJSON(data []byte) error {
elements := make([]pair, 0)
err := json.Unmarshal(data, &elements)
if err == nil {
m.Tree = rbt.NewWith(Compare, Multi)
for _, pair := range elements {
m.Put(pair.Key, pair.Val)
}
}
return err
}
func (m Map) Pack() (re []byte, err error) {
re = append(re, common.WriteUVarInt(m.Size())...)
m.Each(func(key K, value V) {
rekey, _ := rlp.EncodeToBytes(key)
re = append(re, rekey...)
reVal, _ := rlp.EncodeToBytes(value)
re = append(re, reVal...)
})
return re, nil
}
func (m *Map) Unpack(in []byte) (int, error) {
m.Tree = rbt.NewWith(Compare, Multi)
decoder := rlp.NewDecoder(in)
l, err := decoder.ReadUvarint64()
if err != nil {
return 0, err
}
for i := 0; i < int(l); i++ {
k, v := new(K), new(V)
decoder.Decode(k)
decoder.Decode(v)
m.Put(*k, *v)
}
return decoder.GetPos(), nil
}