Changeset - r23629:cfb4492bdba9
[Not reviewed]
master
0 2 0
glx - 6 years ago 2019-04-12 15:55:35
glx@openttd.org
Codechange: use std::vector for _sorted_railtypes
2 files changed with 8 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/rail.h
Show inline comments
 
@@ -461,17 +461,16 @@ RailTypes GetRailTypes(bool introduces);
 
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels = true);
 

	
 
void ResetRailTypes();
 
void InitRailTypes();
 
RailType AllocateRailType(RailTypeLabel label);
 

	
 
extern RailType _sorted_railtypes[RAILTYPE_END];
 
extern uint8 _sorted_railtypes_size;
 
extern std::vector<RailType> _sorted_railtypes;
 
extern RailTypes _railtypes_hidden_mask;
 

	
 
/**
 
 * Loop header for iterating over railtypes, sorted by sortorder.
 
 * @param var Railtype.
 
 */
 
#define FOR_ALL_SORTED_RAILTYPES(var) for (uint8 index = 0; index < _sorted_railtypes_size && (var = _sorted_railtypes[index], true) ; index++)
 
#define FOR_ALL_SORTED_RAILTYPES(var) for (uint8 index = 0; index < _sorted_railtypes.size() && (var = _sorted_railtypes[index], true) ; index++)
 

	
 
#endif /* RAIL_H */
src/rail_cmd.cpp
Show inline comments
 
@@ -41,14 +41,13 @@
 
#include "safeguards.h"
 

	
 
/** Helper type for lists/vectors of trains */
 
typedef std::vector<Train *> TrainList;
 

	
 
RailtypeInfo _railtypes[RAILTYPE_END];
 
RailType _sorted_railtypes[RAILTYPE_END];
 
uint8 _sorted_railtypes_size;
 
std::vector<RailType> _sorted_railtypes;
 
RailTypes _railtypes_hidden_mask;
 

	
 
/** Enum holding the signal offset in the sprite sheet according to the side it is representing. */
 
enum SignalOffsets {
 
	SIGNAL_TO_SOUTHWEST,
 
	SIGNAL_TO_NORTHEAST,
 
@@ -127,15 +126,15 @@ void ResolveRailTypeGUISprites(RailtypeI
 
/**
 
 * Compare railtypes based on their sorting order.
 
 * @param first  The railtype to compare to.
 
 * @param second The railtype to compare.
 
 * @return True iff the first should be sorted before the second.
 
 */
 
static int CDECL CompareRailTypes(const RailType *first, const RailType *second)
 
static bool CompareRailTypes(const RailType &first, const RailType &second)
 
{
 
	return GetRailTypeInfo(*first)->sorting_order - GetRailTypeInfo(*second)->sorting_order;
 
	return GetRailTypeInfo(first)->sorting_order < GetRailTypeInfo(second)->sorting_order;
 
}
 

	
 
/**
 
 * Resolve sprites of custom rail types
 
 */
 
void InitRailTypes()
 
@@ -143,19 +142,19 @@ void InitRailTypes()
 
	for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
 
		RailtypeInfo *rti = &_railtypes[rt];
 
		ResolveRailTypeGUISprites(rti);
 
		if (HasBit(rti->flags, RTF_HIDDEN)) SetBit(_railtypes_hidden_mask, rt);
 
	}
 

	
 
	_sorted_railtypes_size = 0;
 
	_sorted_railtypes.clear();
 
	for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
 
		if (_railtypes[rt].label != 0 && !HasBit(_railtypes_hidden_mask, rt)) {
 
			_sorted_railtypes[_sorted_railtypes_size++] = rt;
 
			_sorted_railtypes.push_back(rt);
 
		}
 
	}
 
	QSortT(_sorted_railtypes, _sorted_railtypes_size, CompareRailTypes);
 
	std::sort(_sorted_railtypes.begin(), _sorted_railtypes.end(), CompareRailTypes);
 
}
 

	
 
/**
 
 * Allocate a new rail type label
 
 */
 
RailType AllocateRailType(RailTypeLabel label)
0 comments (0 inline, 0 general)