-
Notifications
You must be signed in to change notification settings - Fork 15
/
gen_NamePairSet.go
226 lines (190 loc) · 6.07 KB
/
gen_NamePairSet.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
// Code generated by gotemplate. DO NOT EDIT.
// 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 treeset implements a Tree backed by a red-black Tree.
//
// Structure is not thread safe.
//
// Reference: http://en.wikipedia.org/wiki/Set_%28abstract_data_type%29
package generated
import (
"encoding/json"
"fmt"
"strings"
"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"
)
// template type Set(V,Compare,Multi)
func assertNamePairSetImplementation() {
var _ container.Set = (*NamePairSet)(nil)
}
// Set holds elements in a red-black Tree
type NamePairSet struct {
*rbt.Tree
}
var itemExistsNamePairSet = struct{}{}
// NewWith instantiates a new empty set with the custom comparator.
func NewNamePairSet(Value ...common.NamePair) *NamePairSet {
set := &NamePairSet{Tree: rbt.NewWith(common.CompareNamePair, false)}
set.Add(Value...)
return set
}
func CopyFromNamePairSet(ts *NamePairSet) *NamePairSet {
return &NamePairSet{Tree: rbt.CopyFrom(ts.Tree)}
}
func NamePairSetIntersection(a *NamePairSet, b *NamePairSet, callback func(elem common.NamePair)) {
aIterator := a.Iterator()
bIterator := b.Iterator()
if !aIterator.First() || !bIterator.First() {
return
}
for aHasNext, bHasNext := true, true; aHasNext && bHasNext; {
comp := common.CompareNamePair(aIterator.Value(), bIterator.Value())
switch {
case comp > 0:
bHasNext = bIterator.Next()
case comp < 0:
aHasNext = aIterator.Next()
default:
callback(aIterator.Value())
aHasNext = aIterator.Next()
bHasNext = bIterator.Next()
}
}
}
// Add adds the item one to the set.Returns false and the interface if it already exists
func (set *NamePairSet) AddItem(item common.NamePair) (bool, common.NamePair) {
itr := set.Tree.Insert(item, itemExistsNamePairSet)
if itr.IsEnd() {
return false, item
}
return true, itr.Key().(common.NamePair)
}
// Add adds the items (one or more) to the set.
func (set *NamePairSet) Add(items ...common.NamePair) {
for _, item := range items {
set.Tree.Put(item, itemExistsNamePairSet)
}
}
// Remove removes the items (one or more) from the set.
func (set *NamePairSet) Remove(items ...common.NamePair) {
for _, item := range items {
set.Tree.Remove(item)
}
}
// Values returns all items in the set.
func (set *NamePairSet) Values() []common.NamePair {
keys := make([]common.NamePair, set.Size())
it := set.Iterator()
for i := 0; it.Next(); i++ {
keys[i] = it.Value()
}
return keys
}
// Contains checks weather items (one or more) are present in the set.
// All items have to be present in the set for the method to return true.
// Returns true if no arguments are passed at all, i.e. set is always superset of empty set.
func (set *NamePairSet) Contains(items ...common.NamePair) bool {
for _, item := range items {
if iter := set.Get(item); iter.IsEnd() {
return false
}
}
return true
}
// String returns a string representation of container
func (set *NamePairSet) String() string {
str := "TreeSet\n"
items := make([]string, 0)
for _, v := range set.Tree.Keys() {
items = append(items, fmt.Sprintf("%v", v))
}
str += strings.Join(items, ", ")
return str
}
// Iterator returns a stateful iterator whose values can be fetched by an index.
type IteratorNamePairSet struct {
rbt.Iterator
}
// Iterator holding the iterator's state
func (set *NamePairSet) Iterator() IteratorNamePairSet {
return IteratorNamePairSet{Iterator: set.Tree.Iterator()}
}
// Begin returns First Iterator whose position points to the first element
// Return End Iterator when the map is empty
func (set *NamePairSet) Begin() IteratorNamePairSet {
return IteratorNamePairSet{set.Tree.Begin()}
}
// End returns End Iterator
func (set *NamePairSet) End() IteratorNamePairSet {
return IteratorNamePairSet{set.Tree.End()}
}
// Value returns the current element's value.
// Does not modify the state of the iterator.
func (iterator IteratorNamePairSet) Value() common.NamePair {
return iterator.Iterator.Key().(common.NamePair)
}
// Each calls the given function once for each element, passing that element's index and value.
func (set *NamePairSet) Each(f func(value common.NamePair)) {
iterator := set.Iterator()
for iterator.Next() {
f(iterator.Value())
}
}
// Find passes each element of the container to the given function and returns
// the first (index,value) for which the function is true or -1,nil otherwise
// if no element matches the criteria.
func (set *NamePairSet) Find(f func(value common.NamePair) bool) (v common.NamePair) {
iterator := set.Iterator()
for iterator.Next() {
if f(iterator.Value()) {
return iterator.Value()
}
}
return
}
func (set *NamePairSet) LowerBound(item common.NamePair) IteratorNamePairSet {
return IteratorNamePairSet{set.Tree.LowerBound(item)}
}
func (set *NamePairSet) UpperBound(item common.NamePair) IteratorNamePairSet {
return IteratorNamePairSet{set.Tree.UpperBound(item)}
}
// ToJSON outputs the JSON representation of the set.
func (set NamePairSet) MarshalJSON() ([]byte, error) {
return json.Marshal(set.Values())
}
// FromJSON populates the set from the input JSON representation.
func (set *NamePairSet) UnmarshalJSON(data []byte) error {
elements := make([]common.NamePair, 0)
err := json.Unmarshal(data, &elements)
if err == nil {
set.Tree = rbt.NewWith(common.CompareNamePair, false)
set.Add(elements...)
}
return err
}
func (set NamePairSet) Pack() (re []byte, err error) {
re = append(re, common.WriteUVarInt(set.Size())...)
set.Each(func(value common.NamePair) {
reVal, _ := rlp.EncodeToBytes(value)
re = append(re, reVal...)
})
return re, nil
}
func (set *NamePairSet) Unpack(in []byte) (int, error) {
set.Tree = rbt.NewWith(common.CompareNamePair, false)
decoder := rlp.NewDecoder(in)
l, err := decoder.ReadUvarint64()
if err != nil {
return 0, err
}
for i := 0; i < int(l); i++ {
v := new(common.NamePair)
decoder.Decode(v)
set.Add(*v)
}
return decoder.GetPos(), nil
}