-
Notifications
You must be signed in to change notification settings - Fork 49
/
vector.go
235 lines (200 loc) · 5.56 KB
/
vector.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
227
228
229
230
231
232
233
234
235
package stl4go
// Many tricks are from:
// https://github.com/golang/go/wiki/SliceTricks#in-place-deduplicate-comparable
// Vector is a sequence container representing array that can change in size.
type Vector[T any] []T
// MakeVector creates an empty Vector object.
func MakeVector[T any]() Vector[T] {
return (Vector[T])([]T{})
}
// MakeVectorCap creates an empty Vector object with specified capacity.
func MakeVectorCap[T any](c int) Vector[T] {
v := make([]T, 0, c)
return (Vector[T])(v)
}
// VectorOf creates a Vector object with initial values.
func VectorOf[T any](v ...T) Vector[T] {
return (Vector[T])(v)
}
// AsVector casts a slice as a Vector object.
func AsVector[T any](s []T) Vector[T] {
return (Vector[T])(s)
}
// IsEmpty implements the Container interface.
func (v *Vector[T]) IsEmpty() bool {
return len(*v) == 0
}
// Len implements the Container interface.
func (v *Vector[T]) Len() int {
return len(*v)
}
// Cap returns the capacity of the vector.
func (v *Vector[T]) Cap() int {
return cap(*v)
}
// Clear erases all elements from the vector. After this call, Len() returns zero.
// Leaves the Cap() of the vector unchanged.
func (v *Vector[T]) Clear() {
FillZero(*v)
*v = (*v)[0:0]
}
// Reserve increases the capacity of the vector (the total number of elements
// that the vector can hold without requiring reallocation)to a value that's
// greater or equal to l. If l is greater than the current Cap(), new storage
// is allocated, otherwise the function does nothing.
//
// Reserve() does not change the size of the vector.
func (v *Vector[T]) Reserve(l int) {
if cap(*v) < l {
t := make([]T, len(*v), l)
copy(t, *v)
*v = t
}
}
// Shrink removes unused capacity from the vector.
func (v *Vector[T]) Shrink() {
if cap(*v) > len(*v) {
*v = append([]T{}, *v...)
}
}
// At returns the element value at the index i.
// You can also use the [] operator, and it's better.
func (v *Vector[T]) At(i int) T {
return (*v)[i]
}
// Set sets the value of the element at the index i.
// You can also use the [] operator, and it's better.
func (v *Vector[T]) Set(i int, x T) {
(*v)[i] = x
}
// PushBack pushs an element to the end of the vector.
//
// Complexity: O(1) if v.Len() < v.Cap(), therwise O(len(v)).
func (v *Vector[T]) PushBack(x T) {
*v = append(*v, x)
}
// PopBack popups an element from the end of the vector.
// It must be called when IsEmpty() returned false,
// otherwise it will panic.
func (v *Vector[T]) PopBack() T {
var zero T
e := (*v)[v.Len()-1]
(*v)[len(*v)-1] = zero
*v = (*v)[0 : v.Len()-1]
return e
}
// TryPopBack popups an element from the end of the vector.
func (v *Vector[T]) TryPopBack() (T, bool) {
if v.IsEmpty() {
var zero T
return zero, false
}
return v.PopBack(), true
}
// Back returns the element at the end of the vector.
// It must be called when IsEmpty() returned false,
// otherwise it will panic.
func (v Vector[T]) Back() T {
return v[len(v)-1]
}
// Append appends the values x... to the tail of the vector.
func (v *Vector[T]) Append(x ...T) {
*v = append(*v, x...)
}
// Insert inserts the values x... into the vector at index i.
// After the insertion, (*v)[i] == x[0].
// Insert panics if i is out of range.
//
// Complexity: O(len(s) + len(v)).
func (v *Vector[T]) Insert(i int, x ...T) {
s := *v
total := len(s) + len(x)
if total <= cap(s) {
s2 := s[:total]
copy(s2[i+len(x):], s[i:])
copy(s2[i:], x)
*v = s2
return
}
s2 := make([]T, total)
copy(s2, s[:i])
copy(s2[i:], x)
copy(s2[i+len(x):], s[i:])
*v = s2
}
// Remove removes 1 element in the vector.
//
// Complexity: O(len(s) - i).
func (v *Vector[T]) Remove(i int) {
v.RemoveRange(i, i+1)
}
// RemoveRange removes the elements in the range[i, j) from the vector.
func (v *Vector[T]) RemoveRange(i, j int) {
oldV := *v
*v = append((*v)[:i], (*v)[j:]...)
FillZero(oldV[v.Len():])
}
// RemoveLength removes the elements in the range[i, i+len) from the vector.
func (v *Vector[T]) RemoveLength(i int, len int) {
v.RemoveRange(i, i+len)
}
// RemoveIf removes the elements which make cond(x) returns true from the vector.
func (v *Vector[T]) RemoveIf(cond func(T) bool) {
oldV := *v
*v = RemoveIf(*v, cond)
FillZero(oldV[v.Len():])
}
// ForEach iterate the container, apply each element to the cb callback function.
func (v Vector[T]) ForEach(cb func(val T)) {
for _, e := range v {
cb(e)
}
}
// ForEachIf iterate the container, apply each element to the cb callback function,
// stop if cb returns false.
func (v Vector[T]) ForEachIf(cb func(val T) bool) {
for _, e := range v {
if !cb(e) {
break
}
}
}
// ForEachMutable iterate the container, apply pointer of each element to the cb callback function.
func (v Vector[T]) ForEachMutable(cb func(val *T)) {
for i := range v {
cb(&v[i])
}
}
// ForEachMutableIf iterate the container, apply pointer of each element to the cb callback function,
// stop if cb returns false.
func (v Vector[T]) ForEachMutableIf(cb func(val *T) bool) {
for i := range v {
if !cb(&v[i]) {
break
}
}
}
// Iterate returns an iterator to the whole container.
func (v Vector[T]) Iterate() MutableIterator[T] {
return &vectorIterator[T]{v, 0}
}
// IterateRange returns an iterator to the range [i, j) of the container.
func (v Vector[T]) IterateRange(i, j int) MutableIterator[T] {
return &vectorIterator[T]{v[i:j], 0}
}
type vectorIterator[T any] struct {
v Vector[T]
i int
}
func (it vectorIterator[T]) Value() T {
return it.v[it.i]
}
func (it vectorIterator[T]) Pointer() *T {
return &it.v[it.i]
}
func (it vectorIterator[T]) IsNotEnd() bool {
return it.i < len(it.v)
}
func (it *vectorIterator[T]) MoveToNext() {
it.i++
}