diff options
author | Henry Wilson <m3henry@googlemail.com> | 2019-02-18 22:39:06 +0000 |
---|---|---|
committer | PeterN <peter@fuzzle.org> | 2019-03-26 20:15:57 +0000 |
commit | a0f36a50e6324f570985f5010eb0543ec0673aeb (patch) | |
tree | 09f9c9abd097acc244f80366da42cb8702c7ed19 /src/core/smallvec_type.hpp | |
parent | ca2f33c6d025c0c45fb4bc472493290445312de5 (diff) | |
download | openttd-a0f36a50e6324f570985f5010eb0543ec0673aeb.tar.xz |
Codechange: Replaced SmallVector::Append() with std::vector::[push|emplace]_back()
Diffstat (limited to 'src/core/smallvec_type.hpp')
-rw-r--r-- | src/core/smallvec_type.hpp | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/src/core/smallvec_type.hpp b/src/core/smallvec_type.hpp index b989d44c5..5961a9698 100644 --- a/src/core/smallvec_type.hpp +++ b/src/core/smallvec_type.hpp @@ -67,17 +67,6 @@ public: ~SmallVector() = default; /** - * Append an item and return it. - * @param to_add the number of items to append - * @return pointer to newly allocated item - */ - inline T *Append(uint to_add = 1) - { - std::vector<T>::resize(std::vector<T>::size() + to_add); - return this->End() - to_add; - } - - /** * Insert a new item at a specific position into the vector, moving all following items. * @param item Position at which the new item should be inserted * @return pointer to the new item @@ -112,7 +101,7 @@ public: inline bool Include(const T &item) { bool is_member = std::find(std::vector<T>::begin(), std::vector<T>::end(), item) != std::vector<T>::end(); - if (!is_member) *this->Append() = item; + if (!is_member) std::vector<T>::emplace_back(item); return is_member; } @@ -157,6 +146,23 @@ public: } }; +/** + * Helper function to extend a vector by more than one element + * 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; +} + /** * Simple vector template class, with automatic free. |