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
 
@@ -8,46 +8,32 @@
 
/** @file smallmap_type.hpp Simple mapping class targeted for small sets of data. Stored data shall be POD ("Plain Old Data")! */
 

	
 
#ifndef SMALLMAP_TYPE_HPP
 
#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() { }
 

	
 
	/**
 
	 * Finds given key in this map
 
	 * @param key key to find
 
	 * @return &Pair(key, data) if found, this->End() if not
src/fontcache.cpp
Show inline comments
 
@@ -208,13 +208,13 @@ static const byte SHADOW_COLOUR = 2;
 
/** Font cache for fonts that are based on a TrueType font. */
 
class TrueTypeFontCache : public FontCache {
 
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. */
 
	struct GlyphEntry {
 
		Sprite *sprite; ///< The loaded sprite.
 
		byte width;     ///< The width of the glyph.
 
@@ -431,13 +431,13 @@ const void *TrueTypeFontCache::GetFontTa
 
		length = iter->second.first;
 
		return iter->second.second;
 
	}
 

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

	
 

	
 
#ifdef WITH_FREETYPE
 
#include <ft2build.h>
src/linkgraph/linkgraph.h
Show inline comments
 
@@ -14,12 +14,13 @@
 
#include "../core/smallmap_type.hpp"
 
#include "../core/smallmatrix_type.hpp"
 
#include "../station_base.h"
 
#include "../cargotype.h"
 
#include "../date_func.h"
 
#include "linkgraph_type.h"
 
#include <utility>
 

	
 
struct SaveLoad;
 
class LinkGraph;
 

	
 
/**
 
 * Type of the pool for link graph components. Each station can be in at up to
 
@@ -185,26 +186,26 @@ public:
 
		/**
 
		 * A "fake" pointer to enable operator-> on temporaries. As the objects
 
		 * returned from operator* aren't references but real objects, we have
 
		 * 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:
 
		/**
 
		 * Constructor.
 
		 * @param base Array of edges to be iterated.
 
@@ -263,15 +264,15 @@ 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]));
 
		}
 

	
 
		/**
 
		 * Dereference with operator->.
 
		 * @return Fake pointer to Pair of current target NodeID and edge object.
 
		 */
src/linkgraph/linkgraphjob.h
Show inline comments
 
@@ -157,15 +157,15 @@ public:
 

	
 
		/**
 
		 * Dereference.
 
		 * @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]));
 
		}
 

	
 
		/**
 
		 * Dereference. Has to be repeated here as operator* is different than
 
		 * in LinkGraph::EdgeWrapper.
 
		 * @return Fake pointer to pair of NodeID/Edge.
src/newgrf.cpp
Show inline comments
 
@@ -8110,13 +8110,13 @@ static bool ChangeGRFParamValueNames(Byt
 
			continue;
 
		}
 

	
 
		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 {
 
			GRFText *list = nullptr;
 
			AddGRFTextToList(&list, langid, _cur.grfconfig->ident.grfid, false, name_string);
 
			_cur_parameter->value_names.Insert(id, list);
src/newgrf_config.cpp
Show inline comments
 
@@ -259,24 +259,24 @@ GRFParameterInfo::GRFParameterInfo(GRFPa
 
	param_nr(info.param_nr),
 
	first_bit(info.first_bit),
 
	num_bit(info.num_bit),
 
	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));
 
	}
 
}
 

	
 
/** Cleanup all parameter info. */
 
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);
 
	}
 
}
 

	
 
/**
 
 * Get the value of this user-changeable parameter from the given config.
src/newgrf_debug_gui.cpp
Show inline comments
 
@@ -801,13 +801,13 @@ GrfSpecFeature GetGrfSpecFeature(Vehicle
 

	
 

	
 
/**** Sprite Aligner ****/
 

	
 
/** 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;
 
	SmallMap<SpriteID, XyOffs> offs_start_map; ///< Mapping of starting offsets for the sprites which have been aligned in the sprite aligner window.
 

	
 
	SpriteAlignerWindow(WindowDesc *desc, WindowNumber wno) : Window(desc)
src/newgrf_text.cpp
Show inline comments
 
@@ -193,13 +193,13 @@ int LanguageMap::GetReverseMapping(int o
 

	
 
/** Helper structure for mapping choice lists. */
 
struct UnmappedChoiceList : ZeroedMemoryAllocator {
 
	/** Clean everything up. */
 
	~UnmappedChoiceList()
 
	{
 
		for (SmallPair<byte, char *> p : this->strings) {
 
		for (std::pair<byte, char *> p : this->strings) {
 
			free(p.second);
 
		}
 
	}
 

	
 
	/**
 
	 * Initialise the mapping.
src/vehicle.cpp
Show inline comments
 
@@ -2275,13 +2275,13 @@ void Vehicle::HandleLoading(bool mode)
 
 * @param capacities Map to be filled with cargoes and capacities.
 
 */
 
void Vehicle::GetConsistFreeCapacities(SmallMap<CargoID, uint> &capacities) const
 
{
 
	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 {
 
			pair->second += v->cargo_cap - v->cargo.StoredCount();
 
		}
 
	}
0 comments (0 inline, 0 general)