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 22:23:34 +08:00
|
|
|
https://axis-project.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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "base/ccCArray.h"
|
|
|
|
#include "base/ccTypes.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Allocates and initializes a new array with specified capacity */
|
|
|
|
ccArray* ccArrayNew(ssize_t capacity)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (capacity == 0)
|
|
|
|
capacity = 7;
|
|
|
|
|
|
|
|
ccArray* arr = (ccArray*)malloc(sizeof(ccArray));
|
|
|
|
arr->num = 0;
|
|
|
|
arr->arr = (Ref**)calloc(capacity, sizeof(Ref*));
|
|
|
|
arr->max = capacity;
|
|
|
|
|
|
|
|
return arr;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Frees array after removing all remaining objects. Silently ignores nullptr arr. */
|
|
|
|
void ccArrayFree(ccArray*& arr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (arr == nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
ccArrayRemoveAllObjects(arr);
|
|
|
|
|
|
|
|
free(arr->arr);
|
|
|
|
free(arr);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
arr = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccArrayDoubleCapacity(ccArray* arr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
arr->max *= 2;
|
|
|
|
Ref** newArr = (Ref**)realloc(arr->arr, arr->max * sizeof(Ref*));
|
|
|
|
// will fail when there's not enough memory
|
2019-11-23 20:27:39 +08:00
|
|
|
CCASSERT(newArr != 0, "ccArrayDoubleCapacity failed. Not enough memory");
|
2021-12-25 10:04:45 +08:00
|
|
|
arr->arr = newArr;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccArrayEnsureExtraCapacity(ccArray* arr, ssize_t extra)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
while (arr->max < arr->num + extra)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCLOGINFO("cocos2d: ccCArray: resizing ccArray capacity from [%zd] to [%zd].", arr->max, arr->max * 2);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ccArrayDoubleCapacity(arr);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccArrayShrink(ccArray* arr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ssize_t newSize = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// only resize when necessary
|
|
|
|
if (arr->max > arr->num && !(arr->num == 0 && arr->max == 1))
|
|
|
|
{
|
|
|
|
if (arr->num != 0)
|
|
|
|
{
|
|
|
|
newSize = arr->num;
|
|
|
|
arr->max = arr->num;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // minimum capacity of 1, with 0 elements the array would be free'd by realloc
|
|
|
|
newSize = 1;
|
|
|
|
arr->max = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
arr->arr = (Ref**)realloc(arr->arr, newSize * sizeof(Ref*));
|
|
|
|
CCASSERT(arr->arr != nullptr, "could not reallocate the memory");
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-15 19:44:31 +08:00
|
|
|
/** Returns index of first occurrence of object, CC_INVALID_INDEX if object not found. */
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t ccArrayGetIndexOfObject(ccArray* arr, Ref* object)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
const auto arrNum = arr->num;
|
2021-12-25 10:04:45 +08:00
|
|
|
Ref** ptr = arr->arr;
|
|
|
|
for (ssize_t i = 0; i < arrNum; ++i, ++ptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (*ptr == object)
|
2019-11-23 20:27:39 +08:00
|
|
|
return i;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:44:31 +08:00
|
|
|
return CC_INVALID_INDEX;
|
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
|
|
|
bool ccArrayContainsObject(ccArray* arr, Ref* object)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-15 19:44:31 +08:00
|
|
|
return ccArrayGetIndexOfObject(arr, object) != CC_INVALID_INDEX;
|
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
|
|
|
void ccArrayAppendObject(ccArray* arr, Ref* object)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(object != nullptr, "Invalid parameter!");
|
|
|
|
object->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
arr->arr[arr->num] = object;
|
|
|
|
arr->num++;
|
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
|
|
|
void ccArrayAppendObjectWithResize(ccArray* arr, Ref* object)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ccArrayEnsureExtraCapacity(arr, 1);
|
|
|
|
ccArrayAppendObject(arr, object);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Appends objects from plusArr to arr. Behavior undefined if arr doesn't have
|
|
|
|
enough capacity. */
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccArrayAppendArray(ccArray* arr, ccArray* plusArr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0; i < plusArr->num; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ccArrayAppendObject(arr, plusArr->arr[i]);
|
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
|
|
|
void ccArrayAppendArrayWithResize(ccArray* arr, ccArray* plusArr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ccArrayEnsureExtraCapacity(arr, plusArr->num);
|
|
|
|
ccArrayAppendArray(arr, plusArr);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Inserts an object at index */
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccArrayInsertObjectAtIndex(ccArray* arr, Ref* object, ssize_t index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCASSERT(index <= arr->num, "Invalid index. Out of bounds");
|
|
|
|
CCASSERT(object != nullptr, "Invalid parameter!");
|
|
|
|
|
|
|
|
ccArrayEnsureExtraCapacity(arr, 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t remaining = arr->num - index;
|
|
|
|
if (remaining > 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
memmove((void*)&arr->arr[index + 1], (void*)&arr->arr[index], sizeof(Ref*) * remaining);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
object->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
arr->arr[index] = object;
|
|
|
|
arr->num++;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Swaps two objects */
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccArraySwapObjectsAtIndexes(ccArray* arr, ssize_t index1, ssize_t index2)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCASSERT(index1 >= 0 && index1 < arr->num, "(1) Invalid index. Out of bounds");
|
|
|
|
CCASSERT(index2 >= 0 && index2 < arr->num, "(2) Invalid index. Out of bounds");
|
|
|
|
|
|
|
|
Ref* object1 = arr->arr[index1];
|
|
|
|
|
|
|
|
arr->arr[index1] = arr->arr[index2];
|
|
|
|
arr->arr[index2] = object1;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Removes all objects from arr */
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccArrayRemoveAllObjects(ccArray* arr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
while (arr->num > 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
(arr->arr[--arr->num])->release();
|
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
|
|
|
void ccArrayRemoveObjectAtIndex(ccArray* arr, ssize_t index, bool releaseObj /* = true*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCASSERT(arr && arr->num > 0 && index >= 0 && index < arr->num, "Invalid index. Out of bounds");
|
2019-11-23 20:27:39 +08:00
|
|
|
if (releaseObj)
|
|
|
|
{
|
2022-07-15 19:44:31 +08:00
|
|
|
CC_SAFE_RELEASE(arr->arr[index]);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
arr->num--;
|
|
|
|
|
|
|
|
ssize_t remaining = arr->num - index;
|
|
|
|
if (remaining > 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
memmove((void*)&arr->arr[index], (void*)&arr->arr[index + 1], remaining * sizeof(Ref*));
|
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
|
|
|
void ccArrayFastRemoveObjectAtIndex(ccArray* arr, ssize_t index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-15 19:44:31 +08:00
|
|
|
CC_SAFE_RELEASE(arr->arr[index]);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto last = --arr->num;
|
|
|
|
arr->arr[index] = arr->arr[last];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccArrayFastRemoveObject(ccArray* arr, Ref* object)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto index = ccArrayGetIndexOfObject(arr, object);
|
2022-07-15 19:44:31 +08:00
|
|
|
if (index != CC_INVALID_INDEX)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ccArrayFastRemoveObjectAtIndex(arr, index);
|
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
|
|
|
void ccArrayRemoveObject(ccArray* arr, Ref* object, bool releaseObj /* = true*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto index = ccArrayGetIndexOfObject(arr, object);
|
2022-07-15 19:44:31 +08:00
|
|
|
if (index != CC_INVALID_INDEX)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ccArrayRemoveObjectAtIndex(arr, index, releaseObj);
|
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
|
|
|
void ccArrayRemoveArray(ccArray* arr, ccArray* minusArr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0; i < minusArr->num; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ccArrayRemoveObject(arr, minusArr->arr[i]);
|
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
|
|
|
void ccArrayFullRemoveArray(ccArray* arr, ccArray* minusArr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t back = 0;
|
|
|
|
|
|
|
|
for (ssize_t i = 0; i < arr->num; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ccArrayContainsObject(minusArr, arr->arr[i]))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-15 19:44:31 +08:00
|
|
|
CC_SAFE_RELEASE(arr->arr[i]);
|
2021-12-25 10:04:45 +08:00
|
|
|
back++;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
arr->arr[i - back] = arr->arr[i];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
arr->num -= back;
|
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)
|
|
|
|
|
|
|
|
/** Allocates and initializes a new C array with specified capacity */
|
|
|
|
ccCArray* ccCArrayNew(ssize_t capacity)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (capacity == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
capacity = 7;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ccCArray* arr = (ccCArray*)malloc(sizeof(ccCArray));
|
|
|
|
arr->num = 0;
|
|
|
|
arr->arr = (void**)malloc(capacity * sizeof(void*));
|
|
|
|
arr->max = capacity;
|
|
|
|
|
|
|
|
return arr;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Frees C array after removing all remaining values. Silently ignores nullptr arr. */
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccCArrayFree(ccCArray* arr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (arr == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
ccCArrayRemoveAllValues(arr);
|
|
|
|
|
|
|
|
free(arr->arr);
|
|
|
|
free(arr);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Doubles C array capacity */
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccCArrayDoubleCapacity(ccCArray* arr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ccArrayDoubleCapacity((ccArray*)arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Increases array capacity such that max >= num + extra. */
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccCArrayEnsureExtraCapacity(ccCArray* arr, ssize_t extra)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ccArrayEnsureExtraCapacity((ccArray*)arr, extra);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-15 19:44:31 +08:00
|
|
|
/** Returns index of first occurrence of value, CC_INVALID_INDEX if value not found. */
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t ccCArrayGetIndexOfValue(ccCArray* arr, void* value)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0; i < arr->num; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (arr->arr[i] == value)
|
2019-11-23 20:27:39 +08:00
|
|
|
return i;
|
|
|
|
}
|
2022-07-15 19:44:31 +08:00
|
|
|
return CC_INVALID_INDEX;
|
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
|
|
|
bool ccCArrayContainsValue(ccCArray* arr, void* value)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-15 19:44:31 +08:00
|
|
|
return ccCArrayGetIndexOfValue(arr, value) != CC_INVALID_INDEX;
|
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
|
|
|
void ccCArrayInsertValueAtIndex(ccCArray* arr, void* value, ssize_t index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCASSERT(index < arr->max, "ccCArrayInsertValueAtIndex: invalid index");
|
|
|
|
|
|
|
|
auto remaining = arr->num - index;
|
2019-11-23 20:27:39 +08:00
|
|
|
// make sure it has enough capacity
|
|
|
|
if (arr->num + 1 == arr->max)
|
|
|
|
{
|
|
|
|
ccCArrayDoubleCapacity(arr);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
// last Value doesn't need to be moved
|
|
|
|
if (remaining > 0)
|
|
|
|
{
|
|
|
|
// tex coordinates
|
|
|
|
memmove((void*)&arr->arr[index + 1], (void*)&arr->arr[index], sizeof(void*) * remaining);
|
|
|
|
}
|
|
|
|
|
|
|
|
arr->num++;
|
|
|
|
arr->arr[index] = value;
|
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
|
|
|
void ccCArrayAppendValue(ccCArray* arr, void* value)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
arr->arr[arr->num] = value;
|
|
|
|
arr->num++;
|
2019-11-23 20:27:39 +08:00
|
|
|
// double the capacity for the next append action
|
|
|
|
// if the num >= max
|
|
|
|
if (arr->num >= arr->max)
|
|
|
|
{
|
|
|
|
ccCArrayDoubleCapacity(arr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Appends an value. Capacity of arr is increased if needed. */
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccCArrayAppendValueWithResize(ccCArray* arr, void* value)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ccCArrayEnsureExtraCapacity(arr, 1);
|
|
|
|
ccCArrayAppendValue(arr, 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
|
|
|
void ccCArrayAppendArray(ccCArray* arr, ccCArray* plusArr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0; i < plusArr->num; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ccCArrayAppendValue(arr, plusArr->arr[i]);
|
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
|
|
|
void ccCArrayAppendArrayWithResize(ccCArray* arr, ccCArray* plusArr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ccCArrayEnsureExtraCapacity(arr, plusArr->num);
|
|
|
|
ccCArrayAppendArray(arr, plusArr);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Removes all values from arr */
|
2021-12-25 10:04:45 +08:00
|
|
|
void ccCArrayRemoveAllValues(ccCArray* arr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
arr->num = 0;
|
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
|
|
|
void ccCArrayRemoveValueAtIndex(ccCArray* arr, ssize_t index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t last = --arr->num; index < last; index++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
arr->arr[index] = arr->arr[index + 1];
|
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
|
|
|
void ccCArrayFastRemoveValueAtIndex(ccCArray* arr, ssize_t index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t last = --arr->num;
|
|
|
|
arr->arr[index] = arr->arr[last];
|
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
|
|
|
void ccCArrayRemoveValue(ccCArray* arr, void* value)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto index = ccCArrayGetIndexOfValue(arr, value);
|
2022-07-15 19:44:31 +08:00
|
|
|
if (index != CC_INVALID_INDEX)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ccCArrayRemoveValueAtIndex(arr, index);
|
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
|
|
|
void ccCArrayRemoveArray(ccCArray* arr, ccCArray* minusArr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0; i < minusArr->num; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ccCArrayRemoveValue(arr, minusArr->arr[i]);
|
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
|
|
|
void ccCArrayFullRemoveArray(ccCArray* arr, ccCArray* minusArr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t back = 0;
|
|
|
|
|
|
|
|
for (ssize_t i = 0; i < arr->num; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ccCArrayContainsValue(minusArr, arr->arr[i]))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
back++;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
arr->arr[i - back] = arr->arr[i];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
arr->num -= back;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|