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 | |
parent | ca2f33c6d025c0c45fb4bc472493290445312de5 (diff) | |
download | openttd-a0f36a50e6324f570985f5010eb0543ec0673aeb.tar.xz |
Codechange: Replaced SmallVector::Append() with std::vector::[push|emplace]_back()
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/pool_type.hpp | 2 | ||||
-rw-r--r-- | src/core/smallmap_type.hpp | 11 | ||||
-rw-r--r-- | src/core/smallvec_type.hpp | 30 |
3 files changed, 24 insertions, 19 deletions
diff --git a/src/core/pool_type.hpp b/src/core/pool_type.hpp index 4d20ed1ab..a6f7e4fe8 100644 --- a/src/core/pool_type.hpp +++ b/src/core/pool_type.hpp @@ -50,7 +50,7 @@ struct PoolBase { */ PoolBase(PoolType pt) : type(pt) { - *PoolBase::GetPools()->Append() = this; + PoolBase::GetPools()->push_back(this); } virtual ~PoolBase(); diff --git a/src/core/smallmap_type.hpp b/src/core/smallmap_type.hpp index 0bd9bd296..0917c5423 100644 --- a/src/core/smallmap_type.hpp +++ b/src/core/smallmap_type.hpp @@ -143,9 +143,7 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> { inline bool Insert(const T &key, const U &data) { if (this->Contains(key)) return false; - Pair *n = this->Append(); - n->first = key; - n->second = data; + std::vector<Pair>::emplace_back(key, data); return true; } @@ -160,9 +158,10 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> { for (uint i = 0; i < std::vector<Pair>::size(); i++) { if (key == std::vector<Pair>::operator[](i).first) return std::vector<Pair>::operator[](i).second; } - Pair *n = this->Append(); - n->first = key; - return n->second; + /*C++17: Pair &n = */ std::vector<Pair>::emplace_back(); + Pair &n = std::vector<Pair>::back(); + n.first = key; + return n.second; } inline void SortByKey() 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. |