2013-11-26 16:58:33 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 ForzeField Studios S.L. http://forzefield.com
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2013-11-26 16:58:33 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2013-11-26 16:58:33 +08:00
|
|
|
|
|
|
|
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__
|
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCRef.h"
|
2013-11-26 16:58:33 +08:00
|
|
|
#include <vector>
|
2013-12-07 10:46:40 +08:00
|
|
|
#include <functional>
|
2021-12-25 10:04:45 +08:00
|
|
|
#include <algorithm> // for std::find
|
2013-11-26 16:58:33 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/**
|
2015-03-24 11:15:40 +08:00
|
|
|
* @addtogroup base
|
2015-03-27 12:01:20 +08:00
|
|
|
* @{
|
|
|
|
*/
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-03-27 12:01:20 +08:00
|
|
|
|
|
|
|
/*
|
2015-03-23 22:13:04 +08:00
|
|
|
* Similar to std::vector, but it will manage reference count automatically internally.
|
|
|
|
* Which means it will invoke Ref::retain() when adding an element, and invoke Ref::release() when removing an element.
|
|
|
|
* @warn The element should be `Ref` or its sub-class.
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
template <class T>
|
2014-07-14 18:40:10 +08:00
|
|
|
class Vector
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-12-12 14:45:30 +08:00
|
|
|
// ------------------------------------------
|
|
|
|
// Iterators
|
|
|
|
// ------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-24 14:12:58 +08:00
|
|
|
/** Iterator, can be used to loop the Vector. */
|
2016-04-07 13:47:01 +08:00
|
|
|
using iterator = typename std::vector<T>::iterator;
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Const iterator, can be used to loop the Vector. */
|
2016-04-07 13:47:01 +08:00
|
|
|
using const_iterator = typename std::vector<T>::const_iterator;
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Reversed iterator, can be used to loop the Vector in reverse sequence. */
|
2016-04-07 13:47:01 +08:00
|
|
|
using reverse_iterator = typename std::vector<T>::reverse_iterator;
|
2021-12-25 10:04:45 +08:00
|
|
|
/** Reversed iterator, can be used to loop the Vector in reverse sequence. */
|
2016-04-07 13:47:01 +08:00
|
|
|
using const_reverse_iterator = typename std::vector<T>::const_reverse_iterator;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns an iterator pointing the first element of the Vector. */
|
2013-12-12 14:45:30 +08:00
|
|
|
iterator begin() { return _data.begin(); }
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns an iterator pointing the first element of the Vector. */
|
2013-12-12 14:45:30 +08:00
|
|
|
const_iterator begin() const { return _data.begin(); }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-23 22:13:04 +08:00
|
|
|
* Returns an iterator referring to the `past-the-end` element in the Vector container.
|
|
|
|
* The past-the-end element is the theoretical element that would follow the last element in the Vector.
|
|
|
|
* It does not point to any element, and thus shall not be dereferenced.
|
|
|
|
*/
|
2013-12-12 14:45:30 +08:00
|
|
|
iterator end() { return _data.end(); }
|
2015-03-23 22:13:04 +08:00
|
|
|
/**
|
|
|
|
* Returns iterator referring to the `past-the-end` element in the Vector container.
|
|
|
|
* The past-the-end element is the theoretical element that would follow the last element in the Vector.
|
|
|
|
* It does not point to any element, and thus shall not be dereferenced.
|
|
|
|
*/
|
2013-12-12 14:45:30 +08:00
|
|
|
const_iterator end() const { return _data.end(); }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns a const_iterator pointing the first element of the Vector. */
|
2013-12-12 14:45:30 +08:00
|
|
|
const_iterator cbegin() const { return _data.cbegin(); }
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns a const_iterator pointing the `past-the-end` element of the Vector. */
|
2013-12-12 14:45:30 +08:00
|
|
|
const_iterator cend() const { return _data.cend(); }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns a reverse iterator pointing to the last element of the Vector. */
|
2013-12-12 14:45:30 +08:00
|
|
|
reverse_iterator rbegin() { return _data.rbegin(); }
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns a reverse iterator pointing to the last element of the Vector. */
|
2013-12-12 14:45:30 +08:00
|
|
|
const_reverse_iterator rbegin() const { return _data.rbegin(); }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/** Returns a reverse iterator pointing to the theoretical element preceding the
|
2015-03-23 22:13:04 +08:00
|
|
|
* first element of the vector (which is considered its reverse end).
|
|
|
|
*/
|
2013-12-12 14:45:30 +08:00
|
|
|
reverse_iterator rend() { return _data.rend(); }
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns a reverse iterator pointing to the theoretical element preceding the
|
|
|
|
* first element of the vector (which is considered its reverse end).
|
|
|
|
*/
|
2013-12-12 14:45:30 +08:00
|
|
|
const_reverse_iterator rend() const { return _data.rend(); }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns a const_reverse_iterator pointing to the last element in the container (i.e., its reverse beginning). */
|
2013-12-12 14:45:30 +08:00
|
|
|
const_reverse_iterator crbegin() const { return _data.crbegin(); }
|
2021-12-25 10:04:45 +08:00
|
|
|
/** Returns a const_reverse_iterator pointing to the theoretical element preceding the first element in
|
|
|
|
* the container (which is considered its reverse end).
|
2015-03-23 22:13:04 +08:00
|
|
|
*/
|
2013-12-12 14:45:30 +08:00
|
|
|
const_reverse_iterator crend() const { return _data.crend(); }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Constructor. */
|
2021-12-25 10:04:45 +08:00
|
|
|
Vector<T>() : _data()
|
2013-11-28 17:57:13 +08:00
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
static_assert(std::is_convertible<T, Ref*>::value, "Invalid Type for axis::Vector<T>!");
|
2013-11-28 17:57:13 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor with a capacity.
|
2015-03-23 22:13:04 +08:00
|
|
|
* @param capacity Capacity of the Vector.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
explicit Vector<T>(ssize_t capacity) : _data()
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
static_assert(std::is_convertible<T, Ref*>::value, "Invalid Type for axis::Vector<T>!");
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGINFO("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
|
|
|
}
|
|
|
|
|
2016-04-07 13:47:01 +08:00
|
|
|
/** Constructor with initializer list. */
|
|
|
|
Vector<T>(std::initializer_list<T> list)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& element : list)
|
2016-04-07 13:47:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
pushBack(element);
|
2016-04-07 13:47:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Destructor. */
|
2013-12-10 21:20:52 +08:00
|
|
|
~Vector<T>()
|
2013-11-29 16:33:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGINFO("In the destructor of Vector.");
|
2013-12-05 10:35:10 +08:00
|
|
|
clear();
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Copy constructor. */
|
2013-11-28 11:04:39 +08:00
|
|
|
Vector<T>(const Vector<T>& other)
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
static_assert(std::is_convertible<T, Ref*>::value, "Invalid Type for axis::Vector<T>!");
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGINFO("In the copy constructor!");
|
2013-11-29 16:33:15 +08:00
|
|
|
_data = other._data;
|
|
|
|
addRefForAllObjects();
|
2013-11-28 11:04:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Constructor with std::move semantic. */
|
2013-11-28 16:02:03 +08:00
|
|
|
Vector<T>(Vector<T>&& other)
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
static_assert(std::is_convertible<T, Ref*>::value, "Invalid Type for axis::Vector<T>!");
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGINFO("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
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Copy assignment operator. */
|
2013-11-28 16:02:03 +08:00
|
|
|
Vector<T>& operator=(const Vector<T>& other)
|
2013-11-28 11:04:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (this != &other)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGINFO("In the copy assignment operator!");
|
2014-01-02 17:54:52 +08:00
|
|
|
clear();
|
|
|
|
_data = other._data;
|
|
|
|
addRefForAllObjects();
|
|
|
|
}
|
2013-11-28 11:04:39 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Copy assignment operator with std::move semantic. */
|
2013-11-28 16:02:03 +08:00
|
|
|
Vector<T>& operator=(Vector<T>&& other)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (this != &other)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGINFO("In the move assignment operator!");
|
2014-01-03 10:37:26 +08:00
|
|
|
clear();
|
2014-01-02 17:54:52 +08:00
|
|
|
_data = std::move(other._data);
|
|
|
|
}
|
2013-11-28 16:02:03 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// Don't uses operator since we could not decide whether it needs 'retain'/'release'.
|
|
|
|
// T& operator[](int index)
|
|
|
|
// {
|
|
|
|
// return _data[index];
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// const T& operator[](int index) const
|
|
|
|
// {
|
|
|
|
// return _data[index];
|
|
|
|
// }
|
|
|
|
|
|
|
|
/**
|
2015-03-23 22:13:04 +08:00
|
|
|
* Requests that the vector capacity be at least enough to contain n elements.
|
|
|
|
* @param capacity Minimum capacity requested of the Vector.
|
2013-12-12 14:45:30 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void reserve(ssize_t n) { _data.reserve(n); }
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** @brief Returns the size of the storage space currently allocated for the Vector, expressed in terms of elements.
|
|
|
|
* @note This capacity is not necessarily equal to the Vector size.
|
2021-12-25 10:04:45 +08:00
|
|
|
* It can be equal or greater, with the extra space allowing to accommodate for growth without the need to
|
|
|
|
* reallocate on each insertion.
|
|
|
|
* @return The size of the currently allocated storage capacity in the Vector, measured in terms of the number
|
|
|
|
* elements it can hold.
|
2013-12-12 14:45:30 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t capacity() const { return _data.capacity(); }
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** @brief Returns the number of elements in the Vector.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @note This is the number of actual objects held in the Vector, which is not necessarily equal to its storage
|
|
|
|
* capacity.
|
2015-03-23 22:13:04 +08:00
|
|
|
* @return The number of elements in the Vector.
|
2013-12-12 14:45:30 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t size() const { return _data.size(); }
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** @brief Returns whether the Vector is empty (i.e. whether its size is 0).
|
2021-12-25 10:04:45 +08:00
|
|
|
* @note This function does not modify the container in any way. To clear the content of a vector, see
|
|
|
|
* Vector<T>::clear.
|
2013-12-12 14:45:30 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool empty() const { return _data.empty(); }
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns the maximum number of elements that the Vector can hold. */
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t max_size() const { return _data.max_size(); }
|
|
|
|
|
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-12 10:29:07 +08:00
|
|
|
ssize_t getIndex(T object) const
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-12 14:45:30 +08:00
|
|
|
auto iter = std::find(_data.begin(), _data.end(), object);
|
|
|
|
if (iter != _data.end())
|
|
|
|
return iter - _data.begin();
|
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** @brief Find the object in the Vector.
|
|
|
|
* @param object The object to find.
|
|
|
|
* @return Returns an iterator which refers to the element that its value is equals to object.
|
|
|
|
* Returns Vector::end() if not found.
|
2013-12-12 14:45:30 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
const_iterator find(T object) const { return std::find(_data.begin(), _data.end(), object); }
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** @brief Find the object in the Vector.
|
|
|
|
* @param object The object to find.
|
|
|
|
* @return Returns an iterator which refers to the element that its value is equals to object.
|
|
|
|
* Returns Vector::end() if not found.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
iterator find(T object) { return std::find(_data.begin(), _data.end(), object); }
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns the element at position 'index' in the Vector. */
|
2013-12-12 10:29:07 +08:00
|
|
|
T at(ssize_t index) const
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(index >= 0 && index < size(), "index out of range in getObjectAtIndex()");
|
2013-11-26 16:58:33 +08:00
|
|
|
return _data[index];
|
|
|
|
}
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns the first element in the Vector. */
|
2021-12-25 10:04:45 +08:00
|
|
|
T front() const { return _data.front(); }
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns the last element of the Vector. */
|
2021-12-25 10:04:45 +08:00
|
|
|
T back() const { return _data.back(); }
|
2013-11-26 16:58:33 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Returns a random element of the Vector. */
|
2013-11-26 16:58:33 +08:00
|
|
|
T getRandomObject() const
|
|
|
|
{
|
2013-11-29 16:33:15 +08:00
|
|
|
if (!_data.empty())
|
|
|
|
{
|
2016-04-07 16:01:17 +08:00
|
|
|
ssize_t randIdx = RandomHelper::random_int<int>(0, static_cast<int>(_data.size()) - 1);
|
2013-11-29 16:33:15 +08:00
|
|
|
return *(_data.begin() + randIdx);
|
|
|
|
}
|
|
|
|
return nullptr;
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
2015-03-23 22:13:04 +08:00
|
|
|
* Checks whether an object is in the container.
|
|
|
|
* @param object The object to be checked.
|
|
|
|
* @return True if the object is in the container, false if not.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool contains(T object) const { return (std::find(_data.begin(), _data.end(), object) != _data.end()); }
|
2013-11-26 16:58:33 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
2015-03-23 22:13:04 +08:00
|
|
|
* Checks whether two vectors are equal.
|
|
|
|
* @param other The vector to be compared.
|
|
|
|
* @return True if two vectors are equal, false if not.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool equals(const Vector<T>& other) const
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-12 10:29:07 +08:00
|
|
|
ssize_t s = this->size();
|
2013-12-05 10:35:10 +08:00
|
|
|
if (s != other.size())
|
|
|
|
return false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-12 10:29:07 +08:00
|
|
|
for (ssize_t i = 0; i < s; i++)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
if (this->at(i) != other.at(i))
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-12 14:45:30 +08:00
|
|
|
// Adds objects
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Adds a new element at the end of the Vector. */
|
2014-01-03 10:44:43 +08:00
|
|
|
void pushBack(T object)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(object != nullptr, "The object should not be nullptr");
|
2021-12-25 10:04:45 +08:00
|
|
|
_data.push_back(object);
|
2013-11-26 16:58:33 +08:00
|
|
|
object->retain();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Push all elements of an existing Vector to the end of current Vector. */
|
2013-12-17 18:09:07 +08:00
|
|
|
void pushBack(const Vector<T>& other)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& obj : other)
|
|
|
|
{
|
2013-12-17 18:09:07 +08:00
|
|
|
_data.push_back(obj);
|
|
|
|
obj->retain();
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
2015-03-23 22:13:04 +08:00
|
|
|
* Insert an object at certain index.
|
|
|
|
* @param index The index to be inserted at.
|
|
|
|
* @param object The object to be inserted.
|
2013-12-12 14:45:30 +08:00
|
|
|
*/
|
2013-12-12 10:29:07 +08:00
|
|
|
void insert(ssize_t index, T object)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(index >= 0 && index <= size(), "Invalid index!");
|
|
|
|
AXASSERT(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();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-12 14:45:30 +08:00
|
|
|
// Removes Objects
|
2013-11-26 16:58:33 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Removes the last element in the Vector. */
|
2013-12-05 10:35:10 +08:00
|
|
|
void popBack()
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(!_data.empty(), "no objects added");
|
2013-11-26 16:58:33 +08:00
|
|
|
auto last = _data.back();
|
|
|
|
_data.pop_back();
|
|
|
|
last->release();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Remove a certain object in Vector.
|
2013-12-12 14:45:30 +08:00
|
|
|
* @param object The object to be removed.
|
2014-02-21 11:24:37 +08:00
|
|
|
* @param removeAll Whether to remove all elements with the same value.
|
2014-02-21 11:30:55 +08:00
|
|
|
* If its value is 'false', it will just erase the first occurrence.
|
2013-12-12 14:45:30 +08:00
|
|
|
*/
|
2014-02-21 11:24:37 +08:00
|
|
|
void eraseObject(T object, bool removeAll = false)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(object != nullptr, "The object should not be nullptr");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-02-21 11:24:37 +08:00
|
|
|
if (removeAll)
|
|
|
|
{
|
|
|
|
for (auto iter = _data.begin(); iter != _data.end();)
|
|
|
|
{
|
|
|
|
if ((*iter) == object)
|
|
|
|
{
|
|
|
|
iter = _data.erase(iter);
|
|
|
|
object->release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto iter = std::find(_data.begin(), _data.end(), object);
|
|
|
|
if (iter != _data.end())
|
|
|
|
{
|
|
|
|
_data.erase(iter);
|
|
|
|
object->release();
|
|
|
|
}
|
|
|
|
}
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/** @brief Removes from the vector with an iterator.
|
2015-03-23 22:13:04 +08:00
|
|
|
* @param position Iterator pointing to a single element to be removed from the Vector.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @return An iterator pointing to the new location of the element that followed the last element erased by the
|
|
|
|
* function call. This is the container end if the operation erased the last element in the sequence.
|
2013-12-12 14:45:30 +08:00
|
|
|
*/
|
|
|
|
iterator erase(iterator position)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(position >= _data.begin() && position < _data.end(), "Invalid position!");
|
2013-12-12 14:45:30 +08:00
|
|
|
(*position)->release();
|
|
|
|
return _data.erase(position);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** @brief Removes from the Vector with a range of elements ( [first, last) ).
|
|
|
|
* @param first The beginning of the range.
|
|
|
|
* @param last The end of the range, the 'last' will not be removed, it's only for indicating the end of range.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @return An iterator pointing to the new location of the element that followed the last element erased by the
|
|
|
|
* function call. This is the container end if the operation erased the last element in the sequence.
|
2013-12-12 14:45:30 +08:00
|
|
|
*/
|
2014-01-03 20:28:30 +08:00
|
|
|
iterator erase(iterator first, iterator last)
|
2013-12-12 14:45:30 +08:00
|
|
|
{
|
|
|
|
for (auto iter = first; iter != last; ++iter)
|
|
|
|
{
|
|
|
|
(*iter)->release();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-12 14:45:30 +08:00
|
|
|
return _data.erase(first, last);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** @brief Removes from the Vector by index.
|
|
|
|
* @param index The index of the element to be removed from the Vector.
|
|
|
|
* @return An iterator pointing to the successor of Vector[index].
|
2013-12-12 14:45:30 +08:00
|
|
|
*/
|
|
|
|
iterator erase(ssize_t index)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(!_data.empty() && index >= 0 && index < size(), "Invalid index!");
|
2021-12-25 10:04:45 +08:00
|
|
|
auto it = std::next(begin(), index);
|
2013-11-26 16:58:33 +08:00
|
|
|
(*it)->release();
|
2013-12-12 14:45:30 +08:00
|
|
|
return _data.erase(it);
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** @brief Removes all elements from the Vector (which are destroyed), leaving the container with a size of 0.
|
2015-09-22 16:08:23 +08:00
|
|
|
* @note All the elements in the Vector will be released (reference count will be decreased).
|
2013-12-12 14:45:30 +08:00
|
|
|
*/
|
2013-12-05 10:35:10 +08:00
|
|
|
void clear()
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _data)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
it->release();
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
2013-11-28 11:04:39 +08:00
|
|
|
_data.clear();
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rearranging Content
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Swap the values object1 and object2. */
|
2013-12-05 10:35:10 +08:00
|
|
|
void swap(T object1, T object2)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2013-12-12 14:45:30 +08:00
|
|
|
ssize_t idx1 = getIndex(object1);
|
|
|
|
ssize_t idx2 = getIndex(object2);
|
2013-11-26 16:58:33 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(idx1 >= 0 && idx2 >= 0, "invalid object index");
|
2013-11-26 16:58:33 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::swap(_data[idx1], _data[idx2]);
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Swap two elements by indexes. */
|
2013-12-12 10:29:07 +08:00
|
|
|
void swap(ssize_t index1, ssize_t index2)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(index1 >= 0 && index1 < size() && index2 >= 0 && index2 < size(), "Invalid indices");
|
2013-11-26 16:58:33 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::swap(_data[index1], _data[index2]);
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Replace value at index with given object. */
|
2013-12-12 10:29:07 +08:00
|
|
|
void replace(ssize_t index, T object)
|
2013-11-26 16:58:33 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(index >= 0 && index < size(), "Invalid index!");
|
|
|
|
AXASSERT(object != nullptr, "The object should not be nullptr");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-05 10:35:10 +08:00
|
|
|
_data[index]->release();
|
|
|
|
_data[index] = object;
|
|
|
|
object->retain();
|
2013-11-26 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Reverses the Vector. */
|
2021-12-25 10:04:45 +08:00
|
|
|
void reverse() { std::reverse(std::begin(_data), std::end(_data)); }
|
|
|
|
|
2015-03-23 22:13:04 +08:00
|
|
|
/** Requests the container to reduce its capacity to fit its size. */
|
2021-12-25 10:04:45 +08:00
|
|
|
void shrinkToFit() { _data.shrink_to_fit(); }
|
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
protected:
|
2013-12-12 14:45:30 +08:00
|
|
|
/** Retains all the objects in the vector */
|
2013-11-29 16:33:15 +08:00
|
|
|
void addRefForAllObjects()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& obj : _data)
|
|
|
|
{
|
2013-11-29 16:33:15 +08:00
|
|
|
obj->retain();
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2013-11-29 16:33:15 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-26 16:58:33 +08:00
|
|
|
std::vector<T> _data;
|
|
|
|
};
|
|
|
|
|
2015-03-27 12:01:20 +08:00
|
|
|
// end of base group
|
2015-03-24 11:15:40 +08:00
|
|
|
/** @} */
|
2013-11-26 16:58:33 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2013-11-26 16:58:33 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // __CCVECTOR_H__
|