-
Notifications
You must be signed in to change notification settings - Fork 15
/
value_set_str.go
279 lines (239 loc) · 6.42 KB
/
value_set_str.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
package rel
import (
"reflect"
"github.com/arr-ai/wbnf/parser"
)
// StringCharAttr is the standard name for the value-attr of a character tuple.
const StringCharAttr = "@char"
// String is a set of Values.
type String struct {
s []rune
offset int
}
// NewString constructs an array as a relation.
func NewString(s []rune) Set {
return NewOffsetString(s, 0)
}
// NewString constructs an array as a relation.
func NewOffsetString(s []rune, offset int) Set {
if len(s) == 0 {
return None
}
return String{s: s, offset: offset}
}
func AsString(s Set) (String, bool) { //nolint:dupl
if s, ok := s.(String); ok {
return s, true
}
if i := s.Enumerator(); i.MoveNext() {
t, is := i.Current().(StringCharTuple)
if !is {
return String{}, false
}
middleIndex := s.Count()
strs := make([]rune, 2*middleIndex)
strs[middleIndex] = t.char
anchorOffset, minOffset := t.at, t.at
lowestIndex, highestIndex := middleIndex, middleIndex
for i.MoveNext() {
if t, is = i.Current().(StringCharTuple); !is {
return String{}, false
}
if t.at < minOffset {
minOffset = t.at
}
sliceIndex := middleIndex - (anchorOffset - t.at)
strs[sliceIndex] = t.char
if sliceIndex < lowestIndex {
lowestIndex = sliceIndex
}
if sliceIndex > highestIndex {
highestIndex = sliceIndex
}
}
return NewOffsetString(strs[lowestIndex:highestIndex+1], minOffset).(String), true
}
return String{}, true
}
// Hash computes a hash for a String.
func (s String) Hash(seed uintptr) uintptr {
// TODO: Optimize.
h := seed
for e := s.Enumerator(); e.MoveNext(); {
h ^= e.Current().Hash(seed)
}
return h
}
// Equal tests two Sets for equality. Any other type returns false.
func (s String) Equal(v interface{}) bool {
switch x := v.(type) {
case String:
if len(s.s) != len(x.s) {
return false
}
for i, c := range s.s {
if c != x.s[i] {
return false
}
}
return true
case Set:
return newSetFromSet(s).Equal(x)
}
return false
}
// String returns a string representation of a String.
func (s String) String() string {
return string(s.s)
}
// Eval returns the string.
func (s String) Eval(_ Scope) (Value, error) {
return s, nil
}
// Source returns a scanner locating the String's source code.
func (s String) Source() parser.Scanner {
return *parser.NewScanner("")
}
var stringKind = registerKind(204, reflect.TypeOf(String{}))
// Kind returns a number that is unique for each major kind of Value.
func (s String) Kind() int {
return stringKind
}
// Bool returns true iff the tuple has attributes.
func (s String) IsTrue() bool {
if len(s.s) == 0 {
panic("Empty string not allowed (should be == None)")
}
return true
}
// Less returns true iff v is not a number or tuple, or v is a tuple and t
// precedes v in a lexicographical comparison of their name/value pairs.
func (s String) Less(v Value) bool {
if s.Kind() != v.Kind() {
return s.Kind() < v.Kind()
}
return s.String() < v.(String).String()
}
// Negate returns {(negateTag): s}.
func (s String) Negate() Value {
return NewTuple(NewAttr(negateTag, s))
}
// Export exports a String as a string.
func (s String) Export() interface{} {
return string(s.s)
}
// Count returns the number of elements in the String.
func (s String) Count() int {
return len(s.s)
}
// Has returns true iff the given Value is in the String.
func (s String) Has(value Value) bool {
if t, ok := value.(StringCharTuple); ok {
if s.offset <= t.at && t.at < s.offset+len(s.s) {
return t.char == s.s[t.at-s.offset]
}
}
return false
}
func (s String) with(index int, char rune) Set {
if s.index(index) == len(s.s) {
return String{s: append(s.s, char), offset: s.offset}
} else if index == s.offset-1 {
return String{
s: append(append(make([]rune, 0, 1+len(s.s)), char), s.s...),
offset: s.offset - 1,
}
}
return newSetFromSet(s).With(NewStringCharTuple(index, char))
}
// With returns the original String with given value added. Iff the value was
// already present, the original String is returned.
func (s String) With(value Value) Set {
if t, ok := value.(StringCharTuple); ok {
return s.with(t.at, t.char)
}
return newSetFromSet(s).With(value)
}
// Without returns the original String without the given value. Iff the value
// was already absent, the original String is returned.
func (s String) Without(value Value) Set {
if t, ok := value.(StringCharTuple); ok {
if i := s.index(t.at); i >= 0 && i < len(s.s) && t.char == s.s[i] {
if t.at == s.offset+i {
return String{s: s.s[:i], offset: s.offset}
}
return newSetFromSet(s).Without(value)
}
}
return s
}
// Map maps values per f.
func (s String) Map(f func(v Value) Value) Set {
result := NewSet()
for e := s.Enumerator(); e.MoveNext(); {
result = result.With(f(e.Current()))
}
return result
}
// Where returns a new String with all the Values satisfying predicate p.
func (s String) Where(p func(v Value) (bool, error)) (Set, error) {
values := make([]Value, 0, s.Count())
for e := s.Enumerator(); e.MoveNext(); {
value := e.Current()
matches, err := p(value)
if err != nil {
return nil, err
}
if matches {
values = append(values, value)
}
}
return NewSet(values...), nil
}
func (s String) CallAll(arg Value) (Set, error) {
i := int(arg.(Number).Float64()) - s.offset
if i < 0 || i >= len(s.s) {
return None, nil
}
return NewSet(NewNumber(float64(string(s.s)[i]))), nil
}
func (s String) index(pos int) int {
pos -= s.offset
if 0 <= pos && pos <= len(s.s) {
return pos
}
return -1
}
// Enumerator returns an enumerator over the Values in the String.
func (s String) Enumerator() ValueEnumerator {
return &stringValueEnumerator{s: s, i: -1}
}
func (s String) ArrayEnumerator() (OffsetValueEnumerator, bool) {
return &stringOffsetValueEnumerator{stringValueEnumerator{s: s, i: -1}}, true
}
// StringEnumerator represents an enumerator over a String.
type stringValueEnumerator struct {
s String
i int
}
// MoveNext moves the enumerator to the next Value.
func (e *stringValueEnumerator) MoveNext() bool {
if e.i >= len(e.s.s)-1 {
return false
}
e.i++
return true
}
// Current returns the enumerator's current Value.
func (e *stringValueEnumerator) Current() Value {
return NewStringCharTuple(e.s.offset+e.i, e.s.s[e.i])
}
type stringOffsetValueEnumerator struct {
stringValueEnumerator
}
func (e *stringOffsetValueEnumerator) Current() Value {
return NewNumber(float64(e.s.s[e.i]))
}
func (e *stringOffsetValueEnumerator) Offset() int {
return e.s.offset + e.i
}