(svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.

This commit is contained in:
rubidium 2007-09-01 08:31:36 +00:00
parent 0df355bbda
commit 56ab253307
7 changed files with 30 additions and 46 deletions

View File

@ -70,7 +70,7 @@ Money CalculateCompanyValue(const Player* p)
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 @@ int UpdateCompanyRatingAndValue(Player *p, bool update)
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 @@ int UpdateCompanyRatingAndValue(Player *p, bool update)
/* 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;
}

View File

@ -136,7 +136,6 @@ uint GetTownRadiusGroup(const Town* t, TileIndex tile);
void ShowHighscoreTable(int difficulty, int8 rank);
int FindFirstBit(uint32 x);
int CountBitsSet(uint32 value);
void AfterLoadTown();
void UpdatePatches();

View File

@ -420,6 +420,28 @@ static inline int KillFirstBit2x64(int value)
}
}
/**
* 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
*

View File

@ -273,22 +273,6 @@ int FindFirstBit(uint32 value)
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;

View File

@ -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 @@ CommandCost CmdRemoveRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
MarkTileDirtyByTile(tile);
}
}
return CommandCost(CountRoadBits(c) * _price.remove_road);
return CommandCost(COUNTBITS(c) * _price.remove_road);
}
case ROAD_TILE_CROSSING: {
@ -487,7 +474,7 @@ do_clear:;
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));

View File

@ -1083,7 +1083,7 @@ static void RoadZPosAffectSpeed(Vehicle *v, byte old_z)
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;

View File

@ -475,14 +475,6 @@ uint ShowAdditionalText(int x, int y, uint w, EngineID engine)
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 @@ uint ShowRefitOptionsList(int x, int y, uint w, EngineID engine)
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 @@ uint ShowRefitOptionsList(int x, int y, uint w, EngineID engine)
} 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);
}