2012-09-10 14:13:55 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2010 Sangwoo Im
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2012-09-09 22:34:32 +08:00
|
|
|
|
|
|
|
#include "CCSorting.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
class SortedObject : public Object, public SortableObject
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-20 14:15:53 +08:00
|
|
|
SortedObject() : objectID(0) {}
|
2012-09-09 22:34:32 +08:00
|
|
|
virtual void setObjectID(unsigned int objectID) { this->objectID = objectID; }
|
|
|
|
virtual unsigned int getObjectID() { return objectID; }
|
|
|
|
private:
|
|
|
|
unsigned int objectID;
|
|
|
|
};
|
|
|
|
|
2013-03-03 10:32:09 +08:00
|
|
|
#if 0
|
2012-09-09 22:34:32 +08:00
|
|
|
static int _compareObject(const void * val1, const void * val2)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
SortableObject* operand1;
|
|
|
|
SortableObject* operand2;
|
2012-09-09 22:34:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
operand1 = (SortableObject*)val1;
|
|
|
|
operand2 = (SortableObject*)val2;
|
2012-09-09 22:34:32 +08:00
|
|
|
|
|
|
|
if (operand1->getObjectID() > operand2->getObjectID())
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (operand1->getObjectID() < operand2->getObjectID()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-03 10:32:09 +08:00
|
|
|
#endif
|
2012-09-09 22:34:32 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ArrayForObjectSorting::insertSortedObject(SortableObject* object)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
unsigned int idx;
|
2013-06-20 14:15:53 +08:00
|
|
|
Object* pObj = dynamic_cast<Object*>(object);
|
2012-09-09 22:34:32 +08:00
|
|
|
CCAssert(pObj, "Invalid parameter.");
|
|
|
|
idx = this->indexOfSortedObject(object);
|
|
|
|
|
|
|
|
this->insertObject(pObj, idx);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ArrayForObjectSorting::removeSortedObject(SortableObject* object)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
if (this->count() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned int idx;
|
2013-06-20 14:15:53 +08:00
|
|
|
SortableObject* foundObj;
|
2012-09-09 22:34:32 +08:00
|
|
|
idx = this->indexOfSortedObject(object);
|
|
|
|
|
|
|
|
if (idx < this->count() && idx != CC_INVALID_INDEX) {
|
2013-06-20 14:15:53 +08:00
|
|
|
foundObj = dynamic_cast<SortableObject*>(this->objectAtIndex(idx));
|
2012-09-09 22:34:32 +08:00
|
|
|
|
|
|
|
if(foundObj->getObjectID() == object->getObjectID()) {
|
|
|
|
this->removeObjectAtIndex(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ArrayForObjectSorting::setObjectID_ofSortedObject(unsigned int tag, SortableObject* object)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
SortableObject* foundObj;
|
2012-09-09 22:34:32 +08:00
|
|
|
unsigned int idx;
|
|
|
|
|
|
|
|
idx = this->indexOfSortedObject(object);
|
|
|
|
if (idx < this->count() && idx != CC_INVALID_INDEX)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
foundObj = dynamic_cast<SortableObject*>(this->objectAtIndex(idx));
|
|
|
|
Object* pObj = dynamic_cast<Object*>(foundObj);
|
2012-09-09 22:34:32 +08:00
|
|
|
pObj->retain();
|
|
|
|
|
|
|
|
if(foundObj->getObjectID() == object->getObjectID()) {
|
|
|
|
this->removeObjectAtIndex(idx);
|
|
|
|
foundObj->setObjectID(tag);
|
|
|
|
this->insertSortedObject(foundObj);
|
|
|
|
pObj->release();
|
|
|
|
} else {
|
|
|
|
pObj->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
SortableObject* ArrayForObjectSorting::objectWithObjectID(unsigned int tag)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
if (this->count() == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int idx;
|
2013-06-20 14:15:53 +08:00
|
|
|
SortableObject* foundObj;
|
2012-09-09 22:34:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
foundObj = new SortedObject();
|
2012-09-09 22:34:32 +08:00
|
|
|
foundObj->setObjectID(tag);
|
|
|
|
|
|
|
|
idx = this->indexOfSortedObject(foundObj);
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
((SortedObject*)foundObj)->release();
|
2012-09-09 22:34:32 +08:00
|
|
|
foundObj = NULL;
|
|
|
|
|
|
|
|
if (idx < this->count() && idx != CC_INVALID_INDEX)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
foundObj = dynamic_cast<SortableObject*>(this->objectAtIndex(idx));
|
2012-09-09 22:34:32 +08:00
|
|
|
if (foundObj->getObjectID() != tag) {
|
|
|
|
foundObj = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return foundObj;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
unsigned int ArrayForObjectSorting::indexOfSortedObject(SortableObject* object)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
unsigned int idx = 0;
|
|
|
|
if (object)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
// Object* pObj = (Object*)bsearch((Object*)&object, data->arr, data->num, sizeof(Object*), _compareObject);
|
2012-09-09 22:34:32 +08:00
|
|
|
// FIXME: need to use binary search to improve performance
|
2013-06-20 14:15:53 +08:00
|
|
|
Object* pObj = NULL;
|
2012-09-09 22:34:32 +08:00
|
|
|
unsigned int uPrevObjectID = 0;
|
|
|
|
unsigned int uOfSortObjectID = object->getObjectID();
|
|
|
|
|
|
|
|
CCARRAY_FOREACH(this, pObj)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
SortableObject* pSortableObj = dynamic_cast<SortableObject*>(pObj);
|
2012-09-09 22:34:32 +08:00
|
|
|
unsigned int uCurObjectID = pSortableObj->getObjectID();
|
|
|
|
if ( (uOfSortObjectID == uCurObjectID)
|
|
|
|
|| (uOfSortObjectID >= uPrevObjectID && uOfSortObjectID < uCurObjectID))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
uPrevObjectID = uCurObjectID;
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
idx = CC_INVALID_INDEX;
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|