From d4d864216bca38e0ad5a92dbd995f0210e0850db Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 28 Nov 2013 17:57:13 +0800 Subject: [PATCH] issue #2790: Deletes Vector::init, adds Vector::setCapacity. --- cocos/2d/CCEventDispatcher.cpp | 3 +- cocos/2d/CCNode.cpp | 11 +++++++ cocos/2d/CCNode.h | 3 ++ cocos/2d/CCParticleBatchNode.cpp | 11 ++----- cocos/2d/CCSpriteBatchNode.cpp | 7 ++-- cocos/base/CCVector.h | 38 ++++++++++++---------- cocos/editor-support/cocostudio/CCBone.cpp | 2 +- 7 files changed, 41 insertions(+), 34 deletions(-) diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index ade48d38f3..01b4606f75 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -190,7 +190,8 @@ EventDispatcher::~EventDispatcher() void EventDispatcher::visitTarget(Node* node) { long i = 0; - auto children = node->getChildren(); + const Vector& children = node->getChildren(); + long childrenCount = children.count(); if(childrenCount > 0) diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 641a743ed4..df4ade5cf9 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -586,6 +586,12 @@ const char* Node::description() const return String::createWithFormat("", _tag)->getCString(); } +// lazy allocs +void Node::childrenAlloc(void) +{ + _children.setCapacity(4); +} + Node* Node::getChildByTag(int aTag) { 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->_parent == NULL, "child already added. It can't be added again"); + if (_children.count() == 0) + { + this->childrenAlloc(); + } + this->insertChild(child, zOrder); #ifdef CC_USE_PHYSICS diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 37d28b0555..c1fa60b23c 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -1394,6 +1394,9 @@ protected: Node(); virtual ~Node(); virtual bool init(); + + /// lazy allocs + void childrenAlloc(void); /// helper that reorder a child void insertChild(Node* child, int z); diff --git a/cocos/2d/CCParticleBatchNode.cpp b/cocos/2d/CCParticleBatchNode.cpp index d396f29024..e5e1ef2033 100644 --- a/cocos/2d/CCParticleBatchNode.cpp +++ b/cocos/2d/CCParticleBatchNode.cpp @@ -95,11 +95,7 @@ bool ParticleBatchNode::initWithTexture(Texture2D *tex, int capacity) _textureAtlas = new TextureAtlas(); _textureAtlas->initWithTexture(tex, capacity); - // no lazy alloc in this node - if (_children.count() == 0) - { - _children.init(capacity); - } + _children.setCapacity(capacity); _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->getParent() == NULL, "child already added. It can't be added again"); - if (_children.count() == 0 ) - { - _children.init(4); - } + _children.setCapacity(4); //don't use a lazy insert long pos = searchNewPositionInChildrenForZ(z); diff --git a/cocos/2d/CCSpriteBatchNode.cpp b/cocos/2d/CCSpriteBatchNode.cpp index 05c3e2a415..820224a234 100644 --- a/cocos/2d/CCSpriteBatchNode.cpp +++ b/cocos/2d/CCSpriteBatchNode.cpp @@ -92,11 +92,8 @@ bool SpriteBatchNode::initWithTexture(Texture2D *tex, long capacity) updateBlendFunc(); - // no lazy alloc in this node - if (_children.count() == 0) - { - _children.init(capacity); - } + _children.setCapacity(capacity); + _descendants.reserve(capacity); setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR)); diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index 9b9c307b96..c0f69c55b5 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -35,15 +35,19 @@ template class CC_DLL Vector { public: - - static const long DEFAULT_CAPACTIY = 7; - - /** creates an emptry Vector */ - explicit Vector(long capacity=DEFAULT_CAPACTIY) + + Vector() : _data() { - CCLOG("In the default constructor of Vector."); - init(capacity); + + } + + /** creates an emptry Vector */ + explicit Vector(long capacity) + : _data() + { + CCLOG("In the default constructor with capacity of Vector."); + setCapacity(capacity); } virtual ~Vector() { @@ -83,11 +87,16 @@ public: return getObjectAtIndex(index); } - /** Initializes an array with capacity */ - bool init(long capacity) + /** Sets capacity of current array */ + void setCapacity(long capacity) { _data.reserve(capacity); - return true; + } + + /** Returns capacity of the array */ + long getCapacity() const + { + return _data.capacity(); } void copy(const Vector& other) @@ -96,7 +105,7 @@ public: return; removeAllObjects(); - init(other.count()); + setCapacity(other.count()); addObjectsFromArray(other); } @@ -108,12 +117,6 @@ public: 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 */ long getIndexOfObject(T object) const { @@ -238,7 +241,6 @@ public: (*it)->release(); } _data.clear(); - _data.reserve(DEFAULT_CAPACTIY); } /** Fast way to remove a certain object */ diff --git a/cocos/editor-support/cocostudio/CCBone.cpp b/cocos/editor-support/cocostudio/CCBone.cpp index 1e0e87bc94..01392ad560 100644 --- a/cocos/editor-support/cocostudio/CCBone.cpp +++ b/cocos/editor-support/cocostudio/CCBone.cpp @@ -305,7 +305,7 @@ void Bone::addChildBone(Bone *child) if(_children.count() == 0) { - _children.init(4); + _children.setCapacity(4); } if (_children.getIndexOfObject(child) == CC_INVALID_INDEX)