2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 ForzeField Studios S.L. http://forzefield.com
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __CCARRAY_H__
|
|
|
|
#define __CCARRAY_H__
|
|
|
|
|
2013-08-20 08:09:28 +08:00
|
|
|
#define CC_USE_ARRAY_VECTOR 0
|
|
|
|
|
|
|
|
#if CC_USE_ARRAY_VECTOR
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
#include "cocoa/CCObject.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#else
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "support/data_support/ccCArray.h"
|
2013-08-20 08:09:28 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if CC_USE_ARRAY_VECTOR
|
|
|
|
/**
|
|
|
|
* A reference counting-managed pointer for classes derived from RCBase which can
|
|
|
|
* be used as C pointer
|
|
|
|
* Original code: http://www.codeproject.com/Articles/64111/Building-a-Quick-and-Handy-Reference-Counting-Clas
|
|
|
|
* License: http://www.codeproject.com/info/cpol10.aspx
|
|
|
|
*/
|
|
|
|
template < class T >
|
|
|
|
class RCPtr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
//Construct using a C pointer
|
|
|
|
//e.g. RCPtr< T > x = new T();
|
2013-08-22 10:18:59 +08:00
|
|
|
RCPtr(T* ptr = nullptr)
|
2013-08-20 08:09:28 +08:00
|
|
|
: _ptr(ptr)
|
|
|
|
{
|
2013-08-22 10:18:59 +08:00
|
|
|
if(ptr != nullptr) {ptr->retain();}
|
2013-08-20 08:09:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Copy constructor
|
|
|
|
RCPtr(const RCPtr &ptr)
|
|
|
|
: _ptr(ptr._ptr)
|
|
|
|
{
|
|
|
|
// printf("Array: copy constructor: %p\n", this);
|
|
|
|
if(_ptr != NULL) {_ptr->retain();}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Move constructor
|
|
|
|
RCPtr(RCPtr &&ptr)
|
|
|
|
: _ptr(ptr._ptr)
|
|
|
|
{
|
|
|
|
// printf("Array: Move Constructor: %p\n", this);
|
2013-08-22 10:18:59 +08:00
|
|
|
ptr._ptr = nullptr;
|
2013-08-20 08:09:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~RCPtr()
|
|
|
|
{
|
|
|
|
// printf("Array: Destructor: %p\n", this);
|
2013-08-22 10:18:59 +08:00
|
|
|
if(_ptr != nullptr) {_ptr->release();}
|
2013-08-20 08:09:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Assign a pointer
|
|
|
|
//e.g. x = new T();
|
|
|
|
RCPtr &operator=(T* ptr)
|
|
|
|
{
|
|
|
|
// printf("Array: operator= T*: %p\n", this);
|
|
|
|
|
|
|
|
//The following grab and release operations have to be performed
|
|
|
|
//in that order to handle the case where ptr == _ptr
|
|
|
|
//(See comment below by David Garlisch)
|
2013-08-22 10:18:59 +08:00
|
|
|
if(ptr != nullptr) {ptr->retain();}
|
|
|
|
if(_ptr != nullptr) {_ptr->release();}
|
2013-08-20 08:09:28 +08:00
|
|
|
_ptr = ptr;
|
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Assign another RCPtr
|
|
|
|
RCPtr &operator=(const RCPtr &ptr)
|
|
|
|
{
|
|
|
|
// printf("Array: operator= const&: %p\n", this);
|
|
|
|
return (*this) = ptr._ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Retrieve actual pointer
|
|
|
|
T* get() const
|
|
|
|
{
|
|
|
|
return _ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Some overloaded operators to facilitate dealing with an RCPtr
|
|
|
|
//as a conventional C pointer.
|
|
|
|
//Without these operators, one can still use the less transparent
|
|
|
|
//get() method to access the pointer.
|
|
|
|
T* operator->() const {return _ptr;} //x->member
|
|
|
|
T &operator*() const {return *_ptr;} //*x, (*x).member
|
|
|
|
explicit operator T*() const {return _ptr;} //T* y = x;
|
2013-08-22 10:18:59 +08:00
|
|
|
explicit operator bool() const {return _ptr != nullptr;} //if(x) {/*x is not NULL*/}
|
2013-08-20 08:09:28 +08:00
|
|
|
bool operator==(const RCPtr &ptr) {return _ptr == ptr._ptr;}
|
|
|
|
bool operator==(const T *ptr) {return _ptr == ptr;}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *_ptr; //Actual pointer
|
|
|
|
};
|
|
|
|
#endif // CC_USE_ARRAY_VECTOR
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup data_structures
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** @def CCARRAY_FOREACH
|
2013-06-20 14:13:12 +08:00
|
|
|
A convenience macro to iterate over a Array using. It is faster than the "fast enumeration" interface.
|
2012-04-19 14:35:52 +08:00
|
|
|
@since v0.99.4
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
In cocos2d-iphone 1.0.0, This macro have been update to like this:
|
|
|
|
|
|
|
|
#define CCARRAY_FOREACH(__array__, __object__) \
|
|
|
|
if (__array__ && __array__->data->num > 0) \
|
|
|
|
for(id *__arr__ = __array__->data->arr, *end = __array__->data->arr + __array__->data->num-1; \
|
|
|
|
__arr__ <= end && ((__object__ = *__arr__) != nil || true); \
|
|
|
|
__arr__++)
|
|
|
|
|
|
|
|
I found that it's not work in C++. So it keep what it's look like in version 1.0.0-rc3. ---By Bin
|
|
|
|
*/
|
2013-08-20 08:09:28 +08:00
|
|
|
|
|
|
|
#if CC_USE_ARRAY_VECTOR
|
|
|
|
#define CCARRAY_FOREACH(__array__, __object__) \
|
|
|
|
if (__array__) \
|
|
|
|
for( auto __it__ = (__array__)->data.begin(); \
|
|
|
|
__it__ != (__array__)->data.end() && ((__object__) = __it__->get()) != nullptr; \
|
|
|
|
++__it__)
|
|
|
|
|
|
|
|
|
|
|
|
#define CCARRAY_FOREACH_REVERSE(__array__, __object__) \
|
|
|
|
if (__array__) \
|
|
|
|
for( auto __it__ = (__array__)->data.rbegin(); \
|
|
|
|
__it__ != (__array__)->data.rend() && ((__object__) = __it__->get()) != nullptr; \
|
|
|
|
++__it__ )
|
|
|
|
|
|
|
|
|
|
|
|
#define CCARRAY_VERIFY_TYPE(__array__, __type__) void(0)
|
|
|
|
|
|
|
|
#else // ! CC_USE_ARRAY_VECTOR --------------------------
|
|
|
|
|
2012-11-26 22:11:56 +08:00
|
|
|
#define CCARRAY_FOREACH(__array__, __object__) \
|
|
|
|
if ((__array__) && (__array__)->data->num > 0) \
|
2013-06-20 14:13:12 +08:00
|
|
|
for(Object** __arr__ = (__array__)->data->arr, **__end__ = (__array__)->data->arr + (__array__)->data->num-1; \
|
2012-11-26 22:11:56 +08:00
|
|
|
__arr__ <= __end__ && (((__object__) = *__arr__) != NULL/* || true*/); \
|
|
|
|
__arr__++)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-26 22:11:56 +08:00
|
|
|
#define CCARRAY_FOREACH_REVERSE(__array__, __object__) \
|
|
|
|
if ((__array__) && (__array__)->data->num > 0) \
|
2013-06-20 14:13:12 +08:00
|
|
|
for(Object** __arr__ = (__array__)->data->arr + (__array__)->data->num-1, **__end__ = (__array__)->data->arr; \
|
2012-11-26 22:11:56 +08:00
|
|
|
__arr__ >= __end__ && (((__object__) = *__arr__) != NULL/* || true*/); \
|
|
|
|
__arr__--)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
|
2012-11-26 22:11:56 +08:00
|
|
|
#define CCARRAY_VERIFY_TYPE(__array__, __type__) \
|
|
|
|
do { \
|
|
|
|
if ((__array__) && (__array__)->data->num > 0) \
|
2013-06-20 14:13:12 +08:00
|
|
|
for(Object** __arr__ = (__array__)->data->arr, \
|
2012-11-26 22:11:56 +08:00
|
|
|
**__end__ = (__array__)->data->arr + (__array__)->data->num-1; __arr__ <= __end__; __arr__++) \
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(dynamic_cast<__type__>(*__arr__), "element type is wrong!"); \
|
2012-04-19 14:35:52 +08:00
|
|
|
} while(false)
|
|
|
|
#else
|
|
|
|
#define CCARRAY_VERIFY_TYPE(__array__, __type__) void(0)
|
|
|
|
#endif
|
|
|
|
|
2013-08-20 08:09:28 +08:00
|
|
|
#endif // ! CC_USE_ARRAY_VECTOR
|
|
|
|
|
|
|
|
|
|
|
|
// Common defines -----------------------------------------------------------------------------------------------
|
|
|
|
|
2012-06-08 15:17:15 +08:00
|
|
|
#define arrayMakeObjectsPerformSelector(pArray, func, elementType) \
|
|
|
|
do { \
|
|
|
|
if(pArray && pArray->count() > 0) \
|
|
|
|
{ \
|
2013-07-09 14:29:51 +08:00
|
|
|
Object* child; \
|
2012-06-08 15:17:15 +08:00
|
|
|
CCARRAY_FOREACH(pArray, child) \
|
|
|
|
{ \
|
2013-07-09 14:29:51 +08:00
|
|
|
elementType pNode = static_cast<elementType>(child); \
|
2012-06-08 15:17:15 +08:00
|
|
|
if(pNode) \
|
|
|
|
{ \
|
|
|
|
pNode->func(); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
while(false)
|
|
|
|
|
2013-08-21 05:16:43 +08:00
|
|
|
#define arrayMakeObjectsPerformSelectorWithObject(pArray, func, object, elementType) \
|
2012-06-08 15:17:15 +08:00
|
|
|
do { \
|
|
|
|
if(pArray && pArray->count() > 0) \
|
|
|
|
{ \
|
2013-07-09 14:29:51 +08:00
|
|
|
Object* child; \
|
2012-06-08 15:17:15 +08:00
|
|
|
CCARRAY_FOREACH(pArray, child) \
|
|
|
|
{ \
|
2013-07-09 14:29:51 +08:00
|
|
|
elementType pNode = static_cast<elementType>(child); \
|
2012-06-08 15:17:15 +08:00
|
|
|
if(pNode) \
|
|
|
|
{ \
|
2013-08-21 05:16:43 +08:00
|
|
|
pNode->func(object); \
|
2012-06-08 15:17:15 +08:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
while(false)
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
class CC_DLL Array : public Object, public Clonable
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Creates an empty array. Default capacity is 10
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static Array* create();
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Create an array with objects
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-21 05:16:43 +08:00
|
|
|
static Array* create(Object* object, ...) CC_REQUIRES_NULL_TERMINATION;
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Create an array with one object
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-21 05:16:43 +08:00
|
|
|
static Array* createWithObject(Object* object);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Create an array with a default capacity
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-27 09:05:48 +08:00
|
|
|
static Array* createWithCapacity(int capacity);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Create an array with from an existing array
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static Array* createWithArray(Array* otherArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
@brief Generate a Array pointer by file
|
2012-04-19 14:35:52 +08:00
|
|
|
@param pFileName The file name of *.plist file
|
2013-06-20 14:13:12 +08:00
|
|
|
@return The Array pointer generated from the file
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static Array* createWithContentsOfFile(const char* pFileName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
@brief The same meaning as arrayWithContentsOfFile(), but it doesn't call autorelease, so the
|
|
|
|
invoker should call release().
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static Array* createWithContentsOfFileThreadSafe(const char* pFileName);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-16 18:23:41 +08:00
|
|
|
~Array();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Initializes an array
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
bool init();
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Initializes an array with one object
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-21 05:16:43 +08:00
|
|
|
bool initWithObject(Object* object);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Initializes an array with some objects
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-21 05:16:43 +08:00
|
|
|
bool initWithObjects(Object* object, ...) CC_REQUIRES_NULL_TERMINATION;
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Initializes an array with capacity
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-27 09:05:48 +08:00
|
|
|
bool initWithCapacity(int capacity);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Initializes an array with an existing array
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
bool initWithArray(Array* otherArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Querying an Array
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Returns element count of the array
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-27 09:05:48 +08:00
|
|
|
int count() const
|
2013-08-22 10:18:59 +08:00
|
|
|
{
|
2013-08-20 08:09:28 +08:00
|
|
|
#if CC_USE_ARRAY_VECTOR
|
|
|
|
return data.size();
|
|
|
|
#else
|
|
|
|
return data->num;
|
|
|
|
#endif
|
|
|
|
}
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Returns capacity of the array
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-27 09:05:48 +08:00
|
|
|
int capacity() const
|
2013-08-22 10:18:59 +08:00
|
|
|
{
|
2013-08-20 08:09:28 +08:00
|
|
|
#if CC_USE_ARRAY_VECTOR
|
|
|
|
return data.capacity();
|
|
|
|
#else
|
|
|
|
return data->max;
|
|
|
|
#endif
|
|
|
|
}
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Returns index of a certain object, return UINT_MAX if doesn't contain the object
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-20 08:09:28 +08:00
|
|
|
int getIndexOfObject(Object* object) const;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-20 08:09:28 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE int indexOfObject(Object* object) const { return getIndexOfObject(object); }
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Returns an element with a certain index
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-22 10:18:59 +08:00
|
|
|
Object* getObjectAtIndex(int index)
|
|
|
|
{
|
2013-08-22 10:45:47 +08:00
|
|
|
CCASSERT(index>=0 && index < count(), "index out of range in getObjectAtIndex()");
|
2013-08-20 08:09:28 +08:00
|
|
|
#if CC_USE_ARRAY_VECTOR
|
|
|
|
return data[index].get();
|
|
|
|
#else
|
|
|
|
return data->arr[index];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
CC_DEPRECATED_ATTRIBUTE Object* objectAtIndex(int index) { return getObjectAtIndex(index); }
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Returns the last element of the array
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-22 10:18:59 +08:00
|
|
|
Object* getLastObject()
|
|
|
|
{
|
2013-08-20 08:09:28 +08:00
|
|
|
#if CC_USE_ARRAY_VECTOR
|
|
|
|
return data.back().get();
|
|
|
|
#else
|
2013-08-22 10:18:59 +08:00
|
|
|
if(data->num > 0)
|
2013-08-20 08:09:28 +08:00
|
|
|
return data->arr[data->num-1];
|
2013-08-22 10:18:59 +08:00
|
|
|
|
2013-08-20 08:09:28 +08:00
|
|
|
return nullptr;
|
|
|
|
#endif
|
|
|
|
}
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-20 08:09:28 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE Object* lastObject() { return getLastObject(); }
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Returns a random element
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-20 08:09:28 +08:00
|
|
|
Object* getRandomObject();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-20 08:09:28 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE Object* randomObject() { return getRandomObject(); }
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Returns a Boolean value that indicates whether object is present in array.
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
bool containsObject(Object* object) const;
|
2013-09-13 11:41:20 +08:00
|
|
|
/** @since 1.1
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-21 05:16:43 +08:00
|
|
|
bool isEqualToArray(Array* otherArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
// Adding Objects
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Add a certain object
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void addObject(Object* object);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
/** Add all elements of an existing array
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void addObjectsFromArray(Array* otherArray);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Insert a certain object at a certain index
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-20 08:09:28 +08:00
|
|
|
void insertObject(Object* object, int index);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** sets a certain object at a certain index
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-20 08:09:28 +08:00
|
|
|
void setObject(Object* object, int index);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** sets a certain object at a certain index without retaining. Use it with caution
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-22 10:18:59 +08:00
|
|
|
void fastSetObject(Object* object, int index)
|
|
|
|
{
|
2013-08-20 08:09:28 +08:00
|
|
|
#if CC_USE_ARRAY_VECTOR
|
|
|
|
setObject(object, index);
|
|
|
|
#else
|
|
|
|
// no retain
|
|
|
|
data->arr[index] = object;
|
|
|
|
#endif
|
|
|
|
}
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-22 10:18:59 +08:00
|
|
|
void swap( int indexOne, int indexTwo )
|
|
|
|
{
|
2013-08-20 08:09:28 +08:00
|
|
|
CCASSERT(indexOne >=0 && indexOne < count() && indexTwo >= 0 && indexTwo < count(), "Invalid indices");
|
|
|
|
#if CC_USE_ARRAY_VECTOR
|
2013-08-22 10:18:59 +08:00
|
|
|
std::swap(data[indexOne], data[indexTwo]);
|
2013-08-20 08:09:28 +08:00
|
|
|
#else
|
2013-08-22 10:18:59 +08:00
|
|
|
std::swap(data->arr[indexOne], data->arr[indexTwo]);
|
2013-08-20 08:09:28 +08:00
|
|
|
#endif
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Removing Objects
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Remove last object
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-21 05:16:43 +08:00
|
|
|
void removeLastObject(bool releaseObj = true);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Remove a certain object
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-21 05:16:43 +08:00
|
|
|
void removeObject(Object* object, bool releaseObj = true);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Remove an element with a certain index
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-27 09:05:48 +08:00
|
|
|
void removeObjectAtIndex(int index, bool releaseObj = true);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Remove all elements
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void removeObjectsInArray(Array* otherArray);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Remove all objects
|
|
|
|
* @js NA
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
void removeAllObjects();
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Fast way to remove a certain object
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void fastRemoveObject(Object* object);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Fast way to remove an element with a certain index
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-27 09:05:48 +08:00
|
|
|
void fastRemoveObjectAtIndex(int index);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Rearranging Content
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Swap two elements
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void exchangeObject(Object* object1, Object* object2);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Swap two elements with certain indexes
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-27 09:05:48 +08:00
|
|
|
void exchangeObjectAtIndex(int index1, int index2);
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Replace object at index with another object.
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-08-27 09:05:48 +08:00
|
|
|
void replaceObjectAtIndex(int index, Object* object, bool releaseObject = true);
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Revers the array
|
|
|
|
* @js NA
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
void reverseObjects();
|
2013-09-13 11:41:20 +08:00
|
|
|
/* Shrinks the array so the memory footprint corresponds with the number of items
|
|
|
|
* @js NA
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
void reduceMemoryFootprint();
|
2012-06-08 16:22:57 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/* override functions
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void acceptVisitor(DataVisitor &visitor);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-08 23:05:47 +08:00
|
|
|
virtual Array* clone() const;
|
2013-08-20 08:09:28 +08:00
|
|
|
|
|
|
|
// ------------------------------------------
|
|
|
|
// Iterators
|
|
|
|
// ------------------------------------------
|
|
|
|
#if CC_USE_ARRAY_VECTOR
|
|
|
|
typedef std::vector<RCPtr<Object>>::iterator iterator;
|
|
|
|
typedef std::vector<RCPtr<Object>>::const_iterator const_iterator;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-20 08:09:28 +08:00
|
|
|
iterator begin() { return data.begin(); }
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-20 08:09:28 +08:00
|
|
|
iterator end() { return data.end(); }
|
|
|
|
const_iterator cbegin() { return data.cbegin(); }
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-20 08:09:28 +08:00
|
|
|
const_iterator cend() { return data.cend(); }
|
|
|
|
|
|
|
|
std::vector<RCPtr<Object>> data;
|
2013-08-22 11:03:45 +08:00
|
|
|
|
2013-08-20 08:09:28 +08:00
|
|
|
#else
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-22 11:03:45 +08:00
|
|
|
Object** begin() { return &data->arr[0]; }
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-22 11:03:45 +08:00
|
|
|
Object** end() { return &data->arr[data->num]; }
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccArray* data;
|
2013-08-22 11:03:45 +08:00
|
|
|
|
2013-08-20 08:09:28 +08:00
|
|
|
#endif
|
|
|
|
|
2013-08-22 11:03:45 +08:00
|
|
|
//protected:
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Array();
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of data_structure group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __CCARRAY_H__
|