API 0.9.5
Foundation/CPArray/CPMutableArray.j
Go to the documentation of this file.
00001 
00002 
00003 
00012 @implementation CPMutableArray : CPArray
00013 {
00014     id __doxygen__;
00015 }
00016 
00017 // Creating arrays
00023 + (CPArray)arrayWithCapacity:(unsigned)aCapacity
00024 {
00025     return [[self alloc] initWithCapacity:aCapacity];
00026 }
00027 
00032 /*- (id)initWithCapacity:(unsigned)aCapacity
00033 {
00034     return self;
00035 }*/
00036 
00037 // Adding and replacing objects
00042 - (void)addObject:(id)anObject
00043 {
00044     _CPRaiseInvalidAbstractInvocation(self, _cmd);
00045 }
00046 
00051 - (void)addObjectsFromArray:(CPArray)anArray
00052 {
00053     var index = 0,
00054         count = [anArray count];
00055 
00056     for (; index < count; ++index)
00057         [self addObject:[anArray objectAtIndex:index]];
00058 }
00059 
00065 - (void)insertObject:(id)anObject atIndex:(int)anIndex
00066 {
00067     _CPRaiseInvalidAbstractInvocation(self, _cmd);
00068 }
00069 
00075 - (void)insertObjects:(CPArray)objects atIndexes:(CPIndexSet)indexes
00076 {
00077     var indexesCount = [indexes count],
00078         objectsCount = [objects count];
00079 
00080     if (indexesCount !== objectsCount)
00081         [CPException raise:CPRangeException reason:"the counts of the passed-in array (" + objectsCount + ") and index set (" + indexesCount + ") must be identical."];
00082 
00083     var lastIndex = [indexes lastIndex];
00084 
00085     if (lastIndex >= [self count] + indexesCount)
00086         [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 + ")."];
00087 
00088     var index = 0,
00089         currentIndex = [indexes firstIndex];
00090 
00091     for (; index < objectsCount; ++index, currentIndex = [indexes indexGreaterThanIndex:currentIndex])
00092         [self insertObject:[objects objectAtIndex:index] atIndex:currentIndex];
00093 }
00094 
00095 - (unsigned)insertObject:(id)anObject inArraySortedByDescriptors:(CPArray)descriptors
00096 {
00097     var index,
00098         count = [descriptors count];
00099 
00100     if (count)
00101         index = [self indexOfObject:anObject
00102                       inSortedRange:nil
00103                             options:CPBinarySearchingInsertionIndex
00104                     usingComparator:function(lhs, rhs)
00105         {
00106             var index = 0,
00107                 result = CPOrderedSame;
00108 
00109             while (index < count && ((result = [[descriptors objectAtIndex:index] compareObject:lhs withObject:rhs]) === CPOrderedSame))
00110                 ++index;
00111 
00112             return result;
00113         }];
00114 
00115     else
00116         index = [self count];
00117 
00118     [self insertObject:anObject atIndex:index];
00119 
00120     return index;
00121 }
00122 
00128 - (void)replaceObjectAtIndex:(int)anIndex withObject:(id)anObject
00129 {
00130     _CPRaiseInvalidAbstractInvocation(self, _cmd);
00131 }
00132 
00139 - (void)replaceObjectsAtIndexes:(CPIndexSet)indexes withObjects:(CPArray)objects
00140 {
00141     var i = 0,
00142         index = [indexes firstIndex];
00143 
00144     while (index !== CPNotFound)
00145     {
00146         [self replaceObjectAtIndex:index withObject:[objects objectAtIndex:i++]];
00147         index = [indexes indexGreaterThanIndex:index];
00148     }
00149 }
00150 
00159 - (void)replaceObjectsInRange:(CPRange)aRange withObjectsFromArray:(CPArray)anArray range:(CPRange)otherRange
00160 {
00161     [self removeObjectsInRange:aRange];
00162 
00163     if (otherRange && (otherRange.location !== 0 || otherRange.length !== [anArray count]))
00164         anArray = [anArray subarrayWithRange:otherRange];
00165 
00166     var indexes = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(aRange.location, [anArray count])];
00167 
00168     [self insertObjects:anArray atIndexes:indexes];
00169 }
00170 
00178 - (void)replaceObjectsInRange:(CPRange)aRange withObjectsFromArray:(CPArray)anArray
00179 {
00180     [self replaceObjectsInRange:aRange withObjectsFromArray:anArray range:nil];
00181 }
00182 
00187 - (void)setArray:(CPArray)anArray
00188 {
00189     if (self === anArray)
00190         return;
00191 
00192     [self removeAllObjects];
00193     [self addObjectsFromArray:anArray];
00194 }
00195 
00196 // Removing Objects
00200 - (void)removeAllObjects
00201 {
00202     while ([self count])
00203         [self removeLastObject];
00204 }
00205 
00209 - (void)removeLastObject
00210 {
00211     _CPRaiseInvalidAbstractInvocation(self, _cmd);
00212 }
00213 
00218 - (void)removeObject:(id)anObject
00219 {
00220     [self removeObject:anObject inRange:CPMakeRange(0, length)];
00221 }
00222 
00228 - (void)removeObject:(id)anObject inRange:(CPRange)aRange
00229 {
00230     var index;
00231 
00232     while ((index = [self indexOfObject:anObject inRange:aRange]) != CPNotFound)
00233     {
00234         [self removeObjectAtIndex:index];
00235         aRange = CPIntersectionRange(CPMakeRange(index, length - index), aRange);
00236     }
00237 }
00238 
00243 - (void)removeObjectAtIndex:(int)anIndex
00244 {
00245     _CPRaiseInvalidAbstractInvocation(self, _cmd);
00246 }
00247 
00252 - (void)removeObjectsAtIndexes:(CPIndexSet)anIndexSet
00253 {
00254     var index = [anIndexSet lastIndex];
00255 
00256     while (index !== CPNotFound)
00257     {
00258         [self removeObjectAtIndex:index];
00259         index = [anIndexSet indexLessThanIndex:index];
00260     }
00261 }
00262 
00268 - (void)removeObjectIdenticalTo:(id)anObject
00269 {
00270     [self removeObjectIdenticalTo:anObject inRange:CPMakeRange(0, [self count])];
00271 }
00272 
00280 - (void)removeObjectIdenticalTo:(id)anObject inRange:(CPRange)aRange
00281 {
00282     var index,
00283         count = [self count];
00284 
00285     while ((index = [self indexOfObjectIdenticalTo:anObject inRange:aRange]) !== CPNotFound)
00286     {
00287         [self removeObjectAtIndex:index];
00288         aRange = CPIntersectionRange(CPMakeRange(index, (--count) - index), aRange);
00289     }
00290 }
00291 
00296 - (void)removeObjectsInArray:(CPArray)anArray
00297 {
00298     var index = 0,
00299         count = [anArray count];
00300 
00301     for (; index < count; ++index)
00302         [self removeObject:[anArray objectAtIndex:index]];
00303 }
00304 
00309 - (void)removeObjectsInRange:(CPRange)aRange
00310 {
00311     var index = aRange.location,
00312         count = CPMaxRange(aRange);
00313 
00314     while (count-- > index)
00315         [self removeObjectAtIndex:index];
00316 }
00317 
00318 // Rearranging objects
00324 - (void)exchangeObjectAtIndex:(unsigned)anIndex withObjectAtIndex:(unsigned)otherIndex
00325 {
00326     if (anIndex === otherIndex)
00327         return;
00328 
00329     var temporary = [self objectAtIndex:anIndex];
00330 
00331     [self replaceObjectAtIndex:anIndex withObject:[self objectAtIndex:otherIndex]];
00332     [self replaceObjectAtIndex:otherIndex withObject:temporary];
00333 }
00334 
00335 - (void)sortUsingDescriptors:(CPArray)descriptors
00336 {
00337     [self sortUsingFunction:compareObjectsUsingDescriptors context:descriptors];
00338 }
00339 
00345 - (void)sortUsingFunction:(Function)aFunction context:(id)aContext
00346 {
00347     var h,
00348         i,
00349         j,
00350         k,
00351         l,
00352         m,
00353         n = [self count],
00354         o;
00355 
00356     var A,
00357         B = [];
00358 
00359     for (h = 1; h < n; h += h)
00360     {
00361         for (m = n - 1 - h; m >= 0; m -= h + h)
00362         {
00363             l = m - h + 1;
00364             if (l < 0)
00365                 l = 0;
00366 
00367             for (i = 0, j = l; j <= m; i++, j++)
00368                 B[i] = self[j];
00369 
00370             for (i = 0, k = l; k < j && j <= m + h; k++)
00371             {
00372                 A = self[j];
00373                 o = aFunction(A, B[i], aContext);
00374                 if (o >= 0)
00375                     self[k] = B[i++];
00376                 else
00377                 {
00378                     self[k] = A;
00379                     j++;
00380                 }
00381             }
00382 
00383             while (k < j)
00384                 self[k++] = B[i++];
00385         }
00386     }
00387 }
00388 
00393 - (void)sortUsingSelector:(SEL)aSelector
00394 {
00395     [self sortUsingFunction:selectorCompare context:aSelector];
00396 }
00397 
00398 @end
00399 
00400 @implementation CPArray (CPMutableCopying)
00401 
00402 - (id)mutableCopy
00403 {
00404     var r = [CPMutableArray new];
00405     [r addObjectsFromArray:self];
00406     return r;
00407 }
00408 
00409 @end
00410 
00411 var selectorCompare = function selectorCompare(object1, object2, selector)
00412 {
00413     return [object1 performSelector:selector withObject:object2];
00414 }
00415 
00416 // sort using sort descriptors
00417 var compareObjectsUsingDescriptors= function compareObjectsUsingDescriptors(lhs, rhs, descriptors)
00418 {
00419     var result = CPOrderedSame,
00420         i = 0,
00421         n = [descriptors count];
00422 
00423     while (i < n && result === CPOrderedSame)
00424         result = [descriptors[i++] compareObject:lhs withObject:rhs];
00425 
00426     return result;
00427 }
 All Classes Files Functions Variables Defines