Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a1018a6

Browse files
committed
use ranges where we can
1 parent a3cae2a commit a1018a6

8 files changed

Lines changed: 39 additions & 44 deletions

File tree

library/LuaApi.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,11 +2155,9 @@ static int units_getCitizens(lua_State *L) {
21552155
bool include_insane = lua_toboolean(L, 2); // defaults to false
21562156

21572157
std::vector<df::unit *> citizens;
2158-
if (Units::getCitizens(citizens, exclude_residents, include_insane)) {
2159-
Lua::PushVector(L, citizens);
2160-
return 1;
2161-
}
2162-
return 0;
2158+
Units::getCitizens(citizens, exclude_residents, include_insane);
2159+
Lua::PushVector(L, citizens);
2160+
return 1;
21632161
}
21642162

21652163
static int units_getUnitsByNobleRole(lua_State *L) {

library/include/modules/Units.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ DFHACK_EXPORT bool getUnitsInBox(std::vector<df::unit*> &units,
159159
int16_t x2, int16_t y2, int16_t z2);
160160
DFHACK_EXPORT bool getUnitsByNobleRole(std::vector<df::unit *> &units, std::string noble);
161161
DFHACK_EXPORT df::unit *getUnitByNobleRole(std::string noble);
162+
DFHACK_EXPORT void forCitizens(std::function<void(df::unit *)> fn, bool exclude_residents = false, bool include_insane = false);
162163
DFHACK_EXPORT bool getCitizens(std::vector<df::unit *> &citizens, bool exclude_residents = false, bool include_insane = false);
163164

164165
DFHACK_EXPORT int32_t findIndexById(int32_t id);

library/modules/Units.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ distribution.
8989
#include <algorithm>
9090
#include <numeric>
9191
#include <functional>
92+
#include <ranges>
9293

9394
using std::string;
9495
using std::vector;
@@ -920,16 +921,24 @@ df::unit *Units::getUnitByNobleRole(string noble) {
920921
return units[0];
921922
}
922923

923-
bool Units::getCitizens(std::vector<df::unit *> &citizens, bool exclude_residents, bool include_insane) {
924-
for (auto &unit : world->units.active) {
925-
if (isDead(unit) || !isActive(unit))
926-
continue;
924+
static auto citizensRange(bool exclude_residents, bool include_insane) {
925+
return world->units.active |
926+
std::views::filter([=](df::unit * unit) {
927+
if (Units::isDead(unit) || !Units::isActive(unit))
928+
return false;
929+
return Units::isCitizen(unit, include_insane) ||
930+
(!exclude_residents && Units::isResident(unit, include_insane));
931+
});
932+
}
927933

928-
if (isCitizen(unit, include_insane))
929-
citizens.emplace_back(unit);
930-
else if (!exclude_residents && isResident(unit, include_insane))
931-
citizens.emplace_back(unit);
932-
}
934+
void Units::forCitizens(std::function<void(df::unit *)> fn, bool exclude_residents, bool include_insane) {
935+
for (auto unit : citizensRange(exclude_residents, include_insane))
936+
fn(unit);
937+
}
938+
939+
bool Units::getCitizens(std::vector<df::unit *> & citizens, bool exclude_residents, bool include_insane) {
940+
for (auto unit : citizensRange(exclude_residents, include_insane))
941+
citizens.emplace_back(unit);
933942
return true;
934943
}
935944

plugins/autoclothing.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,7 @@ command_result autoclothing(color_ostream &out, vector <string> & parameters)
564564

565565
static void find_needed_clothing_items()
566566
{
567-
vector<df::unit *> citizens;
568-
Units::getCitizens(citizens);
569-
for (auto& unit : citizens)
570-
{
567+
Units::forCitizens([&](auto unit) {
571568
//now check each clothing order to see what the unit might be missing.
572569
for (auto& clothingOrder : clothingOrders)
573570
{
@@ -607,7 +604,7 @@ static void find_needed_clothing_items()
607604
//technically, there's some leeway in sizes, but only caring about exact sizes is simpler.
608605
clothingOrder.total_needed_per_race[unit->race] += neededAmount;
609606
}
610-
}
607+
});
611608
}
612609

613610
static void remove_available_clothing()
@@ -760,10 +757,8 @@ static void generate_control(color_ostream& out)
760757
map<int, int> missingHelms;
761758
map<int, int> missingGloves;
762759
map<int, int> missingPants;
763-
vector<df::unit *> citizens;
764-
Units::getCitizens(citizens);
765-
for (df::unit* unit : citizens)
766-
{
760+
761+
Units::forCitizens([&](auto unit) {
767762
fullUnitList[unit->race]++;
768763
int numArmor = 0, numShoes = 0, numHelms = 0, numGloves = 0, numPants = 0;
769764
for (auto itemId : unit->owned_items)
@@ -808,7 +803,8 @@ static void generate_control(color_ostream& out)
808803
if (numPants == 0)
809804
missingPants[unit->race]++;
810805
DEBUG(control,out) << Translation::TranslateName(Units::getVisibleName(unit)) << " has " << numArmor << " armor, " << numShoes << " shoes, " << numHelms << " helms, " << numGloves << " gloves, " << numPants << " pants" << endl;
811-
}
806+
});
807+
812808
if (missingArmor.size() + missingShoes.size() + missingHelms.size() + missingGloves.size() + missingPants.size() == 0)
813809
{
814810
out << "Everybody has a full set of clothes to wear, congrats!" << endl;

plugins/fastdwarf.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,15 @@ DFhackCExport command_result plugin_onupdate(color_ostream &out) {
156156
bool is_fast = config.get_int(CONFIG_FAST) == 1;
157157
bool is_tele = config.get_int(CONFIG_TELE) == 1;
158158

159-
std::vector<df::unit *> citizens;
160-
Units::getCitizens(citizens);
161-
for (auto & unit : citizens) {
159+
Units::forCitizens([&](auto unit) {
162160
if (is_tele)
163161
do_tele(out, unit);
164162

165163
if (is_fast) {
166164
DEBUG(cycle,out).print("fastifying unit %d\n", unit->id);
167165
Units::setGroupActionTimers(out, unit, 1, df::unit_action_type_group::All);
168166
}
169-
}
167+
});
170168

171169
return CR_OK;
172170
}

plugins/misery.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,12 @@ static void clear_misery(df::unit *unit) {
193193
// clears fake negative thoughts then runs the given lambda
194194
static void affect_units(
195195
std::function<void(df::unit *)> &&process_unit = [](df::unit *){}) {
196-
vector<df::unit *> citizens;
197-
Units::getCitizens(citizens);
198-
for (auto unit : citizens) {
196+
Units::forCitizens([&](auto unit){
199197
if (!unit->status.current_soul)
200-
continue;
201-
198+
return;
202199
clear_misery(unit);
203200
std::forward<std::function<void(df::unit *)> &&>(process_unit)(unit);
204-
}
201+
});
205202
}
206203

207204
static void do_cycle(color_ostream &out) {

plugins/reveal.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,9 @@ command_result revflood(color_ostream &out, vector<string> & params) {
445445
pos = Units::getPosition(unit);
446446
}
447447
if (!pos.isValid()) {
448-
vector<df::unit *> citizens;
449-
Units::getCitizens(citizens);
450-
if (citizens.size())
451-
pos = Units::getPosition(citizens[0]);
448+
Units::forCitizens([&](auto unit) {
449+
pos = Units::getPosition(unit);
450+
});
452451
}
453452
if (!pos.isValid()) {
454453
out.printerr("Please select a unit or place the keyboard cursor at some empty space you want to be unhidden.\n");

plugins/tailor.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,10 @@ class Tailor {
242242

243243
void scan_replacements()
244244
{
245-
vector<df::unit *> citizens;
246-
Units::getCitizens(citizens);
247-
for (auto u : citizens)
248-
{
245+
Units::forCitizens([&](auto u) {
249246
if (Units::isBaby(u) ||
250247
!Units::casteFlagSet(u->race, u->caste, df::enums::caste_raw_flags::EQUIPS))
251-
continue; // skip units we don't control or that can't wear clothes
248+
return; // skip units we don't control or that can't wear clothes
252249

253250
std::set <df::item_type> wearing;
254251
std::set <df::item_type> ordered;
@@ -329,7 +326,7 @@ class Tailor {
329326
needed[std::make_pair(ty, usize)] += 1;
330327
}
331328
}
332-
}
329+
});
333330
}
334331

335332
void create_orders()

0 commit comments

Comments
 (0)