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.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __COCOA_CC_MUTATLE_ARRAY_H__
|
||||
#define __COCOA_CC_MUTATLE_ARRAY_H__
|
||||
#ifndef __COCOA_CC_MUTABLE_ARRAY_H__
|
||||
#define __COCOA_CC_MUTABLE_ARRAY_H__
|
||||
|
||||
#include "CCObject.h"
|
||||
#include <vector>
|
||||
|
@ -44,7 +44,8 @@ public:
|
|||
public:
|
||||
CCMutableArray(unsigned int uSize = 0)
|
||||
{
|
||||
m_array.resize(uSize);
|
||||
if (uSize != 0)
|
||||
m_array.reserve(uSize);
|
||||
}
|
||||
|
||||
virtual ~CCMutableArray(void)
|
||||
|
@ -52,25 +53,9 @@ public:
|
|||
removeAllObjects();
|
||||
}
|
||||
|
||||
unsigned int count(void)
|
||||
inline unsigned int count(void)
|
||||
{
|
||||
unsigned int uCount = 0;
|
||||
|
||||
if (!m_array.empty())
|
||||
{
|
||||
CCMutableArrayIterator it;
|
||||
for (it = m_array.begin(); it != m_array.end(); ++it)
|
||||
{
|
||||
if (*it == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
++uCount;
|
||||
}
|
||||
}
|
||||
|
||||
return uCount;
|
||||
return (unsigned int)m_array.size();
|
||||
}
|
||||
|
||||
unsigned int getIndexOfObject(T pObject)
|
||||
|
@ -82,7 +67,7 @@ public:
|
|||
|
||||
CCMutableArrayIterator iter;
|
||||
unsigned int uRet = 0;
|
||||
int i;
|
||||
unsigned int i;
|
||||
for (iter = m_array.begin(), i = 0; iter != m_array.end(); ++iter, ++i)
|
||||
{
|
||||
if (*iter == pObject)
|
||||
|
@ -118,15 +103,12 @@ public:
|
|||
|
||||
T getLastObject(void)
|
||||
{
|
||||
T pObject = NULL;
|
||||
int count = this->count();
|
||||
CCMutableArrayRevIterator iter = rbegin();
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
pObject = m_array[count - 1];
|
||||
}
|
||||
if (iter != m_array.rend())
|
||||
return *iter;
|
||||
|
||||
return pObject;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
T getObjectAtIndex(unsigned int uIndex)
|
||||
|
@ -153,28 +135,6 @@ public:
|
|||
// add the refrence
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -182,47 +142,48 @@ public:
|
|||
{
|
||||
if (pArray && pArray->count() > 0)
|
||||
{
|
||||
m_array.reserve(count() + pArray->count());
|
||||
CCMutableArrayIterator iter;
|
||||
for (iter = pArray->begin(); iter != pArray->end(); ++iter)
|
||||
{
|
||||
if (*iter)
|
||||
{
|
||||
(*iter)->retain();
|
||||
m_array.push_back(*iter);
|
||||
}
|
||||
m_array.push_back(*iter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void insertObjectAtIndex(T pObject, unsigned int uIndex)
|
||||
{
|
||||
assert(uIndex <= count());
|
||||
// make sure the object is not null
|
||||
if (pObject == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// add the refrence of the object
|
||||
// add the reference of the object
|
||||
pObject->retain();
|
||||
|
||||
// resize the capacity if the index out of it
|
||||
if (uIndex >= m_array.capacity())
|
||||
{
|
||||
m_array.resize(uIndex + 4);
|
||||
m_array.reserve(uIndex + 1);
|
||||
m_array.push_back(pObject);
|
||||
}
|
||||
|
||||
// insert the object
|
||||
m_array.insert(m_array.begin() + uIndex, pObject);
|
||||
else // insert the object
|
||||
m_array.insert(m_array.begin() + uIndex, pObject);
|
||||
}
|
||||
|
||||
// Removing objects
|
||||
void removeLastObject(bool bDeleteObject = true)
|
||||
{
|
||||
int count = this->count();
|
||||
|
||||
if (count > 0)
|
||||
CCMutableArrayRevIterator it = m_array.rbegin();
|
||||
if (it != m_array.rend())
|
||||
{
|
||||
removeObjectAtIndex(count - 1, bDeleteObject);
|
||||
if (bDeleteObject)
|
||||
(*it)->release();
|
||||
m_array.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -288,12 +249,7 @@ public:
|
|||
{
|
||||
CCMutableArrayIterator iter;
|
||||
for (iter = m_array.begin(); iter != m_array.end(); ++iter)
|
||||
{
|
||||
if (*iter)
|
||||
{
|
||||
(*iter)->release();
|
||||
}
|
||||
}
|
||||
(*iter)->release();
|
||||
}
|
||||
|
||||
m_array.clear();
|
||||
|
@ -301,7 +257,7 @@ public:
|
|||
|
||||
void replaceObjectAtIndex(unsigned int uIndex, T pObject, bool bDeleteObject = true)
|
||||
{
|
||||
if (m_array[uIndex] && bDeleteObject)
|
||||
if (bDeleteObject && m_array[uIndex])
|
||||
{
|
||||
m_array[uIndex]->release();
|
||||
}
|
||||
|
@ -315,47 +271,31 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
CCMutableArrayIterator begin(void)
|
||||
inline CCMutableArrayIterator begin(void)
|
||||
{
|
||||
return m_array.begin();
|
||||
}
|
||||
|
||||
CCMutableArrayRevIterator rbegin(void)
|
||||
inline CCMutableArrayRevIterator rbegin(void)
|
||||
{
|
||||
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
|
||||
* to export to lua
|
||||
*/
|
||||
CCMutableArrayIterator endToLua(void)
|
||||
inline CCMutableArrayIterator endToLua(void)
|
||||
{
|
||||
return m_array.end();
|
||||
}
|
||||
|
||||
CCMutableArrayIterator end(void)
|
||||
inline CCMutableArrayIterator end(void)
|
||||
{
|
||||
return m_array.end();
|
||||
}
|
||||
|
||||
CCMutableArrayRevIterator rend(void)
|
||||
inline CCMutableArrayRevIterator rend(void)
|
||||
{
|
||||
return m_array.rend();
|
||||
}
|
||||
|
@ -426,4 +366,4 @@ private:
|
|||
|
||||
}//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)
|
||||
{
|
||||
std::sort(pArray->begin(), pArray->getLastValidIterator(), less);
|
||||
std::sort(pArray->begin(), pArray->end(), less);
|
||||
}
|
||||
|
||||
void CCTouchDispatcher::setPriority(int nPriority, CCTouchDelegate *pDelegate)
|
||||
|
|
Loading…
Reference in New Issue