issue #2790: Deletes Vector::init, adds Vector::setCapacity.

This commit is contained in:
James Chen 2013-11-28 17:57:13 +08:00
parent 90062b656d
commit d4d864216b
7 changed files with 41 additions and 34 deletions

View File

@ -190,7 +190,8 @@ EventDispatcher::~EventDispatcher()
void EventDispatcher::visitTarget(Node* node) void EventDispatcher::visitTarget(Node* node)
{ {
long i = 0; long i = 0;
auto children = node->getChildren(); const Vector<Node*>& children = node->getChildren();
long childrenCount = children.count(); long childrenCount = children.count();
if(childrenCount > 0) if(childrenCount > 0)

View File

@ -586,6 +586,12 @@ const char* Node::description() const
return String::createWithFormat("<Node | Tag = %d>", _tag)->getCString(); return String::createWithFormat("<Node | Tag = %d>", _tag)->getCString();
} }
// lazy allocs
void Node::childrenAlloc(void)
{
_children.setCapacity(4);
}
Node* Node::getChildByTag(int aTag) Node* Node::getChildByTag(int aTag)
{ {
CCASSERT( aTag != Node::INVALID_TAG, "Invalid tag"); CCASSERT( aTag != Node::INVALID_TAG, "Invalid tag");
@ -607,6 +613,11 @@ void Node::addChild(Node *child, int zOrder, int tag)
CCASSERT( child != NULL, "Argument must be non-nil"); CCASSERT( child != NULL, "Argument must be non-nil");
CCASSERT( child->_parent == NULL, "child already added. It can't be added again"); CCASSERT( child->_parent == NULL, "child already added. It can't be added again");
if (_children.count() == 0)
{
this->childrenAlloc();
}
this->insertChild(child, zOrder); this->insertChild(child, zOrder);
#ifdef CC_USE_PHYSICS #ifdef CC_USE_PHYSICS

View File

@ -1394,6 +1394,9 @@ protected:
Node(); Node();
virtual ~Node(); virtual ~Node();
virtual bool init(); virtual bool init();
/// lazy allocs
void childrenAlloc(void);
/// helper that reorder a child /// helper that reorder a child
void insertChild(Node* child, int z); void insertChild(Node* child, int z);

View File

@ -95,11 +95,7 @@ bool ParticleBatchNode::initWithTexture(Texture2D *tex, int capacity)
_textureAtlas = new TextureAtlas(); _textureAtlas = new TextureAtlas();
_textureAtlas->initWithTexture(tex, capacity); _textureAtlas->initWithTexture(tex, capacity);
// no lazy alloc in this node _children.setCapacity(capacity);
if (_children.count() == 0)
{
_children.init(capacity);
}
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED; _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
@ -211,10 +207,7 @@ long ParticleBatchNode::addChildHelper(ParticleSystem* child, int z, int aTag)
CCASSERT( child != NULL, "Argument must be non-nil"); CCASSERT( child != NULL, "Argument must be non-nil");
CCASSERT( child->getParent() == NULL, "child already added. It can't be added again"); CCASSERT( child->getParent() == NULL, "child already added. It can't be added again");
if (_children.count() == 0 ) _children.setCapacity(4);
{
_children.init(4);
}
//don't use a lazy insert //don't use a lazy insert
long pos = searchNewPositionInChildrenForZ(z); long pos = searchNewPositionInChildrenForZ(z);

View File

@ -92,11 +92,8 @@ bool SpriteBatchNode::initWithTexture(Texture2D *tex, long capacity)
updateBlendFunc(); updateBlendFunc();
// no lazy alloc in this node _children.setCapacity(capacity);
if (_children.count() == 0)
{
_children.init(capacity);
}
_descendants.reserve(capacity); _descendants.reserve(capacity);
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR)); setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));

View File

@ -35,15 +35,19 @@ template<class T>
class CC_DLL Vector class CC_DLL Vector
{ {
public: public:
static const long DEFAULT_CAPACTIY = 7; Vector<T>()
/** creates an emptry Vector */
explicit Vector<T>(long capacity=DEFAULT_CAPACTIY)
: _data() : _data()
{ {
CCLOG("In the default constructor of Vector.");
init(capacity); }
/** creates an emptry Vector */
explicit Vector<T>(long capacity)
: _data()
{
CCLOG("In the default constructor with capacity of Vector.");
setCapacity(capacity);
} }
virtual ~Vector<T>() { virtual ~Vector<T>() {
@ -83,11 +87,16 @@ public:
return getObjectAtIndex(index); return getObjectAtIndex(index);
} }
/** Initializes an array with capacity */ /** Sets capacity of current array */
bool init(long capacity) void setCapacity(long capacity)
{ {
_data.reserve(capacity); _data.reserve(capacity);
return true; }
/** Returns capacity of the array */
long getCapacity() const
{
return _data.capacity();
} }
void copy(const Vector<T>& other) void copy(const Vector<T>& other)
@ -96,7 +105,7 @@ public:
return; return;
removeAllObjects(); removeAllObjects();
init(other.count()); setCapacity(other.count());
addObjectsFromArray(other); addObjectsFromArray(other);
} }
@ -108,12 +117,6 @@ public:
return _data.size(); return _data.size();
} }
/** Returns capacity of the array */
long capacity() const
{
return _data.capacity();
}
/** Returns index of a certain object, return UINT_MAX if doesn't contain the object */ /** Returns index of a certain object, return UINT_MAX if doesn't contain the object */
long getIndexOfObject(T object) const long getIndexOfObject(T object) const
{ {
@ -238,7 +241,6 @@ public:
(*it)->release(); (*it)->release();
} }
_data.clear(); _data.clear();
_data.reserve(DEFAULT_CAPACTIY);
} }
/** Fast way to remove a certain object */ /** Fast way to remove a certain object */

View File

@ -305,7 +305,7 @@ void Bone::addChildBone(Bone *child)
if(_children.count() == 0) if(_children.count() == 0)
{ {
_children.init(4); _children.setCapacity(4);
} }
if (_children.getIndexOfObject(child) == CC_INVALID_INDEX) if (_children.getIndexOfObject(child) == CC_INVALID_INDEX)