mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-09-17 16:11:28 +00:00
CPArray sorting: improve performance by 40% (JSC) replacing native js sort() with the merge-sort algorithm.
Test with ojunit CPArrayPerformance.j
This commit is contained in:
+50
-15
@@ -1316,19 +1316,7 @@ CPEnumerationReverse = 1 << 1;
|
||||
|
||||
- (CPArray)sortUsingDescriptors:(CPArray)descriptors
|
||||
{
|
||||
var count = [descriptors count];
|
||||
|
||||
sort(function(lhs, rhs)
|
||||
{
|
||||
var i = 0,
|
||||
result = CPOrderedSame;
|
||||
|
||||
while (i < count)
|
||||
if ((result = [descriptors[i++] compareObject:lhs withObject:rhs]) != CPOrderedSame)
|
||||
return result;
|
||||
|
||||
return result;
|
||||
});
|
||||
[self sortUsingFunction:compareObjectsUsingDescriptors context:descriptors];
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -1338,7 +1326,36 @@ CPEnumerationReverse = 1 << 1;
|
||||
*/
|
||||
- (void)sortUsingFunction:(Function)aFunction context:(id)aContext
|
||||
{
|
||||
sort(function(lhs, rhs) { return aFunction(lhs, rhs, aContext); });
|
||||
var h, i, j, k, l, m, n = [self count];
|
||||
var A, B = [];
|
||||
|
||||
for (h = 1; h < n; h += h)
|
||||
{
|
||||
for (m = n - 1 - h; m >= 0; m -= h + h)
|
||||
{
|
||||
l = m - h + 1;
|
||||
if (l < 0)
|
||||
l = 0;
|
||||
|
||||
for (i = 0, j = l; j <= m; i++, j++)
|
||||
B[i] = self[j];
|
||||
|
||||
for (i = 0, k = l; k < j && j <= m + h; k++)
|
||||
{
|
||||
A = self[j];
|
||||
if (aFunction(A, B[i], aContext) == CPOrderedDescending)
|
||||
self[k] = B[i++];
|
||||
else
|
||||
{
|
||||
self[k] = A;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
while (k < j)
|
||||
self[k++] = B[i++];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -1347,11 +1364,29 @@ CPEnumerationReverse = 1 << 1;
|
||||
*/
|
||||
- (void)sortUsingSelector:(SEL)aSelector
|
||||
{
|
||||
sort(function(lhs, rhs) { return objj_msgSend(lhs, aSelector, rhs); });
|
||||
[self sortUsingFunction:selectorCompare context:aSelector];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
var selectorCompare = function selectorCompare(object1, object2, selector)
|
||||
{
|
||||
return [object1 performSelector:selector withObject:object2];
|
||||
}
|
||||
|
||||
// sort using sort descriptors
|
||||
var compareObjectsUsingDescriptors= function compareObjectsUsingDescriptors(lhs, rhs, descriptors)
|
||||
{
|
||||
var result,
|
||||
i = 0,
|
||||
n = [descriptors count];
|
||||
|
||||
while (i < n && result == CPOrderedSame);
|
||||
result = [descriptors[i++] compareObject:lhs withObject:rhs];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@implementation CPArray (CPCoding)
|
||||
|
||||
- (id)initWithCoder:(CPCoder)aCoder
|
||||
|
||||
Reference in New Issue
Block a user