# HG changeset patch # User skidd13 # Date 1196673559 0 # Node ID 2318a054771929e0bf8ba1da33892c98c565d7c4 # Parent 1c54bc6f4bdf662f2dba2094a162b4792bfb2297 (svn r11564) -Codechange: Increase the usage of the for_each_bit macro and rename it fitting to the naming style diff -r 1c54bc6f4bdf -r 2318a0547719 src/ai/default/default.cpp --- a/src/ai/default/default.cpp Sun Dec 02 21:43:16 2007 +0000 +++ b/src/ai/default/default.cpp Mon Dec 03 09:19:19 2007 +0000 @@ -1653,13 +1653,11 @@ 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 @@ 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 @@ 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 -r 1c54bc6f4bdf -r 2318a0547719 src/core/bitmath_func.cpp --- a/src/core/bitmath_func.cpp Sun Dec 02 21:43:16 2007 +0000 +++ b/src/core/bitmath_func.cpp Mon Dec 03 09:19:19 2007 +0000 @@ -48,7 +48,7 @@ * 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 -r 1c54bc6f4bdf -r 2318a0547719 src/macros.h --- a/src/macros.h Sun Dec 02 21:43:16 2007 +0000 +++ b/src/macros.h Mon Dec 03 09:19:19 2007 +0000 @@ -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 -r 1c54bc6f4bdf -r 2318a0547719 src/roadveh_cmd.cpp --- a/src/roadveh_cmd.cpp Sun Dec 02 21:43:16 2007 +0000 +++ b/src/roadveh_cmd.cpp Mon Dec 03 09:19:19 2007 +0000 @@ -1273,18 +1273,17 @@ 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); + 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; - } + 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 -r 1c54bc6f4bdf -r 2318a0547719 src/station_gui.cpp --- a/src/station_gui.cpp Sun Dec 02 21:43:16 2007 +0000 +++ b/src/station_gui.cpp Mon Dec 03 09:19:19 2007 +0000 @@ -403,8 +403,9 @@ 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 -r 1c54bc6f4bdf -r 2318a0547719 src/town_gui.cpp --- a/src/town_gui.cpp Sun Dec 02 21:43:16 2007 +0000 +++ b/src/town_gui.cpp Mon Dec 03 09:19:19 2007 +0000 @@ -121,8 +121,9 @@ 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; } }