-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCompare.hx
393 lines (338 loc) · 11.1 KB
/
Compare.hx
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
package moon.core;
import moon.core.Types.Comparable;
#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
using haxe.macro.TypeTools;
using haxe.macro.ComplexTypeTools;
#end
/**
* Comparison functions for sorting.
*
* @author Munir Hussin
*/
class Compare
{
/**
* General purpose ascending comparison function.
* Uses Reflect.compare.
* Usage:
* array.sort(Compare.asc);
*/
public static inline function asc<T>(a:T, b:T):Int
{
return Reflect.compare(a, b);
}
/**
* General purpose descending comparison function.
* Uses Reflect.compare.
* Usage:
* array.sort(Compare.desc);
*/
public static inline function desc<T>(a:T, b:T):Int
{
return Reflect.compare(b, a);
}
/**
* Compares a nullable type.
*
* When `nullOrder` is Asc, then null is smaller than non-null values,
* otherwise null is bigger than non-null values.
*
* Usage:
* array.sort(Compare.nullable(Asc, CompareString.asc));
*/
public static function nullable<T>(nullOrder:Order=Asc, cmp:T->T->Int):T->T->Int
{
var av:Int = nullOrder ? -1 : 1;
var bv:Int = nullOrder ? 1 : -1;
return function(a:Null<T>, b:Null<T>):Int
return a == null ? av: b == null ? bv: cmp(a, b);
}
/**
* General purpose comparison function. This handles null as well,
* and null is taken as smaller than non-null values.
*
* Usage:
* array.sort(Compare.any(Asc));
*/
public static inline function any<T>(order:Order=Asc):T->T->Int
{
return order ? nullable(Asc, Compare.asc): nullable(Desc, Compare.desc);
}
/**
* Create a comparison function from a comparable class.
* A comparable class is a class with a compareTo(x:T):Int where T is the class itself.
* This method is able to handle null, where null are smaller than non-null values.
*
* Usage:
* array.sort(Compare.obj(Asc)); // T is inferred from type of array.
* var cmp = Compare.obj(Asc); // T is monomorph until its used.
* var cmp = Compare.obj(Fruit, Asc); // Let T be Fruit.
*/
public static inline function obj<T:Comparable<T>>(cls:Class<T>=null, order:Order=Asc):T->T->Int
{
return order ?
nullable(Asc, function(a:T, b:T):Int return a.compareTo(b)):
nullable(Desc, function(a:T, b:T):Int return -a.compareTo(b));
}
/**
* Automatically infer if the needed type is a Comparable<T> or not using macro.
* If it is, this macro returns Compare.obj(order) otherwise Compare.any(order)
*
* Usage:
* array.sort(Compare.auto(Asc));
*/
public static macro function auto<T>(order:ExprOf<Order>):ExprOf<T->T->Int>
{
var expectedFn = Context.getExpectedType();
function isComparable(type:Type):Bool
{
var ct = type.toComplexType();
var comparableType = Context.typeof(macro (null:moon.core.Types.Comparable<$ct>));
return type.unify(comparableType);
}
return switch (expectedFn)
{
// A->B->Int where A and B unifies
case TFun([{t:a}, {t:b}],
TAbstract(_.get() => { module: "StdTypes", pack: [], name: "Int" }, []))
if (a.unify(b) && b.unify(a)):
if (isComparable(a))
macro moon.core.Compare.obj($order);
else
macro moon.core.Compare.any($order);
case _:
throw "Unsupported type. Expected T->T->Int";
}
}
/**
* Usage:
* array.sort(Compare.float(Asc));
*/
/*public static inline function float(order:Order=Asc):Float->Float->Int
{
return order ? CompareFloat.asc : CompareFloat.desc;
}*/
/**
* When sorting booleans, false is taken as smaller than true.
* Usage:
* array.sort(Compare.bool(Asc));
*/
/*public static inline function bool(order:Order=Asc):Bool->Bool->Int
{
return order ? CompareBool.asc : CompareBool.desc;
}*/
/**
* Usage:
* array.sort(Compare.string(Asc, CaseInsensitive, false));
*/
public static function string(order:Order=Asc, cs:CaseSensitivity=CaseSensitive, natural:Bool=false):String->String->Int
{
return
if (natural)
CompareString.natural(order, cs);
else if (cs)
order ? CompareString.asc : CompareString.desc;
else
order ? CompareString.asci : CompareString.desci;
}
/**
* Transform values to another type, and compares based on that type.
*
* Usage:
* array.sort(Compare.map(o=>o.weight, CompareFloat.asc));
*/
public static inline function map<T, U>(val:T->U, cmp:U->U->Int):T->T->Int
{
return function(a:T, b:T):Int return cmp(val(a), val(b));
}
/**
* Transform values to a comparable value (Int/Float/String), and then sort
* using Compare.asc or Compare.desc.
*
* This is slightly less verbose than using map.
*
* Usage:
* array.sort(Compare.by(o=>o.name, Asc));
*
* // using map
* array.sort(Compare.map(o=>o.name, Compare.asc));
*/
public static inline function by<T,U>(val:T->U, order:Order=Asc):T->T->Int
{
return order ? map(val, Compare.asc) : map(val, Compare.desc);
}
}
/**
* Compares two Float/Int
*/
/*class CompareFloat
{
public static inline function asc(a:Float, b:Float):Int
{
return Std.int(a - b);
}
public static inline function desc(a:Float, b:Float):Int
{
return Std.int(b - a);
}
}*/
/**
* As false is commonly associated with 0, false is taken as smaller than true.
*/
/*class CompareBool
{
public static inline function asc(a:Bool, b:Bool):Int
{
return a == b ? 0 : b ? -1 : 1;
}
public static inline function desc(a:Bool, b:Bool):Int
{
return a == b ? 0 : a ? -1 : 1;
}
}*/
/**
* Compares strings with optional case sensitivity or natural sorting.
*/
class CompareString
{
private static var rxDigits:EReg = ~/(\d+)/;
public static inline function asc(a:String, b:String):Int
{
return a == b ? 0 : a < b ? -1 : 1;
}
public static inline function desc(a:String, b:String):Int
{
return a == b ? 0 : a > b ? -1 : 1;
}
public static inline function asci(a:String, b:String):Int
{
a = a.toLowerCase();
b = b.toLowerCase();
return a == b ? 0 : a < b ? -1 : 1;
}
public static inline function desci(a:String, b:String):Int
{
a = a.toLowerCase();
b = b.toLowerCase();
return a == b ? 0 : a > b ? -1 : 1;
}
private static function naturalHelper(a:String, b:String, cs:CaseSensitivity, strCmpFn:String->String->Int, intCmpFn:Int->Int->Int):Int
{
var cmp:Int = 0;
var aText:String = "";
var bText:String = "";
var aDigits:String = "";
var bDigits:String = "";
if (!cs)
{
a = a.toLowerCase();
b = b.toLowerCase();
}
if (a == b)
return 0;
while (a.length > 0 && b.length > 0)
{
if (rxDigits.match(a))
{
// there's a digit somewhere inside
aText = rxDigits.matchedLeft();
aDigits = rxDigits.matched(1);
a = rxDigits.matchedRight();
}
else
{
// there's no digits at all
aText = a;
aDigits = "0";
a = "";
}
if (rxDigits.match(b))
{
// there's a digit somewhere inside
bText = rxDigits.matchedLeft();
bDigits = rxDigits.matched(1);
b = rxDigits.matchedRight();
}
else
{
// there's no digits at all
bText = b;
bDigits = "0";
b = "";
}
if (aText.length != bText.length)
{
// if lengths of the text are different
// i.e.: abc123 abcd12
// then just sort using the text
return strCmpFn(aText, bText);
}
else
{
// lengths of text are the same
cmp = strCmpFn(aText, bText);
// if the text are different, return that result
if (cmp != 0)
return cmp;
// text content are the same.
// so we sort by digits
// i.e.: abc123 abc42
// BUG: this is bad if digits exceed max int
cmp = intCmpFn(Std.parseInt(aDigits), Std.parseInt(bDigits));
// if the digits are different, return that result
if (cmp != 0)
return cmp;
// digits are the same!
// i.e: abc123 abc123
// get next sequence of text
}
}
return strCmpFn(a, b);
}
public static function naturalCustom(cs:CaseSensitivity=CaseInsensitive, strCmpFn:String->String->Int, intCmpFn:Int->Int->Int):String->String->Int
{
/*if (strCmpFn == null)
strCmpFn = CompareString.asc;
if (intCmpFn == null)
intCmpFn = CompareFloat.asc;*/
return function(a:String, b:String):Int
{
return naturalHelper(a, b, cs, strCmpFn, intCmpFn);
}
}
public static inline function natural(order:Order=Asc, cs:CaseSensitivity=CaseInsensitive):String->String->Int
{
return order ?
naturalCustom(cs, Compare.asc, Compare.asc):
naturalCustom(cs, Compare.desc, Compare.desc);
}
public static inline function naturalAsc(a:String, b:String):Int
{
return naturalCustom(CaseSensitive, Compare.asc, Compare.asc)(a, b);
}
public static inline function naturalDesc(a:String, b:String):Int
{
return naturalCustom(CaseSensitive, Compare.desc, Compare.desc)(a, b);
}
public static inline function naturalAsci(a:String, b:String):Int
{
return naturalCustom(CaseInsensitive, Compare.asc, Compare.asc)(a, b);
}
public static inline function naturalDesci(a:String, b:String):Int
{
return naturalCustom(CaseInsensitive, Compare.desc, Compare.desc)(a, b);
}
}
@:enum abstract Order(Bool) to Bool from Bool
{
var Asc = true;
var Desc = false;
}
@:enum abstract CaseSensitivity(Bool) to Bool from Bool
{
var CaseSensitive = true;
var CaseInsensitive = false;
}