-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAny.hx
More file actions
270 lines (214 loc) · 7.64 KB
/
Any.hx
File metadata and controls
270 lines (214 loc) · 7.64 KB
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
package moon.core;
import Type;
import moon.core.Compare.CompareString;
/**
* ...
* @author Munir Hussin
*/
abstract Any(Dynamic)
{
@:to public inline function toType<T>():T return this;
@:from public static inline function fromType<T>(v:T):Any return new Any(v);
public function new(v:Dynamic) this = v;
/*==================================================
Methods
==================================================*/
public inline function isNull():Bool
{
return this == null;
}
public inline function isString():Bool
{
return Std.is(this, String);
}
public inline function isArray():Bool
{
return Std.is(this, Array);
}
public inline function isEnumValue():Bool
{
return Reflect.isEnumValue(this);
}
public inline function isFunction():Bool
{
return Reflect.isFunction(this);
}
public inline function isObject():Bool
{
return Reflect.isObject(this);
}
public inline function type():ValueType
{
return Type.typeof(this);
}
/*==================================================
Static methods
==================================================*/
public static inline function notEquals(a:Any, b:Any):Bool
{
return !equals(a, b);
}
public static inline function notDeepEquals(a:Any, b:Any):Bool
{
return !deepEquals(a, b);
}
public static function equals(a:Any, b:Any):Bool
{
return switch ([Type.typeof(a), Type.typeof(b)])
{
case [TEnum(p), TEnum(q)]:
p != q ? false : Type.enumEq(a, b);
case _:
a == b;
}
}
/**
* Anonymous objects, arrays and enum value parameters can
* have different references, but if their internal values
* are the same, they're equal.
*
* Instances of different classes will never be equal, even if
* the fields and values are the same.
*
* TODO: Test circular references.
*/
public static function deepEquals(a:Any, b:Any):Bool
{
return checkDeepEquals(a, b, new Memo());
}
private static function checkDeepEquals(a:Any, b:Any, memo:Memo):Bool
{
if (a == b)
{
// same references
return true;
}
else switch ([Type.typeof(a), Type.typeof(b)])
{
case
[TNull, TNull]
| [TInt, TInt]
| [TFloat, TFloat]
| [TFloat, TInt]
| [TInt, TFloat]
| [TBool, TBool]
| [TClass(String), TClass(String)]:
//throw "Unexpected case";
return a == b;
case [TFunction, TFunction]:
return Reflect.compareMethods(a, b);
case [TObject, TObject]:
if (memo.contains(a, b))
{
return true;
}
else
{
memo.push(a, b);
var ak:Array<String> = Reflect.fields(a);
var bk:Array<String> = Reflect.fields(b);
ak.sort(Compare.asc);
bk.sort(Compare.asc);
// different keys means not equal
if (!checkDeepEquals(ak, bk, memo)) return false;
// check every field
for (k in ak)
{
var av:Dynamic = Reflect.field(a, k);
var bv:Dynamic = Reflect.field(b, k);
// found a value that doesn't match, so not equal
if (!checkDeepEquals(av, bv, memo)) return false;
}
// the same!
return true;
}
case [TClass(Array), TClass(Array)]:
if (memo.contains(a, b))
{
return true;
}
else
{
memo.push(a, b);
var ax:Array<Dynamic> = a;
var bx:Array<Dynamic> = b;
// different lengths means not equal
if (ax.length != bx.length) return false;
// check every value
for (i in 0...ax.length)
{
if (!checkDeepEquals(ax[i], bx[i], memo)) return false;
}
// the same!
return true;
}
case [TEnum(p), TEnum(q)]:
if (memo.contains(a, b))
{
return true;
}
else
{
memo.push(a, b);
// different enum types
if (p != q) return false;
var ae:EnumValue = a;
var be:EnumValue = b;
// not using enumEq since we need to run them through deepEquals
if (ae.getIndex() != be.getIndex()) return false;
return checkDeepEquals(ae.getParameters(), be.getParameters(), memo);
}
case [TClass(p), TClass(q)]:
// different classes
if (p != q) return false;
if (memo.contains(a, b))
{
return true;
}
else
{
memo.push(a, b);
// same class should have same keys
var ak:Array<String> = Reflect.fields(a);
//var bk:Array<String> = Reflect.fields(b);
//ak.sort(Compare.asc);
//bk.sort(Compare.asc);
// different keys means not equal
//if (!checkDeepEquals(ak, bk, memo)) return false;
// check every field
for (k in ak)
{
var av:Dynamic = Reflect.getProperty(a, k);
var bv:Dynamic = Reflect.getProperty(b, k);
// found a value that doesn't match, so not equal
if (!checkDeepEquals(av, bv, memo)) return false;
}
// the same!
return true;
}
case [p, q]:
// different types
if (p != q) return false;
// same values
return a == b;
}
}
}
private class Memo
{
public var a:Array<Dynamic> = [];
public var b:Array<Dynamic> = [];
public function new()
{
}
public function contains(x:Dynamic, y:Dynamic):Bool
{
var i = a.indexOf(x);
return i != -1 && i == b.indexOf(y);
}
public function push(x:Dynamic, y:Dynamic):Void
{
a.push(x);
b.push(y);
}
}