24 + (CPArray)arrayWithCapacity:(CPUInteger)aCapacity
26 return [[
self alloc] initWithCapacity:aCapacity];
43 - (void)addObject:(
id)anObject
45 _CPRaiseInvalidAbstractInvocation(
self, _cmd);
52 - (void)addObjectsFromArray:(CPArray)anArray
55 count = [anArray count];
57 for (; index < count; ++index)
58 [self addObject:[anArray objectAtIndex:index]];
66 - (void)insertObject:(
id)anObject atIndex:(CPUInteger)anIndex
68 _CPRaiseInvalidAbstractInvocation(
self, _cmd);
76 - (void)insertObjects:(CPArray)objects atIndexes:(
CPIndexSet)indexes
78 var indexesCount = [indexes
count],
79 objectsCount = [objects count];
81 if (indexesCount !== objectsCount)
82 [
CPException raise:CPRangeException
reason:"the counts of the passed-in array (" + objectsCount + ") and index set (" + indexesCount + ") must be identical."];
86 if (lastIndex >= [
self count] + indexesCount)
87 [
CPException raise:CPRangeException
reason:"the last index (" + lastIndex + ") must be less than the sum of the original count (" + [
self count] + ") and the insertion count (" + indexesCount + ")."];
92 for (; index < objectsCount; ++index, currentIndex = [indexes indexGreaterThanIndex:currentIndex])
93 [self insertObject:[objects objectAtIndex:index] atIndex:currentIndex];
96 - (CPUInteger)insertObject:(
id)anObject inArraySortedByDescriptors:(CPArray)descriptors
99 count = [descriptors count];
102 index = [
self indexOfObject:anObject
104 options:CPBinarySearchingInsertionIndex
105 usingComparator:
function(lhs, rhs)
110 while (index < count && ((result = [[descriptors objectAtIndex:index] compareObject:lhs withObject:rhs]) ===
CPOrderedSame))
117 index = [
self count];
119 [
self insertObject:anObject atIndex:index];
129 - (void)replaceObjectAtIndex:(CPUInteger)anIndex withObject:(
id)anObject
131 _CPRaiseInvalidAbstractInvocation(
self, _cmd);
140 - (void)replaceObjectsAtIndexes:(
CPIndexSet)indexes withObjects:(CPArray)objects
147 [
self replaceObjectAtIndex:index withObject:[objects objectAtIndex:i++]];
160 - (void)replaceObjectsInRange:(CPRange)aRange withObjectsFromArray:(CPArray)anArray range:(CPRange)otherRange
162 [
self removeObjectsInRange:aRange];
164 if (otherRange && (otherRange.location !== 0 || otherRange.length !== [anArray count]))
165 anArray = [anArray subarrayWithRange:otherRange];
169 [
self insertObjects:anArray atIndexes:indexes];
179 - (void)replaceObjectsInRange:(CPRange)aRange withObjectsFromArray:(CPArray)anArray
181 [
self replaceObjectsInRange:aRange withObjectsFromArray:anArray range:nil];
188 - (void)setArray:(CPArray)anArray
190 if (
self === anArray)
193 [
self removeAllObjects];
194 [
self addObjectsFromArray:anArray];
201 - (void)removeAllObjects
204 [
self removeLastObject];
210 - (void)removeLastObject
212 _CPRaiseInvalidAbstractInvocation(
self, _cmd);
219 - (void)removeObject:(
id)anObject
221 [
self removeObject:anObject inRange:CPMakeRange(0, [
self count])];
229 - (void)removeObject:(
id)anObject inRange:(CPRange)aRange
233 while ((index = [
self indexOfObject:anObject inRange:aRange]) !=
CPNotFound)
235 [
self removeObjectAtIndex:index];
244 - (void)removeObjectAtIndex:(CPUInteger)anIndex
246 _CPRaiseInvalidAbstractInvocation(
self, _cmd);
253 - (void)removeObjectsAtIndexes:(
CPIndexSet)anIndexSet
259 [
self removeObjectAtIndex:index];
269 - (void)removeObjectIdenticalTo:(
id)anObject
271 [
self removeObjectIdenticalTo:anObject inRange:CPMakeRange(0, [
self count])];
281 - (void)removeObjectIdenticalTo:(
id)anObject inRange:(CPRange)aRange
284 count = [
self count];
286 while ((index = [
self indexOfObjectIdenticalTo:anObject inRange:aRange]) !==
CPNotFound)
288 [
self removeObjectAtIndex:index];
297 - (void)removeObjectsInArray:(CPArray)anArray
300 count = [anArray count];
302 for (; index < count; ++index)
303 [self removeObject:[anArray objectAtIndex:index]];
310 - (void)removeObjectsInRange:(CPRange)aRange
312 var index = aRange.location,
315 while (count-- > index)
316 [
self removeObjectAtIndex:index];
325 - (void)exchangeObjectAtIndex:(CPUInteger)anIndex withObjectAtIndex:(CPUInteger)otherIndex
327 if (anIndex === otherIndex)
330 var temporary = [
self objectAtIndex:anIndex];
332 [
self replaceObjectAtIndex:anIndex withObject:[
self objectAtIndex:otherIndex]];
333 [
self replaceObjectAtIndex:otherIndex withObject:temporary];
336 - (void)sortUsingDescriptors:(CPArray)descriptors
338 var i = [descriptors count],
344 var d = [descriptors objectAtIndex:i];
345 [jsDescriptors addObject:{ "k": [d key], "a": [d ascending], "s": [d selector]}];
347 sortArrayUsingJSDescriptors(
self, jsDescriptors);
355 - (void)sortUsingFunction:(Function)aFunction context:(
id)aContext
357 sortArrayUsingFunction(
self, aFunction, aContext);
364 - (void)sortUsingSelector:(
SEL)aSelector
366 sortArrayUsingFunction(
self, selectorCompare, aSelector);
371 @implementation CPArray (CPMutableCopying)
376 [r addObjectsFromArray:self];
382 var selectorCompare =
function(object1, object2, selector)
384 return [object1 performSelector:selector withObject:object2];
387 var sortArrayUsingFunction =
function(array, aFunction, aContext)
401 for (h = 1; h < n; h += h)
403 for (m = n - 1 - h; m >= 0; m -= h + h)
409 for (i = 0, j = l; j <= m; i++, j++)
412 for (i = 0, k = l; k < j && j <= m + h; k++)
415 o = aFunction(A, B[i], aContext);
436 var sortArrayUsingJSDescriptors =
function(a, d)
460 cpNull = CPMutableArrayNull;
465 for (h = 1; h < n; h += h)
467 for (m = n - 1 - h; m >= 0; m -= h + h)
474 for (i = 0, j = l; j <= m; i++, j++)
477 for (i = 0, k = l; k < j && j <= m + h; k++)
495 C1[key] = [A valueForKeyPath:key];
516 C2[key] = [B[i] valueForKeyPath:key];
530 if (value1 === nil || value1 === cpNull)
533 o = value2 === nil || value2 === cpNull ?
CPOrderedDescending : objj_msgSend(value1, dd.s, value2);