Skip to content

Commit

Permalink
[CCArray] 200 times faster insert/remove at index
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Sep 13, 2010
1 parent b82035a commit 9740711
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
1 change: 0 additions & 1 deletion cocos2d-ios.xcodeproj/project.pbxproj
Expand Up @@ -8290,7 +8290,6 @@
isa = PBXProject;
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "cocos2d-ios" */;
compatibilityVersion = "Xcode 3.1";
developmentRegion = English;
hasScannedForEncodings = 1;
knownRegions = (
English,
Expand Down
13 changes: 8 additions & 5 deletions cocos2d/Support/ccCArray.h
Expand Up @@ -143,11 +143,12 @@ static inline void ccArrayAppendArrayWithResize(ccArray *arr, ccArray *plusArr)
static inline void ccArrayInsertObjectAtIndex(ccArray *arr, id object, NSUInteger index)
{
NSCAssert(index<=arr->num, @"Invalid index. Out of bounds");

ccArrayEnsureExtraCapacity(arr, 1);

for( NSUInteger i = arr->num; index < i; i--)
arr->arr[i] = arr->arr[i - 1];
int remaining = arr->num - index;
if( remaining > 0)
memmove(arr->arr[index+1], arr->arr[index], sizeof(id) * remaining );

arr->arr[index] = [object retain];
arr->num++;
Expand All @@ -165,9 +166,11 @@ static inline void ccArrayRemoveAllObjects(ccArray *arr)
static inline void ccArrayRemoveObjectAtIndex(ccArray *arr, NSUInteger index)
{
[arr->arr[index] release];
arr->num--;

for( NSUInteger last = --arr->num; index < last; index++)
arr->arr[index] = arr->arr[index + 1];
int remaining = arr->num - index;
if(remaining>0)
memmove(arr->arr[index], arr->arr[index+1], remaining * sizeof(id));
}

/** Removes object at specified index and fills the gap with the last object,
Expand Down

0 comments on commit 9740711

Please sign in to comment.