Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bugfixes, adding Function sorting.

  • Loading branch information...
commit 0d1cc9c657b3a9a3b3363254c264741ef50f96b2 1 parent fab5843
@skyboy authored
Showing with 648 additions and 353 deletions.
  1. +648 −353 skyboy/utils/fastSort.as
View
1,001 skyboy/utils/fastSort.as
@@ -58,21 +58,26 @@ package skyboy.utils {
/**
* fastSort(* obj, uint options [, uint length, uint startIndex] );
* fastSort(* obj, String name, uint options [, uint length, uint startIndex] );
+ * fastSort(* obj, Function sortFunc, uint options [, uint length, uint startIndex] );
+ * fastSort(* obj, Function sortFunc, String name, uint options [, uint length, uint startIndex] );
*
- * @param *: input The object to be sorted. Either an Array, Vector, or any Object (or subclass of) that has a length property and numeric indicies
- * @param *: rest0 Either options (pass in the same you would for Array's sort method) or a String to trigger sortOn functionality (def: stringSort)
- * @param *: rest1 Either options if rest0 is a String or length (def: stringSort or input.length)
- * @param *: rest2 Either length if rest0 is String or startIndex for sorting (def: input.length or 0)
- * @param *: rest3 Either startIndex if rest0 is String or undefined (def: 0 or undefined)
+ * @param *: input The object to be sorted. Any object that has numeric indicies. (required, non-null)
+ * @param Function: sortFunc A function to be passed two objects being sorted that returns a negative number when a < b, positive when b > a and 0 when a == b. (optional)
+ * @param String: name The name of the property to be sorted on. (optional)
+ * @param uint: options Sorting options. 16 for numeric sort, 2 for descending sort, 1 for case insensitive sort, 4 to force String sort when sorting with a Function (the default). Options can be combined with |. (optional, default: 0)
+ * @param uint: length The length of the Object to be sorted. (optional if the object has a length property, required otherwise)
+ * @param uint: startIndex The index to be sorting at. (optional, default: 0)
*/
public function fastSort(input:*, ...rest):void {
//public function fastSort(input:*, rest0:* = 0, rest1:* = undefined, rest2:* = undefined, rest3:* = undefined):void {
if (!input) throw new ArgumentError("Can not sort null");
- var sortON:Boolean = rest[0] is String;
- var optI:uint = int(sortON);
+ var funcO:Boolean = rest[0] is Function;
+ var strI:uint = uint(funcO);
+ var sortON:Boolean = rest[strI] is String;
+ var optI:uint = strI + uint(sortON);
var lenI:uint = 1 + optI;
var startI:uint = 2 + optI;
- if (("length" in input) && (input.length is int)) {// grab length from the input
+ if (("length" in input) && (input.length is Number)) {// grab length from the input
length = input.length;
if (!(rest[startI] is Number)) rest[startI] = 0;
if (rest[lenI] is Number) length = rest[lenI];
@@ -83,42 +88,44 @@ package skyboy.utils {
}
if (!(rest[optI] is Number)) rest[optI] = 0;
optI = rest[optI];
- if (sortON) {
+ optI &= ~(-int(Boolean(optI & FORCESTRING)) & NUMERIC);
+ if (optI & NUMERIC) numVec.length = length + rest[startI];
+ else sortVec.length = length + rest[startI];
+ if (funcO) {
+ if (sortON) sortByOn(input, rest[strI], optI, length, rest[startI], rest[0]);
+ else sortBy(input, optI, length, rest[startI], rest[0]);
+ } else if (sortON) {
if (optI & NUMERIC) {
- numVec.length = length + rest[startI];
- sortOnNumber(input, rest[0], optI, length, rest[startI]);
+ sortOnNumber(input, rest[0], optI, length, rest[startI]);// all values (gain from Number Vector more significant than strong typing sorted-value)
} else {
- sortVec.length = length + rest[startI];
if (input is Array) {
sortOnArray(input, rest[0], optI, length, rest[startI]);
- } else if (input is Vector.<*>) {// numeric vectors not included
+ } else if (input is Vector.<*>) {// numeric vectors not included (how would one sortOn a range of Numbers?)
sortOn(input, rest[0], optI, length, rest[startI]);
} else {
sortOnObject(input, rest[0], optI, length, rest[startI]);
}
}
} else {
- if (!(optI & NUMERIC)) sortVec.length = length + rest[startI];
f = LookupTable[input.constructor];
if (Boolean(f)) f(input, optI, length, rest[startI]);
else sortObject(input, optI, length, rest[startI]);
}
sortVec.length = 0;// clear the vectors (O(1) operation)
- tempVec.length = 0;
numVec.length = 0;
return;
- var length:uint, f:Function;// avoid compiler setting default values for these
+ var length:uint = 0, f:Function = null;// avoid compiler setting default values for these
}
}
-import flash.system.System;
-import flash.utils.Dictionary;
//{
+import flash.utils.Dictionary;
internal const NUMERIC:uint = Array.NUMERIC;
internal const DESCENDING:uint = Array.DESCENDING;
internal const CASEINSENSITIVE:uint = Array.CASEINSENSITIVE;
+internal const FORCESTRING:uint = Array.UNIQUESORT;
+// internal const STABLESORT:uint = Array.RETURNINDEXEDARRAY;
internal const sortVec:Vector.<*> = new Vector.<*>(0xFFFF);
-internal const tempVec:Vector.<*> = new Vector.<*>(0xFFFF);
internal const numVec:Vector.<Number> = new Vector.<Number>(0xFFFF);
internal const LookupTable:Dictionary = new Dictionary;
LookupTable[Vector.<Number>] = sortNumber;
@@ -130,7 +137,7 @@ internal function quickSort(input:Vector.<*>, left:uint, right:uint, d:int):void
if (left >= right) return;
var j:uint = right, i:uint = left;
var size:uint = right - left;
- var pivotPoint:* = input[(right + left) >>> 1], t:*;
+ var pivotPoint:* = input[uint((right >>> 1) + (left >>> 1))], t:*;
do {
if (size < 9) {
pivotPoint = input[left];
@@ -174,7 +181,7 @@ internal function quickSort(input:Vector.<*>, left:uint, right:uint, d:int):void
if (j <= left) return;
i = left;
right = j;
- pivotPoint = input[(right + left) >>> 1];
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
size = right - left;
++d;
} while (true);
@@ -184,7 +191,7 @@ internal function quickSortOn(input:Vector.<*>, sInput:Vector.<*>, left:int, rig
var j:int = right;
var i:int = left;
var size:int = right - left;
- var pivotPoint:* = input[(right + left) >>> 1], t:*;
+ var pivotPoint:* = input[uint((right >>> 1) + (left >>> 1))], t:*;
do {
if (size < 9) {
do {
@@ -238,7 +245,7 @@ internal function quickSortOn(input:Vector.<*>, sInput:Vector.<*>, left:int, rig
if (j <= left) return;
i = left;
right = j;
- pivotPoint = input[(right + left) >>> 1];
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
size = right - left;
++d;
} while (true);
@@ -247,7 +254,7 @@ internal function quickSortArray(input:Array, left:int, right:int, d:int):void {
if (left >= right) return;
var j:int = right, i:int = left;
var size:int = right - left;
- var pivotPoint:* = input[(right + left) >>> 1], t:*;
+ var pivotPoint:* = input[uint((right >>> 1) + (left >>> 1))], t:*;
do {
if (size < 9) {
pivotPoint = input[left];
@@ -291,7 +298,7 @@ internal function quickSortArray(input:Array, left:int, right:int, d:int):void {
if (j <= left) return;
i = left;
right = j;
- pivotPoint = input[(right + left) >>> 1];
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
size = right - left;
++d;
} while (true);
@@ -301,7 +308,7 @@ internal function quickSortOnArray(input:Vector.<*>, sInput:Array, left:int, rig
var j:int = right;
var i:int = left;
var size:int = right - left;
- var pivotPoint:* = input[(right + left) >>> 1], t:*;
+ var pivotPoint:* = input[uint((right >>> 1) + (left >>> 1))], t:*;
do {
if (size < 9) {
do {
@@ -355,7 +362,7 @@ internal function quickSortOnArray(input:Vector.<*>, sInput:Array, left:int, rig
if (j <= left) return;
i = left;
right = j;
- pivotPoint = input[(right + left) >>> 1];
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
size = right - left;
++d;
} while (true);
@@ -364,7 +371,7 @@ internal function quickSortObject(input:Object, left:uint, right:uint, d:int):vo
if (left >= right) return;
var j:uint = right, i:uint = left;
var size:uint = right - left;
- var pivotPoint:* = input[(right + left) >>> 1], t:*;
+ var pivotPoint:* = input[uint((right >>> 1) + (left >>> 1))], t:*;
do {
if (size < 9) {
pivotPoint = input[left];
@@ -408,7 +415,7 @@ internal function quickSortObject(input:Object, left:uint, right:uint, d:int):vo
if (j <= left) return;
i = left;
right = j;
- pivotPoint = input[(right + left) >>> 1];
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
size = right - left;
++d;
} while (true);
@@ -418,7 +425,7 @@ internal function quickSortOnObject(input:Vector.<*>, sInput:Object, left:int, r
var j:int = right;
var i:int = left;
var size:int = right - left;
- var pivotPoint:* = input[(right + left) >>> 1], t:*;
+ var pivotPoint:* = input[uint((right >>> 1) + (left >>> 1))], t:*;
do {
if (size < 9) {
do {
@@ -472,7 +479,7 @@ internal function quickSortOnObject(input:Vector.<*>, sInput:Object, left:int, r
if (j <= left) return;
i = left;
right = j;
- pivotPoint = input[(right + left) >>> 1];
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
size = right - left;
++d;
} while (true);
@@ -482,22 +489,22 @@ internal function quickSortOnNumber(input:Vector.<Number>, sInput:Object, left:i
var j:int = right;
var i:int = left;
var size:int = right - left;
- var pivotPoint:Number = input[(right + left) >>> 1];
+ var pivotPoint:Number = input[uint((right >>> 1) + (left >>> 1))];
do {
if (size < 9) {
do {
pivotPoint = input[left];
do {
++left;
- if (pivotPoint > input[left]) {
- pivotPoint = input[left];
+ e = input[left];
+ if (pivotPoint > e) {
+ pivotPoint = e;
t = sInput[left];
do {
- q = left - 1;
- e = input[q];
- input[left] = e;
- sInput[left] = sInput[q];
- left = q;
+ q = left--;
+ e = input[left];
+ input[q] = e;
+ sInput[q] = sInput[left];
} while (int(left > i) & int(pivotPoint < e));
input[left] = pivotPoint;
sInput[left] = t;
@@ -509,15 +516,154 @@ internal function quickSortOnNumber(input:Vector.<Number>, sInput:Object, left:i
return;
}
while (left < right) {
- if (input[right] > pivotPoint) do {
+ f = input[right];
+ while (f > pivotPoint) {
--right;
- } while (input[right] > pivotPoint);
- if (input[left] < pivotPoint) do {
+ f = input[right];
+ }
+ e = input[left];
+ while (e < pivotPoint) {
++left;
- } while (input[left] < pivotPoint);
+ e = input[left];
+ }
if (left < right) {
+ input[left] = f;
+ input[right] = e;
+ t = sInput[left];
+ sInput[left] = sInput[right];
+ sInput[right] = t;
+ ++left, --right;
+ }
+ }
+ if (left === right) {
+ e = input[left];
+ q = int(e >= pivotPoint);
+ q &= int(right > 0);
+ right -= q;
+ q = int(e <= pivotPoint);
+ left += q;
+ }
+ if (i < right)
+ quickSortOnNumber(input, sInput, i, right, d + 1);
+ if (j > left) {
+ i = left;
+ right = j;
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
+ size = right - left;
+ ++d;
+ } else break
+ } while (true);
+ return;
+ var e:Number = 0, f:Number = 0, t:* = undefined, q:int = 0;
+}
+internal function quickSortByObject(input:Object, left:int, right:int, c:Function):void { // >0 = a>b; <0 = a<b; =0 = b==a
+ if (left >= right) return;
+ var j:int = right;
+ var i:int = left;
+ var size:int = right - left;
+ var pivotPoint:* = input[uint((right >>> 1) + (left >>> 1))], t:*, e:*;
+ do {
+ if (size < 9) {
+ do {
+ pivotPoint = input[left];
+ do {
+ ++left;
+ e = input[left];
+ if (c(pivotPoint, e) > 0) {
+ pivotPoint = e;
+ do {
+ size = left--;
+ e = input[left];
+ input[size] = e;
+ } while (left > i && c(pivotPoint, e) < 0);
+ input[left] = pivotPoint;
+ }
+ } while (left < right);
+ ++i;
+ left = i;
+ } while (i < right);
+ return;
+ }
+ while (left < right) {
+ t = input[right];
+ while (c(t, pivotPoint) > 0) {
+ --right;
+ t = input[right];
+ }
+ e = input[left];
+ while (c(e, pivotPoint) < 0) {
+ ++left;
e = input[left];
- input[left] = input[right];
+ }
+ if (left < right) {
+ input[left] = t;
+ input[right] = e;
+ ++left, --right;
+ }
+ }
+ if (left === right) {
+ e = input[left];
+ t = c(e, pivotPoint)
+ size = int(t >= 0);
+ size &= int(right > 0);
+ right -= size;
+ size = int(t <= 0);
+ left += size;
+ }
+ if (i < right) {
+ quickSortByObject(input, i, right, c);
+ }
+ if (j <= left) return;
+ i = left;
+ right = j;
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
+ size = right - left;
+ } while (true);
+}
+internal function quickSortByOnObject(input:Vector.<*>, sInput:Object, left:int, right:int, c:Function):void { // >0 = a>b; <0 = a<b; =0 = b==a
+ if (left >= right) return;
+ var j:int = right;
+ var i:int = left;
+ var size:int = right - left;
+ var pivotPoint:* = input[uint((right >>> 1) + (left >>> 1))], t:*, e:*;
+ do {
+ if (size < 9) {
+ do {
+ pivotPoint = input[left];
+ do {
+ ++left;
+ e = input[left];
+ if (c(pivotPoint, e) > 0) {
+ pivotPoint = e;
+ t = sInput[left];
+ do {
+ size = left--;
+ e = input[left];
+ input[size] = e;
+ sInput[size] = sInput[left];
+ } while (left > i && c(pivotPoint, e) < 0);
+ input[left] = pivotPoint;
+ sInput[left] = t;
+ }
+ } while (left < right);
+ ++i;
+ left = i;
+ } while (i < right);
+ return;
+ }
+ while (left < right) {
+ t = input[right];
+ while (c(t, pivotPoint) > 0) {
+ --right;
+ t = input[right];
+ }
+ e = input[left];
+ while (c(e, pivotPoint) < 0) {
+ ++left;
+ e = input[left];
+ }
+ if (left < right) {
+ input[left] = t;
input[right] = e;
t = sInput[left];
sInput[left] = sInput[right];
@@ -525,43 +671,114 @@ internal function quickSortOnNumber(input:Vector.<Number>, sInput:Object, left:i
++left, --right;
}
}
- if (right) {
- if (left === right) {
+ if (left === right) {
+ e = input[left];
+ t = c(e, pivotPoint)
+ size = int(t >= 0);
+ size &= int(right > 0);
+ right -= size;
+ size = int(t <= 0);
+ left += size;
+ }
+ if (i < right) {
+ quickSortByOnObject(input, sInput, i, right, c);
+ }
+ if (j <= left) return;
+ i = left;
+ right = j;
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
+ size = right - left;
+ } while (true);
+}
+internal function quickSortByOnNumber(input:Vector.<Number>, sInput:Object, left:int, right:int, c:Function):void { // >0 = a>b; <0 = a<b; =0 = b==a
+ if (left >= right) return;
+ var j:int = right;
+ var i:int = left;
+ var size:int = right - left;
+ var pivotPoint:Number = input[uint((right >>> 1) + (left >>> 1))], t:Number, e:Number, o:*;
+ do {
+ if (size < 9) {
+ do {
+ pivotPoint = input[left];
+ do {
+ ++left;
+ e = input[left];
+ if (c(pivotPoint, e) > 0) {
+ pivotPoint = e;
+ o = sInput[left];
+ do {
+ size = left--;
+ e = input[left];
+ input[size] = e;
+ sInput[size] = sInput[left];
+ } while (left > i && c(pivotPoint, e) < 0);
+ input[left] = pivotPoint;
+ sInput[left] = o;
+ }
+ } while (left < right);
+ ++i;
+ left = i;
+ } while (i < right);
+ return;
+ }
+ while (left < right) {
+ t = input[right];
+ while (c(t, pivotPoint) > 0) {
+ --right;
+ t = input[right];
+ }
+ e = input[left];
+ while (c(e, pivotPoint) < 0) {
+ ++left;
e = input[left];
- right -= int(e >= pivotPoint);
- left += int(e <= pivotPoint);
}
- if (i < right) {
- quickSortOnNumber(input, sInput, i, right, d + 1);
+ if (left < right) {
+ input[left] = t;
+ input[right] = e;
+ o = sInput[left];
+ sInput[left] = sInput[right];
+ sInput[right] = o;
+ ++left, --right;
}
}
- left |= int(!left) & int(!right);
+ if (left === right) {
+ e = input[left];
+ t = c(e, pivotPoint)
+ size = int(t >= 0);
+ size &= int(right > 0);
+ right -= size;
+ size = int(t <= 0);
+ left += size;
+ }
+ if (i < right) {
+ quickSortByOnNumber(input, sInput, i, right, c);
+ }
if (j <= left) return;
i = left;
right = j;
- pivotPoint = input[(right + left) >>> 1];
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
size = right - left;
- ++d;
} while (true);
- return;
- var e:Number, t:*, q:int;
}
internal function quickSortInt(input:Vector.<int>, left:uint, right:uint, d:int):void {
if (left >= right) return;
var j:uint = right, i:uint = left;
var size:uint = right - left;
- var pivotPoint:int = input[(right + left) >>> 1], t:int;
+ var pivotPoint:int = input[uint((right >>> 1) + (left >>> 1))], t:int;
do {
if (size < 9) {
pivotPoint = input[left];
do {
do {
++left;
- if (input[left] < pivotPoint) {
- pivotPoint = input[left];
- do { // this section can be improved.
- input[left--] = input[left];
- } while (left > i && pivotPoint < input[left]);
+ t = input[left];
+ if (t < pivotPoint) {
+ pivotPoint = t;
+ do {
+ size = left--;
+ t = input[left];
+ input[size] = t;
+ } while (left > i && pivotPoint < t);
input[left] = pivotPoint;
}
} while (left < right);
@@ -594,7 +811,7 @@ internal function quickSortInt(input:Vector.<int>, left:uint, right:uint, d:int)
if (j <= left) return;
i = left;
right = j;
- pivotPoint = input[(right + left) >>> 1];
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
size = right - left;
++d;
} while (true);
@@ -603,7 +820,7 @@ internal function quickSortUint(input:Vector.<uint>, left:uint, right:uint, d:in
if (left >= right) return;
var j:uint = right, i:uint = left;
var size:uint = right - left;
- var pivotPoint:uint = input[(right + left) >>> 1], t:uint;
+ var pivotPoint:uint = input[uint((right >>> 1) + (left >>> 1))], t:uint;
do {
if (size < 9) {
pivotPoint = input[left];
@@ -647,7 +864,7 @@ internal function quickSortUint(input:Vector.<uint>, left:uint, right:uint, d:in
if (j <= left) return;
i = left;
right = j;
- pivotPoint = input[(right + left) >>> 1];
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
size = right - left;
++d;
} while (true);
@@ -656,7 +873,7 @@ internal function quickSortNumber(input:Vector.<Number>, left:uint, right:uint,
if (left >= right) return;
var j:uint = right, i:uint = left;
var size:uint = right - left;
- var pivotPoint:Number = input[(right + left) >>> 1], t:Number;
+ var pivotPoint:Number = input[uint((right >>> 1) + (left >>> 1))], t:Number;
do {
if (size < 9) {
pivotPoint = input[left];
@@ -700,16 +917,14 @@ internal function quickSortNumber(input:Vector.<Number>, left:uint, right:uint,
if (j <= left) return;
i = left;
right = j;
- pivotPoint = input[(right + left) >>> 1];
+ pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
size = right - left;
++d;
} while (true);
}
-internal function sort(input:Vector.<*>, options:uint, length:uint, startIndex:uint):void {
- var n:uint = length;
+internal function sort(input:Vector.<*>, options:uint, n:uint, startIndex:uint):void {
if (n < 2) return;
var q:uint = startIndex, left:uint = q, right:uint = q + n;
- var t:*;
while (q !== right) {
t = input[q];
if (t === undefined) {
@@ -724,188 +939,178 @@ internal function sort(input:Vector.<*>, options:uint, length:uint, startIndex:u
} else ++q;
}
if (right > left) {
- q = left;
- while (q < right) {
+ q = right;
+ while (q > left) {
+ --q;
t = input[q];
if (t !== t) {
--right;
input[q] = input[right];
input[right] = NaN;
- } else ++q;
+ }
}
- if ((--right,right)) {
- if (uint(right - 1) > left) {
- if (options === NUMERIC) {
- quickSort(input, left, right, 0);
+ --right;
+ if (right > left) {
+ if (options === NUMERIC) {
+ quickSort(input, left, right, 0);
+ } else {
+ tempVec = sortVec;
+ q = right;
+ if (options & CASEINSENSITIVE) {
+ tempVec[q] = String(input[q]).toLowerCase();
+ while (q > left) --q, tempVec[q] = String(input[q]).toLowerCase();
} else {
- var tempVec:Vector.<*> = sortVec;
- q = right;
- if (!(options & NUMERIC)) if (options & CASEINSENSITIVE) {
- tempVec[q] = String(input[q]).toLowerCase();
- while (q-- > left) tempVec[q] = String(input[q]).toLowerCase();
- } else {
- tempVec[q] = String(input[q]);
- while (q-- > left) tempVec[q] = String(input[q]);
- }
- quickSortOn(tempVec, input, left, right, 0);
+ tempVec[q] = String(input[q]);
+ while (q > left) --q, tempVec[q] = String(input[q]);
}
+ quickSortOn(tempVec, input, left, right, 0);
}
}
}
if (options & DESCENDING) {
- var i:uint = startIndex;
- n += i;
- while (n > i) {
- t = input[i];
- input[i] = input[(--n,n)];
+ options = startIndex;
+ n += options;
+ while (n > options) {
+ --n;
+ t = input[options];
+ input[options] = input[n];
input[n] = t;
- ++i;
+ ++options;
}
}
+ return;
+ var tempVec:Vector.<*>, t:*;
}
-internal function sortOn(input:Vector.<*>, name:String, options:uint, length:int, startIndex:int):void {
- var n:int = length;
+internal function sortOn(input:Vector.<*>, name:String, options:uint, n:uint, startIndex:int):void {
if (n < 2) return;
- var left:int = startIndex, right:int = left + n;
- var tempVec:Vector.<*> = sortVec, i:int = right, j:int = n, t:*;
- while (j) {--j;
+ var left:uint = startIndex, right:uint = left + n, hnan:int;
+ var tempVec:Vector.<*> = sortVec, i:uint = right, j:uint = right, t:*;
+ while (j > left) {--j;
t = input[j];
- t = name in t ? t[name] : t;
+ t = t[name];
if (t === null) {
t = input[j];
input[j] = input[left];
input[left] = t;
tempVec[left] = null;
- ++left;
- ++j;
+ ++left,++j;
} else if (t === undefined) {
- if ((--i,i) !== (--right,right)) {
- tempVec[i] = tempVec[right];
- tempVec[right] = undefined;
- t = input[right];
- input[right] = input[j];
- input[j] = t;
- }
- } else tempVec[(--i,i)] = t;
- if (j === left) break;
+ --i,--right
+ tempVec[i] = tempVec[right];
+ tempVec[right] = undefined;
+ t = input[right];
+ input[right] = input[j];
+ input[j] = t;
+ } else--i, tempVec[i] = t;
+ hnan |= int(t !== t);
}
if (right > left) {
j = right;
- while ((--j,j) > left) {
+ if (hnan) while (j > left) {
+ --j;
t = tempVec[j];
if (t !== t) {
- if (j !== (--right,right)) {
- tempVec[j] = tempVec[right];
- tempVec[right] = NaN;
- t = input[right];
- input[right] = input[j];
- input[j] = t;
- }
+ --right
+ tempVec[j] = tempVec[right];
+ tempVec[right] = NaN;
+ t = input[right];
+ input[right] = input[j];
+ input[j] = t;
}
}
- if ((--right,right)) {
- if (right - 1 > left) {
- if (!(options & NUMERIC)) {
- i = left;
- if (options & CASEINSENSITIVE) {
- tempVec[i] = String(tempVec[i]).toUpperCase();
- while (i < right) ++i, tempVec[i] = String(tempVec[i]).toUpperCase();
- } else {
- tempVec[i] = String(tempVec[i]);
- while (i < right) ++i, tempVec[i] = String(tempVec[i]);
- }
- }
- quickSortOn(tempVec, input, left, right, 0);
+ --right;
+ if (right > left) {
+ i = left;
+ if (options & CASEINSENSITIVE) {
+ tempVec[i] = String(tempVec[i]).toUpperCase();
+ while (i < right) ++i, tempVec[i] = String(tempVec[i]).toUpperCase();
+ } else {
+ tempVec[i] = String(tempVec[i]);
+ while (i < right) ++i, tempVec[i] = String(tempVec[i]);
}
+ quickSortOn(tempVec, input, left, right, 0);
}
}
if (options & DESCENDING) {
- i = startIndex, n += i;
- if (n & 1) {
- t = input[i];
- input[i] = input[(--n,n)];
- input[n] = t;
- ++i;
- }
- n >>>= 1;
+ i = startIndex,
+ options = n + i;
while (n > i) {
+ --options;
t = input[i];
- input[i] = input[(--n,n)];
- input[n] = t;
- ++i;
- t = input[i];
- input[i] = input[(--n,n)];
- input[n] = t;
+ input[i] = input[options];
+ input[options] = t;
++i;
}
}
}
-internal function sortArray(input:Array, options:uint, length:uint, startIndex:uint):void {
- var n:uint = length;
+internal function sortArray(input:Array, options:uint, n:uint, startIndex:uint):void {
if (n < 2) return;
- var q:uint = startIndex, left:uint = q, right:uint = q + n;
- var t:*;
- while (q !== right) {
- t = input[q];
- if (t === undefined) {
- --right;
- input[q] = input[right];
- input[right] = undefined;
- } else if (t === null) {
- input[q] = input[left];
- input[left] = null;
- ++left;
- ++q;
- } else ++q;
- }
- if (right > left) {
- q = left;
- while (q < right) {
+ if (options & NUMERIC) {
+ sortObjectNumber(input, startIndex, n);
+ } else {
+ q = startIndex, left = q, right = q + n, hnan = 1;
+ while (q !== right) {
t = input[q];
- if (t !== t) {
+ if (t === undefined) {
--right;
input[q] = input[right];
- input[right] = NaN;
+ input[right] = undefined;
+ } else if (t === null) {
+ input[q] = input[left];
+ input[left] = null;
+ ++left;
+ ++q;
} else ++q;
+ hnan &= int(t === t);
}
- if ((--right,right)) {
- if (uint(right - 1) > left) {
- if (options === NUMERIC) {
- quickSortArray(input, left, right, 0);
+ if (right > left) {
+ q = right;
+ if (!hnan) while (q > left) {
+ --q;
+ t = input[q];
+ if (t !== t) {
+ --right;
+ input[q] = input[right];
+ input[right] = NaN;
+ }
+ }
+ --right;
+ if (right > left) {
+ tempVec = sortVec;
+ q = right;
+ if (options & CASEINSENSITIVE) {
+ tempVec[q] = String(input[q]).toLowerCase();
+ while (q > left) --q, tempVec[q] = String(input[q]).toLowerCase();
} else {
- var tempVec:Vector.<*> = sortVec;
- q = right;
- if (!(options & NUMERIC)) if (options & CASEINSENSITIVE) {
- tempVec[q] = String(input[q]).toLowerCase();
- while (q > left) --q, tempVec[q] = String(input[q]).toLowerCase();
- } else {
- tempVec[q] = String(input[q]);
- while (q > left) --q, tempVec[q] = String(input[q]);
- }
- quickSortOnArray(tempVec, input, left, right, 0);
+ tempVec[q] = String(input[q]);
+ while (q > left) --q, tempVec[q] = String(input[q]);
}
+ quickSortOnArray(tempVec, input, left, right, 0);
}
}
}
if (options & DESCENDING) {
- var i:uint = startIndex;
- n += i;
- while (n > i) {
- t = input[i];
- input[i] = input[(--n,n)];
+ options = startIndex;
+ n += options;
+ while (n > options) {
+ --n;
+ t = input[options];
+ input[options] = input[n];
input[n] = t;
- ++i;
+ ++options;
}
}
+ return;
+ var tempVec:Vector.<*>, t:*, q:uint, left:uint, right:uint, hnan:int;
}
-internal function sortOnArray(input:Array, name:String, options:uint, length:uint, startIndex:uint):void {
- var n:uint = length;
+internal function sortOnArray(input:Array, name:String, options:uint, n:uint, startIndex:uint):void {
if (n < 2) return;
var left:uint = startIndex, right:uint = left + n;
- var tempVec:Vector.<*> = sortVec, i:uint = right, t:*, j:uint = n;
- while (j) {--j;
+ var tempVec:Vector.<*> = sortVec, i:uint = right, j:uint = n;
+ while (j) {
+ --j;
t = input[j];
- t = name in t ? t[name] : t;
+ t = t[name];
if (t === null) {
t = input[j];
input[j] = input[left];
@@ -914,112 +1119,115 @@ internal function sortOnArray(input:Array, name:String, options:uint, length:uin
++left;
++j;
} else if (t === undefined) {
- if ((--i,i) !== (--right,right)) {
- tempVec[i] = tempVec[right];
- tempVec[right] = undefined;
- t = input[right];
- input[right] = input[j];
- input[j] = t;
- }
- } else tempVec[(--i,i)] = t;
+ --i,--right;
+ tempVec[i] = tempVec[right];
+ tempVec[right] = undefined;
+ t = input[right];
+ input[right] = input[j];
+ input[j] = t;
+ } else --i,tempVec[i] = t;
if (j === left) break;
}
if (right > left) {
j = right;
- while ((--j,j) > left) {
+ while (j > left) {
+ --j;
t = tempVec[j];
if (t !== t) {
- if (j !== (--right,right)) {
- tempVec[j] = tempVec[right];
- tempVec[right] = NaN;
- t = input[right];
- input[right] = input[j];
- input[j] = t;
- }
+ --right;
+ tempVec[j] = tempVec[right];
+ tempVec[right] = NaN;
+ t = input[right];
+ input[right] = input[j];
+ input[j] = t;
}
}
- if ((--right,right)) {
- if (uint(right - 1) > left) {
- if (!(options & NUMERIC)) {
- i = right;
- if (options & CASEINSENSITIVE) {
- tempVec[i] = String(tempVec[i]).toUpperCase();
- while (i > left) --i, tempVec[i] = String(tempVec[i]).toUpperCase();
- } else {
- tempVec[i] = String(tempVec[i]);
- while (i > left) --i, tempVec[i] = String(tempVec[i]);
- }
- }
- quickSortOnArray(tempVec, input, left, right, 0);
+ --right;
+ if (right > left) {
+ i = right;
+ if (options & CASEINSENSITIVE) {
+ tempVec[i] = String(tempVec[i]).toUpperCase();
+ while (i > left) --i, tempVec[i] = String(tempVec[i]).toUpperCase();
+ } else {
+ tempVec[i] = String(tempVec[i]);
+ while (i > left) --i, tempVec[i] = String(tempVec[i]);
}
+ quickSortOnArray(tempVec, input, left, right, 0);
}
}
if (options & DESCENDING) {
- i = startIndex, n += i;
- while (n > i) {
+ i = startIndex;
+ options = i + n;
+ while (options > i) {
+ --options;
t = input[i];
- input[i] = input[(--n,n)];
- input[n] = t;
+ input[i] = input[options];
+ input[options] = t;
++i;
}
}
+ return;
+ var t:*;
}
-internal function sortObject(input:Object, options:uint, length:uint, startIndex:uint):void {
- var n:int = length;
+internal function sortObject(input:Object, options:uint, n:uint, startIndex:uint):void {
if (n < 2) return;
- var q:int = startIndex, left:uint = q, right:uint = q + n;
- var t:*;
- while (uint(q) !== right) {
- t = input[q];
- if (t === undefined) {
- --right;
- input[q] = input[right];
- input[right] = undefined;
- } else if (t === null) {
- input[q] = input[left];
- input[left] = null;
- ++left;
- ++q;
- } else ++q;
- }
- if (right > left) {
- q = left;
- while (uint(q) < right) {
+ if (options & NUMERIC) {
+ sortObjectNumber(input, startIndex, n);
+ } else {
+ q = startIndex, left = q, right = q + n, hnan = 1;
+ while (q < right) {
t = input[q];
- if (t !== t) {
+ if (t === undefined) {
--right;
input[q] = input[right];
- input[right] = NaN;
+ input[right] = undefined;
+ } else if (t === null) {
+ input[q] = input[left];
+ input[left] = null;
+ ++left;
+ ++q;
} else ++q;
+ hnan &= int(t === t);
}
- if ((--right,right)) {
- if (uint(right - 1) > left) {
- if (options & NUMERIC) {
- quickSortObject(input, left, right, 0);
+ if (right > left) {
+ q = right;
+ if (!hnan) while (q < left) {
+ --q;
+ t = input[q];
+ if (t !== t) {
+ --right;
+ input[q] = input[right];
+ input[right] = NaN;
+ }
+ }
+ --right;
+ if (right > left) {
+ tempVec = sortVec;
+ q = right;
+ if (options & CASEINSENSITIVE) {
+ tempVec[q] = String(input[q]).toLowerCase();
+ while (q > left) --q, tempVec[q] = String(input[q]).toLowerCase();
} else {
- var tempVec:Vector.<*> = sortVec;
- q = right;
- if (options & CASEINSENSITIVE) {
- tempVec[q] = String(input[q]).toLowerCase();
- while (q > left) --q, tempVec[q] = String(input[q]).toLowerCase();
- } else {
- tempVec[q] = String(input[q]);
- while (q > left) --q, tempVec[q] = String(input[q]);
- }
- quickSortOnObject(tempVec, input, left, right, 0);
+ tempVec[q] = String(input[q]);
+ while (q > left) --q, tempVec[q] = String(input[q]);
}
+ quickSortOnObject(tempVec, input, left, right, 0);
}
}
}
if (options & DESCENDING) {
- var i:int = 0;
- while (n > i) {
- t = input[i];
- input[i] = input[(--n,n)];
+ options = startIndex;
+ n += options;
+ while (n > options) {
+ --n;
+ t = input[options];
+ input[options] = input[n];
input[n] = t;
- ++i;
+ ++options;
}
}
+ return;
+ var tempVec:Vector.<*>, t:*, q:uint, left:uint, right:uint, hnan:int;
}
internal function sortOnObject(input:Object, name:String, options:uint, n:uint, startIndex:uint):void {
if (n < 2) return;
@@ -1027,100 +1235,113 @@ internal function sortOnObject(input:Object, name:String, options:uint, n:uint,
var tempVec:Vector.<*> = sortVec, i:uint = right, t:*, j:uint = right;
while (j > left) {--j;
t = input[j];
- t = name in t ? t[name] : t;
+ t = t[name];
if (t === null) {
t = input[j];
input[j] = input[left];
input[left] = t;
tempVec[left] = null;
- ++left;
- ++j;
+ ++left,++j;
} else if (t === undefined) {
- if ((--i,i) !== (--right,right)) {
- tempVec[i] = tempVec[right];
- tempVec[right] = undefined;
- t = input[right];
- input[right] = input[j];
- input[j] = t;
- }
- } else tempVec[(--i,i)] = t;
- if (j === left) break;
+ --right,--i;
+ tempVec[i] = tempVec[right];
+ tempVec[right] = undefined;
+ t = input[right];
+ input[right] = input[j];
+ input[j] = t;
+ } else --i,tempVec[i] = t;
}
if (right > left) {
j = right;
while (j > left) { --j;
t = tempVec[j];
if (t !== t) {
- if (j !== (--right,right)) {
- tempVec[j] = tempVec[right];
- tempVec[right] = NaN;
- t = input[right];
- input[right] = input[j];
- input[j] = t;
- }
+ --right;
+ tempVec[j] = tempVec[right];
+ tempVec[right] = NaN;
+ t = input[right];
+ input[right] = input[j];
+ input[j] = t;
}
}
--right;
- if (right) {
- if (uint(right - 1) > left) {
- if (!(options & NUMERIC)) {
- i = right;
- if (options & CASEINSENSITIVE) {
- tempVec[i] = String(tempVec[i]).toUpperCase();
- while (i > left) --i, tempVec[i] = String(tempVec[i]).toUpperCase();
- } else {
- tempVec[i] = String(tempVec[i]);
- while (i > left) --i, tempVec[i] = String(tempVec[i]);
- }
- }
- quickSortOnObject(tempVec, input, left, right, 0);
+ if (right > left) {
+ i = right;
+ if (options & CASEINSENSITIVE) {
+ tempVec[i] = String(tempVec[i]).toUpperCase();
+ while (i > left) --i, tempVec[i] = String(tempVec[i]).toUpperCase();
+ } else {
+ tempVec[i] = String(tempVec[i]);
+ while (i > left) --i, tempVec[i] = String(tempVec[i]);
}
+ quickSortOnObject(tempVec, input, left, right, 0);
}
}
if (options & DESCENDING) {
- i = startIndex, n += i;
- --n;
+ i = startIndex
+ options = i + n - 1;
while (n > i) {
t = input[i];
- input[i] = input[n];
- input[n] = t;
- ++i, --n;
+ input[i] = input[options];
+ input[options] = t;
+ ++i, --options;
}
}
}
+internal function sortObjectNumber(input:Object, startIndex:uint, n:uint):void {
+ var left:uint = startIndex;
+ n += startIndex;
+ var dat:Vector.<Number> = numVec, s:int = 1, p:Number = input[startIndex];
+ for (; startIndex < n; ++startIndex) {
+ e = input[startIndex];
+ dat[startIndex] = e;
+ s &= int(!(e > p));
+ p = e;
+ if (e !== e) {
+ --n;
+ input[startIndex] = input[n];
+ input[n] = e;
+ --startIndex;
+ }
+ }
+ --n;
+ if (!s) quickSortOnNumber(dat, input, left, n, 0);
+ return;
+ var e:Number = 0;
+}
internal function sortOnNumber(input:Object, name:String, options:uint, length:uint, startIndex:uint):void {
if (length < 2) return;
- var left:uint = startIndex, right:uint = left + length;
- var tempVec:Vector.<Number> = numVec, i:uint = right, j:uint = right;
- var p:Number = input[j - 1][name], s:int = 1;
- while (j > left) { --j;
+ var right:uint = startIndex + length;
+ var tempVec:Vector.<Number> = numVec, j:uint = startIndex;
+ var p:Number = input[j][name], s:int = 1;
+ for (; j < right; ++j) {
e = input[j];
t = e[name];
- s &= int(!(t > p)); // ! > instead of <= to account for NaN
+ tempVec[j] = t;
+ s &= int(!(t > p));// ! > instead of <= to account for NaN
+ p = t;
if (t !== t) {
--right;
- tempVec[j] = tempVec[right];
input[j] = input[right];
input[right] = e;
+ --j;
}
}
--right;
- if (int(!s) & int(right > left)) {
- quickSortOnNumber(tempVec, input, left, right, 0);
- }
+ if (!s) quickSortOnNumber(tempVec, input, startIndex, right, 0);
if (options & DESCENDING) {
- i = startIndex;
- options = i + length - 1;
- while (options > i) {
- e = input[i];
- input[i] = input[options];
+ j = startIndex;
+ options = j + length - 1;
+ while (options > j) {
+ e = input[j];
+ input[j] = input[options];
input[options] = e;
- ++i;
+ ++j;
--options;
}
}
return;
- var t:Number, e:*;
+ var t:Number = 0, e:* = undefined;
}
internal function sortNumber(input:Vector.<Number>, options:uint, n:uint, startIndex:uint):void {
if (n < 2) return;
@@ -1140,46 +1361,56 @@ internal function sortNumber(input:Vector.<Number>, options:uint, n:uint, startI
} else {
var tempVec:Vector.<*> = sortVec;
q = right;
- if (!(options & NUMERIC)) {
+ if (options & CASEINSENSITIVE) {
+ tempVec[q] = String(tempVec[q]).toUpperCase();
+ while (q > left) --q, tempVec[q] = String(tempVec[q]).toUpperCase();
+ } else {
tempVec[q] = String(input[q]);
- while (q-- > left) tempVec[q] = String(input[q]);
+ while (q > left) --q, tempVec[q] = String(input[q]);
}
quickSortOnObject(tempVec, input, left, right, 0);
}
}
if (options & DESCENDING) {
- var i:uint = startIndex;
- n += i;
- while (n > i) {
- t = input[i];
- input[i] = input[(--n,n)];
+ options = startIndex;
+ n += options;
+ while (n > options) {
+ --n;
+ t = input[options];
+ input[options] = input[n];
input[n] = t;
- ++i;
+ ++options;
}
}
}
-internal function sortInt(input:Vector.<int>, options:uint, length:uint, startIndex:uint):void {
- if (length < 2) return;
- var q:uint = startIndex, left:uint = q, right:uint = q + length - 1;
+internal function sortInt(input:Vector.<int>, options:uint, n:uint, startIndex:uint):void {
+ if (n < 2) return;
+ var q:uint = startIndex, left:uint = q, right:uint = q + n - 1;
if (right > left) {
if (options & NUMERIC) {
- quickSortInt(input, left, right, length);
+ quickSortInt(input, left, right, n);
} else {
var tempVec:Vector.<*> = sortVec;
q = right;
- tempVec[q] = String(input[q]);
- while (q < right) ++q, tempVec[q] = String(input[q]);
+ if (options & CASEINSENSITIVE) {
+ tempVec[q] = String(tempVec[q]).toUpperCase();
+ while (q > left) --q, tempVec[q] = String(tempVec[q]).toUpperCase();
+ } else {
+ tempVec[q] = String(input[q]);
+ while (q > left) --q, tempVec[q] = String(input[q]);
+ }
quickSortOnObject(tempVec, input, left, right, 0);
}
}
var t:int;
if (options & DESCENDING) {
options = startIndex;
- length += options;
- while (length > options) {
+ n += options;
+ while (n > options) {
+ --n;
t = input[options];
- input[options] = input[(--length,length)];
- input[length] = t;
+ input[options] = input[n];
+ input[n] = t;
++options;
}
}
@@ -1193,32 +1424,96 @@ internal function sortUint(input:Vector.<uint>, options:uint, n:uint, startIndex
} else {
var tempVec:Vector.<*> = sortVec;
q = right;
- if (!(options & NUMERIC)) {
+ if (options & CASEINSENSITIVE) {
+ tempVec[q] = String(tempVec[q]).toUpperCase();
+ while (q > left) --q, tempVec[q] = String(tempVec[q]).toUpperCase();
+ } else {
tempVec[q] = String(input[q]);
- while (q-- > left) tempVec[q] = String(input[q]);
+ while (q > left) --q, tempVec[q] = String(input[q]);
}
quickSortOnObject(tempVec, input, left, right, 0);
}
}
if (options & DESCENDING) {
- var i:uint = startIndex;
- n += i;
- while (n > i) {
- t = input[i];
- input[i] = input[(--n,n)];
+ options = startIndex;
+ n += options;
+ while (n > options) {
+ --n;
+ t = input[options];
+ input[options] = input[n];
input[n] = t;
- ++i;
+ ++options;
}
}
}
-internal function sortVector(input:*, options:uint, length:uint, startIndex:uint):void {
- if (input is Vector.<*>) sort(input, options, length, startIndex);
- else sortObject(input, options, length, startIndex);
+internal function sortVector(input:*, options:uint, n:uint, startIndex:uint):void {
+ if (input is Vector.<*>) sort(input, options, n, startIndex);
+ else sortObject(input, options, n, startIndex);
+}
+internal function sortBy(input:Object, options:uint, n:uint, startIndex:uint, sortFunc:Function):void {
+ if (n < 2) return;
+ q = startIndex, left = q, right = q + n;
+ --right;
+ if (options & FORCESTRING) {
+ tempVec = sortVec;
+ if (options & CASEINSENSITIVE) {
+ for (; q <= right; ++q) tempVec[q] = String(input[q]).toLowerCase();
+ } else {
+ for (; q <= right; ++q) tempVec[q] = String(input[q]);
+ }
+ quickSortByOnObject(tempVec, input, left, right, sortFunc);
+ } else if (options & NUMERIC) {
+ nVec = numVec;
+ for (n = q; n <= right; ++n) e = input[n], nVec[n] = e;
+ quickSortByOnNumber(nVec, input, left, right, sortFunc);
+ } else {
+ quickSortByObject(input, left, right, sortFunc);
+ }
+ if (options & DESCENDING) {
+ options = startIndex;
+ n = right;
+ while (n > options) {
+ t = input[options];
+ input[options] = input[n];
+ input[n] = t;
+ ++options, --n;
+ }
+ }
+ return;
+ var tempVec:Vector.<*> = null, nVec:Vector.<Number> = null, t:* = null, q:uint, left:uint, right:uint, e:Number;
+}
+internal function sortByOn(input:Object, name:String, options:uint, n:uint, startIndex:uint, sortFunc:Function):void {
+ if (n < 2) return;
+ q = startIndex, left = q, right = q + n;
+ --right;
+ if (options & FORCESTRING) {
+ tempVec = sortVec;
+ if (options & CASEINSENSITIVE) {
+ for (; q <= right; ++q) t = input[q], tempVec[q] = String(t[name]).toLowerCase();
+ } else {
+ for (; q <= right; ++q) t = input[q], tempVec[q] = String(t[name]);
+ }
+ quickSortByOnObject(tempVec, input, left, right, sortFunc);
+ } else if (options & NUMERIC) {
+ nVec = numVec;
+ for (n = q; n <= right; ++n) t = input[n], e = t[name], nVec[n] = e;
+ quickSortByOnNumber(nVec, input, left, right, sortFunc);
+ } else {
+ tempVec = sortVec;
+ for (n = q; n <= right; ++n) t = input[n], tempVec[n] = t[name];
+ quickSortByOnObject(tempVec, input, left, right, sortFunc);
+ }
+ if (options & DESCENDING) {
+ options = startIndex;
+ n = right;
+ while (n > options) {
+ t = input[options];
+ input[options] = input[n];
+ input[n] = t;
+ ++options, --n;
+ }
+ }
+ return;
+ var tempVec:Vector.<*> = null, nVec:Vector.<Number> = null, t:* = null, q:uint, left:uint, right:uint, e:Number;
}
-
-
-
-
-
-
//}
Please sign in to comment.
Something went wrong with that request. Please try again.