/
indexable.go
275 lines (214 loc) · 7.94 KB
/
indexable.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
package core
import (
"strconv"
"sync"
"slices"
"github.com/inoxlang/inox/internal/core/symbolic"
)
var _ = []Indexable{
(*String)(nil), (*Array)(nil), (*List)(nil), (*Tuple)(nil), (*RuneSlice)(nil), (*ByteSlice)(nil),
(*IntRange)(nil), (*RuneRange)(nil), (*OrderedPair)(nil), KeyList{},
}
type Indexable interface {
Iterable
// At should panic if the index is out of bounds.
At(ctx *Context, i int) Value
Len() int
}
type Array []Value
func NewArrayFrom(elements ...Value) *Array {
if elements == nil {
elements = []Value{}
}
array := Array(elements)
return &array
}
func NewArray(ctx *Context, elements ...Value) *Array {
return NewArrayFrom(elements...)
}
func (a *Array) At(ctx *Context, i int) Value {
return (*a)[i]
}
func (a *Array) Len() int {
return len(*a)
}
func (a *Array) slice(start int, end int) Sequence {
slice := (*a)[start:end]
return &slice
}
type KeyList []string
func (l KeyList) At(ctx *Context, i int) Value {
return String(l[i])
}
func (l KeyList) Len() int {
return len(l)
}
// A List represents a sequence of elements, List implements Value.
// The elements are stored in an underlyingList that is suited for the number and kind of elements, for example
// if the elements are all integers the underlying list will (ideally) be an *IntList.
type List struct {
underlyingList
elemType Pattern
lock sync.Mutex // exclusive access for initializing .watchers & .mutationCallbacks
mutationCallbacks *MutationCallbacks
watchers *ValueWatchers
watchingDepth WatchingDepth
elementMutationCallbacks []CallbackHandle
}
func newList(underlyingList underlyingList) *List {
return &List{underlyingList: underlyingList}
}
func WrapUnderlyingList(l underlyingList) *List {
return &List{underlyingList: l}
}
// the caller can modify the result.
func (list *List) GetOrBuildElements(ctx *Context) []Serializable {
entries := IterateAll(ctx, list.Iterator(ctx, IteratorConfiguration{}))
values := make([]Serializable, len(entries))
for i, e := range entries {
values[i] = e[1].(Serializable)
}
return values
}
func (l *List) Prop(ctx *Context, name string) Value {
switch name {
case "append":
return WrapGoMethod(l.append)
case "dequeue":
return WrapGoMethod(l.Dequeue)
case "pop":
return WrapGoMethod(l.Pop)
case "sorted":
return WrapGoMethod(l.Sorted)
case "sort_by":
return WrapGoMethod(l.SortBy)
case "len":
return Int(l.Len())
default:
panic(FormatErrPropertyDoesNotExist(name, l))
}
}
func (*List) SetProp(ctx *Context, name string, value Value) error {
return ErrCannotSetProp
}
func (*List) PropertyNames(ctx *Context) []string {
return symbolic.LIST_PROPNAMES
}
func (l *List) set(ctx *Context, i int, v Value) {
prevElement := l.underlyingList.At(ctx, i)
l.underlyingList.set(ctx, i, v)
if l.elementMutationCallbacks != nil {
l.removeElementMutationCallbackNoLock(ctx, i, prevElement.(Serializable))
l.addElementMutationCallbackNoLock(ctx, i, v)
}
mutation := NewSetElemAtIndexMutation(ctx, i, v.(Serializable), ShallowWatching, Path("/"+strconv.Itoa(i)))
//inform watchers & microtasks about the update
l.watchers.InformAboutAsync(ctx, mutation, mutation.Depth, true)
l.mutationCallbacks.CallMicrotasks(ctx, mutation)
}
func (l *List) SetSlice(ctx *Context, start, end int, seq Sequence) {
if l.elementMutationCallbacks != nil {
for i := start; i < end; i++ {
prevElement := l.underlyingList.At(ctx, i)
l.removeElementMutationCallbackNoLock(ctx, i, prevElement.(Serializable))
}
}
l.underlyingList.SetSlice(ctx, start, end, seq)
if l.elementMutationCallbacks != nil {
for i := start; i < end; i++ {
l.addElementMutationCallbackNoLock(ctx, i, l.underlyingList.At(ctx, i))
}
}
path := Path("/" + strconv.Itoa(int(start)) + ".." + strconv.Itoa(int(end-1)))
mutation := NewSetSliceAtRangeMutation(ctx, NewIntRange(int64(start), int64(end-1)), seq.(Serializable), ShallowWatching, path)
l.mutationCallbacks.CallMicrotasks(ctx, mutation)
l.watchers.InformAboutAsync(ctx, mutation, ShallowWatching, true)
}
func (l *List) insertElement(ctx *Context, v Value, i Int) {
l.underlyingList.insertElement(ctx, v, i)
if l.elementMutationCallbacks != nil {
l.elementMutationCallbacks = slices.Insert(l.elementMutationCallbacks, int(i), FIRST_VALID_CALLBACK_HANDLE-1)
l.addElementMutationCallbackNoLock(ctx, int(i), v)
}
mutation := NewInsertElemAtIndexMutation(ctx, int(i), v.(Serializable), ShallowWatching, Path("/"+strconv.Itoa(int(i))))
//inform watchers & microtasks about the update
l.watchers.InformAboutAsync(ctx, mutation, mutation.Depth, true)
l.mutationCallbacks.CallMicrotasks(ctx, mutation)
}
func (l *List) insertSequence(ctx *Context, seq Sequence, i Int) {
l.underlyingList.insertSequence(ctx, seq, i)
if l.elementMutationCallbacks != nil {
seqLen := seq.Len()
l.elementMutationCallbacks = slices.Insert(l.elementMutationCallbacks, int(i), makeMutationCallbackHandles(seqLen)...)
seqIndex := 0
for index := i; index < i+Int(seqLen); index++ {
l.addElementMutationCallbackNoLock(ctx, int(index), seq.At(ctx, seqIndex))
seqIndex++
}
}
mutation := NewInsertSequenceAtIndexMutation(ctx, int(i), seq, ShallowWatching, Path("/"+strconv.Itoa(int(i))))
//inform watchers & microtasks about the update
l.watchers.InformAboutAsync(ctx, mutation, mutation.Depth, true)
l.mutationCallbacks.CallMicrotasks(ctx, mutation)
}
func (l *List) appendSequence(ctx *Context, seq Sequence) {
l.insertSequence(ctx, seq, Int(l.Len()))
}
func (l *List) append(ctx *Context, elements ...Serializable) {
index := l.Len()
l.underlyingList.append(ctx, elements...)
seq := NewWrappedValueList(elements...)
if l.elementMutationCallbacks != nil {
seqLen := seq.Len()
l.elementMutationCallbacks = slices.Insert(l.elementMutationCallbacks, index, makeMutationCallbackHandles(seqLen)...)
for i := index; i < index+len(elements); i++ {
l.addElementMutationCallbackNoLock(ctx, int(i), seq.At(ctx, int(i-index)))
}
}
mutation := NewInsertSequenceAtIndexMutation(ctx, index, seq, ShallowWatching, Path("/"+strconv.Itoa(index)))
//inform watchers & microtasks about the update
l.watchers.InformAboutAsync(ctx, mutation, mutation.Depth, true)
l.mutationCallbacks.CallMicrotasks(ctx, mutation)
}
func (l *List) removePosition(ctx *Context, i Int) {
l.underlyingList.removePosition(ctx, i)
if l.elementMutationCallbacks != nil {
l.removeElementMutationCallbackNoLock(ctx, int(i), l.underlyingList.At(ctx, int(i)).(Serializable))
l.elementMutationCallbacks = slices.Replace(l.elementMutationCallbacks, int(i), int(i+1))
}
mutation := NewRemovePositionMutation(ctx, int(i), ShallowWatching, Path("/"+strconv.Itoa(int(i))))
//inform watchers & microtasks about the update
l.watchers.InformAboutAsync(ctx, mutation, mutation.Depth, true)
l.mutationCallbacks.CallMicrotasks(ctx, mutation)
}
func (l *List) Dequeue(ctx *Context) Serializable {
if l.Len() == 0 {
panic(ErrCannotDequeueFromEmptyList)
}
elem := l.At(ctx, 0)
l.removePosition(ctx, 0)
return elem.(Serializable)
}
func (l *List) Pop(ctx *Context) Serializable {
lastIndex := l.Len() - 1
if lastIndex < 0 {
panic(ErrCannotPopFromEmptyList)
}
elem := l.At(ctx, lastIndex)
l.removePosition(ctx, Int(lastIndex))
return elem.(Serializable)
}
func (l *List) removePositionRange(ctx *Context, r IntRange) {
l.underlyingList.removePositionRange(ctx, r)
if l.elementMutationCallbacks != nil {
for index := int(r.start); index < int(r.end); index++ {
l.removeElementMutationCallbackNoLock(ctx, index, l.underlyingList.At(ctx, index).(Serializable))
}
l.elementMutationCallbacks = slices.Replace(l.elementMutationCallbacks, int(r.start), int(r.end))
}
path := Path("/" + strconv.Itoa(int(r.KnownStart())) + ".." + strconv.Itoa(int(r.InclusiveEnd())))
mutation := NewRemovePositionRangeMutation(ctx, r, ShallowWatching, path)
//inform watchers & microtasks about the update
l.watchers.InformAboutAsync(ctx, mutation, mutation.Depth, true)
l.mutationCallbacks.CallMicrotasks(ctx, mutation)
}