(svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
--- a/src/economy.cpp Sat Sep 01 08:04:08 2007 +0000
+++ b/src/economy.cpp Sat Sep 01 08:31:36 2007 +0000
@@ -70,7 +70,7 @@
uint num = 0;
FOR_ALL_STATIONS(st) {
- if (st->owner == owner) num += CountBitsSet(st->facilities);
+ if (st->owner == owner) num += COUNTBITS(st->facilities);
}
value.AddCost(num * _price.station_value * 25);
@@ -146,7 +146,7 @@
const Station* st;
FOR_ALL_STATIONS(st) {
- if (st->owner == owner) num += CountBitsSet(st->facilities);
+ if (st->owner == owner) num += COUNTBITS(st->facilities);
}
_score_part[owner][SCORE_STATIONS] = num;
}
@@ -191,7 +191,7 @@
/* Generate score for variety of cargo */
{
- uint num = CountBitsSet(p->cargo_types);
+ uint num = COUNTBITS(p->cargo_types);
_score_part[owner][SCORE_CARGO] = num;
if (update) p->cargo_types = 0;
}
--- a/src/functions.h Sat Sep 01 08:04:08 2007 +0000
+++ b/src/functions.h Sat Sep 01 08:31:36 2007 +0000
@@ -136,7 +136,6 @@
void ShowHighscoreTable(int difficulty, int8 rank);
int FindFirstBit(uint32 x);
-int CountBitsSet(uint32 value);
void AfterLoadTown();
void UpdatePatches();
--- a/src/macros.h Sat Sep 01 08:04:08 2007 +0000
+++ b/src/macros.h Sat Sep 01 08:31:36 2007 +0000
@@ -421,6 +421,28 @@
}
/**
+ * Counts the number of set bits in a variable.
+ *
+ * @param value the value to count the number of bits in.
+ * @return the number of bits.
+ */
+template<typename T> static inline uint COUNTBITS(T value)
+{
+ uint num;
+
+ /* This loop is only called once for every bit set by clearing the lowest
+ * bit in each loop. The number of bits is therefore equal to the number of
+ * times the loop was called. It was found at the following website:
+ * http://graphics.stanford.edu/~seander/bithacks.html */
+
+ for (num = 0; value != 0; num++) {
+ value &= (T)(value - 1);
+ }
+
+ return num;
+}
+
+/**
* Returns true if value a has only one bit set to 1
*
* This macro returns true if only one bit is set.
--- a/src/misc.cpp Sat Sep 01 08:04:08 2007 +0000
+++ b/src/misc.cpp Sat Sep 01 08:31:36 2007 +0000
@@ -273,22 +273,6 @@
return i;
}
-int CountBitsSet(uint32 value)
-{
- int num;
-
- /* This loop is only called once for every bit set by clearing the lowest
- * bit in each loop. The number of bits is therefore equal to the number of
- * times the loop was called. It was found at the following website:
- * http://graphics.stanford.edu/~seander/bithacks.html */
-
- for (num = 0; value != 0; num++) {
- value &= value - 1;
- }
-
- return num;
-}
-
static void Save_NAME()
{
int i;
--- a/src/road_cmd.cpp Sat Sep 01 08:04:08 2007 +0000
+++ b/src/road_cmd.cpp Sat Sep 01 08:31:36 2007 +0000
@@ -33,19 +33,6 @@
#include "tunnel_map.h"
#include "misc/autoptr.hpp"
-
-static uint CountRoadBits(RoadBits r)
-{
- uint count = 0;
-
- if (r & ROAD_NW) ++count;
- if (r & ROAD_SW) ++count;
- if (r & ROAD_SE) ++count;
- if (r & ROAD_NE) ++count;
- return count;
-}
-
-
bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, Owner owner, bool *edge_road, RoadType rt)
{
RoadBits present;
@@ -225,7 +212,7 @@
MarkTileDirtyByTile(tile);
}
}
- return CommandCost(CountRoadBits(c) * _price.remove_road);
+ return CommandCost(COUNTBITS(c) * _price.remove_road);
}
case ROAD_TILE_CROSSING: {
@@ -487,7 +474,7 @@
pieces &= ComplementRoadBits(existing);
}
- cost.AddCost(CountRoadBits(pieces) * _price.build_road);
+ cost.AddCost(COUNTBITS(pieces) * _price.build_road);
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
/* Pay for *every* tile of the bridge or tunnel */
cost.MultiplyCost(DistanceManhattan(IsTunnel(tile) ? GetOtherTunnelEnd(tile) : GetOtherBridgeEnd(tile), tile));
--- a/src/roadveh_cmd.cpp Sat Sep 01 08:04:08 2007 +0000
+++ b/src/roadveh_cmd.cpp Sat Sep 01 08:31:36 2007 +0000
@@ -1083,7 +1083,7 @@
static int PickRandomBit(uint bits)
{
uint i;
- uint num = RandomRange(CountBitsSet(bits));
+ uint num = RandomRange(COUNTBITS(bits));
for (i = 0; !(bits & 1) || (int)--num >= 0; bits >>= 1, i++) {}
return i;
--- a/src/vehicle_gui.cpp Sat Sep 01 08:04:08 2007 +0000
+++ b/src/vehicle_gui.cpp Sat Sep 01 08:31:36 2007 +0000
@@ -475,14 +475,6 @@
return DrawStringMultiLine(x, y, STR_02BD, w);
}
-/** Count the number of bits that are set in a mask */
-static uint CountBits(uint32 mask)
-{
- uint c = 0;
- for (; mask != 0; mask >>= 1) if (HASBIT(mask, 0)) c++;
- return c;
-}
-
/** Display list of cargo types of the engine, for the purchase information window */
uint ShowRefitOptionsList(int x, int y, uint w, EngineID engine)
{
@@ -493,7 +485,7 @@
char *b = _userstring;
/* Draw nothing if the engine is not refittable */
- if (CountBits(cmask) <= 1) return 0;
+ if (COUNTBITS(cmask) <= 1) return 0;
b = InlineString(b, STR_PURCHASE_INFO_REFITTABLE_TO);
@@ -503,7 +495,7 @@
} else {
/* Check if we are able to refit to more cargo types and unable to. If
* so, invert the cargo types to list those that we can't refit to. */
- if (CountBits(cmask ^ lmask) < CountBits(cmask)) {
+ if (COUNTBITS(cmask ^ lmask) < COUNTBITS(cmask)) {
cmask ^= lmask;
b = InlineString(b, STR_PURCHASE_INFO_ALL_BUT);
}