2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2007 Scott Lembcke
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-07-09 07:07:01 +08:00
|
|
|
https://adxeproject.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
@file
|
|
|
|
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
|
|
|
|
faster because:
|
|
|
|
- it uses a plain C interface so it doesn't incur Objective-c messaging overhead
|
|
|
|
- it assumes you know what you're doing, so it doesn't spend time on safety checks
|
|
|
|
(index out of bounds, required capacity etc.)
|
|
|
|
- comparisons are done using pointer equality instead of isEqual
|
|
|
|
|
|
|
|
There are 2 kind of functions:
|
|
|
|
- ccArray functions that manipulates objective-c objects (retain and release are performed)
|
|
|
|
- ccCArray functions that manipulates values like if they were standard C structures (no retain/release is performed)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CC_ARRAY_H
|
|
|
|
#define CC_ARRAY_H
|
|
|
|
/// @cond DO_NOT_SHOW
|
|
|
|
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/CCRef.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
// Easy integration
|
2021-12-25 10:04:45 +08:00
|
|
|
#define CCARRAYDATA_FOREACH(__array__, __object__) \
|
|
|
|
__object__ = __array__->arr[0]; \
|
|
|
|
for (ssize_t i = 0, num = __array__->num; i < num; i++, __object__ = __array__->arr[i])
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
typedef struct _ccArray
|
|
|
|
{
|
|
|
|
ssize_t num, max;
|
|
|
|
Ref** arr;
|
2019-11-23 20:27:39 +08:00
|
|
|
} ccArray;
|
|
|
|
|
|
|
|
/** Allocates and initializes a new array with specified capacity */
|
2020-10-06 16:46:38 +08:00
|
|
|
CC_DLL ccArray* ccArrayNew(ssize_t capacity);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Frees array after removing all remaining objects. Silently ignores nil arr. */
|
2020-10-06 16:46:38 +08:00
|
|
|
CC_DLL void ccArrayFree(ccArray*& arr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Doubles array capacity */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayDoubleCapacity(ccArray* arr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Increases array capacity such that max >= num + extra. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayEnsureExtraCapacity(ccArray* arr, ssize_t extra);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** shrinks the array so the memory footprint corresponds with the number of items */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayShrink(ccArray* arr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Returns index of first occurrence of object, NSNotFound if object not found. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL ssize_t ccArrayGetIndexOfObject(ccArray* arr, Ref* object);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Returns a Boolean value that indicates whether object is present in array. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL bool ccArrayContainsObject(ccArray* arr, Ref* object);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Appends an object. Behavior undefined if array doesn't have enough capacity. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayAppendObject(ccArray* arr, Ref* object);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Appends an object. Capacity of arr is increased if needed. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayAppendObjectWithResize(ccArray* arr, Ref* object);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/** Appends objects from plusArr to arr.
|
2019-11-23 20:27:39 +08:00
|
|
|
Behavior undefined if arr doesn't have enough capacity. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayAppendArray(ccArray* arr, ccArray* plusArr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Appends objects from plusArr to arr. Capacity of arr is increased if needed. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayAppendArrayWithResize(ccArray* arr, ccArray* plusArr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Inserts an object at index */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayInsertObjectAtIndex(ccArray* arr, Ref* object, ssize_t index);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Swaps two objects */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArraySwapObjectsAtIndexes(ccArray* arr, ssize_t index1, ssize_t index2);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Removes all objects from arr */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayRemoveAllObjects(ccArray* arr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Removes object at specified index and pushes back all subsequent objects.
|
|
|
|
Behavior undefined if index outside [0, num-1]. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayRemoveObjectAtIndex(ccArray* arr, ssize_t index, bool releaseObj = true);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Removes object at specified index and fills the gap with the last object,
|
|
|
|
thereby avoiding the need to push back subsequent objects.
|
|
|
|
Behavior undefined if index outside [0, num-1]. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayFastRemoveObjectAtIndex(ccArray* arr, ssize_t index);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayFastRemoveObject(ccArray* arr, Ref* object);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Searches for the first occurrence of object and removes it. If object is not
|
|
|
|
found the function has no effect. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayRemoveObject(ccArray* arr, Ref* object, bool releaseObj = true);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Removes from arr all objects in minusArr. For each object in minusArr, the
|
|
|
|
first matching instance in arr will be removed. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayRemoveArray(ccArray* arr, ccArray* minusArr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Removes from arr all objects in minusArr. For each object in minusArr, all
|
|
|
|
matching instances in arr will be removed. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccArrayFullRemoveArray(ccArray* arr, ccArray* minusArr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
//
|
2019-11-23 20:27:39 +08:00
|
|
|
// // ccCArray for Values (c structures)
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
typedef struct _ccCArray
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
ssize_t num, max;
|
|
|
|
void** arr;
|
|
|
|
} ccCArray;
|
|
|
|
|
|
|
|
/** Allocates and initializes a new C array with specified capacity */
|
2020-10-06 16:46:38 +08:00
|
|
|
CC_DLL ccCArray* ccCArrayNew(ssize_t capacity);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Frees C array after removing all remaining values. Silently ignores nil arr. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayFree(ccCArray* arr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Doubles C array capacity */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayDoubleCapacity(ccCArray* arr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Increases array capacity such that max >= num + extra. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayEnsureExtraCapacity(ccCArray* arr, ssize_t extra);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Returns index of first occurrence of value, NSNotFound if value not found. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL ssize_t ccCArrayGetIndexOfValue(ccCArray* arr, void* value);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Returns a Boolean value that indicates whether value is present in the C array. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL bool ccCArrayContainsValue(ccCArray* arr, void* value);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Inserts a value at a certain position. Behavior undefined if array doesn't have enough capacity */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayInsertValueAtIndex(ccCArray* arr, void* value, ssize_t index);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Appends an value. Behavior undefined if array doesn't have enough capacity. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayAppendValue(ccCArray* arr, void* value);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Appends an value. Capacity of arr is increased if needed. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayAppendValueWithResize(ccCArray* arr, void* value);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Appends values from plusArr to arr. Behavior undefined if arr doesn't have
|
|
|
|
enough capacity. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayAppendArray(ccCArray* arr, ccCArray* plusArr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Appends values from plusArr to arr. Capacity of arr is increased if needed. */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayAppendArrayWithResize(ccCArray* arr, ccCArray* plusArr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Removes all values from arr */
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayRemoveAllValues(ccCArray* arr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Removes value at specified index and pushes back all subsequent values.
|
|
|
|
Behavior undefined if index outside [0, num-1].
|
|
|
|
@since v0.99.4
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayRemoveValueAtIndex(ccCArray* arr, ssize_t index);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Removes value at specified index and fills the gap with the last value,
|
|
|
|
thereby avoiding the need to push back subsequent values.
|
|
|
|
Behavior undefined if index outside [0, num-1].
|
|
|
|
@since v0.99.4
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayFastRemoveValueAtIndex(ccCArray* arr, ssize_t index);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Searches for the first occurrence of value and removes it. If value is not found the function has no effect.
|
|
|
|
@since v0.99.4
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayRemoveValue(ccCArray* arr, void* value);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/** Removes from arr all values in minusArr. For each Value in minusArr, the first matching instance in arr will be
|
|
|
|
removed.
|
2019-11-23 20:27:39 +08:00
|
|
|
@since v0.99.4
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayRemoveArray(ccCArray* arr, ccCArray* minusArr);
|
2019-11-23 20:27:39 +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
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_DLL void ccCArrayFullRemoveArray(ccCArray* arr, ccCArray* minusArr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_END
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/// @endcond
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // CC_ARRAY_H
|