From 9740711dc45060ae912b7a36c95a1ed98cd8c79d Mon Sep 17 00:00:00 2001 From: manucorporat Date: Mon, 13 Sep 2010 21:26:44 +0200 Subject: [PATCH] [CCArray] 200 times faster insert/remove at index --- cocos2d-ios.xcodeproj/project.pbxproj | 1 - cocos2d/Support/ccCArray.h | 13 ++++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/cocos2d-ios.xcodeproj/project.pbxproj b/cocos2d-ios.xcodeproj/project.pbxproj index 11c1b1031de..dc77f142322 100755 --- a/cocos2d-ios.xcodeproj/project.pbxproj +++ b/cocos2d-ios.xcodeproj/project.pbxproj @@ -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, diff --git a/cocos2d/Support/ccCArray.h b/cocos2d/Support/ccCArray.h index 21d6be0080c..8ff002f4611 100644 --- a/cocos2d/Support/ccCArray.h +++ b/cocos2d/Support/ccCArray.h @@ -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++; @@ -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,