forked from mirror/OpenTTD
Codechange: Replaced SmallVector::Append() with std::vector::[push|emplace]_back()
This commit is contained in:
@@ -50,7 +50,7 @@ struct PoolBase {
|
||||
*/
|
||||
PoolBase(PoolType pt) : type(pt)
|
||||
{
|
||||
*PoolBase::GetPools()->Append() = this;
|
||||
PoolBase::GetPools()->push_back(this);
|
||||
}
|
||||
|
||||
virtual ~PoolBase();
|
||||
|
@@ -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()
|
||||
|
@@ -66,17 +66,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
|
||||
@@ -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.
|
||||
|
Reference in New Issue
Block a user