2010-08-04 16:54:58 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
#ifndef __COCOA_CC_MUTATLE_ARRAY_H__
|
|
|
|
#define __COCOA_CC_MUTATLE_ARRAY_H__
|
2010-08-04 16:54:58 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCObject.h"
|
2010-08-04 16:54:58 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <assert.h>
|
2010-09-02 10:33:39 +08:00
|
|
|
#include <stdarg.h>
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
namespace cocos2d {
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
// the element should be pointer of CCObject or it's sub class
|
|
|
|
template<class T = CCObject*>
|
|
|
|
class CCMutableArray : public CCObject
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
public:
|
2011-03-07 17:11:57 +08:00
|
|
|
typedef std::vector<T> CCObjectArray;
|
|
|
|
typedef typename CCObjectArray::iterator CCMutableArrayIterator;
|
|
|
|
typedef typename CCObjectArray::reverse_iterator CCMutableArrayRevIterator;
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
public:
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArray(unsigned int uSize = 0)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
m_array.resize(uSize);
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
~CCMutableArray(void)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
removeAllObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int count(void)
|
|
|
|
{
|
2010-08-30 13:42:17 +08:00
|
|
|
unsigned int uCount = 0;
|
2011-04-19 11:23:02 +08:00
|
|
|
|
|
|
|
if (!m_array.empty())
|
2010-08-30 13:42:17 +08:00
|
|
|
{
|
2011-04-19 11:23:02 +08:00
|
|
|
CCMutableArrayIterator it;
|
|
|
|
for (it = m_array.begin(); it != m_array.end(); ++it)
|
2010-08-30 13:42:17 +08:00
|
|
|
{
|
2011-04-19 11:23:02 +08:00
|
|
|
if (*it == NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-08-30 13:42:17 +08:00
|
|
|
|
2011-04-19 11:23:02 +08:00
|
|
|
++uCount;
|
|
|
|
}
|
2010-08-30 13:42:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return uCount;
|
2010-08-04 16:54:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int getIndexOfObject(T pObject)
|
|
|
|
{
|
|
|
|
if (m_array.empty() || (pObject == NULL))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArrayIterator iter;
|
2010-08-04 16:54:58 +08:00
|
|
|
unsigned int uRet = 0;
|
|
|
|
int i;
|
|
|
|
for (iter = m_array.begin(), i = 0; iter != m_array.end(); ++iter, ++i)
|
|
|
|
{
|
|
|
|
if (*iter == pObject)
|
|
|
|
{
|
|
|
|
uRet = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return uRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool containsObject(T pObject)
|
|
|
|
{
|
|
|
|
if (m_array.empty() || (! pObject))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bRet = false;
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArrayIterator iter;
|
2010-08-04 16:54:58 +08:00
|
|
|
for (iter = m_array.begin(); iter != m_array.end(); ++iter)
|
|
|
|
{
|
|
|
|
if (*iter == pObject)
|
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
T getLastObject(void)
|
|
|
|
{
|
|
|
|
T pObject = NULL;
|
|
|
|
int count = this->count();
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
pObject = m_array[count - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return pObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
T getObjectAtIndex(unsigned int uIndex)
|
|
|
|
{
|
|
|
|
assert(uIndex < count());
|
|
|
|
|
|
|
|
if (uIndex >= count())
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_array[uIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adding objects
|
|
|
|
void addObject(T pObject)
|
|
|
|
{
|
|
|
|
// make sure the pointer is not null
|
|
|
|
if (pObject == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the refrence
|
|
|
|
pObject->retain();
|
|
|
|
|
2010-08-10 18:11:45 +08:00
|
|
|
// if the vector is empty, push back
|
|
|
|
if (m_array.empty())
|
|
|
|
{
|
|
|
|
m_array.push_back(pObject);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find a position to store
|
|
|
|
int count = 0;;
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArrayIterator it;
|
2010-08-10 18:11:45 +08:00
|
|
|
for (it = m_array.begin(); it != m_array.end(); ++it)
|
|
|
|
{
|
|
|
|
if (*it == NULL)
|
|
|
|
{
|
2010-08-10 18:23:43 +08:00
|
|
|
m_array[count] = pObject;
|
2010-08-10 18:11:45 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
2010-08-04 16:54:58 +08:00
|
|
|
// the array is full, push back
|
|
|
|
m_array.push_back(pObject);
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
void addObjectsFromArray(CCMutableArray<T> *pArray)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
if (pArray && pArray->count() > 0)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArrayIterator iter;
|
2010-08-04 16:54:58 +08:00
|
|
|
for (iter = pArray->begin(); iter != pArray->end(); ++iter)
|
|
|
|
{
|
|
|
|
if (*iter)
|
|
|
|
{
|
2010-09-13 11:09:15 +08:00
|
|
|
(*iter)->retain();
|
2010-08-04 16:54:58 +08:00
|
|
|
m_array.push_back(*iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void insertObjectAtIndex(T pObject, unsigned int uIndex)
|
|
|
|
{
|
|
|
|
// make sure the object is not null
|
|
|
|
if (pObject == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the refrence of the object
|
|
|
|
pObject->retain();
|
|
|
|
|
2010-08-13 10:14:47 +08:00
|
|
|
// resize the capacity if the index out of it
|
|
|
|
if (uIndex >= m_array.capacity())
|
|
|
|
{
|
|
|
|
m_array.resize(uIndex + 4);
|
|
|
|
}
|
|
|
|
|
2010-08-04 16:54:58 +08:00
|
|
|
// insert the object
|
|
|
|
m_array.insert(m_array.begin() + uIndex, pObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removing objects
|
2010-09-01 12:01:48 +08:00
|
|
|
void removeLastObject(bool bDeleteObject = true)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
int count = this->count();
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
2010-09-01 12:01:48 +08:00
|
|
|
removeObjectAtIndex(count - 1, bDeleteObject);
|
2010-08-04 16:54:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-01 12:01:48 +08:00
|
|
|
void removeObject(T pObject, bool bDeleteObject = true)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
if (m_array.empty() || (! pObject))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArrayIterator iter;
|
2010-08-04 16:54:58 +08:00
|
|
|
int i;
|
|
|
|
for (iter = m_array.begin(), i = 0; iter != m_array.end(); ++iter, ++i)
|
|
|
|
{
|
|
|
|
if (*iter == pObject)
|
|
|
|
{
|
|
|
|
m_array.erase(iter);
|
2010-09-01 12:01:48 +08:00
|
|
|
|
|
|
|
if (bDeleteObject)
|
|
|
|
{
|
|
|
|
pObject->release();
|
|
|
|
}
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
void removeObjectsInArray(CCMutableArray<T>* pDeleteArray)
|
2010-08-18 17:56:59 +08:00
|
|
|
{
|
|
|
|
if(pDeleteArray && pDeleteArray->count())
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArrayIterator it;
|
2011-06-21 13:30:03 +08:00
|
|
|
for( it = pDeleteArray->m_array.begin(); it != pDeleteArray->m_array.end(); ++it)
|
2010-08-18 17:56:59 +08:00
|
|
|
{
|
|
|
|
removeObject(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-01 12:01:48 +08:00
|
|
|
void removeObjectAtIndex(unsigned int uIndex, bool bDeleteObject = true)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2010-09-11 16:18:18 +08:00
|
|
|
if (m_array.empty())
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-01 12:01:48 +08:00
|
|
|
if (bDeleteObject)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2010-09-01 12:01:48 +08:00
|
|
|
T pObject = m_array.at(uIndex);
|
|
|
|
if (pObject)
|
|
|
|
{
|
|
|
|
pObject->release();
|
|
|
|
}
|
2010-08-04 16:54:58 +08:00
|
|
|
}
|
2010-09-01 12:01:48 +08:00
|
|
|
|
2010-08-04 16:54:58 +08:00
|
|
|
m_array.erase(m_array.begin() + uIndex);
|
|
|
|
}
|
|
|
|
|
2010-09-01 12:01:48 +08:00
|
|
|
void removeAllObjects(bool bDeleteObject = true)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2010-09-01 12:01:48 +08:00
|
|
|
if (bDeleteObject)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArrayIterator iter;
|
2010-09-01 12:01:48 +08:00
|
|
|
for (iter = m_array.begin(); iter != m_array.end(); ++iter)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2010-09-01 12:01:48 +08:00
|
|
|
if (*iter)
|
|
|
|
{
|
|
|
|
(*iter)->release();
|
|
|
|
}
|
2010-08-04 16:54:58 +08:00
|
|
|
}
|
2010-09-01 12:01:48 +08:00
|
|
|
}
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
m_array.clear();
|
|
|
|
}
|
|
|
|
|
2010-09-01 12:01:48 +08:00
|
|
|
void replaceObjectAtIndex(unsigned int uIndex, T pObject, bool bDeleteObject = true)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2010-09-01 12:01:48 +08:00
|
|
|
if (m_array[uIndex] && bDeleteObject)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2010-09-01 12:01:48 +08:00
|
|
|
m_array[uIndex]->release();
|
2010-08-04 16:54:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_array[uIndex] = pObject;
|
|
|
|
|
|
|
|
// add the ref
|
|
|
|
if (pObject)
|
|
|
|
{
|
|
|
|
pObject->retain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArrayIterator begin(void)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
return m_array.begin();
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArrayRevIterator rbegin(void)
|
2010-08-09 16:27:36 +08:00
|
|
|
{
|
|
|
|
return m_array.rbegin();
|
|
|
|
}
|
|
|
|
|
2011-06-20 17:31:38 +08:00
|
|
|
/*
|
|
|
|
* end is a keyword of lua, so should use other name
|
|
|
|
* to export to lua
|
|
|
|
*/
|
2011-05-31 14:04:14 +08:00
|
|
|
CCMutableArrayIterator endToLua(void)
|
|
|
|
{
|
|
|
|
return m_array.end();
|
|
|
|
}
|
2011-06-20 17:31:38 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArrayIterator end(void)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
return m_array.end();
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArrayRevIterator rend(void)
|
2010-08-09 16:27:36 +08:00
|
|
|
{
|
|
|
|
return m_array.rend();
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArray<T>* copy(void)
|
2010-08-11 14:56:00 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArray* pArray = new CCMutableArray();
|
2010-08-11 14:56:00 +08:00
|
|
|
|
|
|
|
pArray->m_array.assign(m_array.begin(), m_array.end());
|
|
|
|
|
|
|
|
if(pArray->count() > 0)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArrayIterator it;
|
2011-06-21 13:30:03 +08:00
|
|
|
for(it = pArray->begin(); it != pArray->end(); ++it)
|
2010-08-11 14:56:00 +08:00
|
|
|
{
|
|
|
|
if(*it)
|
|
|
|
{
|
|
|
|
(*it)->retain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pArray;
|
|
|
|
}
|
|
|
|
|
2010-08-04 16:54:58 +08:00
|
|
|
public:
|
2011-03-07 17:11:57 +08:00
|
|
|
static CCMutableArray<T>* arrayWithObjects(T pObject1, ...)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArray<T> *pArray = new CCMutableArray<T>();
|
2011-07-24 08:30:49 +08:00
|
|
|
pArray->autorelease();
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
va_list params;
|
|
|
|
va_start(params, pObject1);
|
|
|
|
|
|
|
|
T pFirst = pObject1;
|
|
|
|
while (pFirst)
|
|
|
|
{
|
|
|
|
pArray->addObject(pFirst);
|
|
|
|
pFirst = va_arg(params, T);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(params);
|
|
|
|
|
|
|
|
return pArray;
|
|
|
|
}
|
|
|
|
|
2011-07-24 08:30:49 +08:00
|
|
|
static CCMutableArray<T>* arrayWithArray(CCMutableArray<T> *pSrcArray)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2011-07-24 08:30:49 +08:00
|
|
|
CCMutableArray<T> *pDestArray = NULL;
|
|
|
|
|
|
|
|
if (pSrcArray == NULL)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2011-07-24 08:30:49 +08:00
|
|
|
pDestArray = new CCMutableArray<T>();
|
2010-08-04 16:54:58 +08:00
|
|
|
}
|
2011-07-24 08:30:49 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
pDestArray = pSrcArray->copy();
|
|
|
|
}
|
|
|
|
|
|
|
|
pDestArray->autorelease();
|
|
|
|
|
|
|
|
return pDestArray;
|
2010-08-04 16:54:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<T> m_array;
|
|
|
|
};
|
|
|
|
|
|
|
|
}//namespace cocos2d
|
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
#endif // __COCOA_CC_MUTATLE_ARRAY_H__
|