-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimmutable_float32.go
366 lines (300 loc) · 9.98 KB
/
immutable_float32.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// +build !ignore_autogenerated
// Code generated by collections-gen. DO NOT EDIT.
package collections
import (
"sort"
)
// ImmutableFloat32 is an immutable collection of float32 values.
type ImmutableFloat32 struct {
items []float32
}
// NewImmutableFloat32 creates a new immutable collection from a slice of float32.
func NewImmutableFloat32(items []float32) *ImmutableFloat32 {
return &ImmutableFloat32{items}
}
// Interface returns the underlying slice used by the collection as interface{}
// value.
func (c *ImmutableFloat32) Interface() interface{} {
return c.items
}
// Items returns the underlying slice of float32 values used by the
// collection.
func (c *ImmutableFloat32) Items() []float32 {
return c.items
}
// EachIndex calls fn for every item in the collection. The slice index of the
// item is passed to fn as the second argument.
func (c *ImmutableFloat32) EachIndex(fn func(float32, int)) {
for i, item := range c.items {
fn(item, i)
}
}
// Each calls fn for every item in the collection.
func (c *ImmutableFloat32) Each(fn func(float32)) {
c.EachIndex(func(item float32, _ int) {
fn(item)
})
}
// IndexOf searches for el in the collection and returns the first index where
// el is found. If el is not present in the collection IndexOf will return -1.
func (c *ImmutableFloat32) IndexOf(el float32) int {
for i, item := range c.items {
if item == el {
return i
}
}
return -1
}
// First returns the first item from the collection. Will panic if the
// underlying slice is empty.
func (c *ImmutableFloat32) First() float32 {
return c.Nth(0)
}
// FirstN returns the first n float32 items of the collection. Will
// return less than n items if the underlying slice's length is < n.
func (c *ImmutableFloat32) FirstN(n int) []float32 {
if n > c.Len() {
return c.Copy().Items()
}
return c.Slice(0, n)
}
// Last returns the last item from the collection. Will panic if the underlying
// slice is empty.
func (c *ImmutableFloat32) Last() float32 {
return c.Nth(c.Len() - 1)
}
// LastN returns the last n float32 items of the collection. Will return
// less than n items if the underlying slice's length is < n.
func (c *ImmutableFloat32) LastN(n int) []float32 {
if c.Len()-n < 0 {
return c.Copy().Items()
}
return c.Slice(c.Len()-n, c.Len())
}
// Get returns the item at pos from the collection. Will panic if the
// underlying slice is shorter than pos+1.
func (c *ImmutableFloat32) Get(pos int) float32 {
return c.Nth(pos)
}
// Nth returns the nth item from the collection. Will panic if the underlying
// slice is shorter than pos+1.
func (c *ImmutableFloat32) Nth(pos int) float32 {
return c.items[pos]
}
// Len returns the length of the underlying float32 slice.
func (c *ImmutableFloat32) Len() int {
return len(c.items)
}
// Cap returns the capacity of the underlying float32 slice.
func (c *ImmutableFloat32) Cap() int {
return cap(c.items)
}
// Append appends items and returns the collection. The
// original collection will not be modified.
func (c *ImmutableFloat32) Append(items ...float32) *ImmutableFloat32 {
d := c.Copy()
d.items = append(d.items, items...)
return d
}
// Prepend prepends items and returns the collection. The
// original collection will not be modified.
func (c *ImmutableFloat32) Prepend(items ...float32) *ImmutableFloat32 {
d := c.Copy()
d.items = append(items, d.items...)
return d
}
// Copy creates a copy of the collection and the underlying float32 slice.
func (c *ImmutableFloat32) Copy() *ImmutableFloat32 {
s := make([]float32, c.Len(), c.Len())
copy(s, c.items)
return NewImmutableFloat32(s)
}
// Filter collects all items for which fn evaluates to true into a new
// collection. The original collection is not altered.
func (c *ImmutableFloat32) Filter(fn func(float32) bool) *ImmutableFloat32 {
d := c.Copy()
s := d.items[:0]
for _, item := range d.items {
if fn(item) {
s = append(s, item)
}
}
var zeroValue float32
for i := len(s); i < len(d.items); i++ {
d.items[i] = zeroValue
}
d.items = s
return d
}
// Collect collects all items for which fn evaluates to true into a new
// collection. The original collection is not altered.
func (c *ImmutableFloat32) Collect(fn func(float32) bool) *ImmutableFloat32 {
return c.Filter(fn)
}
// Reject collects all items for which fn evaluates to false into a new
// collection. The original collection is not altered.
func (c *ImmutableFloat32) Reject(fn func(float32) bool) *ImmutableFloat32 {
return c.Filter(func(v float32) bool {
return !fn(v)
})
}
// Partition partitions the collection into two new collections. The first
// collection contains all items where fn evaluates to true, the second one all
// items where fn evaluates to false.
func (c *ImmutableFloat32) Partition(fn func(float32) bool) (*ImmutableFloat32, *ImmutableFloat32) {
lhs := make([]float32, 0, c.Len())
rhs := make([]float32, 0, c.Len())
for _, item := range c.items {
if fn(item) {
lhs = append(lhs, item)
} else {
rhs = append(rhs, item)
}
}
return NewImmutableFloat32(lhs), NewImmutableFloat32(rhs)
}
// Map calls fn for each item in the collection an replaces its value with the
// result of fn. The result is a new collection. The original
// collection is not modified.
func (c *ImmutableFloat32) Map(fn func(float32) float32) *ImmutableFloat32 {
return c.MapIndex(func(item float32, _ int) float32 {
return fn(item)
})
}
// MapIndex calls fn for each item in the collection an replaces its value with the
// result of fn. The result is a new collection. The original
// collection is not modified.
func (c *ImmutableFloat32) MapIndex(fn func(float32, int) float32) *ImmutableFloat32 {
d := c.Copy()
for i, item := range d.items {
d.items[i] = fn(item, i)
}
return d
}
// Reduce calls fn for each item in c and reduces the result into reducer. The
// reducer contains the value returned by the call to fn for the previous item.
// Reducer will be the zero float32 value on the first invocation.
func (c *ImmutableFloat32) Reduce(fn func(reducer float32, item float32) float32) float32 {
var reducer float32
for _, item := range c.items {
reducer = fn(reducer, item)
}
return reducer
}
// Find returns the first item for which fn evaluates to true. If the
// collection does not contain a matching item, Find will return the zero
// float32 value. If you need to distinguish zero values from a condition
// that did not match any item consider using FindOk instead.
func (c *ImmutableFloat32) Find(fn func(float32) bool) float32 {
item, _ := c.FindOk(fn)
return item
}
// FindOk returns the first item for which fn evaluates to true. If the
// collection does not contain a matching item, FindOk will return the zero
// float32 value. The second return value denotes whether the condition
// matched any item or not.
func (c *ImmutableFloat32) FindOk(fn func(float32) bool) (float32, bool) {
for _, item := range c.items {
if fn(item) {
return item, true
}
}
var zeroValue float32
return zeroValue, false
}
// Any returns true as soon as fn evaluates to true for one item in c.
func (c *ImmutableFloat32) Any(fn func(float32) bool) bool {
for _, item := range c.items {
if fn(item) {
return true
}
}
return false
}
// All returns true if fn evaluates to true for all items in c.
func (c *ImmutableFloat32) All(fn func(float32) bool) bool {
for _, item := range c.items {
if !fn(item) {
return false
}
}
return true
}
// Contains returns true if the collection contains el.
func (c *ImmutableFloat32) Contains(el float32) bool {
for _, item := range c.items {
if item == el {
return true
}
}
return false
}
// Sort sorts the collection using the passed in comparator func.
// The result will be a copy of c which is sorted, the original collection is
// not altered.
func (c *ImmutableFloat32) Sort(fn func(float32, float32) bool) *ImmutableFloat32 {
d := c.Copy()
sort.Slice(d.items, d.lessFunc(fn))
return d
}
// IsSorted returns true if the collection is sorted in the order defined by
// the passed in comparator func.
func (c *ImmutableFloat32) IsSorted(fn func(float32, float32) bool) bool {
return sort.SliceIsSorted(c.items, c.lessFunc(fn))
}
func (c *ImmutableFloat32) lessFunc(fn func(float32, float32) bool) func(int, int) bool {
return func(i, j int) bool {
return fn(c.items[i], c.items[j])
}
}
// Reverse copies the collection and returns it with the order of all items
// reversed.
func (c *ImmutableFloat32) Reverse() *ImmutableFloat32 {
d := c.Copy()
for l, r := 0, len(d.items)-1; l < r; l, r = l+1, r-1 {
d.items[l], d.items[r] = d.items[r], d.items[l]
}
return d
}
// Remove removes the collection item at position pos. Will panic if pos is out
// of bounds.
// The result is a new collection, the original is not modified.
func (c *ImmutableFloat32) Remove(pos int) *ImmutableFloat32 {
d := c.Copy()
d.items = append(d.items[:pos], d.items[pos+1:]...)
return d
}
// RemoveItem removes all instances of item from the collection and returns it.
// The result is a new collection, the original is not modified.
func (c *ImmutableFloat32) RemoveItem(item float32) *ImmutableFloat32 {
d := c.Copy()
for i, el := range d.items {
if el == item {
d.items = append(d.items[:i], d.items[i+1:]...)
}
}
return d
}
// InsertItem inserts item into the collection at position pos. Will panic if
// pos is out of bounds.
// The result is a new collection, the original is not modified.
func (c *ImmutableFloat32) InsertItem(item float32, pos int) *ImmutableFloat32 {
var zeroValue float32
d := c.Copy()
d.items = append(d.items, zeroValue)
copy(d.items[pos+1:], d.items[pos:])
d.items[pos] = item
return d
}
// Cut returns a copy of the underlying float32 slice with the items
// between index i and j removed. Will panic if i or j is out of bounds of the
// underlying slice.
func (c *ImmutableFloat32) Cut(i, j int) []float32 {
d := c.Copy()
return append(d.items[:i], d.items[j:]...)
}
// Slice returns the float32 items between slice index i and j. Will
// panic if i or j is out of bounds.
func (c *ImmutableFloat32) Slice(i, j int) []float32 {
return c.Copy().items[i:j]
}