-
Notifications
You must be signed in to change notification settings - Fork 153
/
array.go
159 lines (142 loc) · 3.37 KB
/
array.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
package values
import (
"regexp"
"sort"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/semantic"
)
// Array represents an sequence of elements
// All elements must be the same type
type Array interface {
Value
Get(i int) Value
Set(i int, v Value)
Append(v Value)
Len() int
Range(func(i int, v Value))
Sort(func(i, j Value) bool)
}
type array struct {
t semantic.MonoType
elements []Value
}
func (a *array) Dynamic() Dynamic {
panic(UnexpectedKind(a.Type().Nature(), semantic.Dynamic))
}
func NewArray(arrType semantic.MonoType) Array {
return NewArrayWithBacking(arrType, nil)
}
func NewArrayWithBacking(arrType semantic.MonoType, elements []Value) Array {
if arrType.Nature() != semantic.Array {
panic(UnexpectedKind(arrType.Nature(), semantic.Array))
}
return &array{
t: arrType,
elements: elements,
}
}
func (a *array) IsNull() bool {
return false
}
func (a *array) Type() semantic.MonoType {
return a.t
}
func (a *array) Get(i int) Value {
if i >= len(a.elements) {
panic(errors.Newf(codes.Internal, "index out of bounds: i:%d len:%d", i, len(a.elements)))
}
return a.elements[i]
}
func (a *array) Set(i int, v Value) {
if i >= len(a.elements) {
panic(errors.Newf(codes.Internal, "index out of bounds: i:%d len:%d", i, len(a.elements)))
}
a.elements[i] = v
}
func (a *array) Append(v Value) {
a.elements = append(a.elements, v)
}
func (a *array) Range(f func(i int, v Value)) {
for i, v := range a.elements {
f(i, v)
}
}
func (a *array) Len() int {
return len(a.elements)
}
func (a *array) Sort(f func(i, j Value) bool) {
sort.Slice(a.elements, func(i, j int) bool {
return f(a.elements[i], a.elements[j])
})
}
func (a *array) Str() string {
panic(UnexpectedKind(semantic.Array, semantic.String))
}
func (o *array) Bytes() []byte {
panic(UnexpectedKind(semantic.Array, semantic.Bytes))
}
func (a *array) Int() int64 {
panic(UnexpectedKind(semantic.Array, semantic.Int))
}
func (a *array) UInt() uint64 {
panic(UnexpectedKind(semantic.Array, semantic.UInt))
}
func (a *array) Float() float64 {
panic(UnexpectedKind(semantic.Array, semantic.Float))
}
func (a *array) Bool() bool {
panic(UnexpectedKind(semantic.Array, semantic.Bool))
}
func (a *array) Time() Time {
panic(UnexpectedKind(semantic.Array, semantic.Time))
}
func (a *array) Duration() Duration {
panic(UnexpectedKind(semantic.Array, semantic.Duration))
}
func (a *array) Regexp() *regexp.Regexp {
panic(UnexpectedKind(semantic.Array, semantic.Regexp))
}
func (a *array) Array() Array {
return a
}
func (a *array) Object() Object {
panic(UnexpectedKind(semantic.Array, semantic.Object))
}
func (a *array) Function() Function {
panic(UnexpectedKind(semantic.Array, semantic.Function))
}
func (a *array) Dict() Dictionary {
panic(UnexpectedKind(semantic.Array, semantic.Dictionary))
}
func (a *array) Vector() Vector {
panic(UnexpectedKind(semantic.Array, semantic.Vector))
}
func (a *array) Equal(rhs Value) bool {
if !a.Type().Equal(rhs.Type()) {
return false
}
r := rhs.Array()
if a.Len() != r.Len() {
return false
}
length := a.Len()
for i := 0; i < length; i++ {
aVal := a.Get(i)
rVal := r.Get(i)
if !aVal.Equal(rVal) {
return false
}
}
return true
}
func (a *array) Retain() {
a.Range(func(i int, v Value) {
v.Retain()
})
}
func (a *array) Release() {
a.Range(func(i int, v Value) {
v.Release()
})
}