Merge pull request #1736 from cacaodev/_CPJavaScriptArray-objectsAtIndexes

_CPJavaScriptArray -objectsAtIndexes: implementation.
This commit is contained in:
Alexander Ljungberg
2013-02-04 17:24:48 +00:00
2 changed files with 69 additions and 0 deletions
+23
View File
@@ -120,6 +120,29 @@ var concat = Array.prototype.concat,
return self[anIndex];
}
- (CPArray)objectsAtIndexes:(CPIndexSet)indexes
{
if ([indexes lastIndex] >= self.length)
[CPException raise:CPRangeException reason:_cmd + " indexes out of bounds"];
var ranges = indexes._ranges,
count = ranges.length,
result = [],
i = 0;
for (; i < count; i++)
{
var range = ranges[i],
loc = range.location,
len = range.length,
subArray = self.slice(loc, loc + len);
result.splice.apply(result, [result.length, 0].concat(subArray));
}
return result;
}
- (CPUInteger)indexOfObject:(id)anObject inRange:(CPRange)aRange
{
// Only use isEqual: if our object is a CPObject.
+46
View File
@@ -212,6 +212,52 @@ var ELEMENTS = 100,
}
}
- (void)testObjectsAtIndexesSpeed
{
var SIZE = 1000,
c = SIZE,
r = rr = REPEATS = 100,
location = 0,
array = [CPArray array],
indexes = [CPIndexSet indexSet];
while (c--)
array.push(""+c);
while (location < SIZE - 10)
{
var rangeLength = ROUND(10 * RAND());
[indexes addIndexesInRange:CPMakeRange(location, rangeLength)];
location += rangeLength + ROUND(10 * RAND());
}
var d = new Date();
while (r--)
var test1 = [array _previous_objectsAtIndexes:indexes];
var dd = new Date();
while (rr--)
var test2 = [array objectsAtIndexes:indexes];
var ddd = new Date();
CPLog.warn("\n_CPJavaScriptArray -objectsAtIndexes:");
CPLog.warn(" CPArray -objectsAtIndexes: " + (dd - d) + "ms.");
CPLog.warn("_CPJavaScriptArray -objectsAtIndexes: " + (ddd - dd) + "ms.");
if (![test1 isEqual:test2])
[self fail:"_CPJavaScriptArray -objectsAtIndexes: returns an wrong value"];
}
@end
@implementation _CPJavaScriptArray (ObjectsAtIndexes)
- (CPArray)_previous_objectsAtIndexes:(CPIndexSet)indexes
{
return [super objectsAtIndexes:indexes];
}
@end
@implementation CPArray (NativeSort)