34 join = Array.prototype.join,
35 push = Array.prototype.push;
37 #define FORWARD_TO_CONCRETE_CLASS()\
38 if (self === _CPSharedPlaceholderArray)\
40 arguments[0] = [_CPJavaScriptArray alloc];\
41 return objj_msgSend.apply(this, arguments);\
67 return [_CPPlaceholderArray alloc];
77 return [[
self alloc] init];
87 return [[
self alloc] initWithArray:anArray];
95 + (id)arrayWithObject:(
id)anObject
97 return [[
self alloc] initWithObjects:anObject];
105 + (id)arrayWithObjects:(
id)anObject, ...
107 arguments[0] = [
self alloc];
108 arguments[1] =
@selector(initWithObjects:);
110 return objj_msgSend.apply(
this, arguments);
119 + (id)arrayWithObjects:(
id)objects count:(
unsigned)aCount
121 return [[
self alloc] initWithObjects:objects count:aCount];
152 - (id)initWithArray:(
CPArray)anArray copyItems:(BOOL)shouldCopyItems
160 - (id)initWithObjects:(
id)anObject, ...
171 - (id)initWithObjects:(
id)objects count:(
unsigned)aCount
177 - (id)initWithCapacity:(
unsigned)aCapacity
187 - (BOOL)containsObject:(
id)anObject
192 - (BOOL)containsObjectIdenticalTo:(
id)anObject
202 _CPRaiseInvalidAbstractInvocation(
self, _cmd);
210 var count = [
self count];
223 var count = [
self count];
235 - (id)objectAtIndex:(
int)anIndex
237 _CPRaiseInvalidAbstractInvocation(
self, _cmd);
250 while ((index = [indexes indexGreaterThanIndex:index]) !==
CPNotFound)
251 objects.push([
self objectAtIndex:index]);
263 return [[_CPArrayEnumerator alloc] initWithArray:self];
273 return [[_CPReverseArrayEnumerator alloc] initWithArray:self];
283 - (CPUInteger)indexOfObject:(
id)anObject
296 - (CPUInteger)indexOfObject:(
id)anObject inRange:(CPRange)aRange
299 if (anObject && anObject.isa)
301 var index = aRange ? aRange.location : 0,
302 count = aRange ? CPMaxRange(aRange) : [
self count];
304 for (; index < count; ++index)
305 if ([[
self objectAtIndex:index]
isEqual:anObject])
319 - (CPUInteger)indexOfObjectIdenticalTo:(
id)anObject
332 - (CPUInteger)indexOfObjectIdenticalTo:(
id)anObject inRange:(CPRange)aRange
334 var index = aRange ? aRange.location : 0,
335 count = aRange ? CPMaxRange(aRange) : [
self count];
337 for (; index < count; ++index)
338 if ([
self objectAtIndex:index] === anObject)
352 - (unsigned)indexOfObjectPassingTest:(Function )aPredicate
366 - (unsigned)indexOfObjectPassingTest:(Function )aPredicate context:(
id)aContext
381 - (unsigned)indexOfObjectWithOptions:(CPEnumerationOptions)options passingTest:(Function )aPredicate
397 - (unsigned)indexOfObjectWithOptions:(CPEnumerationOptions)options passingTest:(Function )aPredicate context:(
id)aContext
403 var index = [
self count] - 1,
414 for (; index !== stop; index += increment)
415 if (aPredicate([
self objectAtIndex:index], index, aContext))
421 - (CPUInteger)indexOfObject:(
id)anObject
422 inSortedRange:(CPRange)aRange
423 options:(CPBinarySearchingOptions)options
424 usingComparator:(Function)aComparator
428 _CPRaiseInvalidArgumentException(
self, _cmd,
"comparator is nil");
431 _CPRaiseInvalidArgumentException(
self, _cmd,
432 "both CPBinarySearchingFirstEqual and CPBinarySearchingLastEqual options cannot be specified");
434 var count = [
self count];
439 var first = aRange ? aRange.location : 0,
440 last = (aRange ? CPMaxRange(aRange) : [
self count]) - 1;
443 _CPRaiseRangeException(
self, _cmd, first, count);
446 _CPRaiseRangeException(
self, _cmd, last, count);
448 while (first <= last)
450 var middle = FLOOR((first + last) / 2),
451 result = aComparator(anObject, [
self objectAtIndex:middle]);
462 while (middle > first && aComparator(anObject, [
self objectAtIndex:middle - 1]) ===
CPOrderedSame)
467 while (middle < last && aComparator(anObject, [
self objectAtIndex:middle + 1]) ===
CPOrderedSame)
479 return MAX(first, 0);
492 - (
CPIndexSet)indexesOfObjectsPassingTest:(Function )aPredicate
506 - (
CPIndexSet)indexesOfObjectsPassingTest:(Function )aPredicate context:(
id)aContext
521 - (
CPIndexSet)indexesOfObjectsWithOptions:(CPEnumerationOptions)options passingTest:(Function )aPredicate
537 - (
CPIndexSet)indexesOfObjectsWithOptions:(CPEnumerationOptions)options passingTest:(Function )aPredicate context:(
id)aContext
543 var index = [
self count] - 1,
556 for (; index !== stop; index += increment)
557 if (aPredicate([
self objectAtIndex:index], index, aContext))
569 - (void)makeObjectsPerformSelector:(
SEL)aSelector
580 - (void)makeObjectsPerformSelector:(
SEL)aSelector withObject:(
id)anObject
585 - (void)makeObjectsPerformSelector:(
SEL)aSelector withObjects:(
CPArray)objects
589 reason:"makeObjectsPerformSelector:withObjects: 'aSelector' can't be nil"];
592 count = [
self count];
596 var argumentsArray = [[nil, aSelector] arrayByAddingObjectsFromArray:objects];
598 for (; index < count; ++index)
601 objj_msgSend.apply(
this, argumentsArray);
606 for (; index < count; ++index)
607 objj_msgSend([
self objectAtIndex:index], aSelector);
610 - (void)enumerateObjectsUsingBlock:(Function )aFunction
615 count = [
self count],
617 shouldStopRef = AT_REF(shouldStop);
619 for (; index < count; ++index)
621 aFunction([
self objectAtIndex:index], index, shouldStopRef);
627 - (void)enumerateObjectsWithOptions:(CPEnumerationOptions)options usingBlock:(Function )aFunction
633 var index = [
self count] - 1,
644 for (; index !== stop; index += increment)
646 aFunction([
self objectAtIndex:index], index, AT_REF(shouldStop));
658 - (id)firstObjectCommonWithArray:(
CPArray)anArray
660 var count = [
self count];
662 if (![anArray count] || !count)
667 for (; index < count; ++index)
671 if ([anArray containsObject:
object])
681 - (BOOL)isEqualToArray:(
id)anArray
683 if (
self === anArray)
686 if (![anArray isKindOfClass:
CPArray])
689 var count = [
self count],
690 otherCount = [anArray
count];
692 if (anArray === nil || count !== otherCount)
697 for (; index < count; ++index)
703 if (lhs !== rhs && (lhs && !lhs.isa || rhs && !rhs.isa || ![lhs
isEqual:rhs]))
715 - (Array)_javaScriptArrayCopy
718 count = [
self count],
721 for (; index < count; ++index)
722 push.call(copy, [self objectAtIndex:index]);
736 var argumentArray = [
self _javaScriptArrayCopy];
740 push.call(argumentArray, anObject);
742 return objj_msgSend([
self class],
@selector(arrayWithArray:), argumentArray);
754 var anArray = anArray.
isa === _CPJavaScriptArray ? anArray : [anArray _javaScriptArrayCopy],
755 argumentArray =
concat.call([
self _javaScriptArrayCopy], anArray);
757 return objj_msgSend([
self class],
@selector(arrayWithArray:), argumentArray);
785 if (aRange.location < 0 || CPMaxRange(aRange) >
self.length)
788 var index = aRange.location,
789 count = CPMaxRange(aRange),
792 for (; index < count; ++index)
793 push.call(argumentArray, [
self objectAtIndex:index]);
795 return objj_msgSend([
self class],
@selector(arrayWithArray:), argumentArray);
804 var sorted = [
self copy];
806 [sorted sortUsingDescriptors:descriptors];
814 - (
CPArray)sortedArrayUsingFunction:(Function)aFunction
827 - (
CPArray)sortedArrayUsingFunction:(Function)aFunction context:(
id)aContext
829 var sorted = [
self copy];
831 [sorted sortUsingFunction:aFunction context:aContext];
840 - (
CPArray)sortedArrayUsingSelector:(
SEL)aSelector
842 var sorted = [
self copy];
844 [sorted sortUsingSelector:aSelector];
861 return join.call([
self _javaScriptArrayCopy], aString);
872 count = [
self count],
875 for (; index < count; ++index)
878 description +=
"\n\t";
883 if (index !== count - 1)
884 description +=
",\n\t";
889 return description +
"]";
903 count = [
self count],
906 for (; index < count; ++index)
907 if (
self[index].
isa && [
self[index] isKindOfClass:[
CPString class]] && [filterTypes containsObject:[
self[index] pathExtension]])
908 array.push(
self[index]);
921 return [[
self class] arrayWithArray:self];
930 return [aCoder decodeObjectForKey:@"CP.objects"];
935 [aCoder _encodeArrayOfObjects:self forKey:@"CP.objects"];
947 - (id)initWithArray:(
CPArray)anArray
962 if (++_index >= [_array count])
965 return [_array objectAtIndex:_index];
971 @implementation _CPReverseArrayEnumerator :
CPEnumerator
977 - (id)initWithArray:(
CPArray)anArray
984 _index = [_array count];
995 return [_array objectAtIndex:_index];
1000 var _CPSharedPlaceholderArray = nil;
1001 @implementation _CPPlaceholderArray :
CPArray
1008 if (!_CPSharedPlaceholderArray)
1009 _CPSharedPlaceholderArray = [
super alloc];
1011 return _CPSharedPlaceholderArray;