summaryrefslogtreecommitdiff
path: root/src/core/smallmap_type.hpp
diff options
context:
space:
mode:
authorHenry Wilson <m3henry@googlemail.com>2018-09-20 22:41:43 +0100
committerPeterN <peter@fuzzle.org>2019-03-26 20:15:57 +0000
commit4b349c0f90c3f7b6a39171cec41cd98dcd0d88b7 (patch)
treecc3ae4e4a7a4defd8a672ae9dd3983bd02b654f4 /src/core/smallmap_type.hpp
parent7a32cf1401d289f04682aa91fbfb779cbb2378e4 (diff)
downloadopenttd-4b349c0f90c3f7b6a39171cec41cd98dcd0d88b7.tar.xz
Codechange: [core] Implement SmallVector using std::vector
The public and protected interface to SmallVector are unchanged SmallVector now requires that items be default constructible This isn't an issue since some contained items were previously created uninitialized. Temporary default constructors are added to the following structs - SmallPair - SmallStackItem - GRFPresence Where vector<bool> is required, transition immediately to std::vector to avoid returning proxy object references.
Diffstat (limited to 'src/core/smallmap_type.hpp')
-rw-r--r--src/core/smallmap_type.hpp51
1 files changed, 36 insertions, 15 deletions
diff --git a/src/core/smallmap_type.hpp b/src/core/smallmap_type.hpp
index dda0fc2a1..44ace6b45 100644
--- a/src/core/smallmap_type.hpp
+++ b/src/core/smallmap_type.hpp
@@ -27,6 +27,7 @@ struct SmallPair {
/** Initializes this Pair with data */
inline SmallPair(const T &first, const U &second) : first(first), second(second) { }
+ SmallPair() = default;
};
/**
@@ -56,8 +57,8 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
*/
inline const Pair *Find(const T &key) const
{
- for (uint i = 0; i < this->items; i++) {
- if (key == this->data[i].first) return &this->data[i];
+ for (uint i = 0; i < std::vector<Pair>::size(); i++) {
+ if (key == std::vector<Pair>::operator[](i).first) return &std::vector<Pair>::operator[](i);
}
return this->End();
}
@@ -69,12 +70,23 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
*/
inline Pair *Find(const T &key)
{
- for (uint i = 0; i < this->items; i++) {
- if (key == this->data[i].first) return &this->data[i];
+ for (uint i = 0; i < std::vector<Pair>::size(); i++) {
+ if (key == std::vector<Pair>::operator[](i).first) return &std::vector<Pair>::operator[](i);
}
return this->End();
}
+ inline const Pair *End() const
+ {
+ return &*std::vector<Pair>::end();
+ }
+
+ inline Pair *End()
+ {
+ return &*std::vector<Pair>::end();
+ }
+
+
/**
* Tests whether a key is assigned in this map.
* @param key key to test
@@ -86,6 +98,16 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
}
/**
+ * Tests whether a key is assigned in this map.
+ * @param key key to test
+ * @return true iff the item is present
+ */
+ inline bool Contains(const T &key)
+ {
+ return this->Find(key) != this->End();
+ }
+
+ /**
* Removes given pair from this map
* @param pair pair to remove
* @note it has to be pointer to pair in this map. It is overwritten by the last item.
@@ -93,7 +115,7 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
inline void Erase(Pair *pair)
{
assert(pair >= this->Begin() && pair < this->End());
- *pair = this->data[--this->items];
+ SmallVector<Pair, S>::Erase(pair);
}
/**
@@ -104,13 +126,12 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
*/
inline bool Erase(const T &key)
{
- for (uint i = 0; i < this->items; i++) {
- if (key == this->data[i].first) {
- this->data[i] = this->data[--this->items];
- return true;
- }
- }
- return false;
+ Pair *pair = this->Find(key);
+ if (pair == this->End())
+ return false;
+
+ SmallVector<Pair, S>::Erase(pair);
+ return true;
}
/**
@@ -136,8 +157,8 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
*/
inline U &operator[](const T &key)
{
- for (uint i = 0; i < this->items; i++) {
- if (key == this->data[i].first) return this->data[i].second;
+ 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;
@@ -146,7 +167,7 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
inline void SortByKey()
{
- QSortT(this->Begin(), this->items, KeySorter);
+ QSortT(this->Begin(), std::vector<Pair>::size(), KeySorter);
}
static int CDECL KeySorter(const Pair *a, const Pair *b)