Skip to content

Commit

Permalink
Used std::distance instead of iterator arithmetic when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Feb 19, 2018
1 parent 515dbdd commit 4ccb962
Show file tree
Hide file tree
Showing 26 changed files with 52 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/actions/advancement.cpp
Expand Up @@ -193,7 +193,7 @@ namespace

for(std::vector<std::string>::const_iterator a = options.begin(); a != options.end(); ++a) {
if (std::find(allowed.begin(), allowed.end(), *a) != allowed.end()){
res = a - options.begin();
res = std::distance(options.begin(), a);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/actions/move.cpp
Expand Up @@ -152,7 +152,7 @@ game_events::pump_result_t get_village(const map_location& loc, int side, bool *
// We strip the village off all other sides, unless it is held by an ally
// and our side is already defeated (and thus we can't occupy it)
for(std::vector<team>::iterator i = teams.begin(); i != teams.end(); ++i) {
int i_side = i - teams.begin() + 1;
int i_side = std::distance(teams.begin(), i) + 1;
if (!t || not_defeated || t->is_enemy(i_side)) {
if(i->owns_village(loc)) {
old_owner_side = i_side;
Expand Down
2 changes: 1 addition & 1 deletion src/ai/simulated_actions.cpp
Expand Up @@ -195,7 +195,7 @@ void helper_check_village(const map_location& loc, int side){
// Strip the village off all other sides.
int old_owner_side = 0;
for(std::vector<team>::iterator i = teams.begin(); i != teams.end(); ++i){
int i_side = i - teams.begin() + 1;
int i_side = std::distance(teams.begin(), i) + 1;
if(!t || has_leader || t->is_enemy(i_side)){
if(i->owns_village(loc)){
old_owner_side = i_side;
Expand Down
2 changes: 1 addition & 1 deletion src/config.cpp
Expand Up @@ -667,7 +667,7 @@ void config::remove_children(config_key_type key, std::function<bool(const confi

auto child_it = std::find_if(pos->second.begin(), pos->second.end(), predicate);
while(child_it != pos->second.end()) {
unsigned index = child_it - pos->second.begin();
unsigned index = std::distance(pos->second.begin(), child_it);
remove_child(pos, index);
child_it = std::find_if(pos->second.begin() + index, pos->second.end(), predicate);
}
Expand Down
10 changes: 5 additions & 5 deletions src/display.cpp
Expand Up @@ -249,7 +249,7 @@ display::display(const display_context * dc, std::weak_ptr<wb::manager> wb, repo

set_idle_anim_rate(preferences::idle_anim_rate());

zoom_index_ = std::find(zoom_levels.begin(), zoom_levels.end(), zoom_) - zoom_levels.begin();
zoom_index_ = std::distance(zoom_levels.begin(), std::find(zoom_levels.begin(), zoom_levels.end(), zoom_));

image::set_zoom(zoom_);

Expand Down Expand Up @@ -1737,7 +1737,7 @@ const theme::action* display::action_pressed()
{
for(auto i = action_buttons_.begin(); i != action_buttons_.end(); ++i) {
if((*i)->pressed()) {
const size_t index = i - action_buttons_.begin();
const size_t index = std::distance(action_buttons_.begin(), i);
if(index >= theme_.actions().size()) {
assert(false);
return nullptr;
Expand All @@ -1753,7 +1753,7 @@ const theme::menu* display::menu_pressed()
{
for(auto i = menu_buttons_.begin(); i != menu_buttons_.end(); ++i) {
if((*i)->pressed()) {
const size_t index = i - menu_buttons_.begin();
const size_t index = std::distance(menu_buttons_.begin(), i);
if(index >= theme_.menus().size()) {
assert(false);
return nullptr;
Expand All @@ -1774,7 +1774,7 @@ void display::enable_menu(const std::string& item, bool enable)
);

if(hasitem != menu->items().end()) {
const size_t index = menu - theme_.menus().begin();
const size_t index = std::distance(theme_.menus().begin(), menu);
if(index >= menu_buttons_.size()) {
continue;
}
Expand Down Expand Up @@ -2055,7 +2055,7 @@ bool display::set_zoom(unsigned int amount, const bool validate_value_and_set_in
}

new_zoom = *iter;
zoom_index_ = iter - zoom_levels.begin();
zoom_index_ = std::distance(zoom_levels.begin(), iter);
}

const SDL_Rect& area = map_area();
Expand Down
2 changes: 1 addition & 1 deletion src/editor/palette/location_palette.cpp
Expand Up @@ -384,7 +384,7 @@ void location_palette::add_item(const std::string& id)
pos = items_.size() - 1;
}
else {
pos = itor - items_.begin();
pos = std::distance(items_.begin(), itor);
}
selected_item_ = id;
items_start_ = std::max(pos - num_visible_items() + 1, items_start_);
Expand Down
2 changes: 1 addition & 1 deletion src/formula/formula.cpp
Expand Up @@ -971,7 +971,7 @@ class string_expression : public formula_expression
}

const std::string formula_str(i+1, j);
const int pos = i - str.begin();
const int pos = std::distance(str.begin(), i);
if(j - i == 2 && (i[1] == '(' || i[1] == '\'' || i[1] == ')')) {
// Bracket contained nothing but a quote or parenthesis.
// This means it was intended as a literal quote or square bracket.
Expand Down
2 changes: 1 addition & 1 deletion src/game_board.cpp
Expand Up @@ -335,7 +335,7 @@ void game_board::write_config(config & cfg) const
{
cfg["next_underlying_unit_id"] = unit_id_manager_.get_save_id();
for(std::vector<team>::const_iterator t = teams_.begin(); t != teams_.end(); ++t) {
int side_num = t - teams_.begin() + 1;
int side_num = std::distance(teams_.begin(), t) + 1;

config& side = cfg.add_child("side");
t->write(side);
Expand Down
2 changes: 1 addition & 1 deletion src/game_events/action_wml.cpp
Expand Up @@ -487,7 +487,7 @@ WML_HANDLER_FUNCTION(recall,, cfg)
const unit_filter lfilt(leader_filter); // Note that if leader_filter is null, this correctly gives a null filter that matches all units.
for(std::vector<unit_ptr>::iterator u = avail.begin(); u != avail.end(); ++u) {
DBG_NG << "checking unit against filter...\n";
scoped_recall_unit auto_store("this_unit", player_id, u - avail.begin());
scoped_recall_unit auto_store("this_unit", player_id, std::distance(avail.begin(), u));
if (ufilt(*(*u), map_location())) {
DBG_NG << (*u)->id() << " matched the filter...\n";
const unit_ptr to_recruit = *u;
Expand Down
4 changes: 2 additions & 2 deletions src/game_initialization/connect_engine.cpp
Expand Up @@ -137,7 +137,7 @@ connect_engine::connect_engine(saved_game& state, const bool first_scenario, mp_

team_name = "Team " + std::to_string(original_team_names.size());
} else {
team_name = "Team " + std::to_string(name_itor - original_team_names.begin() + 1);
team_name = "Team " + std::to_string(std::distance(original_team_names.begin(), name_itor) + 1);
}

user_team_name = team_prefix + side_str;
Expand Down Expand Up @@ -971,7 +971,7 @@ side_engine::side_engine(const config& cfg, connect_engine& parent_engine, const

if(iter != color_options_.end()) {
color_id_ = *iter;
color_ = iter - color_options_.begin();
color_ = std::distance(color_options_.begin(), iter);
} else {
color_options_.push_back(custom_color);

Expand Down
2 changes: 1 addition & 1 deletion src/game_initialization/flg_manager.cpp
Expand Up @@ -444,7 +444,7 @@ void flg_manager::select_default_faction()
});

if(default_faction_it != choosable_factions_.end()) {
set_current_faction(default_faction_it - choosable_factions_.begin());
set_current_faction(std::distance(choosable_factions_.begin(), default_faction_it));
} else {
set_current_faction(0u);
}
Expand Down
2 changes: 1 addition & 1 deletion src/game_initialization/singleplayer.cpp
Expand Up @@ -78,7 +78,7 @@ bool enter_create_mode(saved_game& state, jump_to_campaign_info jump_to_campaign
return false;
}

create_eng.set_current_level(campaign - campaigns.begin());
create_eng.set_current_level(std::distance(campaigns.begin(), campaign));
}

state.classification().random_mode = random_mode;
Expand Down
12 changes: 7 additions & 5 deletions src/gui/dialogs/addon/manager.cpp
Expand Up @@ -582,10 +582,12 @@ boost::dynamic_bitset<> addon_manager::get_type_filter_visibility(const window&
boost::dynamic_bitset<> res;

for(const auto& a : addons_) {
int index = std::find_if(type_filter_types_.begin(), type_filter_types_.end(),
[&a](const std::pair<ADDON_TYPE, std::string>& entry) {
return entry.first == a.second.type;
}) - type_filter_types_.begin();
int index = std::distance(type_filter_types_.begin(),

This comment has been minimized.

Copy link
@CelticMinstrel

CelticMinstrel Feb 20, 2018

Member

I think here it'd be better to store the find_if result in a separate intermediate variable.

std::find_if(type_filter_types_.begin(), type_filter_types_.end(),
[&a](const std::pair<ADDON_TYPE, std::string>& entry) {
return entry.first == a.second.type;
})
);
res.push_back(toggle_states[index]);
}

Expand Down Expand Up @@ -622,7 +624,7 @@ void addon_manager::on_order_changed(window& window, unsigned int sort_column, l
menu_button& order_menu = find_widget<menu_button>(&window, "order_dropdown", false);
auto order_it = std::find_if(all_orders_.begin(), all_orders_.end(),
[sort_column](const addon_order& order) {return order.column_index == static_cast<int>(sort_column);});
int index = 2 * (order_it - all_orders_.begin());
int index = 2 * (std::distance(all_orders_.begin(), order_it));
if(order == listbox::SORT_DESCENDING) {
++index;
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/dialogs/campaign_selection.cpp
Expand Up @@ -95,7 +95,7 @@ void campaign_selection::campaign_selected(window& window)
if(!tree.selected_item()->id().empty()) {
auto iter = std::find(page_ids_.begin(), page_ids_.end(), tree.selected_item()->id());

const int choice = iter - page_ids_.begin();
const int choice = std::distance(page_ids_.begin(), iter);
if(iter == page_ids_.end()) {
return;
}
Expand Down Expand Up @@ -337,7 +337,7 @@ void campaign_selection::post_show(window& window)
if(!tree.selected_item()->id().empty()) {
auto iter = std::find(page_ids_.begin(), page_ids_.end(), tree.selected_item()->id());
if(iter != page_ids_.end()) {
choice_ = iter - page_ids_.begin();
choice_ = std::distance(page_ids_.begin(), iter);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/gui/dialogs/multiplayer/mp_create_game.cpp
Expand Up @@ -198,7 +198,7 @@ void mp_create_game::pre_show(window& win)
});

if(index != level_types_.end()) {
return index - level_types_.begin();
return std::distance(level_types_.begin(), index);
}

return 0;
Expand Down Expand Up @@ -278,9 +278,9 @@ void mp_create_game::pre_show(window& win)
rfm_options[1]["tooltip"] = _("No Mirror: No two players will get the same faction");
rfm_options[2]["tooltip"] = _("No Ally Mirror: No two allied players will get the same faction");

const int initial_index = std::find(rfm_types_.begin(), rfm_types_.end(),
const int initial_index = std::distance(rfm_types_.begin(), std::find(rfm_types_.begin(), rfm_types_.end(),
mp_game_settings::RANDOM_FACTION_MODE::string_to_enum(prefs::random_faction_mode(), mp_game_settings::RANDOM_FACTION_MODE::DEFAULT))
- rfm_types_.begin();
);

menu_button& rfm_menu_button = find_widget<menu_button>(&win, "random_faction_mode", false);

Expand Down Expand Up @@ -462,7 +462,7 @@ void mp_create_game::sync_with_depcheck(window& window)
selected_game_index_ = new_level_index.second;

auto& game_types_list = find_widget<menu_button>(&window, "game_types", false);
game_types_list.set_value(std::find_if(level_types_.begin(), level_types_.begin(), [&](const level_type_info& info){ return info.first == new_level_index.first; }) - level_types_.begin());
game_types_list.set_value(std::distance(level_types_.begin(), std::find_if(level_types_.begin(), level_types_.begin(), [&](const level_type_info& info){ return info.first == new_level_index.first; })));

if(different_type) {
display_games_of_type(window, new_level_index.first, create_engine_.current_level().id());
Expand Down Expand Up @@ -661,7 +661,7 @@ void mp_create_game::regenerate_random_map(window& window)
int mp_create_game::convert_to_game_filtered_index(const unsigned int initial_index)
{
const std::vector<size_t>& filtered_indices = create_engine_.get_filtered_level_indices(create_engine_.current_level_type());
return std::find(filtered_indices.begin(), filtered_indices.end(), initial_index) - filtered_indices.begin();
return std::distance(filtered_indices.begin(), std::find(filtered_indices.begin(), filtered_indices.end(), initial_index));
}

void mp_create_game::update_details(window& win)
Expand Down
2 changes: 1 addition & 1 deletion src/gui/dialogs/multiplayer/mp_options_helper.cpp
Expand Up @@ -288,7 +288,7 @@ void mp_options_helper::display_custom_options(const std::string& type, int node
auto iter = std::find(combo_values.begin(), combo_values.end(), val.str());

if(iter != combo_values.end()) {
menu->set_selected(iter - combo_values.begin());
menu->set_selected(std::distance(combo_values.begin(), iter));
}

menu->connect_click_handler(
Expand Down
10 changes: 5 additions & 5 deletions src/gui/dialogs/preferences_dialog.cpp
Expand Up @@ -135,8 +135,8 @@ void preferences_dialog::set_resolution_list(menu_button& res_list, CVideo& vide
options.push_back(std::move(option));
}

const unsigned current_res = std::find(resolutions_.begin(), resolutions_.end(),
video.current_resolution()) - resolutions_.begin();
const unsigned current_res = std::distance(resolutions_.begin(), std::find(resolutions_.begin(), resolutions_.end(),
video.current_resolution()));

res_list.set_values(options, current_res);
}
Expand Down Expand Up @@ -314,7 +314,7 @@ void preferences_dialog::post_build(window& window)
[&](widget& w) { disable_widget_on_toggle<slider>(window, w, "turbo_slider"); }, true);

const auto accl_load = [this]()->int {
return std::find(accl_speeds_.begin(), accl_speeds_.end(), turbo_speed()) - accl_speeds_.begin() + 1;
return std::distance(accl_speeds_.begin() + 1, std::find(accl_speeds_.begin(), accl_speeds_.end(), turbo_speed()));
};

const auto accl_save = [this](int i) {
Expand Down Expand Up @@ -652,8 +652,8 @@ void preferences_dialog::post_build(window& window)
option_ids.push_back(choice["id"]);
}

const unsigned selected = std::find(option_ids.begin(), option_ids.end(),
get(pref_name, option["default"].str())) - option_ids.begin();
const unsigned selected = std::distance(option_ids.begin(), std::find(option_ids.begin(), option_ids.end(),
get(pref_name, option["default"].str())));

menu_button* setter_widget = build_single_widget_instance<menu_button>("menu_button");
setter_widget->set_id("setter");
Expand Down
4 changes: 2 additions & 2 deletions src/gui/widgets/chatbox.cpp
Expand Up @@ -429,9 +429,9 @@ lobby_chat_window* chatbox::find_or_create_window(const std::string& name,

void chatbox::close_window_button_callback(std::string room_name, bool& handled, bool& halt)
{
const int index = std::find_if(open_windows_.begin(), open_windows_.end(),
const int index = std::distance(open_windows_.begin(), std::find_if(open_windows_.begin(), open_windows_.end(),
[&room_name](const lobby_chat_window& room) { return room.name == room_name; }
) - open_windows_.begin();
));

close_window(index);

Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/tree_view.cpp
Expand Up @@ -80,7 +80,7 @@ int tree_view::remove_node(tree_view_node* node)

assert(node_itor != siblings.end());

const int position = node_itor - siblings.begin();
const int position = std::distance(siblings.begin(), node_itor);

siblings.erase(node_itor);

Expand Down
2 changes: 1 addition & 1 deletion src/menu_events.cpp
Expand Up @@ -1674,7 +1674,7 @@ void console_handler::do_choose_level()
}
}
std::sort(options.begin(), options.end());
int choice = std::lower_bound(options.begin(), options.end(), next) - options.begin();
int choice = std::distance(options.begin(), std::lower_bound(options.begin(), options.end(), next));
{
gui2::dialogs::simple_item_selector dlg(_("Choose Scenario (Debug!)"), "", options);
dlg.set_selected_index(choice);
Expand Down
2 changes: 1 addition & 1 deletion src/recall_list_manager.cpp
Expand Up @@ -70,7 +70,7 @@ size_t recall_list_manager::find_index(const std::string & unit_id) const
std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(),
[&unit_id](const unit_ptr & ptr) { return ptr->id() == unit_id; });

return it - recall_list_.begin();
return std::distance(recall_list_.begin(), it);
}

unit_ptr recall_list_manager::extract_if_matches_id(const std::string &unit_id)
Expand Down
2 changes: 1 addition & 1 deletion src/scripting/game_lua_kernel.cpp
Expand Up @@ -3619,7 +3619,7 @@ int game_lua_kernel::intf_set_time_of_day(lua_State * L)
if (lua_isstring(L, 2)) {
area_id = lua_tostring(L, 1);
std::vector<std::string> area_ids = tod_man().get_area_ids();
area_i = std::find(area_ids.begin(), area_ids.end(), area_id) - area_ids.begin();
area_i = std::distance(area_ids.begin(), std::find(area_ids.begin(), area_ids.end(), area_id));
if(area_i >= area_ids.size()) {
return luaL_argerror(L, 1, "invalid time area ID");
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/metrics.cpp
Expand Up @@ -85,7 +85,7 @@ void metrics::record_sample(const simple_wml::string_span& name,
if(samples_.size() > 30) {
return;
}
int index = isample - samples_.begin();
int index = std::distance(samples_.begin(), isample);
simple_wml::string_span dup_name(name.duplicate());
sample new_sample;
new_sample.name = dup_name;
Expand Down
8 changes: 4 additions & 4 deletions src/server/server.cpp
Expand Up @@ -139,7 +139,7 @@ static bool make_delete_diff(const simple_wml::node& src,
if(itor == children.end()) {
return false;
}
const int index = itor - children.begin();
const int index = std::distance(children.begin(), itor);
simple_wml::node& del = top->add_child("delete_child");
del.set_attr_int("index", index);
del.add_child(type);
Expand Down Expand Up @@ -171,7 +171,7 @@ static bool make_change_diff(const simple_wml::node& src,

simple_wml::node& diff = *top;
simple_wml::node& del = diff.add_child("delete_child");
const int index = itor - children.begin();
const int index = std::distance(children.begin(), itor);
del.set_attr_int("index", index);
del.add_child(type);

Expand Down Expand Up @@ -1168,7 +1168,7 @@ void server::cleanup_game(game* game_ptr)
const simple_wml::node::child_list::const_iterator g =
std::find(games.begin(), games.end(), game_ptr->description());
if (g != games.end()) {
const size_t index = g - games.begin();
const size_t index = std::distance(games.begin(), g);
gamelist->remove_child("game", index);
} else {
// Can happen when the game ends before the scenario was transferred.
Expand Down Expand Up @@ -1634,7 +1634,7 @@ void server::remove_player(socket_ptr socket)
g->remove_player(socket, true, false);

const simple_wml::node::child_list& users = games_and_users_list_.root().children("user");
const size_t index = std::find(users.begin(), users.end(), iter->info().config_address()) - users.begin();
const size_t index = std::distance(users.begin(), std::find(users.begin(), users.end(), iter->info().config_address()));

// Notify other players in lobby
simple_wml::document diff;
Expand Down
6 changes: 3 additions & 3 deletions src/server/simple_wml.cpp
Expand Up @@ -497,13 +497,13 @@ void node::remove_child(const string_span& name, size_t index)
return;
}

remove_ordered_child(itor - children_.begin(), index);
remove_ordered_child(std::distance(children_.begin(), itor), index);

debug_delete(list[index]);
list.erase(list.begin() + index);

if(list.empty()) {
remove_ordered_child_list(itor - children_.begin());
remove_ordered_child_list(std::distance(children_.begin(), itor));
children_.erase(itor);
}
}
Expand Down Expand Up @@ -659,7 +659,7 @@ int node::get_children(const string_span& name)
{
for(child_map::iterator i = children_.begin(); i != children_.end(); ++i) {
if(i->first == name) {
return i - children_.begin();
return std::distance(children_.begin(), i);
}
}

Expand Down

0 comments on commit 4ccb962

Please sign in to comment.