2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-06-08 14:11:48 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2007 Scott Lembcke
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
2010-08-09 11:46:35 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
/**
|
2010-08-09 11:46:35 +08:00
|
|
|
@file
|
2012-06-08 14:11:48 +08:00
|
|
|
based on Chipmunk cpArray.
|
|
|
|
ccArray is a faster alternative to NSMutableArray, it does pretty much the
|
|
|
|
same thing (stores NSObjects and retains/releases them appropriately). It's
|
2010-08-09 11:46:35 +08:00
|
|
|
faster because:
|
2012-06-08 14:11:48 +08:00
|
|
|
- it uses a plain C interface so it doesn't incur Objective-c messaging overhead
|
2010-08-09 11:46:35 +08:00
|
|
|
- it assumes you know what you're doing, so it doesn't spend time on safety checks
|
2012-06-08 14:11:48 +08:00
|
|
|
(index out of bounds, required capacity etc.)
|
2010-08-09 11:46:35 +08:00
|
|
|
- comparisons are done using pointer equality instead of isEqual
|
2012-06-08 14:11:48 +08:00
|
|
|
|
|
|
|
There are 2 kind of functions:
|
2012-09-17 15:02:24 +08:00
|
|
|
- ccArray functions that manipulates objective-c objects (retain and release are performed)
|
2012-06-08 14:11:48 +08:00
|
|
|
- ccCArray functions that manipulates values like if they were standard C structures (no retain/release is performed)
|
2010-08-09 11:46:35 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CC_ARRAY_H
|
|
|
|
#define CC_ARRAY_H
|
|
|
|
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "ccMacros.h"
|
|
|
|
#include "cocoa/CCObject.h"
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-08-17 11:18:58 +08:00
|
|
|
#include <limits.h>
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
NS_CC_BEGIN
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2013-08-27 09:05:48 +08:00
|
|
|
extern const int CC_INVALID_INDEX;
|
2010-09-24 18:10:32 +08:00
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
// Easy integration
|
|
|
|
#define CCARRAYDATA_FOREACH(__array__, __object__) \
|
2013-08-27 09:05:48 +08:00
|
|
|
__object__=__array__->arr[0]; for(int i=0, num=__array__->num; i<num; i++, __object__=__array__->arr[i]) \
|
2012-06-08 14:11:48 +08:00
|
|
|
|
|
|
|
|
2012-06-08 16:22:57 +08:00
|
|
|
typedef struct _ccArray {
|
2013-08-27 09:05:48 +08:00
|
|
|
int num, max;
|
2013-06-20 14:13:12 +08:00
|
|
|
Object** arr;
|
2010-08-09 11:46:35 +08:00
|
|
|
} ccArray;
|
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
/** Allocates and initializes a new array with specified capacity */
|
2013-08-27 09:05:48 +08:00
|
|
|
ccArray* ccArrayNew(int capacity);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Frees array after removing all remaining objects. Silently ignores nil arr. */
|
2012-06-08 16:22:57 +08:00
|
|
|
void ccArrayFree(ccArray*& arr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Doubles array capacity */
|
2012-06-08 14:11:48 +08:00
|
|
|
void ccArrayDoubleCapacity(ccArray *arr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Increases array capacity such that max >= num + extra. */
|
2013-08-27 09:05:48 +08:00
|
|
|
void ccArrayEnsureExtraCapacity(ccArray *arr, int extra);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** shrinks the array so the memory footprint corresponds with the number of items */
|
2012-06-08 14:11:48 +08:00
|
|
|
void ccArrayShrink(ccArray *arr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** Returns index of first occurrence of object, NSNotFound if object not found. */
|
2013-08-27 09:05:48 +08:00
|
|
|
int ccArrayGetIndexOfObject(ccArray *arr, Object* object);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Returns a Boolean value that indicates whether object is present in array. */
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ccArrayContainsObject(ccArray *arr, Object* object);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** Appends an object. Behavior undefined if array doesn't have enough capacity. */
|
2013-06-20 14:13:12 +08:00
|
|
|
void ccArrayAppendObject(ccArray *arr, Object* object);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Appends an object. Capacity of arr is increased if needed. */
|
2013-06-20 14:13:12 +08:00
|
|
|
void ccArrayAppendObjectWithResize(ccArray *arr, Object* object);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
/** Appends objects from plusArr to arr.
|
2012-09-17 15:02:24 +08:00
|
|
|
Behavior undefined if arr doesn't have enough capacity. */
|
2012-06-08 14:11:48 +08:00
|
|
|
void ccArrayAppendArray(ccArray *arr, ccArray *plusArr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Appends objects from plusArr to arr. Capacity of arr is increased if needed. */
|
2012-06-08 14:11:48 +08:00
|
|
|
void ccArrayAppendArrayWithResize(ccArray *arr, ccArray *plusArr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2011-07-05 12:05:19 +08:00
|
|
|
/** Inserts an object at index */
|
2013-08-27 09:05:48 +08:00
|
|
|
void ccArrayInsertObjectAtIndex(ccArray *arr, Object* object, int index);
|
2011-04-21 14:46:15 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Swaps two objects */
|
2013-08-27 09:05:48 +08:00
|
|
|
void ccArraySwapObjectsAtIndexes(ccArray *arr, int index1, int index2);
|
2011-07-05 12:05:19 +08:00
|
|
|
|
2010-08-09 11:46:35 +08:00
|
|
|
/** Removes all objects from arr */
|
2012-06-08 14:11:48 +08:00
|
|
|
void ccArrayRemoveAllObjects(ccArray *arr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Removes object at specified index and pushes back all subsequent objects.
|
2012-09-17 15:02:24 +08:00
|
|
|
Behavior undefined if index outside [0, num-1]. */
|
2013-08-27 09:05:48 +08:00
|
|
|
void ccArrayRemoveObjectAtIndex(ccArray *arr, int index, bool bReleaseObj = true);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Removes object at specified index and fills the gap with the last object,
|
|
|
|
thereby avoiding the need to push back subsequent objects.
|
2012-09-17 15:02:24 +08:00
|
|
|
Behavior undefined if index outside [0, num-1]. */
|
2013-08-27 09:05:48 +08:00
|
|
|
void ccArrayFastRemoveObjectAtIndex(ccArray *arr, int index);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ccArrayFastRemoveObject(ccArray *arr, Object* object);
|
2011-04-21 14:46:15 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** Searches for the first occurrence of object and removes it. If object is not
|
2010-08-09 11:46:35 +08:00
|
|
|
found the function has no effect. */
|
2013-06-20 14:13:12 +08:00
|
|
|
void ccArrayRemoveObject(ccArray *arr, Object* object, bool bReleaseObj = true);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Removes from arr all objects in minusArr. For each object in minusArr, the
|
|
|
|
first matching instance in arr will be removed. */
|
2012-06-08 14:11:48 +08:00
|
|
|
void ccArrayRemoveArray(ccArray *arr, ccArray *minusArr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Removes from arr all objects in minusArr. For each object in minusArr, all
|
|
|
|
matching instances in arr will be removed. */
|
2012-06-08 14:11:48 +08:00
|
|
|
void ccArrayFullRemoveArray(ccArray *arr, ccArray *minusArr);
|
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
// #pragma mark ccCArray for Values (c structures)
|
|
|
|
|
2012-06-08 16:22:57 +08:00
|
|
|
typedef struct _ccCArray {
|
2013-08-27 09:05:48 +08:00
|
|
|
int num, max;
|
2012-06-08 17:15:00 +08:00
|
|
|
void** arr;
|
2012-06-08 16:22:57 +08:00
|
|
|
} ccCArray;
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Allocates and initializes a new C array with specified capacity */
|
2013-08-27 09:05:48 +08:00
|
|
|
ccCArray* ccCArrayNew(int capacity);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Frees C array after removing all remaining values. Silently ignores nil arr. */
|
2012-06-08 14:11:48 +08:00
|
|
|
void ccCArrayFree(ccCArray *arr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Doubles C array capacity */
|
2012-06-08 14:11:48 +08:00
|
|
|
void ccCArrayDoubleCapacity(ccCArray *arr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Increases array capacity such that max >= num + extra. */
|
2013-08-27 09:05:48 +08:00
|
|
|
void ccCArrayEnsureExtraCapacity(ccCArray *arr, int extra);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** Returns index of first occurrence of value, NSNotFound if value not found. */
|
2013-08-27 09:05:48 +08:00
|
|
|
int ccCArrayGetIndexOfValue(ccCArray *arr, void* value);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Returns a Boolean value that indicates whether value is present in the C array. */
|
2012-06-08 16:22:57 +08:00
|
|
|
bool ccCArrayContainsValue(ccCArray *arr, void* value);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** Inserts a value at a certain position. Behavior undefined if array doesn't have enough capacity */
|
2013-08-27 09:05:48 +08:00
|
|
|
void ccCArrayInsertValueAtIndex( ccCArray *arr, void* value, int index);
|
2010-08-27 14:13:32 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** Appends an value. Behavior undefined if array doesn't have enough capacity. */
|
2012-06-08 16:22:57 +08:00
|
|
|
void ccCArrayAppendValue(ccCArray *arr, void* value);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Appends an value. Capacity of arr is increased if needed. */
|
2012-06-08 16:22:57 +08:00
|
|
|
void ccCArrayAppendValueWithResize(ccCArray *arr, void* value);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** Appends values from plusArr to arr. Behavior undefined if arr doesn't have
|
2010-08-09 11:46:35 +08:00
|
|
|
enough capacity. */
|
2012-06-08 14:11:48 +08:00
|
|
|
void ccCArrayAppendArray(ccCArray *arr, ccCArray *plusArr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Appends values from plusArr to arr. Capacity of arr is increased if needed. */
|
2012-06-08 14:11:48 +08:00
|
|
|
void ccCArrayAppendArrayWithResize(ccCArray *arr, ccCArray *plusArr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Removes all values from arr */
|
2012-06-08 14:11:48 +08:00
|
|
|
void ccCArrayRemoveAllValues(ccCArray *arr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Removes value at specified index and pushes back all subsequent values.
|
2012-09-17 15:02:24 +08:00
|
|
|
Behavior undefined if index outside [0, num-1].
|
2012-06-08 14:11:48 +08:00
|
|
|
@since v0.99.4
|
|
|
|
*/
|
2013-08-27 09:05:48 +08:00
|
|
|
void ccCArrayRemoveValueAtIndex(ccCArray *arr, int index);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
|
|
|
/** Removes value at specified index and fills the gap with the last value,
|
|
|
|
thereby avoiding the need to push back subsequent values.
|
2012-09-17 15:02:24 +08:00
|
|
|
Behavior undefined if index outside [0, num-1].
|
2012-06-08 14:11:48 +08:00
|
|
|
@since v0.99.4
|
|
|
|
*/
|
2013-08-27 09:05:48 +08:00
|
|
|
void ccCArrayFastRemoveValueAtIndex(ccCArray *arr, int index);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** Searches for the first occurrence of value and removes it. If value is not found the function has no effect.
|
2012-06-08 14:11:48 +08:00
|
|
|
@since v0.99.4
|
|
|
|
*/
|
2012-06-08 16:22:57 +08:00
|
|
|
void ccCArrayRemoveValue(ccCArray *arr, void* value);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
/** Removes from arr all values in minusArr. For each Value in minusArr, the first matching instance in arr will be removed.
|
|
|
|
@since v0.99.4
|
|
|
|
*/
|
|
|
|
void ccCArrayRemoveArray(ccCArray *arr, ccCArray *minusArr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
/** Removes from arr all values in minusArr. For each value in minusArr, all matching instances in arr will be removed.
|
|
|
|
@since v0.99.4
|
|
|
|
*/
|
|
|
|
void ccCArrayFullRemoveArray(ccCArray *arr, ccCArray *minusArr);
|
2010-08-09 11:46:35 +08:00
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2010-08-09 11:46:35 +08:00
|
|
|
#endif // CC_ARRAY_H
|