Changeset - r24208:240daa324e1b
[Not reviewed]
master
0 9 0
Michael Lutz - 4 years ago 2020-05-17 21:31:44
michi@icosahedron.de
Codechange: Replace SmallPair with std::pair.

std::pair is already the smallest possible pair, and it already handles non-POD types correctly.
9 files changed with 24 insertions and 37 deletions:
0 comments (0 inline, 0 general)
src/core/smallmap_type.hpp
Show inline comments
 
@@ -11,40 +11,26 @@
 
#define SMALLMAP_TYPE_HPP
 

	
 
#include "smallvec_type.hpp"
 
#include <utility>
 

	
 
/**
 
 * Simple pair of data. Both types have to be POD ("Plain Old Data")!
 
 * @tparam T Key type.
 
 * @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().
 
 * Implementation of simple mapping class.
 
 * It has inherited accessors from std::vector().
 
 * @tparam T Key type.
 
 * @tparam U Value type.
 
 * @tparam S Unit of allocation.
 
 *
 
 * @see SmallVector
 
 * @see std::vector
 
 */
 
template <typename T, typename U>
 
struct SmallMap : std::vector<SmallPair<T, U> > {
 
	typedef ::SmallPair<T, U> Pair;
 
struct SmallMap : std::vector<std::pair<T, U> > {
 
	typedef std::pair<T, U> Pair;
 
	typedef Pair *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() { }
 
	/** Data are freed in SmallVector destructor */
 
	/** Data are freed in std::vector destructor */
 
	inline ~SmallMap() { }
 

	
 
	/**
src/fontcache.cpp
Show inline comments
 
@@ -211,7 +211,7 @@ protected:
 
	int req_size;  ///< Requested 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.
 

	
 
	/** Container for information about a glyph. */
 
@@ -434,7 +434,7 @@ const void *TrueTypeFontCache::GetFontTa
 

	
 
	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;
 
}
 

	
src/linkgraph/linkgraph.h
Show inline comments
 
@@ -17,6 +17,7 @@
 
#include "../cargotype.h"
 
#include "../date_func.h"
 
#include "linkgraph_type.h"
 
#include <utility>
 

	
 
struct SaveLoad;
 
class LinkGraph;
 
@@ -188,20 +189,20 @@ public:
 
		 * to return something that implements operator->, but isn't a pointer
 
		 * from operator->. A fake pointer.
 
		 */
 
		class FakePointer : public SmallPair<NodeID, Tedge_wrapper> {
 
		class FakePointer : public std::pair<NodeID, Tedge_wrapper> {
 
		public:
 

	
 
			/**
 
			 * Construct a fake pointer from a pair of NodeID and edge.
 
			 * @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->.
 
			 * @return Pair being "pointed" to.
 
			 */
 
			SmallPair<NodeID, Tedge_wrapper> *operator->() { return this; }
 
			std::pair<NodeID, Tedge_wrapper> *operator->() { return this; }
 
		};
 

	
 
	public:
 
@@ -266,9 +267,9 @@ public:
 
		 * Dereference with operator*.
 
		 * @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]));
 
		}
 

	
 
		/**
src/linkgraph/linkgraphjob.h
Show inline comments
 
@@ -160,9 +160,9 @@ public:
 
		 * @return Pair of the edge currently pointed to and the ID of its
 
		 *         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]));
 
		}
 

	
 
		/**
src/newgrf.cpp
Show inline comments
 
@@ -8113,7 +8113,7 @@ static bool ChangeGRFParamValueNames(Byt
 
		byte langid = buf->ReadByte();
 
		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()) {
 
			AddGRFTextToList(&val_name->second, langid, _cur.grfconfig->ident.grfid, false, name_string);
 
		} else {
src/newgrf_config.cpp
Show inline comments
 
@@ -262,7 +262,7 @@ GRFParameterInfo::GRFParameterInfo(GRFPa
 
	complete_labels(info.complete_labels)
 
{
 
	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));
 
	}
 
}
 
@@ -273,7 +273,7 @@ GRFParameterInfo::~GRFParameterInfo()
 
	CleanUpGRFText(this->name);
 
	CleanUpGRFText(this->desc);
 
	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);
 
	}
 
}
src/newgrf_debug_gui.cpp
Show inline comments
 
@@ -804,7 +804,7 @@ GrfSpecFeature GetGrfSpecFeature(Vehicle
 

	
 
/** Window used for aligning sprites. */
 
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.
 
	Scrollbar *vscroll;
src/newgrf_text.cpp
Show inline comments
 
@@ -196,7 +196,7 @@ struct UnmappedChoiceList : ZeroedMemory
 
	/** Clean everything up. */
 
	~UnmappedChoiceList()
 
	{
 
		for (SmallPair<byte, char *> p : this->strings) {
 
		for (std::pair<byte, char *> p : this->strings) {
 
			free(p.second);
 
		}
 
	}
src/vehicle.cpp
Show inline comments
 
@@ -2278,7 +2278,7 @@ void Vehicle::GetConsistFreeCapacities(S
 
{
 
	for (const Vehicle *v = this; v != nullptr; v = v->Next()) {
 
		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()) {
 
			capacities.push_back({v->cargo_type, v->cargo_cap - v->cargo.StoredCount()});
 
		} else {
0 comments (0 inline, 0 general)