Codechange: Replace SmallPair with std::pair.

std::pair is already the smallest possible pair, and it already handles non-POD types correctly.
This commit is contained in:
Michael Lutz 2020-05-17 23:31:44 +02:00
parent 7309bdec48
commit f2b40f40aa
9 changed files with 24 additions and 37 deletions

View File

@ -11,40 +11,26 @@
#define SMALLMAP_TYPE_HPP #define SMALLMAP_TYPE_HPP
#include "smallvec_type.hpp" #include "smallvec_type.hpp"
#include <utility>
/** /**
* Simple pair of data. Both types have to be POD ("Plain Old Data")! * Implementation of simple mapping class.
* @tparam T Key type. * It has inherited accessors from std::vector().
* @tparam U Value type.
*/
template <typename T, typename U>
struct SmallPair {
T first;
U second;
/** Initializes this Pair with data */
inline SmallPair(const T &first, const U &second) : first(first), second(second) { }
SmallPair() = default;
};
/**
* Implementation of simple mapping class. Both types have to be POD ("Plain Old Data")!
* It has inherited accessors from SmallVector().
* @tparam T Key type. * @tparam T Key type.
* @tparam U Value type. * @tparam U Value type.
* @tparam S Unit of allocation. * @tparam S Unit of allocation.
* *
* @see SmallVector * @see std::vector
*/ */
template <typename T, typename U> template <typename T, typename U>
struct SmallMap : std::vector<SmallPair<T, U> > { struct SmallMap : std::vector<std::pair<T, U> > {
typedef ::SmallPair<T, U> Pair; typedef std::pair<T, U> Pair;
typedef Pair *iterator; typedef Pair *iterator;
typedef const Pair *const_iterator; typedef const Pair *const_iterator;
/** Creates new SmallMap. Data are initialized in SmallVector constructor */ /** Creates new SmallMap. Data are initialized in std::vector constructor */
inline SmallMap() { } inline SmallMap() { }
/** Data are freed in SmallVector destructor */ /** Data are freed in std::vector destructor */
inline ~SmallMap() { } inline ~SmallMap() { }
/** /**

View File

@ -211,7 +211,7 @@ protected:
int req_size; ///< Requested font size. int req_size; ///< Requested font size.
int used_size; ///< Used font size. int used_size; ///< Used font size.
typedef SmallMap<uint32, SmallPair<size_t, const void*> > FontTable; ///< Table with font table cache typedef SmallMap<uint32, std::pair<size_t, const void*> > FontTable; ///< Table with font table cache
FontTable font_tables; ///< Cached font tables. FontTable font_tables; ///< Cached font tables.
/** Container for information about a glyph. */ /** Container for information about a glyph. */
@ -434,7 +434,7 @@ const void *TrueTypeFontCache::GetFontTable(uint32 tag, size_t &length)
const void *result = this->InternalGetFontTable(tag, length); const void *result = this->InternalGetFontTable(tag, length);
this->font_tables.Insert(tag, SmallPair<size_t, const void *>(length, result)); this->font_tables.Insert(tag, std::pair<size_t, const void *>(length, result));
return result; return result;
} }

View File

@ -17,6 +17,7 @@
#include "../cargotype.h" #include "../cargotype.h"
#include "../date_func.h" #include "../date_func.h"
#include "linkgraph_type.h" #include "linkgraph_type.h"
#include <utility>
struct SaveLoad; struct SaveLoad;
class LinkGraph; class LinkGraph;
@ -188,20 +189,20 @@ public:
* to return something that implements operator->, but isn't a pointer * to return something that implements operator->, but isn't a pointer
* from operator->. A fake pointer. * from operator->. A fake pointer.
*/ */
class FakePointer : public SmallPair<NodeID, Tedge_wrapper> { class FakePointer : public std::pair<NodeID, Tedge_wrapper> {
public: public:
/** /**
* Construct a fake pointer from a pair of NodeID and edge. * Construct a fake pointer from a pair of NodeID and edge.
* @param pair Pair to be "pointed" to (in fact shallow-copied). * @param pair Pair to be "pointed" to (in fact shallow-copied).
*/ */
FakePointer(const SmallPair<NodeID, Tedge_wrapper> &pair) : SmallPair<NodeID, Tedge_wrapper>(pair) {} FakePointer(const std::pair<NodeID, Tedge_wrapper> &pair) : std::pair<NodeID, Tedge_wrapper>(pair) {}
/** /**
* Retrieve the pair by operator->. * Retrieve the pair by operator->.
* @return Pair being "pointed" to. * @return Pair being "pointed" to.
*/ */
SmallPair<NodeID, Tedge_wrapper> *operator->() { return this; } std::pair<NodeID, Tedge_wrapper> *operator->() { return this; }
}; };
public: public:
@ -266,9 +267,9 @@ public:
* Dereference with operator*. * Dereference with operator*.
* @return Pair of current target NodeID and edge object. * @return Pair of current target NodeID and edge object.
*/ */
SmallPair<NodeID, Tedge_wrapper> operator*() const std::pair<NodeID, Tedge_wrapper> operator*() const
{ {
return SmallPair<NodeID, Tedge_wrapper>(this->current, Tedge_wrapper(this->base[this->current])); return std::pair<NodeID, Tedge_wrapper>(this->current, Tedge_wrapper(this->base[this->current]));
} }
/** /**

View File

@ -160,9 +160,9 @@ public:
* @return Pair of the edge currently pointed to and the ID of its * @return Pair of the edge currently pointed to and the ID of its
* other end. * other end.
*/ */
SmallPair<NodeID, Edge> operator*() const std::pair<NodeID, Edge> operator*() const
{ {
return SmallPair<NodeID, Edge>(this->current, Edge(this->base[this->current], this->base_anno[this->current])); return std::pair<NodeID, Edge>(this->current, Edge(this->base[this->current], this->base_anno[this->current]));
} }
/** /**

View File

@ -8113,7 +8113,7 @@ static bool ChangeGRFParamValueNames(ByteReader *buf)
byte langid = buf->ReadByte(); byte langid = buf->ReadByte();
const char *name_string = buf->ReadString(); const char *name_string = buf->ReadString();
SmallPair<uint32, GRFText *> *val_name = _cur_parameter->value_names.Find(id); std::pair<uint32, GRFText *> *val_name = _cur_parameter->value_names.Find(id);
if (val_name != _cur_parameter->value_names.End()) { if (val_name != _cur_parameter->value_names.End()) {
AddGRFTextToList(&val_name->second, langid, _cur.grfconfig->ident.grfid, false, name_string); AddGRFTextToList(&val_name->second, langid, _cur.grfconfig->ident.grfid, false, name_string);
} else { } else {

View File

@ -262,7 +262,7 @@ GRFParameterInfo::GRFParameterInfo(GRFParameterInfo &info) :
complete_labels(info.complete_labels) complete_labels(info.complete_labels)
{ {
for (uint i = 0; i < info.value_names.size(); i++) { for (uint i = 0; i < info.value_names.size(); i++) {
SmallPair<uint32, GRFText *> *data = info.value_names.data() + i; std::pair<uint32, GRFText *> *data = info.value_names.data() + i;
this->value_names.Insert(data->first, DuplicateGRFText(data->second)); this->value_names.Insert(data->first, DuplicateGRFText(data->second));
} }
} }
@ -273,7 +273,7 @@ GRFParameterInfo::~GRFParameterInfo()
CleanUpGRFText(this->name); CleanUpGRFText(this->name);
CleanUpGRFText(this->desc); CleanUpGRFText(this->desc);
for (uint i = 0; i < this->value_names.size(); i++) { for (uint i = 0; i < this->value_names.size(); i++) {
SmallPair<uint32, GRFText *> *data = this->value_names.data() + i; std::pair<uint32, GRFText *> *data = this->value_names.data() + i;
CleanUpGRFText(data->second); CleanUpGRFText(data->second);
} }
} }

View File

@ -804,7 +804,7 @@ GrfSpecFeature GetGrfSpecFeature(VehicleType type)
/** Window used for aligning sprites. */ /** Window used for aligning sprites. */
struct SpriteAlignerWindow : Window { struct SpriteAlignerWindow : Window {
typedef SmallPair<int16, int16> XyOffs; ///< Pair for x and y offsets of the sprite before alignment. First value contains the x offset, second value y offset. typedef std::pair<int16, int16> XyOffs; ///< Pair for x and y offsets of the sprite before alignment. First value contains the x offset, second value y offset.
SpriteID current_sprite; ///< The currently shown sprite. SpriteID current_sprite; ///< The currently shown sprite.
Scrollbar *vscroll; Scrollbar *vscroll;

View File

@ -196,7 +196,7 @@ struct UnmappedChoiceList : ZeroedMemoryAllocator {
/** Clean everything up. */ /** Clean everything up. */
~UnmappedChoiceList() ~UnmappedChoiceList()
{ {
for (SmallPair<byte, char *> p : this->strings) { for (std::pair<byte, char *> p : this->strings) {
free(p.second); free(p.second);
} }
} }

View File

@ -2278,7 +2278,7 @@ void Vehicle::GetConsistFreeCapacities(SmallMap<CargoID, uint> &capacities) cons
{ {
for (const Vehicle *v = this; v != nullptr; v = v->Next()) { for (const Vehicle *v = this; v != nullptr; v = v->Next()) {
if (v->cargo_cap == 0) continue; if (v->cargo_cap == 0) continue;
SmallPair<CargoID, uint> *pair = capacities.Find(v->cargo_type); std::pair<CargoID, uint> *pair = capacities.Find(v->cargo_type);
if (pair == capacities.End()) { if (pair == capacities.End()) {
capacities.push_back({v->cargo_type, v->cargo_cap - v->cargo.StoredCount()}); capacities.push_back({v->cargo_type, v->cargo_cap - v->cargo.StoredCount()});
} else { } else {