From 5fb1edf7a41990bbd7692a3692270afd47c881ea Mon Sep 17 00:00:00 2001 From: Henry Wilson Date: Sat, 30 Dec 2017 20:50:00 +0000 Subject: [PATCH 01/31] Added new adapter functions to help transition from SmallVector --- src/core/smallvec_type.hpp | 119 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) diff --git a/src/core/smallvec_type.hpp b/src/core/smallvec_type.hpp index 8676265d2..74818d135 100644 --- a/src/core/smallvec_type.hpp +++ b/src/core/smallvec_type.hpp @@ -7,7 +7,9 @@ * 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 . */ -/** @file smallvec_type.hpp Simple vector class that allows allocating an item without the need to copy this->data needlessly. */ +/** @file smallvec_type.hpp Simple vector class that allows allocating an item without the need to copy this->data needlessly. + * @note The std::vector adapter functions are _temporary_ substitutions for methods of SmallVector + */ #ifndef SMALLVEC_TYPE_HPP #define SMALLVEC_TYPE_HPP @@ -15,6 +17,121 @@ #include "alloc_func.hpp" #include "mem_func.hpp" +#include +#include + +/** Get a const iterator to a matching element in the vector. + * @param vec the vector to be searched + * @param elem the element to search fo + * @tparam T the contained type of the vector + */ +template +typename std::vector::const_iterator Find(const std::vector& vec, const T& elem) { + return std::find(vec.begin(), vec.end(), elem); +} + +/** Get a non-const iterator to a matching element in the vector. + * @param vec the vector to be searched + * @param elem the element to search for + * @tparam T the contained type of the vector + */ +template +typename std::vector::iterator Find(std::vector& vec, const T& elem) { + return std::find(vec.begin(), vec.end(), elem); +} + +/** Get the index of the first matching element. + * @param vec the vector to be searched + * @param elem the element to search for + * @tparam T the contained type of the vector + */ +template +int FindIndex(const std::vector& vec, const T& elem) { + auto it = Find(vec, elem); + if (vec.end() == it) return -1; + return std::distance(vec.begin(), it); +} + +/** Check whether an element exists. + * @param vec the vector to be searched + * @param elem the element to search for + * @tparam T the contained type of the vector + */ +template +bool Contains(const std::vector& vec, const T& elem) { + return Find(vec, elem) != vec.end(); +} + +/** Count instances of an element. + * @param vec the vector to be searched + * @param elem the element to count instances of + * @tparam T the contained type of the vector + */ +template +size_t Count(const std::vector& vec, const T& elem) { + return std::count(vec.begin(), vec.end(), elem); +} + +/** Append an element if it does not already exist in the vector. + * @param vec the vector to be modified + * @param elem the element to add for and remove if missing + * @tparam T the contained type of the vector + * @tparam U the universal reference type for perfect forwarding + */ +template +bool Include(std::vector& vec, U&& elem) { + auto missing = not Contains(vec, elem); + if (missing) vec.push_back(std::forward(elem)); + return missing; +} + +/** Remove an element referenced in the vector and move the last element to take its place. + * @param vec the vector to be modified + * @param it the element to remove + * @tparam T the contained type of the vector + */ +template +typename std::vector::iterator Erase(std::vector& vec, typename std::vector::iterator it) { + assert(vec.begin() <= it and vec.end() > it); + if (std::prev(vec.end()) != it) std::swap(vec.back(), *it); + vec.pop_back(); + return it; +} + +/** Remove an element if it exists in the vector and move the last element to take its place. + * @param vec the vector to be modified + * @param elem the element to search for and remove if found + * @tparam T the contained type of the vector + */ +template +bool Exclude(std::vector& vec, const T& elem) { + auto it = Find(vec, elem); + if (vec.end() == it) return false; + Erase(vec, it); + return true; +} + +/** Clear a vector and then force it to reduce its capacity. + * @param vec the vector to be modified + * @tparam T the contained type of the vector + */ +template +void Reset(std::vector& vec) { + vec.clear(); + vec.shrink_to_fit(); +} + +/** Extend a vector by an amount and return an iterator to the new elements. + * @param vec the vector to be modified + * @param num the number of elements to add + * @tparam T the contained type of the vector + */ +template +typename std::vector::iterator Extend(std::vector& vec, size_t num) { + vec.resize(vec.size() + num); + return vec.end() - num; +} + /** * Simple vector template class. * -- 2.14.1