2013-11-26 16:58:33 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 ForzeField Studios S.L. http://forzefield.com
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __CCVECTOR_H__
|
|
|
|
#define __CCVECTOR_H__
|
|
|
|
|
2013-12-07 10:46:40 +08:00
|
|
|
#include "ccMacros.h"
|
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
#include <vector>
|
2013-12-07 10:46:40 +08:00
|
|
|
#include <functional>
|
2013-12-04 10:32:42 +08:00
|
|
|
#include <algorithm> // std::for_each
|
2013-11-26 16:58:33 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class CC_DLL Vector
|
|
|
|
{
|
|
|
|
public:
|
2013-11-28 17:57:13 +08:00
|
|
|
Vector<T>()
|
|
|
|
: _data()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2013-11-28 16:02:03 +08:00
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
/** creates an emptry Vector */
|
2013-12-05 17:38:08 +08:00
|
|
|
explicit Vector<T>(int capacity)
|
2013-11-26 16:58:33 +08:00
|
|
|
: _data()
|
|
|
|
{
|
2013-12-04 16:37:08 +08:00
|
|
|
CCLOGINFO("In the default constructor with capacity of Vector.");
|
2013-12-05 10:35:10 +08:00
|
|
|
reserve(capacity);
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
2013-11-29 16:33:15 +08:00
|
|
|
virtual ~Vector<T>()
|
|
|
|
{
|
2013-12-04 16:37:08 +08:00
|
|
|
CCLOGINFO("In the destructor of Vector.");
|
2013-12-05 10:35:10 +08:00
|
|
|
clear();
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
2013-11-28 11:04:39 +08:00
|
|
|
Vector<T>(const Vector<T>& other)
|
|
|
|
{
|
2013-12-04 16:37:08 +08:00
|
|
|
CCLOGINFO("In the copy constructor!");
|
2013-11-29 16:33:15 +08:00
|
|
|
_data = other._data;
|
|
|
|
addRefForAllObjects();
|
2013-11-28 11:04:39 +08:00
|
|
|
}
|
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
/** Move constructor */
|
|
|
|
Vector<T>(Vector<T>&& other)
|
|
|
|
{
|
2013-12-04 16:37:08 +08:00
|
|
|
CCLOGINFO("In the move constructor of Vector!");
|
2013-12-10 15:58:13 +08:00
|
|
|
_data = std::move(other._data);
|
2013-11-28 16:02:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<T>& operator=(const Vector<T>& other)
|
2013-11-28 11:04:39 +08:00
|
|
|
{
|
2013-12-04 16:37:08 +08:00
|
|
|
CCLOGINFO("In the copy assignment operator!");
|
2013-12-05 10:35:10 +08:00
|
|
|
clear();
|
2013-11-29 16:33:15 +08:00
|
|
|
_data = other._data;
|
|
|
|
addRefForAllObjects();
|
2013-11-28 11:04:39 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
Vector<T>& operator=(Vector<T>&& other)
|
|
|
|
{
|
2013-12-04 16:37:08 +08:00
|
|
|
CCLOGINFO("In the move assignment operator!");
|
2013-12-10 15:58:13 +08:00
|
|
|
_data = std::move(other._data);
|
2013-11-28 16:02:03 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-05 10:35:10 +08:00
|
|
|
// Don't uses operator since we could not decide whether it needs 'retain'/'release'.
|
2013-12-05 17:38:08 +08:00
|
|
|
// T& operator[](int index)
|
2013-11-29 16:33:15 +08:00
|
|
|
// {
|
|
|
|
// return _data[index];
|
|
|
|
// }
|
|
|
|
//
|
2013-12-05 17:38:08 +08:00
|
|
|
// const T& operator[](int index) const
|
2013-11-29 16:33:15 +08:00
|
|
|
// {
|
|
|
|
// return _data[index];
|
|
|
|
// }
|
2013-11-28 11:04:39 +08:00
|
|
|
|
2013-11-28 17:57:13 +08:00
|
|
|
/** Sets capacity of current array */
|
2013-12-05 17:38:08 +08:00
|
|
|
void reserve(int capacity)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
|
|
|
_data.reserve(capacity);
|
2013-11-28 17:57:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns capacity of the array */
|
2013-12-05 17:38:08 +08:00
|
|
|
int capacity() const
|
2013-11-28 17:57:13 +08:00
|
|
|
{
|
|
|
|
return _data.capacity();
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
2013-11-28 11:04:39 +08:00
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
// Querying an Array
|
|
|
|
|
|
|
|
/** Returns element count of the array */
|
2013-12-05 17:38:08 +08:00
|
|
|
int size() const
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-06 16:32:06 +08:00
|
|
|
return static_cast<int>(_data.size());
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
2013-11-28 18:23:06 +08:00
|
|
|
bool empty() const
|
|
|
|
{
|
|
|
|
return _data.empty();
|
|
|
|
}
|
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
/** Returns index of a certain object, return UINT_MAX if doesn't contain the object */
|
2013-12-05 17:38:08 +08:00
|
|
|
int getIndex(T object) const
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 17:38:08 +08:00
|
|
|
int i = 0;
|
2013-11-26 16:58:33 +08:00
|
|
|
for (auto it = _data.begin(); it != _data.end(); ++it, ++i)
|
|
|
|
{
|
|
|
|
if (*it == object)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns an element with a certain index */
|
2013-12-05 17:38:08 +08:00
|
|
|
T at(int index) const
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
CCASSERT( index >= 0 && index < size(), "index out of range in getObjectAtIndex()");
|
2013-11-26 16:58:33 +08:00
|
|
|
return _data[index];
|
|
|
|
}
|
|
|
|
|
2013-12-05 10:35:10 +08:00
|
|
|
T front() const
|
|
|
|
{
|
|
|
|
return _data.front();
|
|
|
|
}
|
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
/** Returns the last element of the array */
|
2013-12-05 10:35:10 +08:00
|
|
|
T back() const
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
|
|
|
return _data.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns a random element */
|
|
|
|
T getRandomObject() const
|
|
|
|
{
|
2013-11-29 16:33:15 +08:00
|
|
|
if (!_data.empty())
|
|
|
|
{
|
|
|
|
int randIdx = rand() % _data.size();
|
|
|
|
return *(_data.begin() + randIdx);
|
|
|
|
}
|
|
|
|
return nullptr;
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns a Boolean value that indicates whether object is present in array. */
|
2013-12-05 10:35:10 +08:00
|
|
|
bool contains(T object) const
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
|
|
|
return( std::find(_data.begin(), _data.end(), object) != _data.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** returns true if the the arrays are equal */
|
2013-12-05 10:35:10 +08:00
|
|
|
bool equals(const Vector<T> &other)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
size_t s = this->size();
|
|
|
|
if (s != other.size())
|
|
|
|
return false;
|
|
|
|
|
2013-12-05 17:38:08 +08:00
|
|
|
for (int i = 0; i < s; i++)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
if (!this->at(i)->isEqual(other.at(i)))
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adding Objects
|
|
|
|
|
|
|
|
/** Add a certain object */
|
2013-12-05 10:35:10 +08:00
|
|
|
void pushBack(T object)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
CCASSERT(object != nullptr, "The object should not be nullptr");
|
2013-11-26 16:58:33 +08:00
|
|
|
_data.push_back( object );
|
|
|
|
object->retain();
|
|
|
|
}
|
2013-12-05 10:35:10 +08:00
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
/** Add all elements of an existing array */
|
2013-12-05 10:35:10 +08:00
|
|
|
void insert(const Vector<T>& other)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
for( auto it = other.begin(); it != other.end(); ++it ) {
|
2013-11-26 16:58:33 +08:00
|
|
|
_data.push_back( *it );
|
|
|
|
(*it)->retain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Insert a certain object at a certain index */
|
2013-12-05 17:38:08 +08:00
|
|
|
void insert(int index, T object)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
CCASSERT(index >= 0 && index <= size(), "Invalid index!");
|
|
|
|
CCASSERT(object != nullptr, "The object should not be nullptr");
|
2013-11-28 16:02:03 +08:00
|
|
|
_data.insert((std::begin(_data) + index), object);
|
2013-11-26 16:58:33 +08:00
|
|
|
object->retain();
|
|
|
|
}
|
2013-12-05 10:35:10 +08:00
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
// Removing Objects
|
|
|
|
|
|
|
|
/** Remove last object */
|
2013-12-05 10:35:10 +08:00
|
|
|
void popBack()
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
CCASSERT(!_data.empty(), "no objects added");
|
2013-11-26 16:58:33 +08:00
|
|
|
auto last = _data.back();
|
|
|
|
_data.pop_back();
|
|
|
|
last->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Remove a certain object */
|
2013-12-07 14:18:42 +08:00
|
|
|
void removeObject(T object, bool toRelease = true)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
CCASSERT(object != nullptr, "The object should not be nullptr");
|
|
|
|
auto iter = std::find(_data.begin(), _data.end(), object);
|
|
|
|
if (iter != _data.end())
|
|
|
|
_data.erase(iter);
|
2013-12-07 14:18:42 +08:00
|
|
|
if (toRelease)
|
|
|
|
object->release();
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Removes an element with a certain index */
|
2013-12-05 17:38:08 +08:00
|
|
|
void remove(int index)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
CCASSERT(!_data.empty() && index >=0 && index < size(), "Invalid index!");
|
2013-11-26 16:58:33 +08:00
|
|
|
auto it = std::next( begin(), index );
|
|
|
|
(*it)->release();
|
2013-11-28 11:04:39 +08:00
|
|
|
_data.erase(it);
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Removes all objects */
|
2013-12-05 10:35:10 +08:00
|
|
|
void clear()
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
|
|
|
for( auto it = std::begin(_data); it != std::end(_data); ++it ) {
|
|
|
|
(*it)->release();
|
|
|
|
}
|
2013-11-28 11:04:39 +08:00
|
|
|
_data.clear();
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rearranging Content
|
|
|
|
|
|
|
|
/** Swap two elements */
|
2013-12-05 10:35:10 +08:00
|
|
|
void swap(T object1, T object2)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 17:38:08 +08:00
|
|
|
auto idx1 = getIndex(object1);
|
|
|
|
auto idx2 = getIndex(object2);
|
2013-11-26 16:58:33 +08:00
|
|
|
|
2013-12-05 10:35:10 +08:00
|
|
|
CCASSERT(idx1>=0 && idx2>=0, "invalid object index");
|
2013-11-26 16:58:33 +08:00
|
|
|
|
|
|
|
std::swap( _data[idx1], _data[idx2] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Swap two elements with certain indexes */
|
2013-12-05 17:38:08 +08:00
|
|
|
void swap(int index1, int index2)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
CCASSERT(index1 >=0 && index1 < size() && index2 >= 0 && index2 < size(), "Invalid indices");
|
2013-11-26 16:58:33 +08:00
|
|
|
|
|
|
|
std::swap( _data[index1], _data[index2] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Replace object at index with another object. */
|
2013-12-05 17:38:08 +08:00
|
|
|
void replace(int index, T object)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
CCASSERT(index >= 0 && index < size(), "Invalid index!");
|
|
|
|
CCASSERT(object != nullptr, "The object should not be nullptr");
|
|
|
|
|
|
|
|
_data[index]->release();
|
|
|
|
_data[index] = object;
|
|
|
|
object->retain();
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** reverses the array */
|
2013-12-05 10:35:10 +08:00
|
|
|
void reverse()
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
|
|
|
std::reverse( std::begin(_data), std::end(_data) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Shrinks the array so the memory footprint corresponds with the number of items */
|
2013-12-05 10:35:10 +08:00
|
|
|
void shrinkToFit()
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
|
|
|
_data.shrink_to_fit();
|
|
|
|
}
|
2013-11-28 16:02:03 +08:00
|
|
|
|
2013-12-07 10:46:40 +08:00
|
|
|
void forEach(const std::function<void(T)>& callback)
|
2013-11-28 16:02:03 +08:00
|
|
|
{
|
2013-12-07 10:46:40 +08:00
|
|
|
if (empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::for_each(_data.cbegin(), _data.cend(), [&callback](const T& obj){
|
|
|
|
callback(obj);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void forEach(const std::function<void(T)>& callback) const
|
|
|
|
{
|
|
|
|
if (empty())
|
2013-11-28 16:02:03 +08:00
|
|
|
return;
|
|
|
|
|
2013-12-03 16:21:55 +08:00
|
|
|
std::for_each(_data.cbegin(), _data.cend(), [&callback](const T& obj){
|
2013-11-28 16:02:03 +08:00
|
|
|
callback(obj);
|
|
|
|
});
|
|
|
|
}
|
2013-11-26 16:58:33 +08:00
|
|
|
|
2013-12-07 10:46:40 +08:00
|
|
|
void forEachReverse(const std::function<void(T)>& callback)
|
|
|
|
{
|
|
|
|
if (empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::for_each(_data.crbegin(), _data.crend(), [&callback](const T& obj){
|
|
|
|
callback(obj);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void forEachReverse(const std::function<void(T)>& callback) const
|
2013-11-29 10:37:40 +08:00
|
|
|
{
|
2013-12-07 10:46:40 +08:00
|
|
|
if (empty())
|
2013-11-29 10:37:40 +08:00
|
|
|
return;
|
|
|
|
|
2013-12-03 16:21:55 +08:00
|
|
|
std::for_each(_data.crbegin(), _data.crend(), [&callback](const T& obj){
|
2013-11-29 10:37:40 +08:00
|
|
|
callback(obj);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-07 14:18:42 +08:00
|
|
|
void sort(const std::function<bool(T, T)>& callback)
|
|
|
|
{
|
|
|
|
if (empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::sort(_data.begin(), _data.end(), [&callback](T a, T b) -> bool{
|
|
|
|
return callback(a, b);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
// ------------------------------------------
|
|
|
|
// Iterators
|
|
|
|
// ------------------------------------------
|
|
|
|
typedef typename std::vector<T>::iterator iterator;
|
|
|
|
typedef typename std::vector<T>::const_iterator const_iterator;
|
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
typedef typename std::vector<T>::reverse_iterator reverse_iterator;
|
|
|
|
typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator;
|
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
iterator begin() { return _data.begin(); }
|
2013-11-28 11:04:39 +08:00
|
|
|
const_iterator begin() const { return _data.begin(); }
|
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
iterator end() { return _data.end(); }
|
2013-11-28 11:04:39 +08:00
|
|
|
const_iterator end() const { return _data.end(); }
|
|
|
|
|
|
|
|
const_iterator cbegin() const { return _data.cbegin(); }
|
|
|
|
const_iterator cend() const { return _data.cend(); }
|
2013-11-28 16:02:03 +08:00
|
|
|
|
|
|
|
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(); }
|
|
|
|
|
2013-11-29 11:36:42 +08:00
|
|
|
const_reverse_iterator crbegin() const { return _data.crbegin(); }
|
|
|
|
const_reverse_iterator crend() const { return _data.crend(); }
|
2013-11-29 16:33:15 +08:00
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
protected:
|
2013-11-29 16:33:15 +08:00
|
|
|
|
|
|
|
void addRefForAllObjects()
|
|
|
|
{
|
|
|
|
std::for_each(_data.begin(), _data.end(), [](T obj){
|
|
|
|
obj->retain();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
std::vector<T> _data;
|
|
|
|
};
|
|
|
|
|
|
|
|
// end of data_structure group
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __CCVECTOR_H__
|