/
equals.go
218 lines (197 loc) · 5.26 KB
/
equals.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
// File provided by the K Framework Go backend. Timestamp: 2019-08-13 18:25:08.138
package ieletestingmodel
import (
"bytes"
)
// Equals performs a deep comparison, recursively.
func (ms *ModelState) Equals(ref1 KReference, ref2 KReference) bool {
if ref1 == ref2 {
// identical references means the same object
return true
}
// int types
intEquals, isInt := ms.IntEquals(ref1, ref2)
if isInt {
return intEquals
}
refType1, dataRef1, value1 := parseKrefBasic(ref1)
refType2, dataRef2, value2 := parseKrefBasic(ref2)
// for non-int types, refTypes should be equal
if refType1 != refType2 {
return false
}
// collection types
if isCollectionType(refType1) {
_, _, _, _, index1 := parseKrefCollection(ref1)
_, _, _, _, index2 := parseKrefCollection(ref2)
obj1 := ms.getData(dataRef1).getReferencedObject(index1)
obj2 := ms.getData(dataRef2).getReferencedObject(index2)
return obj1.equals(ms, obj2)
}
switch refType1 {
case boolRef:
return false // if they were equal, ref1 == ref2 condition would already have returned true
case bottomRef:
panic("there shouldn't be different references of type bottomRef, only one")
case emptyKseqRef:
panic("there shouldn't be different references of type emptyKseqRef, only one")
case nonEmptyKseqRef:
return ms.ksequenceEquals(ref1, ref2)
case kapplyRef:
if ms.KApplyLabel(ref1) != ms.KApplyLabel(ref2) {
return false
}
if ms.KApplyArity(ref1) != ms.KApplyArity(ref2) {
return false
}
argSlice1 := ms.kapplyArgSlice(ref1)
argSlice2 := ms.kapplyArgSlice(ref2)
for i := 0; i < len(argSlice1); i++ {
if !ms.Equals(argSlice1[i], argSlice2[i]) {
return false
}
}
return true
case stringRef:
str1, _ := ms.GetString(ref1)
str2, _ := ms.GetString(ref2)
return str1 == str2
case bytesRef:
bytes1, _ := ms.GetBytes(ref1)
bytes2, _ := ms.GetBytes(ref2)
return bytes.Equal(bytes1, bytes2)
case ktokenRef:
_, _, sort1, length1, index1 := parseKrefKToken(ref1)
_, _, sort2, length2, index2 := parseKrefKToken(ref2)
if sort1 != sort2 {
return false
}
if length1 != length2 {
return false
}
val1 := ms.getData(dataRef1).allBytes[index1 : index1+length1]
val2 := ms.getData(dataRef2).allBytes[index2 : index2+length2]
return bytes.Equal(val1, val2)
default:
// object types
obj1 := ms.getData(dataRef1).getReferencedObject(value1)
obj2 := ms.getData(dataRef2).getReferencedObject(value2)
return obj1.equals(ms, obj2)
}
}
func (k *InjectedKLabel) equals(ms *ModelState, arg KObject) bool {
other, typeOk := arg.(*InjectedKLabel)
if !typeOk {
panic("equals between different types should have been handled during reference Equals")
}
if k.Label != other.Label {
return false
}
return true
}
func (k *KVariable) equals(ms *ModelState, arg KObject) bool {
other, typeOk := arg.(*KVariable)
if !typeOk {
panic("equals between different types should have been handled during reference Equals")
}
if k.Name != other.Name {
return false
}
return true
}
func (k *Map) equals(ms *ModelState, arg KObject) bool {
other, typeOk := arg.(*Map)
if !typeOk {
panic("equals between different types should have been handled during reference Equals")
}
if len(k.Data) != len(other.Data) {
return false
}
for key, val := range k.Data {
otherVal, found := other.Data[key]
if !found {
return false
}
if !ms.Equals(val, otherVal) {
return false
}
}
return true
}
func (k *List) equals(ms *ModelState, arg KObject) bool {
other, typeOk := arg.(*List)
if !typeOk {
panic("equals between different types should have been handled during reference Equals")
}
if k.Sort != other.Sort {
return false
}
if k.Label != other.Label {
return false
}
if len(k.Data) != len(other.Data) {
return false
}
for i := 0; i < len(k.Data); i++ {
if !ms.Equals(k.Data[i], other.Data[i]) {
return false
}
}
return true
}
func (k *Set) equals(ms *ModelState, arg KObject) bool {
other, typeOk := arg.(*Set)
if !typeOk {
panic("equals between different types should have been handled during reference Equals")
}
if len(k.Data) != len(other.Data) {
return false
}
for key := range k.Data {
_, found := other.Data[key]
if !found {
return false
}
}
return true
}
func (k *Array) equals(ms *ModelState, arg KObject) bool {
other, typeOk := arg.(*Array)
if !typeOk {
panic("equals between different types should have been handled during reference Equals")
}
if k.Sort != other.Sort {
return false
}
return k.Data.Equals(other.Data)
}
func (k *MInt) equals(ms *ModelState, arg KObject) bool {
other, typeOk := arg.(*MInt)
if !typeOk {
panic("equals between different types should have been handled during reference Equals")
}
return k.Value == other.Value
}
func (k *Float) equals(ms *ModelState, arg KObject) bool {
other, typeOk := arg.(*Float)
if !typeOk {
panic("equals between different types should have been handled during reference Equals")
}
return k.Value == other.Value
}
func (k *StringBuffer) equals(ms *ModelState, arg KObject) bool {
return k == arg
}
func (ms *ModelState) ksequenceEquals(ref1 KReference, ref2 KReference) bool {
s1 := ms.KSequenceToSlice(ref1)
s2 := ms.KSequenceToSlice(ref2)
if len(s1) != len(s2) {
return false
}
for i := 0; i < len(s1); i++ {
if !ms.Equals(s1[i], s2[i]) {
return false
}
}
return true
}