2008-04-16 21:01:09 +02:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-06-19 12:19:02 +02:00
|
|
|
/** @file smallvec_type.hpp Simple vector class that allows allocating an item without the need to copy this->data needlessly. */
|
2008-04-16 21:01:09 +02:00
|
|
|
|
2008-06-19 12:19:02 +02:00
|
|
|
#ifndef SMALLVEC_TYPE_HPP
|
|
|
|
#define SMALLVEC_TYPE_HPP
|
2008-04-16 21:01:09 +02:00
|
|
|
|
2008-06-19 12:19:02 +02:00
|
|
|
#include "alloc_func.hpp"
|
2010-09-03 23:50:51 +02:00
|
|
|
#include "mem_func.hpp"
|
2018-09-20 23:41:43 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
2008-05-30 11:32:24 +02:00
|
|
|
|
2008-06-19 11:33:50 +02:00
|
|
|
/**
|
|
|
|
* Simple vector template class.
|
|
|
|
*
|
|
|
|
* @note There are no asserts in the class so you have
|
|
|
|
* to care about that you grab an item which is
|
|
|
|
* inside the list.
|
|
|
|
*
|
2010-07-31 23:02:56 +02:00
|
|
|
* @tparam T The type of the items stored
|
|
|
|
* @tparam S The steps of allocation
|
2008-06-19 11:33:50 +02:00
|
|
|
*/
|
2008-05-24 12:02:49 +02:00
|
|
|
template <typename T, uint S>
|
2018-09-20 23:41:43 +02:00
|
|
|
class SmallVector : public std::vector<T> {
|
2008-06-19 12:19:02 +02:00
|
|
|
public:
|
2018-09-20 23:41:43 +02:00
|
|
|
SmallVector() = default;
|
2008-04-16 21:01:09 +02:00
|
|
|
|
2011-05-02 19:42:12 +02:00
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
* @param other The other vector to copy.
|
|
|
|
*/
|
2018-09-20 23:41:43 +02:00
|
|
|
SmallVector(const SmallVector &other) = default;
|
2012-07-29 22:02:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic copy constructor.
|
|
|
|
* @param other The other vector to copy.
|
|
|
|
*/
|
2010-09-04 01:04:02 +02:00
|
|
|
template <uint X>
|
2018-09-20 23:41:43 +02:00
|
|
|
SmallVector(const SmallVector<T, X> &other) : std::vector<T>(other)
|
2010-09-03 23:50:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-05-02 19:42:12 +02:00
|
|
|
/**
|
|
|
|
* Assignment.
|
2012-07-29 22:02:25 +02:00
|
|
|
* @param other The other vector to assign.
|
|
|
|
*/
|
2018-09-20 23:41:43 +02:00
|
|
|
SmallVector &operator=(const SmallVector &other) = default;
|
2012-07-29 22:02:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic assignment.
|
|
|
|
* @param other The other vector to assign.
|
2011-05-02 19:42:12 +02:00
|
|
|
*/
|
2010-09-04 01:04:02 +02:00
|
|
|
template <uint X>
|
|
|
|
SmallVector &operator=(const SmallVector<T, X> &other)
|
|
|
|
{
|
2018-09-25 21:44:39 +02:00
|
|
|
std::vector<T>::operator=(other);
|
2010-09-04 01:04:02 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-09-20 23:41:43 +02:00
|
|
|
~SmallVector() = default;
|
2008-04-16 21:01:09 +02:00
|
|
|
|
2008-12-20 18:09:44 +01:00
|
|
|
/**
|
|
|
|
* Tests whether a item is present in the vector, and appends it to the end if not.
|
|
|
|
* The '!=' operator of T is used for comparison.
|
|
|
|
* @param item Item to test for
|
|
|
|
* @return true iff the item is was already present
|
|
|
|
*/
|
2011-12-20 18:57:56 +01:00
|
|
|
inline bool Include(const T &item)
|
2008-12-20 18:09:44 +01:00
|
|
|
{
|
2019-02-12 23:59:12 +01:00
|
|
|
bool is_member = std::find(std::vector<T>::begin(), std::vector<T>::end(), item) != std::vector<T>::end();
|
2019-02-18 23:39:06 +01:00
|
|
|
if (!is_member) std::vector<T>::emplace_back(item);
|
2008-12-20 18:09:44 +01:00
|
|
|
return is_member;
|
|
|
|
}
|
|
|
|
|
2008-06-19 11:33:50 +02:00
|
|
|
/**
|
|
|
|
* Get the pointer to the first item (const)
|
|
|
|
*
|
|
|
|
* @return the pointer to the first item
|
|
|
|
*/
|
2011-12-20 18:57:56 +01:00
|
|
|
inline const T *Begin() const
|
2008-04-16 21:01:09 +02:00
|
|
|
{
|
2018-09-20 23:41:43 +02:00
|
|
|
return std::vector<T>::data();
|
2008-04-16 21:01:09 +02:00
|
|
|
}
|
|
|
|
|
2008-06-19 11:33:50 +02:00
|
|
|
/**
|
|
|
|
* Get the pointer to the first item
|
|
|
|
*
|
|
|
|
* @return the pointer to the first item
|
|
|
|
*/
|
2011-12-20 18:57:56 +01:00
|
|
|
inline T *Begin()
|
2008-04-16 22:01:04 +02:00
|
|
|
{
|
2018-09-20 23:41:43 +02:00
|
|
|
return std::vector<T>::data();
|
2008-04-16 22:01:04 +02:00
|
|
|
}
|
|
|
|
|
2008-06-19 11:33:50 +02:00
|
|
|
/**
|
|
|
|
* Get the pointer behind the last valid item (const)
|
|
|
|
*
|
|
|
|
* @return the pointer behind the last valid item
|
|
|
|
*/
|
2011-12-20 18:57:56 +01:00
|
|
|
inline const T *End() const
|
2008-04-16 21:01:09 +02:00
|
|
|
{
|
2018-09-20 23:41:43 +02:00
|
|
|
return std::vector<T>::data() + std::vector<T>::size();
|
2008-04-16 21:01:09 +02:00
|
|
|
}
|
2008-04-16 22:01:04 +02:00
|
|
|
|
2008-06-19 11:33:50 +02:00
|
|
|
/**
|
|
|
|
* Get the pointer behind the last valid item
|
|
|
|
*
|
|
|
|
* @return the pointer behind the last valid item
|
|
|
|
*/
|
2011-12-20 18:57:56 +01:00
|
|
|
inline T *End()
|
2008-04-16 22:01:04 +02:00
|
|
|
{
|
2018-09-20 23:41:43 +02:00
|
|
|
return std::vector<T>::data() + std::vector<T>::size();
|
2008-04-16 22:01:04 +02:00
|
|
|
}
|
2008-04-16 21:01:09 +02:00
|
|
|
};
|
|
|
|
|
2019-02-18 23:39:06 +01:00
|
|
|
/**
|
2019-02-20 20:27:10 +01:00
|
|
|
* Helper function to get the index of an item
|
|
|
|
* Consider using std::set, std::unordered_set or std::flat_set in new code
|
|
|
|
*
|
|
|
|
* @param vec A reference to the vector to be extended
|
|
|
|
* @param item Reference to the item to be search for
|
|
|
|
*
|
|
|
|
* @return Index of element if found, otherwise -1
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
int find_index(std::vector<T> const& vec, T const& item)
|
|
|
|
{
|
|
|
|
auto const it = std::find(vec.begin(), vec.end(), item);
|
|
|
|
if (it != vec.end()) return it - vec.begin();
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to append N default-constructed elements and get a pointer to the first new element
|
2019-02-18 23:39:06 +01:00
|
|
|
* Consider using std::back_inserter in new code
|
|
|
|
*
|
|
|
|
* @param vec A reference to the vector to be extended
|
|
|
|
* @param num The number of elements to default-construct
|
|
|
|
*
|
|
|
|
* @return Pointer to the first new element
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline T* grow(std::vector<T>& vec, std::size_t num)
|
|
|
|
{
|
|
|
|
const std::size_t pos = vec.size();
|
|
|
|
vec.resize(pos + num);
|
|
|
|
return vec.data() + pos;
|
|
|
|
}
|
|
|
|
|
2008-07-22 16:17:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple vector template class, with automatic free.
|
|
|
|
*
|
|
|
|
* @note There are no asserts in the class so you have
|
|
|
|
* to care about that you grab an item which is
|
|
|
|
* inside the list.
|
|
|
|
*
|
|
|
|
* @param T The type of the items stored, must be a pointer
|
|
|
|
* @param S The steps of allocation
|
|
|
|
*/
|
|
|
|
template <typename T, uint S>
|
|
|
|
class AutoFreeSmallVector : public SmallVector<T, S> {
|
|
|
|
public:
|
|
|
|
~AutoFreeSmallVector()
|
|
|
|
{
|
|
|
|
this->Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all items from the list.
|
|
|
|
*/
|
2011-12-20 18:57:56 +01:00
|
|
|
inline void Clear()
|
2008-07-22 16:17:29 +02:00
|
|
|
{
|
2018-09-20 23:41:43 +02:00
|
|
|
for (uint i = 0; i < std::vector<T>::size(); i++) {
|
|
|
|
free(std::vector<T>::operator[](i));
|
2008-07-22 16:17:29 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 23:41:43 +02:00
|
|
|
std::vector<T>::clear();
|
2008-07-22 16:17:29 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-19 22:05:46 +01:00
|
|
|
/**
|
|
|
|
* Simple vector template class, with automatic delete.
|
|
|
|
*
|
|
|
|
* @note There are no asserts in the class so you have
|
|
|
|
* to care about that you grab an item which is
|
|
|
|
* inside the list.
|
|
|
|
*
|
|
|
|
* @param T The type of the items stored, must be a pointer
|
|
|
|
* @param S The steps of allocation
|
|
|
|
*/
|
|
|
|
template <typename T, uint S>
|
|
|
|
class AutoDeleteSmallVector : public SmallVector<T, S> {
|
|
|
|
public:
|
|
|
|
~AutoDeleteSmallVector()
|
|
|
|
{
|
|
|
|
this->Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all items from the list.
|
|
|
|
*/
|
2011-12-20 18:57:56 +01:00
|
|
|
inline void Clear()
|
2011-12-19 22:05:46 +01:00
|
|
|
{
|
2018-09-20 23:41:43 +02:00
|
|
|
for (uint i = 0; i < std::vector<T>::size(); i++) {
|
|
|
|
delete std::vector<T>::operator[](i);
|
2011-12-19 22:05:46 +01:00
|
|
|
}
|
|
|
|
|
2018-09-20 23:41:43 +02:00
|
|
|
std::vector<T>::clear();
|
2011-12-19 22:05:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-05-02 19:42:12 +02:00
|
|
|
typedef AutoFreeSmallVector<char*, 4> StringList; ///< Type for a list of strings.
|
2009-04-03 14:49:58 +02:00
|
|
|
|
2008-06-19 12:19:02 +02:00
|
|
|
#endif /* SMALLVEC_TYPE_HPP */
|