Container associating units to locations. More...
#include <map.hpp>
Classes | |
struct | const_iter_types |
struct | iterator_base |
struct | standard_iter_types |
struct | unit_pod |
The pointer to the unit and a reference counter to record the number of extant iteratorspointing to this unit. More... | |
Public Types | |
typedef iterator_base< standard_iter_types > | unit_iterator |
typedef iterator_base< const_iter_types > | const_unit_iterator |
typedef unit_iterator | iterator |
typedef const_unit_iterator | const_iterator |
using | umap_retval_pair_t = std::pair< unit_iterator, bool > |
Public Member Functions | |
unit_map () | |
unit_map (const unit_map &that) | |
unit_map & | operator= (const unit_map &that) |
~unit_map () | |
void | swap (unit_map &o) |
unit_iterator | find (std::size_t id) |
unit_iterator | find (const map_location &loc) |
const_unit_iterator | find (const map_location &loc) const |
const_unit_iterator | find (std::size_t id) const |
template<typename T > | |
unit_ptr | find_unit_ptr (const T &val) |
template<typename T > | |
unit_const_ptr | find_unit_ptr (const T &val) const |
unit_iterator | find_leader (int side) |
const_unit_iterator | find_leader (int side) const |
unit_iterator | find_first_leader (int side) |
std::vector< unit_iterator > | find_leaders (int side) |
std::vector< const_unit_iterator > | find_leaders (int side) const |
std::size_t | count (const map_location &loc) const |
unit_iterator | begin () |
const_unit_iterator | begin () const |
unit_iterator | end () |
const_unit_iterator | end () const |
std::size_t | size () const |
std::size_t | num_iters () const |
bool | empty () const |
void | clear (bool force=false) |
umap_retval_pair_t | add (const map_location &l, const unit &u) |
Adds a copy of unit u at location l of the map. More... | |
umap_retval_pair_t | insert (unit_ptr p) |
Inserts the unit pointed to by p into the map. More... | |
umap_retval_pair_t | move (const map_location &src, const map_location &dst) |
Moves a unit from location src to location dst. More... | |
umap_retval_pair_t | replace (const map_location &l, unit_ptr p) |
Works like unit_map::add; but l is emptied first, if needed. More... | |
std::size_t | erase (const map_location &l) |
Erases the unit at location l, if any. More... | |
template<typename T > | |
std::size_t | erase (const T &iter) |
Erases a unit given by a pointer or an iterator. More... | |
unit_ptr | extract (const map_location &loc) |
Extracts a unit from the map. More... | |
bool | self_check () const |
Checks invariants. More... | |
bool | has_unit (const unit *const u) const |
Is the unit in the map? More... | |
bool | has_unit_at (const map_location &loc) const |
Tests whether a unit exists at the given location. More... | |
Private Types | |
typedef std::map< std::size_t, unit_pod > | umap |
typedef std::unordered_map< map_location, umap::iterator > | lmap |
Map of location to umap iterator. More... | |
Private Member Functions | |
umap::iterator | begin_core () const |
bool | is_valid (const umap::const_iterator &i) const |
bool | is_valid (const lmap::const_iterator &i) const |
bool | is_found (const umap::const_iterator &i) const |
bool | is_found (const lmap::const_iterator &i) const |
template<typename X > | |
unit_map::unit_iterator | make_unit_iterator (const X &i) |
template<typename X > | |
unit_map::const_unit_iterator | make_const_unit_iterator (const X &i) const |
Private Attributes | |
umap | umap_ |
underlying_id -> unit_pod. More... | |
lmap | lmap_ |
location -> umap::iterator. More... | |
Container associating units to locations.
The indirection location -> underlying_id -> unit ensures that iterators stay valid even if WML modifies or moves units on the fly. They even stay valid if a unit is erased from the map and another unit with the same underlying id is inserted in the map. In other words, it is a doubly indexed ordered map with persistent iterators (that never invalidate).
The unordered map provides O(1) find times. The ordered map ensures ordering of units by underlying_id. The reference counting is what guarantees the persistent iterators. Storing an iterator only prevents that dead unit's id-map entry from being recovered.
Use the std::pair<iterator, bool> format which checks the preconditions and returns false (second) to indicate failure and no change to the unit_map. True indicates success along with the new iterator (first).
Storing the result iterator prevents the old iterator from entering the fallback recovery code. This is faster than the old methodology of find to check if empty, insert, and then find to check for success. It is the same method as std::map uses, the C++ way.
unit_iterator i; //results are stored here
ADD: std::pair<unit_iterator, bool> try_add(units->add(loc, unit)); if(try_add.second) { i = try_add.first; }
MOVE: std::pair<unit_iterator, bool> try_add(units->move(src, dst)); if(try_add.second) { i = try_add.first; }
INSERT: std::pair<unit_iterator, bool> try_add(units->insert(unitp)); if(try_add.second) { i = try_add.first; }
REPLACE (preferred over erase and then add) std::pair<unit_iterator, bool> try_add(units->move(loc, unit)); if(try_add.second){ i = try_add.first; }
typedef unit_iterator unit_map::iterator |
|
private |
|
private |
using unit_map::umap_retval_pair_t = std::pair<unit_iterator, bool> |
unit_map::unit_map | ( | const unit_map & | that | ) |
unit_map::umap_retval_pair_t unit_map::add | ( | const map_location & | l, |
const unit & | u | ||
) |
Adds a copy of unit u at location l of the map.
Definition at line 77 of file map.cpp.
References unit::clone(), insert(), p, and self_check().
Referenced by BOOST_AUTO_TEST_CASE(), editor::editor_action_unit::perform_without_undo(), temporary_unit_placer::temporary_unit_placer(), and unit_map().
|
inline |
Definition at line 419 of file map.hpp.
References begin_core(), and make_unit_iterator().
Referenced by ai::readonly_context_impl::calculate_moves(), actions::check_recall_location(), actions::check_recruit_location(), ai::ai_default_rca::move_to_targets_phase::choose_move(), events::mouse_handler::cycle_units(), ai::stage_unit_formulas::do_play_stage(), ai::ai_default_rca::goto_phase::evaluate(), ai::ai_default_rca::get_healing_phase::evaluate(), ai::ai_default_rca::retreat_phase::evaluate(), wfl::move_candidate_action::evaluate(), wfl::attack_candidate_action::evaluate(), find_first_leader(), find_leader(), find_leaders(), ai::default_ai_context_impl::find_targets(), unit_filter::first_match_on_map(), actions::get_recalls(), actions::get_recruits(), ai::formula_ai::get_value(), ai::ai_default_rca::get_villages_phase::get_villages(), impl_unit_type_next(), unit_map::iterator_base< iter_types >::operator--(), game_board::replace_map(), events::mouse_handler::select_hex(), gui2::dialogs::unit_mode_controller::show_array(), gui2::dialogs::unit_mode_controller::show_unit(), gui2::dialogs::show_unit_list(), gui2::dialogs::unit_mode_controller::show_var(), game_state::side_can_recruit_on(), editor::map_context::to_config(), and verify().
|
inline |
Definition at line 424 of file map.hpp.
References begin_core(), and make_const_unit_iterator().
|
private |
void unit_map::clear | ( | bool | force = false | ) |
Definition at line 243 of file map.cpp.
References DBG_NG, i, is_valid(), lmap_, num_iters(), and umap_.
Referenced by ~unit_map().
|
inline |
Definition at line 414 of file map.hpp.
References lmap_.
Referenced by ai::ai_default_rca::move_cost_calculator::cost(), ai::ai_default_rca::move_leader_to_keep_phase::evaluate(), ai::attack_analysis::execute_self(), ai::default_ai_context_impl::find_targets(), ai::ai_default_rca::move_to_targets_phase::form_group(), wfl::attack_map_callable::get_value(), game_display::highlight_hex(), ai::ai_default_rca::move_to_targets_phase::move_group(), and verify().
|
inline |
Definition at line 446 of file map.hpp.
References lmap_.
Referenced by editor::editor_controller::can_execute_command().
|
inline |
Definition at line 429 of file map.hpp.
References make_unit_iterator(), and umap_.
Referenced by unit::ability_active(), ai::ai_default_rca::move_to_targets_phase::access_points(), scoped_xy_unit::activate(), ai::attack_analysis::analyze(), wb::move::apply_temp_modifier(), backstab_check(), BOOST_AUTO_TEST_CASE(), ai::readonly_context_impl::calculate_moves(), editor::editor_controller::can_execute_command(), editor::editor_controller::change_unit_id(), actions::check_recall_location(), actions::check_recruit_location(), ai::ai_default_rca::move_to_targets_phase::choose_move(), actions::shroud_clearer::clear_unit(), editor::mouse_action_unit::click_left(), wfl::attack_map_callable::collect_possible_attacks(), ai::default_ai_context_impl::count_free_hexes_in_castle(), events::mouse_handler::cycle_units(), ai::ai_default_rca::aspect_attacks_base::do_attack_analysis(), ai::stage_unit_formulas::do_play_stage(), editor::mouse_action_unit::drag_end_left(), display::draw_invalidated(), game_display::draw_movement_info(), ai::ai_default_rca::goto_phase::evaluate(), ai::ai_default_rca::get_healing_phase::evaluate(), ai::ai_default_rca::retreat_phase::evaluate(), wfl::move_candidate_action::evaluate(), wfl::attack_candidate_action::evaluate(), ai::ai_default_rca::get_villages_phase::execute(), wfl::set_unit_var_callable::execute_self(), ai::attack_analysis::execute_self(), unit_filter_impl::unit_filter_compound::fill(), find_first_leader(), find_leader(), find_leaders(), ai::default_ai_context_impl::find_targets(), events::mouse_handler::find_unit(), find_unit_ptr(), pathfind::find_vacant_tile(), game_board::find_visible_unit(), actions::shroud_clearer::fire_events(), unit_filter::first_match_on_map(), footsteps_images(), ai::ai_default_rca::move_to_targets_phase::form_group(), unit::get_abilities(), unit::get_ability_bool(), tod_manager::get_illuminated_time_of_day(), gui2::dialogs::game_stats::get_leader(), actions::get_recalls(), actions::get_recruits(), gui2::dialogs::get_title_suffix(), ai::formula_ai::get_value(), ai::ai_default_rca::get_villages_phase::get_villages(), game_board::get_visible_unit(), has_unit_at(), attack_type::has_weapon_ability(), impl_unit_type_next(), ai::default_recruitment::recruitment::is_enemy_in_radius(), editor::mouse_action_unit::move(), ai::ai_default_rca::move_to_targets_phase::move_group(), actions::move_unit_and_record(), wb::manager::on_mouseover_change(), game_events::filter_attack::operator()(), unit_map::iterator_base< iter_types >::operator++(), editor::editor_action_unit_delete::perform(), editor::editor_action_unit_facing::perform_without_undo(), ai::readonly_context_impl::power_projection(), ai::ai_default_rca::move_to_targets_phase::rate_group(), editor::editor_controller::rename_unit(), game_board::replace_map(), actions::move_unit_spectator::reset(), unit_display::reset_helpers(), events::mouse_handler::select_hex(), ai::formula_ai::shortest_path_calculator(), gui2::dialogs::unit_mode_controller::show_list(), gui2::dialogs::show_unit_list(), gui2::dialogs::team_mode_controller::show_units(), game_state::side_can_recruit_on(), attack_type::special_active_impl(), editor::map_context::to_config(), actions::undo::move_action::undo(), actions::undo::recall_action::undo(), actions::undo::recruit_action::undo(), editor::editor_controller::unit_description(), unit_display::unit_recruited(), unit_map::iterator_base< iter_types >::valid_entry(), unit_map::iterator_base< iter_types >::valid_exit(), verify(), and attack_type::weapon_specials_impl_adj().
|
inline |
Definition at line 434 of file map.hpp.
References make_const_unit_iterator(), and umap_.
std::size_t unit_map::erase | ( | const map_location & | l | ) |
Erases the unit at location l, if any.
Definition at line 288 of file map.cpp.
References extract(), and self_check().
Referenced by advance_unit(), erase(), game_lua_kernel::intf_erase_unit(), game_lua_kernel::intf_put_recall_unit(), editor::editor_action_unit_delete::perform_without_undo(), game_lua_kernel::put_unit_helper(), replace(), game_board::replace_map(), SYNCED_COMMAND_HANDLER_FUNCTION(), actions::undo::recall_action::undo(), actions::undo::recruit_action::undo(), and temporary_unit_placer::~temporary_unit_placer().
std::size_t unit_map::erase | ( | const T & | iter | ) |
unit_ptr unit_map::extract | ( | const map_location & | loc | ) |
Extracts a unit from the map.
The unit is no longer owned by the map. It can be reinserted later, if needed.
Definition at line 258 of file map.cpp.
References DBG_NG, i, lmap_, self_check(), and umap_.
Referenced by ai::attack_analysis::analyze(), wb::suppose_dead::apply_temp_modifier(), BOOST_AUTO_TEST_CASE(), erase(), game_lua_kernel::intf_extract_unit(), wb::recall::remove_temp_modifier(), and wb::recruit::remove_temp_modifier().
unit_map::unit_iterator unit_map::find | ( | const map_location & | loc | ) |
Definition at line 313 of file map.cpp.
References lmap_, and self_check().
|
inline |
unit_map::unit_iterator unit_map::find | ( | std::size_t | id | ) |
Definition at line 301 of file map.cpp.
References i, self_check(), and umap_.
Referenced by unit::ability_active(), ai::ai_default_rca::move_to_targets_phase::access_points(), scoped_xy_unit::activate(), unit_creator::add_unit(), replay::add_unit_checksum(), advance_unit(), ai::attack_analysis::analyze(), wb::move::apply_temp_modifier(), attack_unit_and_advance(), backstab_check(), battle_context::battle_context(), ai::readonly_context_impl::best_defensive_position(), BOOST_AUTO_TEST_CASE(), editor::editor_controller::can_execute_command(), editor::editor_controller::change_unit_id(), ai::readonly_context_impl::check_attack_action(), actions::check_recall_location(), actions::check_recruit_location(), wb::move::check_validity(), wb::suppose_dead::check_validity(), ai::ai_default_rca::move_to_targets_phase::choose_move(), actions::shroud_clearer::clear_unit(), editor::mouse_action_unit::click_left(), wfl::attack_map_callable::collect_possible_attacks(), ai::default_ai_context_impl::count_free_hexes_in_castle(), wfl::gamestate::DEFINE_WFL_FUNCTION(), ai::ai_default_rca::aspect_attacks_base::do_attack_analysis(), ai::recall_result::do_check_after(), ai::recruit_result::do_check_after(), ai::stopunit_result::do_check_after(), ai::attack_result::do_check_before(), ai::attack_result::do_execute(), ai::move_result::do_execute(), ai::stopunit_result::do_execute(), editor::editor_controller::do_execute_command(), do_replay_handle(), editor::mouse_action_unit::drag_end_left(), display::draw_invalidated(), game_display::draw_movement_info(), ai::ai_default_rca::goto_phase::evaluate(), ai::ai_default_rca::combat_phase::evaluate(), ai::ai_default_rca::move_leader_to_keep_phase::evaluate(), ai::ai_default_rca::get_healing_phase::evaluate(), ai::ai_default_rca::get_villages_phase::execute(), ai::ai_default_rca::leader_shares_keep_phase::execute(), wb::attack::execute(), wb::move::execute(), wb::recruit::execute(), ai::readwrite_context_impl::execute_attack_action(), wfl::set_unit_var_callable::execute_self(), ai::attack_analysis::execute_self(), unit_filter_impl::unit_filter_compound::fill(), find(), game_board::find_unit(), find_unit_ptr(), pathfind::find_vacant_tile(), ai::ai_default_rca::get_villages_phase::find_villages(), game_board::find_visible_unit(), actions::shroud_clearer::fire_events(), footsteps_images(), ai::ai_default_rca::move_to_targets_phase::form_group(), lua_unit::get(), unit::get_abilities(), unit::get_ability_bool(), editor::editor_controller::get_action_state(), tod_manager::get_illuminated_time_of_day(), actions::get_recalls(), actions::get_recruits(), lua_unit::get_shared(), wb::manager::get_temp_move_unit(), ai::move_result::get_unit(), ai::stopunit_result::get_unit(), game_events::entity_location::get_unit(), wb::move::get_unit(), wb::suppose_dead::get_unit(), display_context::get_visible_unit(), display_context::get_visible_unit_shared_ptr(), has_unit_at(), game_board::has_visible_unit(), attack_type::has_weapon_ability(), ai::helper_advance_unit(), impl_unit_type_next(), game_lua_kernel::intf_find_cost_map(), game_lua_kernel::intf_find_path(), game_lua_kernel::intf_find_reach(), game_lua_kernel::intf_find_vision_range(), game_lua_kernel::intf_get_unit(), game_lua_kernel::intf_teleport(), ai::default_recruitment::recruitment::is_enemy_in_radius(), ai::lua_sticky_candidate_action_wrapper::lua_sticky_candidate_action_wrapper(), luaW_pushfaivariant(), pathfind::mark_route(), terrain_filter::match_internal(), unit_animation::matches(), wb::move::move(), editor::mouse_action_unit::move(), ai::ai_default_rca::move_to_targets_phase::move_group(), events::mouse_handler::move_unit_along_route(), actions::move_unit_and_record(), wb::manager::on_mouseover_change(), game_events::filter_unit::operator()(), game_events::filter_attack::operator()(), editor::editor_action_unit_delete::perform(), editor::editor_action_unit::perform_without_undo(), editor::editor_action_unit_replace::perform_without_undo(), editor::editor_action_unit_facing::perform_without_undo(), wb::manager::post_draw(), ai::readonly_context_impl::power_projection(), wb::manager::pre_draw(), ai::ai_default_rca::move_to_targets_phase::rate_group(), wb::move::remove_temp_modifier(), wb::suppose_dead::remove_temp_modifier(), editor::editor_controller::rename_unit(), unit_display::reset_helpers(), events::mouse_handler::select_hex(), wb::highlighter::set_mouseover_hex(), ai::formula_ai::shortest_path_calculator(), events::mouse_handler::show_attack_dialog(), events::mouse_handler::show_attack_options(), game_state::side_can_recruit_on(), ai::simulated_attack(), ai::simulated_stopunit(), attack_type::special_active_impl(), wb::suppose_dead::suppose_dead(), SYNCED_COMMAND_HANDLER_FUNCTION(), pathfind::teleport_map::teleport_map(), actions::undo::move_action::undo(), actions::undo::recall_action::undo(), actions::undo::recruit_action::undo(), unit_display::unit_attack(), display_context::unit_can_move(), editor::editor_controller::unit_description(), unit_display::unit_recruited(), verify(), pathfind::vision_path::vision_path(), attack_type::weapon_specials_impl_adj(), and display_context::would_be_discovered().
|
inline |
unit_map::unit_iterator unit_map::find_first_leader | ( | int | side | ) |
unit_map::unit_iterator unit_map::find_leader | ( | int | side | ) |
Definition at line 319 of file map.cpp.
References begin(), end(), and i.
Referenced by ai::ai_default_rca::get_villages_phase::evaluate(), ai::ai_default_rca::get_villages_phase::execute(), ai::ai_default_rca::move_to_targets_phase::execute(), find_leader(), unit_creator::find_location(), ai::default_ai_context_impl::find_targets(), gui2::dialogs::game_stats::get_leader(), gui2::dialogs::get_title_suffix(), ai::formula_ai::get_value(), ai::helper_check_village(), ai::readonly_context_impl::leader_can_reach_keep(), ai::readonly_context_impl::recalculate_move_maps(), game_display::scroll_to_leader(), game_board::side_change_controller(), game_board::side_drop_to(), and game_board::team_is_defeated().
|
inline |
Definition at line 403 of file map.hpp.
References find_leader().
std::vector< unit_map::unit_iterator > unit_map::find_leaders | ( | int | side | ) |
Definition at line 347 of file map.cpp.
References begin(), end(), and i.
Referenced by ai::default_recruitment::recruitment::do_combat_analysis(), ai::ai_default_rca::move_leader_to_goals_phase::evaluate(), ai::ai_default_rca::move_leader_to_keep_phase::evaluate(), ai::ai_default_rca::retreat_phase::evaluate(), ai::ai_default_rca::leader_shares_keep_phase::evaluate(), ai::default_recruitment::recruitment::evaluate(), ai::ai_default_rca::leader_shares_keep_phase::execute(), ai::default_recruitment::recruitment::execute(), find_leaders(), ai::default_recruitment::recruitment::get_cost_map_of_side(), and game_events::WML_HANDLER_FUNCTION().
std::vector< unit_map::const_unit_iterator > unit_map::find_leaders | ( | int | side | ) | const |
Definition at line 361 of file map.cpp.
References find_leaders().
|
inline |
|
inline |
bool unit_map::has_unit | ( | const unit *const | u | ) | const |
bool unit_map::has_unit_at | ( | const map_location & | loc | ) | const |
unit_map::umap_retval_pair_t unit_map::insert | ( | unit_ptr | p | ) |
Inserts the unit pointed to by p into the map.
If insertion into either the unit or location map fails, all operations are reverted.
The one oddity is that to facilitate non-invalidating iterators, the list sometimes has nullptr pointers which should be used when they correspond to uids previously used.
Definition at line 134 of file map.cpp.
References DBG_NG, ERR_NG, lmap_, make_unit_iterator(), unit::name(), p, unit_map::unit_pod::ref_count, self_check(), umap_, unit_map::unit_pod::unit, and map_location::valid().
Referenced by add(), advance_unit(), ai::attack_analysis::analyze(), wb::recall::apply_temp_modifier(), wb::recruit::apply_temp_modifier(), BOOST_AUTO_TEST_CASE(), ai::default_ai_context_impl::count_free_hexes_in_castle(), ai::default_recruitment::recruitment::do_combat_analysis(), ai::helper_place_unit(), game_lua_kernel::intf_put_unit(), editor::map_context::load_scenario(), actions::place_recruit(), wb::suppose_dead::remove_temp_modifier(), replace(), SYNCED_COMMAND_HANDLER_FUNCTION(), temporary_unit_mover::~temporary_unit_mover(), temporary_unit_placer::~temporary_unit_placer(), and temporary_unit_remover::~temporary_unit_remover().
|
inlineprivate |
|
inlineprivate |
Definition at line 556 of file map.hpp.
Referenced by is_valid(), make_const_unit_iterator(), and make_unit_iterator().
|
inlineprivate |
Definition at line 551 of file map.hpp.
References i, and is_found().
|
inlineprivate |
|
inlineprivate |
|
inlineprivate |
unit_map::umap_retval_pair_t unit_map::move | ( | const map_location & | src, |
const map_location & | dst | ||
) |
Moves a unit from location src to location dst.
Definition at line 93 of file map.cpp.
References DBG_NG, i, lmap_, make_unit_iterator(), p, and self_check().
Referenced by ai::attack_analysis::analyze(), game_lua_kernel::intf_teleport(), editor::editor_action_unit_replace::perform_without_undo(), ai::simulated_move(), temporary_unit_mover::temporary_unit_mover(), actions::undo::move_action::undo(), and temporary_unit_mover::~temporary_unit_mover().
std::size_t unit_map::num_iters | ( | ) | const |
unit_map::umap_retval_pair_t unit_map::replace | ( | const map_location & | l, |
unit_ptr | p | ||
) |
Works like unit_map::add; but l is emptied first, if needed.
Definition at line 215 of file map.cpp.
References erase(), insert(), p, and self_check().
Referenced by unit_creator::add_unit(), ai::helper_advance_unit(), lua_unit::put_map(), and SYNCED_COMMAND_HANDLER_FUNCTION().
bool unit_map::self_check | ( | ) | const |
|
inline |
Definition at line 439 of file map.hpp.
References lmap_.
Referenced by BOOST_AUTO_TEST_CASE(), and verify().
void unit_map::swap | ( | unit_map & | o | ) |
Definition at line 52 of file map.cpp.
References lmap_, num_iters(), swap(), and umap_.
Referenced by operator=(), and swap().
|
private |
|
mutableprivate |
underlying_id -> unit_pod.
This requires that underlying_id be unique (which is enforced in unit_map::insert).
Definition at line 590 of file map.hpp.
Referenced by begin_core(), clear(), end(), extract(), find(), has_unit(), insert(), is_found(), make_const_unit_iterator(), make_unit_iterator(), num_iters(), self_check(), and swap().