This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdicts.go
143 lines (129 loc) · 3.21 KB
/
dicts.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
package goal
import "fmt"
// D represents a dictionary.
type D struct {
keys Array
values Array
}
// NewD returns a dictionary. Both keys and values should be arrays, and
// they should have the same length.
func NewD(keys, values V) V {
xv, ok := keys.bv.(Array)
if !ok {
panic(fmt.Sprintf("NewD(keys, values) : keys not an array (%s)", keys.Type()))
}
yv, ok := values.bv.(Array)
if !ok {
panic(fmt.Sprintf("NewD(keys, values) : values not an array (%s)", values.Type()))
}
if xv.Len() != yv.Len() {
panic(fmt.Sprintf("NewD(keys, values) : length mismatch (%d vs %d)", xv.Len(), yv.Len()))
}
return NewV(&D{keys: xv, values: yv})
}
func newDictValues(keys Array, values V) V {
if values.IsPanic() {
return values
}
v := values.bv.(Array)
return NewV(&D{keys: keys, values: v})
}
// Keys returns the keys of the dictionary.
func (d *D) Keys() V {
return NewV(d.keys)
}
// Values returns the values of the dictionary.
func (d *D) Values() V {
return NewV(d.values)
}
// Matches returns true if the two values match like in x~y.
func (d *D) Matches(y BV) bool {
switch yv := y.(type) {
case *D:
return d.keys.Matches(yv.keys) && d.values.Matches(yv.values)
default:
return false
}
}
// Type returns the name of the value's type.
func (d *D) Type() string {
return "d"
}
// Len returns the length of the dictionary, that is the common length to its
// key and value arrays.
func (d *D) Len() int {
return d.keys.Len()
}
func dict(x, y V) V {
if x.IsI() {
return moddivpad(x.I(), y)
}
if x.IsF() {
if !isI(x.F()) {
return Panicf("i!y : non-integer i (%g)", x.F())
}
return moddivpad(int64(x.F()), y)
}
xv, ok := x.bv.(Array)
if !ok {
return panicType("X!Y", "X", x)
}
yv, ok := y.bv.(Array)
if !ok {
return panicType("X!Y", "X", x)
}
if xv.Len() != yv.Len() {
return panicLength("X!Y", xv.Len(), yv.Len())
}
return NewV(&D{keys: xv, values: yv})
}
func dictAmendKVI(xd *D, yk Array) (Array, Array, V) {
keys, values := xd.keys, xd.values.sclone()
ykv := NewV(yk)
yk.IncrRC()
ky := findArray(keys, ykv)
nkeys := keys.Len()
max := maxIndices(ky)
if max == int64(nkeys) {
b := equalIV(max, ky)
flags := keys.getFlags() & flagDistinct
keys = join(NewV(keys), distinct(replicate(b, ykv))).bv.(Array)
keys.setFlags(flags)
values = padArrayMut(keys.Len()-nkeys, values)
ky = findArray(keys, ykv)
}
yk.DecrRC()
return keys, values, ky
}
func dictMerge(xd, yd *D) V {
keys, values, ky := dictAmendKVI(xd, yd.keys)
var r Array
switch kyv := ky.bv.(type) {
case *AB:
r = mergeAtIs(values, kyv.elts, yd.values)
case *AI:
r = mergeAtIs(values, kyv.elts, yd.values)
}
return NewV(&D{keys: keys, values: canonicalArray(r)})
}
func mergeAtIs[I integer](x Array, y []I, z Array) Array {
if sameType(x, z) {
return amend4RightIsATs(x, y, z)
}
return amend4RightIsArrays(x, y, z)
}
func dictArith(xd, yd *D, f func(V, V) V) V {
keys, values, ky := dictAmendKVI(xd, yd.keys)
var err error
var r Array
switch kyv := ky.bv.(type) {
case *AB:
r, err = arithAmendIsArray(values, kyv.elts, f, yd.values)
case *AI:
r, err = arithAmendIsArray(values, kyv.elts, f, yd.values)
}
if err != nil {
return Panicf("%v", err)
}
return NewV(&D{keys: keys, values: canonicalArray(r)})
}