-
Notifications
You must be signed in to change notification settings - Fork 2
/
value.go
443 lines (355 loc) · 10.3 KB
/
value.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright 2019 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package diviner
import (
"encoding/gob"
"fmt"
"hash"
"hash/fnv"
"sort"
"strings"
"github.com/grailbio/base/writehash"
)
func init() {
gob.Register(Float(0))
gob.Register(Int(0))
gob.Register(String(""))
gob.Register(Bool(false))
gob.Register(List{})
gob.Register(Dict{})
gob.Register(&Map{})
}
// Kind represents the kind of a value.
type Kind int
const (
Integer Kind = iota
Real
Str
Seq
ValueDict
Boolean
)
func (k Kind) String() string {
switch k {
case Integer:
return "integer"
case Real:
return "real"
case Str:
return "string"
case Seq:
return "seq"
case ValueDict:
return "valuedict"
case Boolean:
return "boolean"
default:
panic(k)
}
}
// Value is the type of parameter values. Values must be
// directly comparable.
type Value interface {
// String returns a textual description of the parameter value.
String() string
// Kind returns the kind of this value.
Kind() Kind
// Equal tells whether two values are equal. Values of different
// kinds are never equal.
Equal(Value) bool
// Less returns true if the value is less than the provided value.
// Less is defined only for values of the same type.
Less(Value) bool
// Float returns the floating point value of float-typed values.
Float() float64
// Int returns the integer value of integer-typed values.
Int() int64
// Str returns the string of string-typed values.
Str() string
// Bool returns the boolean of boolean-typed values.
Bool() bool
// Len returns the length of a sequence value.
Len() int
// Index returns the value at an index of a sequence.
Index(i int) Value
// Hash adds the value's hask to the provided hasher.
Hash(h hash.Hash)
}
// Zero returns the zero value for the provided kind.
func Zero(kind Kind) Value {
switch kind {
default:
panic(kind)
case Integer:
return Int(0)
case Real:
return Float(0)
case Str:
return String("")
case ValueDict:
return new(Values)
case Boolean:
return Bool(false)
case Seq:
return new(List)
}
}
// Int is an integer-typed value.
type Int int64
// String implements Value.
func (v Int) String() string { return fmt.Sprint(int64(v)) }
// Kind implements Value.
func (Int) Kind() Kind { return Integer }
func (v Int) Equal(w Value) bool {
if w.Kind() != Integer {
return false
}
return v.Int() == w.Int()
}
// Less implements Value.
func (v Int) Less(w Value) bool {
return v.Int() < w.Int()
}
// Float implements Value.
func (Int) Float() float64 { panic("Float on Int") }
// Str implements Value.
func (Int) Str() string { panic("Str on Int") }
// Int implements Value.
func (v Int) Int() int64 { return int64(v) }
func (Int) Bool() bool { panic("Bool on Int") }
func (Int) Len() int { panic("Len on Int") }
func (Int) Index(int) Value { panic("Index on Int") }
func (Int) Put(key string, value Value) { panic("Put on Int") }
func (Int) Get(key string) Value { panic("Get on Int") }
func (Int) Range(func(key string, value Value)) { panic("Range on Int") }
func (v Int) Hash(h hash.Hash) {
writehash.Uint64(h, uint64(v))
}
// Float is a float-typed value.
type Float float64
// String implements Value.
func (v Float) String() string { return fmt.Sprint(float64(v)) }
// Kind implements Value.
func (Float) Kind() Kind { return Real }
func (v Float) Equal(w Value) bool {
if w.Kind() != Real {
return false
}
return v.Float() == w.Float()
}
// Less implements Value.
func (v Float) Less(w Value) bool {
return v.Float() < w.Float()
}
// Float implements Value.
func (v Float) Float() float64 { return float64(v) }
// Str implements Value.
func (Float) Str() string { panic("Str on Float") }
// Int implements Value.
func (Float) Int() int64 { panic("Int on Float") }
func (Float) Bool() bool { panic("Bool on Float") }
func (Float) Len() int { panic("Len on Float") }
func (Float) Index(int) Value { panic("Index on Float") }
func (Float) Put(key string, value Value) { panic("Put on Float") }
func (Float) Get(key string) Value { panic("Get on Float") }
func (Float) Range(func(key string, value Value)) { panic("Range on Float") }
func (v Float) Hash(h hash.Hash) {
writehash.Float64(h, v.Float())
}
// String is a string-typed value.
type String string
func (v String) Equal(w Value) bool {
if w.Kind() != Str {
return false
}
return v.Str() == w.Str()
}
// Less implements Value.
func (v String) Less(w Value) bool {
return v.Str() < w.Str()
}
// String implements Value.
func (v String) String() string { return string(v) }
// Kind implements Value.
func (String) Kind() Kind { return Str }
// Float implements Value.
func (String) Float() float64 { panic("Float on String") }
// Int implements Value.
func (String) Int() int64 { panic("Int on String") }
func (String) Bool() bool { panic("Bool on String") }
// Str implements Value.
func (v String) Str() string { return string(v) }
func (String) Len() int { panic("Len on String") }
func (String) Index(int) Value { panic("Index on String") }
func (String) Put(key string, value Value) { panic("Put on String") }
func (String) Get(key string) Value { panic("Get on String") }
func (String) Range(func(key string, value Value)) { panic("Range on String") }
func (v String) Hash(h hash.Hash) {
writehash.String(h, v.Str())
}
// Bool is a boolean-typed value.
type Bool bool
// String implements Value.
func (v Bool) String() string { return fmt.Sprint(bool(v)) }
// Kind implements Value.
func (Bool) Kind() Kind { return Boolean }
func (v Bool) Equal(w Value) bool {
if w.Kind() != Boolean {
return false
}
return v.Bool() == w.Bool()
}
// Less implements Value.
func (v Bool) Less(w Value) bool {
return !v.Bool() && w.Bool()
}
// Bool implements Value.
func (v Bool) Float() float64 { panic("Float on Bool") }
// Str implements Value.
func (Bool) Str() string { panic("Str on Bool") }
// Int implements Value.
func (Bool) Int() int64 { panic("Int on Bool") }
// Bool implements Value.
func (v Bool) Bool() bool { return bool(v) }
func (Bool) Len() int { panic("Len on Bool") }
func (Bool) Index(int) Value { panic("Index on Bool") }
func (Bool) Put(key string, value Value) { panic("Put on Bool") }
func (Bool) Get(key string) Value { panic("Get on Bool") }
func (Bool) Range(func(key string, value Value)) { panic("Range on Bool") }
func (v Bool) Hash(h hash.Hash) {
writehash.Bool(h, bool(v))
}
// List is a list-typed value.
type List []Value
func (l List) Equal(m Value) bool {
if m.Kind() != Seq {
return false
}
if l.Len() != m.Len() {
return false
}
for i := 0; i < l.Len(); i++ {
if !l.Index(i).Equal(m.Index(i)) {
return false
}
}
return true
}
// Less implements Value.
func (l List) Less(m Value) bool {
for i := 0; i < l.Len(); i++ {
if m.Len() <= i {
break
}
if l.Index(i).Less(m.Index(i)) {
return true
} else if m.Index(i).Less(l.Index(i)) {
return false
}
}
return l.Len() < m.Len()
}
// String implements Value.
func (l List) String() string {
elems := make([]string, l.Len())
for i := range elems {
elems[i] = l.Index(i).String()
}
return "[" + strings.Join(elems, ", ") + "]"
}
// Kind implements Value.
func (List) Kind() Kind { return Seq }
// Float implements Value.
func (List) Float() float64 { panic("Float on List") }
// Int implements Value.
func (List) Int() int64 { panic("Int on List") }
// Str implements Value.
func (List) Str() string { panic("Str on List") }
func (List) Bool() bool { panic("Bool on List") }
// Len returns the length of the list.
func (l List) Len() int { return len(l) }
// Index returns the ith element of the list.
func (l List) Index(i int) Value { return l[i] }
func (List) Put(key string, value Value) { panic("Put on *List") }
func (List) Get(key string) Value { panic("Get on *List") }
func (List) Range(func(key string, value Value)) { panic("Range on *List") }
func (l List) Hash(h hash.Hash) {
for _, v := range l {
v.Hash(h)
}
}
// A NamedValue is a value that is assigned a name.
type NamedValue struct {
Name string
Value
}
// Values is a sorted set of named values, used as a concrete
// instantiation of a set of parameters.
//
// Note: the representation of Values is not optimal. Ideally we
// would store it as a sorted list of NamedValues. However, backwards
// compatibility (by way of gob) forces our hand here.
type Values map[string]Value
// Dict is a Value that represents a map from string to a Value.
type Dict = Values
// String returns a (stable) textual description of the value set.
func (v Values) String() string {
elems := make([]string, v.Len())
for i, v := range v.Sorted() {
elems[i] = fmt.Sprintf("%s=%s", v.Name, v.Value)
}
return strings.Join(elems, ",")
}
func (Values) Kind() Kind { return ValueDict }
func (v Values) Equal(wv Value) bool {
w, ok := wv.(Values)
if !ok {
// For backward compatibility. Older diviner used *Value as well as Value.
wp, ok := wv.(*Values)
if !ok {
return false
}
w = *wp
}
if v.Len() != w.Len() {
return false
}
vlist, wlist := v.Sorted(), w.Sorted()
for i := range vlist {
if vlist[i].Name != wlist[i].Name {
return false
}
if !vlist[i].Value.Equal(wlist[i].Value) {
return false
}
}
return true
}
func (Values) Less(Value) bool { panic("Less on Values") }
func (Values) Float() float64 { panic("Float on Values") }
func (Values) Int() int64 { panic("Int on Values") }
func (Values) Str() string { panic("Str on Values") }
func (Values) Bool() bool { panic("Bool on Values") }
func (v Values) Len() int { return len(v) }
func (Values) Index(i int) Value { panic("Index on Values") }
func (v Values) Hash(h hash.Hash) {
for _, v := range v.Sorted() {
String(v.Name).Hash(h)
v.Value.Hash(h)
}
}
func (v Values) Sorted() []NamedValue {
vals := make([]NamedValue, 0, len(v))
for k, v := range v {
vals = append(vals, NamedValue{k, v})
}
sort.Slice(vals, func(i, j int) bool { return vals[i].Name < vals[j].Name })
return vals
}
// Hash returns a 64-bit hash for the value v.
func Hash(v Value) uint64 {
h := fnv.New64a()
v.Hash(h)
return h.Sum64()
}