1
0
Fork 0

(svn r10122) -Codechange: Add a CountBitsSet function and use it to replace some less efficient loops.

release/0.6
maedhros 2007-06-12 22:13:49 +00:00
parent f3f744d36a
commit 12be876131
4 changed files with 22 additions and 20 deletions

View File

@ -67,10 +67,7 @@ int64 CalculateCompanyValue(const Player* p)
uint num = 0; uint num = 0;
FOR_ALL_STATIONS(st) { FOR_ALL_STATIONS(st) {
if (st->owner == owner) { if (st->owner == owner) num += CountBitsSet(st->facilities);
uint facil = st->facilities;
do num += (facil&1); while (facil >>= 1);
}
} }
value = num * _price.station_value * 25; value = num * _price.station_value * 25;
@ -144,10 +141,7 @@ int UpdateCompanyRatingAndValue(Player *p, bool update)
const Station* st; const Station* st;
FOR_ALL_STATIONS(st) { FOR_ALL_STATIONS(st) {
if (st->owner == owner) { if (st->owner == owner) num += CountBitsSet(st->facilities);
int facil = st->facilities;
do num += facil&1; while (facil>>=1);
}
} }
_score_part[owner][SCORE_STATIONS] = num; _score_part[owner][SCORE_STATIONS] = num;
} }
@ -196,9 +190,7 @@ int UpdateCompanyRatingAndValue(Player *p, bool update)
/* Generate score for variety of cargo */ /* Generate score for variety of cargo */
{ {
uint cargo = p->cargo_types; uint num = CountBitsSet(p->cargo_types);
uint num = 0;
do num += cargo&1; while (cargo>>=1);
_score_part[owner][SCORE_CARGO] = num; _score_part[owner][SCORE_CARGO] = num;
if (update) p->cargo_types = 0; if (update) p->cargo_types = 0;
} }

View File

@ -147,9 +147,11 @@ Town *ClosestTownFromTile(TileIndex tile, uint threshold);
void ChangeTownRating(Town *t, int add, int max); void ChangeTownRating(Town *t, int add, int max);
uint GetTownRadiusGroup(const Town* t, TileIndex tile); uint GetTownRadiusGroup(const Town* t, TileIndex tile);
int FindFirstBit(uint32 x);
void ShowHighscoreTable(int difficulty, int8 rank); void ShowHighscoreTable(int difficulty, int8 rank);
int FindFirstBit(uint32 x);
int CountBitsSet(uint32 value);
void AfterLoadTown(); void AfterLoadTown();
void UpdatePatches(); void UpdatePatches();
void AskExitGame(); void AskExitGame();

View File

@ -270,6 +270,21 @@ int FindFirstBit(uint32 value)
return i; 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() static void Save_NAME()
{ {

View File

@ -1085,15 +1085,8 @@ static void RoadZPosAffectSpeed(Vehicle *v, byte old_z)
static int PickRandomBit(uint bits) static int PickRandomBit(uint bits)
{ {
uint num = 0;
uint b = bits;
uint i; uint i;
uint num = RandomRange(CountBitsSet(bits));
do {
if (b & 1) num++;
} while (b >>= 1);
num = RandomRange(num);
for (i = 0; !(bits & 1) || (int)--num >= 0; bits >>= 1, i++) {} for (i = 0; !(bits & 1) || (int)--num >= 0; bits >>= 1, i++) {}
return i; return i;