From 82913a21348a741eaacee6c81a57eba9a2c00250 Mon Sep 17 00:00:00 2001 From: skidd13 Date: Mon, 3 Dec 2007 09:19:19 +0000 Subject: (svn r11564) -Codechange: Increase the usage of the for_each_bit macro and rename it fitting to the naming style --- src/ai/default/default.cpp | 16 +++++++--------- src/core/bitmath_func.cpp | 2 +- src/macros.h | 18 ++++++++++++++---- src/roadveh_cmd.cpp | 23 +++++++++++------------ src/station_gui.cpp | 5 +++-- src/town_gui.cpp | 5 +++-- 6 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/ai/default/default.cpp b/src/ai/default/default.cpp index e0aa1ae00..d1979ed82 100644 --- a/src/ai/default/default.cpp +++ b/src/ai/default/default.cpp @@ -1653,13 +1653,11 @@ clear_town_stuff:; k = 0; // Build the rail - for (i = 0; i != 6; i++, j >>= 1) { - if (j & 1) { - k = i; - ret = DoCommand(c, railtype, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL); - if (CmdFailed(ret)) return CMD_ERROR; - total_cost.AddCost(ret); - } + FOR_EACH_SET_BIT(i, j) { + k = i; + ret = DoCommand(c, railtype, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL); + if (CmdFailed(ret)) return CMD_ERROR; + total_cost.AddCost(ret); } /* signals too? */ @@ -2854,7 +2852,6 @@ static bool AiCheckRoadFinished(Player *p) TileIndex tile; DiagDirection dir = p->ai.cur_dir_a; uint32 bits; - int i; are.dest = p->ai.cur_tile_b; tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDiagDir(dir)); @@ -2865,7 +2862,8 @@ static bool AiCheckRoadFinished(Player *p) are.best_dist = (uint)-1; - for_each_bit(i, bits) { + uint i; + FOR_EACH_SET_BIT(i, bits) { FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, ROADTYPES_ROAD, (DiagDirection)_dir_by_track[i], (TPFEnumProc*)AiEnumFollowRoad, NULL, &are); } diff --git a/src/core/bitmath_func.cpp b/src/core/bitmath_func.cpp index 53139308a..bbfad72ff 100644 --- a/src/core/bitmath_func.cpp +++ b/src/core/bitmath_func.cpp @@ -48,7 +48,7 @@ uint8 FindFirstBit(uint32 x) * Search the last set bit in a 32 bit variable. * * This algorithm is a static implementation of a log - * conguence search algorithm. It checks the first half + * conguence search algorithm. It checks the second half * if there is a bit set search there further. And this * way further. If no bit is set return 0. * diff --git a/src/macros.h b/src/macros.h index 51329ab19..5ec7b96e0 100644 --- a/src/macros.h +++ b/src/macros.h @@ -20,10 +20,20 @@ */ #define IS_CUSTOM_SPRITE(sprite) ((sprite) >= SPR_SIGNALS_BASE) - -#define for_each_bit(_i, _b) \ - for (_i = 0; _b != 0; _i++, _b >>= 1) \ - if (_b & 1) +/** + * Do an operation for each set set bit in a value. + * + * This macros is used to do an operation for each set + * bit in a variable. The first variable can be reused + * in the operation due to it's the bit position counter. + * The second variable will be cleared during the usage + * + * @param i The position counter + * @param b The value which we check for set bits + */ +#define FOR_EACH_SET_BIT(i, b) \ + for (i = 0; b != 0; i++, b >>= 1) \ + if (b & 1) static inline uint16 ReadLE16Aligned(const void* x) diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 6e747460a..f19558a78 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -1273,18 +1273,17 @@ do_it:; uint best_dist = (uint)-1; uint best_maxlen = (uint)-1; uint bitmask = (uint)trackdirs; - for (int i = 0; bitmask != 0; bitmask >>= 1, i++) { - if (HasBit(bitmask, 0)) { - if (best_track == INVALID_TRACKDIR) best_track = (Trackdir)i; // in case we don't find the path, just pick a track - frd.maxtracklen = (uint)-1; - frd.mindist = (uint)-1; - FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, v->u.road.compatible_roadtypes, _road_pf_directions[i], EnumRoadTrackFindDist, NULL, &frd); - - if (frd.mindist < best_dist || (frd.mindist == best_dist && frd.maxtracklen < best_maxlen)) { - best_dist = frd.mindist; - best_maxlen = frd.maxtracklen; - best_track = (Trackdir)i; - } + uint i; + FOR_EACH_SET_BIT(i, bitmask) { + if (best_track == INVALID_TRACKDIR) best_track = (Trackdir)i; // in case we don't find the path, just pick a track + frd.maxtracklen = (uint)-1; + frd.mindist = (uint)-1; + FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, v->u.road.compatible_roadtypes, _road_pf_directions[i], EnumRoadTrackFindDist, NULL, &frd); + + if (frd.mindist < best_dist || (frd.mindist == best_dist && frd.maxtracklen < best_maxlen)) { + best_dist = frd.mindist; + best_maxlen = frd.maxtracklen; + best_track = (Trackdir)i; } } } diff --git a/src/station_gui.cpp b/src/station_gui.cpp index 04438acd5..fe5f11fbf 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -403,8 +403,9 @@ static void PlayerStationsWndProc(Window *w, WindowEvent *e) ToggleBit(facilities, e->we.click.widget - STATIONLIST_WIDGET_TRAIN); w->ToggleWidgetLoweredState(e->we.click.widget); } else { - for (uint i = 0; facilities != 0; i++, facilities >>= 1) { - if (HasBit(facilities, 0)) w->RaiseWidget(i + STATIONLIST_WIDGET_TRAIN); + uint i; + FOR_EACH_SET_BIT(i, facilities) { + w->RaiseWidget(i + STATIONLIST_WIDGET_TRAIN); } SetBit(facilities, e->we.click.widget - STATIONLIST_WIDGET_TRAIN); w->LowerWidget(e->we.click.widget); diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 452210e55..ccc723e41 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -121,8 +121,9 @@ uint GetMaskOfTownActions(int *nump, PlayerID pid, const Town *t) static int GetNthSetBit(uint32 bits, int n) { if (n >= 0) { - for (uint i = 0; bits != 0; bits >>= 1, i++) { - if (bits & 1) n--; + uint i; + FOR_EACH_SET_BIT(i, bits) { + n--; if (n < 0) return i; } } -- cgit v1.2.3-54-g00ecf