Changeset - r14450:8f83ebfe28cc
[Not reviewed]
master
0 8 0
smatz - 15 years ago 2010-02-05 17:05:58
smatz@openttd.org
(svn r19019) -Codechange: use HasExactlyOneBit() and HasAtMostOneBit() instead of CountBits() where possible
8 files changed with 35 insertions and 11 deletions:
0 comments (0 inline, 0 general)
src/ai/api/ai_station.cpp
Show inline comments
 
@@ -53,7 +53,7 @@
 
		DEBUG(ai, 0, "GetCoverageRadius(): coverage radius of airports needs to be requested via AIAirport::GetAirportCoverageRadius(), as it requires AirportType");
 
		return -1;
 
	}
 
	if (CountBits(station_type) != 1) return -1;
 
	if (!HasExactlyOneBit(station_type)) return -1;
 
	if (!_settings_game.station.modified_catchment) return CA_UNMODIFIED;
 

	
 
	switch (station_type) {
 
@@ -89,7 +89,7 @@
 
/* static */ bool AIStation::HasStationType(StationID station_id, StationType station_type)
 
{
 
	if (!IsValidStation(station_id)) return false;
 
	if (CountBits(station_type) != 1) return false;
 
	if (!HasExactlyOneBit(station_type)) return false;
 

	
 
	return (::Station::Get(station_id)->facilities & station_type) != 0;
 
}
src/ai/api/ai_tile.cpp
Show inline comments
 
@@ -35,7 +35,7 @@
 
			if (::GetRoadTypes(tile) != ROADTYPES_ROAD) return false;
 
			/* Depots and crossings aren't considered buildable */
 
			if (::GetRoadTileType(tile) != ROAD_TILE_NORMAL) return false;
 
			if (CountBits(::GetRoadBits(tile, ROADTYPE_ROAD)) != 1) return false;
 
			if (!HasExactlyOneBit(::GetRoadBits(tile, ROADTYPE_ROAD))) return false;
 
			if (::IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)) return true;
 
			if (::IsRoadOwner(tile, ROADTYPE_ROAD, _current_company)) return true;
 
			return false;
src/ai/api/ai_waypoint.cpp
Show inline comments
 
@@ -31,7 +31,7 @@
 
/* static */ bool AIWaypoint::HasWaypointType(StationID waypoint_id, WaypointType waypoint_type)
 
{
 
	if (!IsValidWaypoint(waypoint_id)) return false;
 
	if (CountBits(waypoint_type) != 1) return false;
 
	if (!HasExactlyOneBit(waypoint_type)) return false;
 

	
 
	return (::Waypoint::Get(waypoint_id)->facilities & waypoint_type) != 0;
 
}
src/core/bitmath_func.hpp
Show inline comments
 
@@ -255,6 +255,30 @@ static inline uint CountBits(T value)
 
}
 

	
 
/**
 
 * Test whether \a value has exactly 1 bit set
 
 *
 
 * @param value the value to test.
 
 * @return does \a value have exactly 1 bit set?
 
 */
 
template <typename T>
 
static FORCEINLINE bool HasExactlyOneBit(T value)
 
{
 
	return value != 0 && (value & (value - 1)) == 0;
 
}
 

	
 
/**
 
 * Test whether \a value has at most 1 bit set
 
 *
 
 * @param value the value to test.
 
 * @return does \a value have at most 1 bit set?
 
 */
 
template <typename T>
 
static FORCEINLINE bool HasAtMostOneBit(T value)
 
{
 
	return (value & (value - 1)) == 0;
 
}
 

	
 
/**
 
 * ROtate x Left by n
 
 *
 
 * @note Assumes a byte has 8 bits
src/road.cpp
Show inline comments
 
@@ -64,7 +64,7 @@ RoadBits CleanUpRoadBits(const TileIndex
 

	
 
					/* Accept only connective tiles */
 
					connective = (neighbor_rb & mirrored_rb) || // Neighbor has got the fitting RoadBit
 
							CountBits(neighbor_rb) == 1; // Neighbor has got only one Roadbit
 
							HasExactlyOneBit(neighbor_rb); // Neighbor has got only one Roadbit
 

	
 
				} break;
 

	
src/road_cmd.cpp
Show inline comments
 
@@ -422,7 +422,7 @@ static CommandCost CheckRoadSlope(Slope 
 
				return CommandCost();
 
			}
 
		} else {
 
			if (CountBits(existing) == 1 && GetRoadFoundation(tileh, existing) == FOUNDATION_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
 
			if (HasExactlyOneBit(existing) && GetRoadFoundation(tileh, existing) == FOUNDATION_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
 
			return CommandCost();
 
		}
 
	}
 
@@ -911,7 +911,7 @@ static CommandCost ClearTile_Road(TileIn
 
			RoadBits b = GetAllRoadBits(tile);
 

	
 
			/* Clear the road if only one piece is on the tile OR we are not using the DC_AUTO flag */
 
			if ((CountBits(b) == 1 && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) {
 
			if ((HasExactlyOneBit(b) && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) {
 
				RoadTypes rts = GetRoadTypes(tile);
 
				CommandCost ret(EXPENSES_CONSTRUCTION);
 
				for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
 
@@ -1145,7 +1145,7 @@ static void DrawRoadBits(TileInfo *ti)
 
	}
 

	
 
	/* If there are no road bits, return, as there is nothing left to do */
 
	if (CountBits(road) < 2) return;
 
	if (HasAtMostOneBit(road)) return;
 

	
 
	/* Draw extra details. */
 
	for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
 
@@ -1322,7 +1322,7 @@ static void TileLoop_Road(TileIndex tile
 
			/* Show an animation to indicate road work */
 
			if (t->road_build_months != 0 &&
 
					(DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) &&
 
					IsNormalRoad(tile) && CountBits(GetAllRoadBits(tile)) > 1 ) {
 
					IsNormalRoad(tile) && !HasAtMostOneBit(GetAllRoadBits(tile))) {
 
				if (GetFoundationSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile) && Chance16(1, 40)) {
 
					StartRoadWorks(tile);
 

	
src/roadveh_cmd.cpp
Show inline comments
 
@@ -1355,7 +1355,7 @@ again:
 
		uint turn_around_start_frame = RVC_TURN_AROUND_START_FRAME;
 

	
 
		RoadBits tram;
 
		if (v->roadtype == ROADTYPE_TRAM && !IsRoadDepotTile(v->tile) && CountBits(tram = GetAnyRoadBits(v->tile, ROADTYPE_TRAM, true)) == 1) {
 
		if (v->roadtype == ROADTYPE_TRAM && !IsRoadDepotTile(v->tile) && HasExactlyOneBit(tram = GetAnyRoadBits(v->tile, ROADTYPE_TRAM, true))) {
 
			/*
 
			 * The tram is turning around with one tram 'roadbit'. This means that
 
			 * it is using the 'big' corner 'drive data'. However, to support the
src/vehicle_gui.cpp
Show inline comments
 
@@ -520,7 +520,7 @@ uint ShowRefitOptionsList(int left, int 
 
	char *b = string;
 

	
 
	/* Draw nothing if the engine is not refittable */
 
	if (CountBits(cmask) <= 1) return y;
 
	if (HasAtMostOneBit(cmask)) return y;
 

	
 
	b = InlineString(b, STR_PURCHASE_INFO_REFITTABLE_TO);
 

	
0 comments (0 inline, 0 general)