mirror of https://github.com/axmolengine/axmol.git
CCMutableArray optimization
This commit is contained in:
parent
0a9f6c1f09
commit
807ae36d6a
|
@ -22,8 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifndef __COCOA_CC_MUTATLE_ARRAY_H__
|
#ifndef __COCOA_CC_MUTABLE_ARRAY_H__
|
||||||
#define __COCOA_CC_MUTATLE_ARRAY_H__
|
#define __COCOA_CC_MUTABLE_ARRAY_H__
|
||||||
|
|
||||||
#include "CCObject.h"
|
#include "CCObject.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -44,7 +44,8 @@ public:
|
||||||
public:
|
public:
|
||||||
CCMutableArray(unsigned int uSize = 0)
|
CCMutableArray(unsigned int uSize = 0)
|
||||||
{
|
{
|
||||||
m_array.resize(uSize);
|
if (uSize != 0)
|
||||||
|
m_array.reserve(uSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~CCMutableArray(void)
|
virtual ~CCMutableArray(void)
|
||||||
|
@ -52,25 +53,9 @@ public:
|
||||||
removeAllObjects();
|
removeAllObjects();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int count(void)
|
inline unsigned int count(void)
|
||||||
{
|
{
|
||||||
unsigned int uCount = 0;
|
return (unsigned int)m_array.size();
|
||||||
|
|
||||||
if (!m_array.empty())
|
|
||||||
{
|
|
||||||
CCMutableArrayIterator it;
|
|
||||||
for (it = m_array.begin(); it != m_array.end(); ++it)
|
|
||||||
{
|
|
||||||
if (*it == NULL)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
++uCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return uCount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int getIndexOfObject(T pObject)
|
unsigned int getIndexOfObject(T pObject)
|
||||||
|
@ -82,7 +67,7 @@ public:
|
||||||
|
|
||||||
CCMutableArrayIterator iter;
|
CCMutableArrayIterator iter;
|
||||||
unsigned int uRet = 0;
|
unsigned int uRet = 0;
|
||||||
int i;
|
unsigned int i;
|
||||||
for (iter = m_array.begin(), i = 0; iter != m_array.end(); ++iter, ++i)
|
for (iter = m_array.begin(), i = 0; iter != m_array.end(); ++iter, ++i)
|
||||||
{
|
{
|
||||||
if (*iter == pObject)
|
if (*iter == pObject)
|
||||||
|
@ -118,15 +103,12 @@ public:
|
||||||
|
|
||||||
T getLastObject(void)
|
T getLastObject(void)
|
||||||
{
|
{
|
||||||
T pObject = NULL;
|
CCMutableArrayRevIterator iter = rbegin();
|
||||||
int count = this->count();
|
|
||||||
|
|
||||||
if (count > 0)
|
if (iter != m_array.rend())
|
||||||
{
|
return *iter;
|
||||||
pObject = m_array[count - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
return pObject;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
T getObjectAtIndex(unsigned int uIndex)
|
T getObjectAtIndex(unsigned int uIndex)
|
||||||
|
@ -153,28 +135,6 @@ public:
|
||||||
// add the refrence
|
// add the refrence
|
||||||
pObject->retain();
|
pObject->retain();
|
||||||
|
|
||||||
// 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;;
|
|
||||||
CCMutableArrayIterator it;
|
|
||||||
for (it = m_array.begin(); it != m_array.end(); ++it)
|
|
||||||
{
|
|
||||||
if (*it == NULL)
|
|
||||||
{
|
|
||||||
m_array[count] = pObject;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
++count;
|
|
||||||
}
|
|
||||||
|
|
||||||
// the array is full, push back
|
|
||||||
m_array.push_back(pObject);
|
m_array.push_back(pObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,47 +142,48 @@ public:
|
||||||
{
|
{
|
||||||
if (pArray && pArray->count() > 0)
|
if (pArray && pArray->count() > 0)
|
||||||
{
|
{
|
||||||
|
m_array.reserve(count() + pArray->count());
|
||||||
CCMutableArrayIterator iter;
|
CCMutableArrayIterator iter;
|
||||||
for (iter = pArray->begin(); iter != pArray->end(); ++iter)
|
for (iter = pArray->begin(); iter != pArray->end(); ++iter)
|
||||||
{
|
{
|
||||||
if (*iter)
|
if (*iter)
|
||||||
{
|
|
||||||
(*iter)->retain();
|
(*iter)->retain();
|
||||||
m_array.push_back(*iter);
|
m_array.push_back(*iter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void insertObjectAtIndex(T pObject, unsigned int uIndex)
|
void insertObjectAtIndex(T pObject, unsigned int uIndex)
|
||||||
{
|
{
|
||||||
|
assert(uIndex <= count());
|
||||||
// make sure the object is not null
|
// make sure the object is not null
|
||||||
if (pObject == NULL)
|
if (pObject == NULL)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add the refrence of the object
|
// add the reference of the object
|
||||||
pObject->retain();
|
pObject->retain();
|
||||||
|
|
||||||
// resize the capacity if the index out of it
|
// resize the capacity if the index out of it
|
||||||
if (uIndex >= m_array.capacity())
|
if (uIndex >= m_array.capacity())
|
||||||
{
|
{
|
||||||
m_array.resize(uIndex + 4);
|
m_array.reserve(uIndex + 1);
|
||||||
|
m_array.push_back(pObject);
|
||||||
}
|
}
|
||||||
|
else // insert the object
|
||||||
// insert the object
|
|
||||||
m_array.insert(m_array.begin() + uIndex, pObject);
|
m_array.insert(m_array.begin() + uIndex, pObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removing objects
|
// Removing objects
|
||||||
void removeLastObject(bool bDeleteObject = true)
|
void removeLastObject(bool bDeleteObject = true)
|
||||||
{
|
{
|
||||||
int count = this->count();
|
CCMutableArrayRevIterator it = m_array.rbegin();
|
||||||
|
if (it != m_array.rend())
|
||||||
if (count > 0)
|
|
||||||
{
|
{
|
||||||
removeObjectAtIndex(count - 1, bDeleteObject);
|
if (bDeleteObject)
|
||||||
|
(*it)->release();
|
||||||
|
m_array.pop_back();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -288,20 +249,15 @@ public:
|
||||||
{
|
{
|
||||||
CCMutableArrayIterator iter;
|
CCMutableArrayIterator iter;
|
||||||
for (iter = m_array.begin(); iter != m_array.end(); ++iter)
|
for (iter = m_array.begin(); iter != m_array.end(); ++iter)
|
||||||
{
|
|
||||||
if (*iter)
|
|
||||||
{
|
|
||||||
(*iter)->release();
|
(*iter)->release();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_array.clear();
|
m_array.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void replaceObjectAtIndex(unsigned int uIndex, T pObject, bool bDeleteObject = true)
|
void replaceObjectAtIndex(unsigned int uIndex, T pObject, bool bDeleteObject = true)
|
||||||
{
|
{
|
||||||
if (m_array[uIndex] && bDeleteObject)
|
if (bDeleteObject && m_array[uIndex])
|
||||||
{
|
{
|
||||||
m_array[uIndex]->release();
|
m_array[uIndex]->release();
|
||||||
}
|
}
|
||||||
|
@ -315,47 +271,31 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CCMutableArrayIterator begin(void)
|
inline CCMutableArrayIterator begin(void)
|
||||||
{
|
{
|
||||||
return m_array.begin();
|
return m_array.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
CCMutableArrayRevIterator rbegin(void)
|
inline CCMutableArrayRevIterator rbegin(void)
|
||||||
{
|
{
|
||||||
return m_array.rbegin();
|
return m_array.rbegin();
|
||||||
}
|
}
|
||||||
|
|
||||||
CCMutableArrayIterator getLastValidIterator(void)
|
|
||||||
{
|
|
||||||
CCMutableArrayIterator iter;
|
|
||||||
CCMutableArrayIterator ret;
|
|
||||||
for (iter = m_array.begin(); iter != m_array.end(); ++iter)
|
|
||||||
{
|
|
||||||
ret = iter;
|
|
||||||
if (! (*iter))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* end is a keyword of lua, so should use other name
|
* end is a keyword of lua, so should use other name
|
||||||
* to export to lua
|
* to export to lua
|
||||||
*/
|
*/
|
||||||
CCMutableArrayIterator endToLua(void)
|
inline CCMutableArrayIterator endToLua(void)
|
||||||
{
|
{
|
||||||
return m_array.end();
|
return m_array.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
CCMutableArrayIterator end(void)
|
inline CCMutableArrayIterator end(void)
|
||||||
{
|
{
|
||||||
return m_array.end();
|
return m_array.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
CCMutableArrayRevIterator rend(void)
|
inline CCMutableArrayRevIterator rend(void)
|
||||||
{
|
{
|
||||||
return m_array.rend();
|
return m_array.rend();
|
||||||
}
|
}
|
||||||
|
@ -426,4 +366,4 @@ private:
|
||||||
|
|
||||||
}//namespace cocos2d
|
}//namespace cocos2d
|
||||||
|
|
||||||
#endif // __COCOA_CC_MUTATLE_ARRAY_H__
|
#endif // __COCOA_CC_MUTABLE_ARRAY_H__
|
||||||
|
|
|
@ -264,7 +264,7 @@ CCTouchHandler* CCTouchDispatcher::findHandler(CCTouchDelegate *pDelegate)
|
||||||
|
|
||||||
void CCTouchDispatcher::rearrangeHandlers(CCMutableArray<CCTouchHandler*> *pArray)
|
void CCTouchDispatcher::rearrangeHandlers(CCMutableArray<CCTouchHandler*> *pArray)
|
||||||
{
|
{
|
||||||
std::sort(pArray->begin(), pArray->getLastValidIterator(), less);
|
std::sort(pArray->begin(), pArray->end(), less);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CCTouchDispatcher::setPriority(int nPriority, CCTouchDelegate *pDelegate)
|
void CCTouchDispatcher::setPriority(int nPriority, CCTouchDelegate *pDelegate)
|
||||||
|
|
Loading…
Reference in New Issue