2013-10-12 15:25:45 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010 ForzeField Studios S.L. http://forzefield.com
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "deprecated/CCArray.h"
|
2014-04-09 22:30:49 +08:00
|
|
|
#include "deprecated/CCString.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
#if CC_USE_ARRAY_VECTOR
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|
|
|
|
// std::vector implementation
|
|
|
|
// ----------------------------------------------------------------------------------
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
__Array::__Array()
|
2014-04-09 22:53:59 +08:00
|
|
|
: data(nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array* __Array::create()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__Array* array = new (std::nothrow) __Array();
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
if (array && array->initWithCapacity(7))
|
|
|
|
{
|
|
|
|
array->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
__Array* __Array::createWithObject(Ref* object)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__Array* array = new (std::nothrow) __Array();
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
if (array && array->initWithObject(object))
|
|
|
|
{
|
|
|
|
array->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
__Array* __Array::create(Ref* object, ...)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args,object);
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array* array = create();
|
2013-10-12 15:25:45 +08:00
|
|
|
if (array && object)
|
|
|
|
{
|
|
|
|
array->addObject(object);
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* i = va_arg(args, Ref*);
|
2013-10-12 15:25:45 +08:00
|
|
|
while (i)
|
|
|
|
{
|
|
|
|
array->addObject(i);
|
2014-02-20 10:53:49 +08:00
|
|
|
i = va_arg(args, Ref*);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array* __Array::createWithArray(__Array* otherArray)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return otherArray->clone();
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array* __Array::createWithCapacity(int capacity)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(capacity>=0, "Invalid capacity");
|
|
|
|
|
2015-12-16 14:02:55 +08:00
|
|
|
__Array* array = new (std::nothrow) __Array();
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
if (array && array->initWithCapacity(capacity))
|
|
|
|
{
|
|
|
|
array->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
__Array* __Array::createWithContentsOfFile(const std::string& fileName)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array* ret = __Array::createWithContentsOfFileThreadSafe(fileName);
|
2013-10-12 15:25:45 +08:00
|
|
|
if (ret != nullptr)
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
__Array* __Array::createWithContentsOfFileThreadSafe(const std::string& fileName)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return FileUtils::getInstance()->createArrayWithContentsOfFile(fileName);
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
bool __Array::init()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return initWithCapacity(7);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
bool __Array::initWithObject(Ref* object)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
bool ret = initWithCapacity(7);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
addObject(object);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initializes an array with some objects */
|
2014-02-20 10:53:49 +08:00
|
|
|
bool __Array::initWithObjects(Ref* object, ...)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(object == nullptr);
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, object);
|
|
|
|
|
|
|
|
if (object)
|
|
|
|
{
|
|
|
|
this->addObject(object);
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* i = va_arg(args, Ref*);
|
2013-10-12 15:25:45 +08:00
|
|
|
while (i)
|
|
|
|
{
|
|
|
|
this->addObject(i);
|
2014-02-20 10:53:49 +08:00
|
|
|
i = va_arg(args, Ref*);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
bool __Array::initWithCapacity(int capacity)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(capacity>=0, "Invalid capacity");
|
|
|
|
|
|
|
|
data.reserve(capacity);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
bool __Array::initWithArray(__Array* otherArray)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
data = otherArray->data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
ssize_t __Array::getIndexOfObject(Ref* object) const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
auto it = data.begin();
|
|
|
|
|
2013-12-12 10:29:07 +08:00
|
|
|
for (ssize_t i = 0; it != data.end(); ++it, ++i)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (it->get() == object)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* __Array::getRandomObject()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (data.size()==0)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
float r = CCRANDOM_0_1();
|
|
|
|
|
|
|
|
if (r == 1) // to prevent from accessing data-arr[data->num], out of range.
|
|
|
|
{
|
|
|
|
r = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r *= data.size();
|
|
|
|
|
|
|
|
return data[r].get();
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
bool __Array::containsObject(Ref* object) const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-12 10:29:07 +08:00
|
|
|
ssize_t i = this->getIndexOfObject(object);
|
2013-12-03 11:09:39 +08:00
|
|
|
return (i >= 0);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
bool __Array::isEqualToArray(__Array* otherArray)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-12 10:29:07 +08:00
|
|
|
for (ssize_t i = 0; i < this->count(); ++i)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (!this->getObjectAtIndex(i)->isEqual(otherArray->getObjectAtIndex(i)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::addObject(Ref* object)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-03 11:09:39 +08:00
|
|
|
data.push_back(RCPtr<Object>(object));
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::addObjectsFromArray(__Array* otherArray)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
data.insert(data.end(), otherArray->data.begin(), otherArray->data.end());
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::insertObject(Ref* object, int index)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-03 11:09:39 +08:00
|
|
|
data.insert(std::begin(data) + index, RCPtr<Object>(object));
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::setObject(Ref* object, int index)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
data[index] = RCPtr<Object>(object);
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::removeLastObject(bool releaseObj)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(data.size(), "no objects added");
|
|
|
|
data.pop_back();
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::removeObject(Ref* object, bool releaseObj /* ignored */)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-03 11:09:39 +08:00
|
|
|
data.erase(std::remove(data.begin(), data.end(), object));
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 10:29:07 +08:00
|
|
|
void __Array::removeObjectAtIndex(ssize_t index, bool releaseObj /* ignored */)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
auto obj = data[index];
|
2013-12-03 11:09:39 +08:00
|
|
|
data.erase(data.begin() + index);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::removeObjectsInArray(__Array* otherArray)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(false, "not implemented");
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::removeAllObjects()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
data.erase(std::begin(data), std::end(data));
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::fastRemoveObjectAtIndex(int index)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
removeObjectAtIndex(index);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::fastRemoveObject(Ref* object)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
removeObject(object);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::exchangeObject(Ref* object1, Ref* object2)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-12 10:29:07 +08:00
|
|
|
ssize_t idx1 = getIndexOfObject(object1);
|
|
|
|
ssize_t idx2 = getIndexOfObject(object2);
|
2013-10-12 15:25:45 +08:00
|
|
|
|
2013-12-03 11:09:39 +08:00
|
|
|
CCASSERT(idx1 >= 0 && idx2 >= 2, "invalid object index");
|
2013-10-12 15:25:45 +08:00
|
|
|
|
2013-12-03 11:09:39 +08:00
|
|
|
std::swap(data[idx1], data[idx2]);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 10:29:07 +08:00
|
|
|
void __Array::exchangeObjectAtIndex(ssize_t index1, ssize_t index2)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-03 11:09:39 +08:00
|
|
|
std::swap(data[index1], data[index2]);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::replaceObjectAtIndex(int index, Ref* object, bool releaseObject /* ignored */)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
data[index] = object;
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::reverseObjects()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-03 11:09:39 +08:00
|
|
|
std::reverse(std::begin(data), std::end(data));
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::reduceMemoryFootprint()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
// N/A
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array::~Array()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCLOGINFO("deallocing Array: %p - len: %d", this, count() );
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array* __Array::clone() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__Array* ret = new (std::nothrow) __Array();
|
2013-10-12 15:25:45 +08:00
|
|
|
ret->autorelease();
|
|
|
|
ret->initWithCapacity(this->data.size() > 0 ? this->data.size() : 1);
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj = nullptr;
|
|
|
|
Ref* tmpObj = nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
Clonable* clonable = nullptr;
|
|
|
|
CCARRAY_FOREACH(this, obj)
|
|
|
|
{
|
|
|
|
clonable = dynamic_cast<Clonable*>(obj);
|
|
|
|
if (clonable)
|
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
tmpObj = dynamic_cast<Ref*>(clonable->clone());
|
2013-10-12 15:25:45 +08:00
|
|
|
if (tmpObj)
|
|
|
|
{
|
|
|
|
ret->addObject(tmpObj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOGWARN("%s isn't clonable.", typeid(*obj).name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::acceptVisitor(DataVisitor &visitor)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
visitor.visit(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|
|
|
|
// ccArray implementation
|
|
|
|
// ----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array::__Array()
|
2013-10-12 15:25:45 +08:00
|
|
|
: data(nullptr)
|
|
|
|
{
|
|
|
|
// init();
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array* __Array::create()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__Array* array = new (std::nothrow) __Array();
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
if (array && array->initWithCapacity(7))
|
|
|
|
{
|
|
|
|
array->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
__Array* __Array::createWithObject(Ref* object)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__Array* array = new (std::nothrow) __Array();
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
if (array && array->initWithObject(object))
|
|
|
|
{
|
|
|
|
array->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
__Array* __Array::create(Ref* object, ...)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args,object);
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array* array = create();
|
2013-10-12 15:25:45 +08:00
|
|
|
if (array && object)
|
|
|
|
{
|
|
|
|
array->addObject(object);
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref *i = va_arg(args, Ref*);
|
2013-10-12 15:25:45 +08:00
|
|
|
while (i)
|
|
|
|
{
|
|
|
|
array->addObject(i);
|
2014-02-20 10:53:49 +08:00
|
|
|
i = va_arg(args, Ref*);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array* __Array::createWithArray(__Array* otherArray)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return otherArray->clone();
|
|
|
|
}
|
|
|
|
|
2013-12-12 10:29:07 +08:00
|
|
|
__Array* __Array::createWithCapacity(ssize_t capacity)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(capacity>=0, "Invalid capacity");
|
|
|
|
|
2015-12-16 14:02:55 +08:00
|
|
|
__Array* array = new (std::nothrow) __Array();
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
if (array && array->initWithCapacity(capacity))
|
|
|
|
{
|
|
|
|
array->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
__Array* __Array::createWithContentsOfFile(const std::string& fileName)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array* ret = __Array::createWithContentsOfFileThreadSafe(fileName);
|
2013-10-12 15:25:45 +08:00
|
|
|
if (ret != nullptr)
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
__Array* __Array::createWithContentsOfFileThreadSafe(const std::string& fileName)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueVector arr = FileUtils::getInstance()->getValueVectorFromFile(fileName);
|
2013-12-04 17:28:14 +08:00
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array* ret = __Array::createWithCapacity(static_cast<int>(arr.size()));
|
2013-12-20 05:34:41 +08:00
|
|
|
|
|
|
|
for(const auto &value : arr) {
|
2013-12-11 17:15:27 +08:00
|
|
|
ret->addObject(__String::create(value.asString()));
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2013-12-04 17:28:14 +08:00
|
|
|
|
|
|
|
return ret;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
bool __Array::init()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(!data, "Array cannot be re-initialized");
|
|
|
|
|
|
|
|
return initWithCapacity(7);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
bool __Array::initWithObject(Ref* object)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(!data, "Array cannot be re-initialized");
|
|
|
|
|
|
|
|
bool ret = initWithCapacity(7);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
addObject(object);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initializes an array with some objects */
|
2014-02-20 10:53:49 +08:00
|
|
|
bool __Array::initWithObjects(Ref* object, ...)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(!data, "Array cannot be re-initialized");
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(object == nullptr);
|
2014-02-20 21:44:40 +08:00
|
|
|
CC_BREAK_IF(!initWithCapacity(1));
|
|
|
|
|
2013-10-12 15:25:45 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args, object);
|
|
|
|
|
|
|
|
if (object)
|
|
|
|
{
|
|
|
|
this->addObject(object);
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* i = va_arg(args, Ref*);
|
2013-10-12 15:25:45 +08:00
|
|
|
while (i)
|
|
|
|
{
|
|
|
|
this->addObject(i);
|
2014-02-20 10:53:49 +08:00
|
|
|
i = va_arg(args, Ref*);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-12 10:29:07 +08:00
|
|
|
bool __Array::initWithCapacity(ssize_t capacity)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(capacity>=0 && !data, "Array cannot be re-initialized");
|
|
|
|
|
|
|
|
data = ccArrayNew(capacity);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
bool __Array::initWithArray(__Array* otherArray)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(!data, "Array cannot be re-initialized");
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(! initWithCapacity(otherArray->data->num));
|
|
|
|
|
|
|
|
addObjectsFromArray(otherArray);
|
|
|
|
ret = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
ssize_t __Array::getIndexOfObject(Ref* object) const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return ccArrayGetIndexOfObject(data, object);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* __Array::getRandomObject()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (data->num == 0)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
float r = CCRANDOM_0_1();
|
|
|
|
|
|
|
|
if (r == 1) // to prevent from accessing data-arr[data->num], out of range.
|
|
|
|
{
|
|
|
|
r = 0;
|
|
|
|
}
|
|
|
|
|
2013-12-06 16:32:06 +08:00
|
|
|
return data->arr[static_cast<int>(data->num * r)];
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
bool __Array::containsObject(Ref* object) const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return ccArrayContainsObject(data, object);
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
bool __Array::isEqualToArray(__Array* otherArray)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-06 16:32:06 +08:00
|
|
|
for (int i = 0; i < this->count(); ++i)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
//FIXME:james
|
|
|
|
if (this->getObjectAtIndex(i) != otherArray->getObjectAtIndex(i))
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::addObject(Ref* object)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(data, "Array not initialized");
|
|
|
|
ccArrayAppendObjectWithResize(data, object);
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::addObjectsFromArray(__Array* otherArray)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(data, "Array not initialized");
|
|
|
|
ccArrayAppendArrayWithResize(data, otherArray->data);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::insertObject(Ref* object, ssize_t index)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(data, "Array not initialized");
|
|
|
|
ccArrayInsertObjectAtIndex(data, object, index);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::setObject(Ref* object, ssize_t index)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-03 11:09:39 +08:00
|
|
|
CCASSERT(index >= 0 && index < count(), "Invalid index");
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
if (object != data->arr[index])
|
|
|
|
{
|
|
|
|
data->arr[index]->release();
|
|
|
|
data->arr[index] = object;
|
|
|
|
object->retain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::removeLastObject(bool releaseObj)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(data->num, "no objects added");
|
2013-12-03 11:09:39 +08:00
|
|
|
ccArrayRemoveObjectAtIndex(data, data->num - 1, releaseObj);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::removeObject(Ref* object, bool releaseObj/* = true*/)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
ccArrayRemoveObject(data, object, releaseObj);
|
|
|
|
}
|
|
|
|
|
2013-12-12 10:29:07 +08:00
|
|
|
void __Array::removeObjectAtIndex(ssize_t index, bool releaseObj)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
ccArrayRemoveObjectAtIndex(data, index, releaseObj);
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::removeObjectsInArray(__Array* otherArray)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
ccArrayRemoveArray(data, otherArray->data);
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::removeAllObjects()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
ccArrayRemoveAllObjects(data);
|
|
|
|
}
|
|
|
|
|
2013-12-12 10:29:07 +08:00
|
|
|
void __Array::fastRemoveObjectAtIndex(ssize_t index)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
ccArrayFastRemoveObjectAtIndex(data, index);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::fastRemoveObject(Ref* object)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
ccArrayFastRemoveObject(data, object);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::exchangeObject(Ref* object1, Ref* object2)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-03 11:09:39 +08:00
|
|
|
auto index1 = ccArrayGetIndexOfObject(data, object1);
|
2013-11-05 08:31:36 +08:00
|
|
|
if (index1 == CC_INVALID_INDEX)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-03 11:09:39 +08:00
|
|
|
auto index2 = ccArrayGetIndexOfObject(data, object2);
|
2013-11-05 08:31:36 +08:00
|
|
|
if (index2 == CC_INVALID_INDEX)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ccArraySwapObjectsAtIndexes(data, index1, index2);
|
|
|
|
}
|
|
|
|
|
2013-12-12 10:29:07 +08:00
|
|
|
void __Array::exchangeObjectAtIndex(ssize_t index1, ssize_t index2)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
ccArraySwapObjectsAtIndexes(data, index1, index2);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Array::replaceObjectAtIndex(ssize_t index, Ref* object, bool releaseObject/* = true*/)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
ccArrayInsertObjectAtIndex(data, object, index);
|
2016-06-04 20:17:53 +08:00
|
|
|
ccArrayRemoveObjectAtIndex(data, index + 1, releaseObject);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::reverseObjects()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (data->num > 1)
|
|
|
|
{
|
|
|
|
// floorf(), since in the case of an even number, the number of swaps stays the same
|
2013-12-12 10:29:07 +08:00
|
|
|
auto count = static_cast<ssize_t>(floorf(data->num/2.f));
|
|
|
|
ssize_t maxIndex = data->num - 1;
|
2013-10-12 15:25:45 +08:00
|
|
|
|
2013-12-12 10:29:07 +08:00
|
|
|
for (ssize_t i = 0; i < count ; ++i)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
ccArraySwapObjectsAtIndexes(data, i, maxIndex);
|
|
|
|
--maxIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::reduceMemoryFootprint()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
ccArrayShrink(data);
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array::~__Array()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCLOGINFO("deallocing Array: %p - len: %d", this, count() );
|
|
|
|
|
|
|
|
ccArrayFree(data);
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
__Array* __Array::clone() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__Array* ret = new (std::nothrow) __Array();
|
2013-10-12 15:25:45 +08:00
|
|
|
ret->autorelease();
|
|
|
|
ret->initWithCapacity(this->data->num > 0 ? this->data->num : 1);
|
|
|
|
|
2016-06-03 13:42:44 +08:00
|
|
|
if (data->num <= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj = nullptr;
|
|
|
|
Ref* tmpObj = nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
Clonable* clonable = nullptr;
|
2016-06-03 13:42:44 +08:00
|
|
|
CC_ASSERT(data->num > 0);
|
|
|
|
for (Ref** arr = data->arr, **end = data->arr + data->num - 1;
|
|
|
|
arr <= end && ((obj = *arr) != nullptr); arr++)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
clonable = dynamic_cast<Clonable*>(obj);
|
|
|
|
if (clonable)
|
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
tmpObj = dynamic_cast<Ref*>(clonable->clone());
|
2013-10-12 15:25:45 +08:00
|
|
|
if (tmpObj)
|
|
|
|
{
|
|
|
|
ret->addObject(tmpObj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOGWARN("%s isn't clonable.", typeid(*obj).name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
void __Array::acceptVisitor(DataVisitor &visitor)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
visitor.visit(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // uses ccArray
|
|
|
|
|
|
|
|
NS_CC_END
|