From 093e310cab6865937aabbc30d340ddbb164a15fd Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 17:50:30 +0800 Subject: [PATCH] =?UTF-8?q?issue=20#2790:=20Vector::remove=20=E2=80=94>=20?= =?UTF-8?q?Vector::erase.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cocos/base/CCVector.h | 78 ++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index cddc02c25d..7d1bb80435 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -37,6 +37,34 @@ template class CC_DLL Vector { public: + // ------------------------------------------ + // Iterators + // ------------------------------------------ + typedef typename std::vector::iterator iterator; + typedef typename std::vector::const_iterator const_iterator; + + typedef typename std::vector::reverse_iterator reverse_iterator; + typedef typename std::vector::const_reverse_iterator const_reverse_iterator; + + iterator begin() { return _data.begin(); } + const_iterator begin() const { return _data.begin(); } + + iterator end() { return _data.end(); } + const_iterator end() const { return _data.end(); } + + const_iterator cbegin() const { return _data.cbegin(); } + const_iterator cend() const { return _data.cend(); } + + reverse_iterator rbegin() { return _data.rbegin(); } + const_reverse_iterator rbegin() const { return _data.rbegin(); } + + reverse_iterator rend() { return _data.rend(); } + const_reverse_iterator rend() const { return _data.rend(); } + + const_reverse_iterator crbegin() const { return _data.crbegin(); } + const_reverse_iterator crend() const { return _data.crend(); } + + /** Constructor */ Vector() : _data() { @@ -228,9 +256,9 @@ public: _data.pop_back(); last->release(); } - + /** Remove a certain object */ - void removeObject(T object, bool toRelease = true) + void erase(T object, bool toRelease = true) { CCASSERT(object != nullptr, "The object should not be nullptr"); auto iter = std::find(_data.begin(), _data.end(), object); @@ -241,7 +269,24 @@ public: } /** Removes an element with a certain index */ - void remove(int index) + iterator erase(const_iterator position) + { + CCASSERT(position >= _data.begin() && position < _data.end(), "Invalid position!"); + (*position)->release(); + return _data.erase(position); + } + + iterator erase(const_iterator first, const_iterator last) + { + for (auto iter = first; iter != last; ++iter) + { + (*iter)->release(); + } + + return _data.erase(first, last); + } + + void erase(int index) { CCASSERT(!_data.empty() && index >=0 && index < size(), "Invalid index!"); auto it = std::next( begin(), index ); @@ -352,33 +397,6 @@ public: }); } - // ------------------------------------------ - // Iterators - // ------------------------------------------ - typedef typename std::vector::iterator iterator; - typedef typename std::vector::const_iterator const_iterator; - - typedef typename std::vector::reverse_iterator reverse_iterator; - typedef typename std::vector::const_reverse_iterator const_reverse_iterator; - - iterator begin() { return _data.begin(); } - const_iterator begin() const { return _data.begin(); } - - iterator end() { return _data.end(); } - const_iterator end() const { return _data.end(); } - - const_iterator cbegin() const { return _data.cbegin(); } - const_iterator cend() const { return _data.cend(); } - - reverse_iterator rbegin() { return _data.rbegin(); } - const_reverse_iterator rbegin() const { return _data.rbegin(); } - - reverse_iterator rend() { return _data.rend(); } - const_reverse_iterator rend() const { return _data.rend(); } - - const_reverse_iterator crbegin() const { return _data.crbegin(); } - const_reverse_iterator crend() const { return _data.crend(); } - protected: void addRefForAllObjects()