Added function for splicing children between config objects.

This speeds up configuration loading by 50%.
This commit is contained in:
Guillaume Melquiond 2010-08-09 15:57:32 +00:00
parent 985285e8dc
commit 0a996b70d5
3 changed files with 32 additions and 9 deletions

View file

@ -435,6 +435,28 @@ void config::clear_children(const std::string& key)
}
}
void config::splice_children(config &src, const std::string &key)
{
check_valid(src);
child_map::iterator i_src = src.children.find(key);
if (i_src == src.children.end()) return;
src.ordered_children.erase(std::remove_if(src.ordered_children.begin(),
src.ordered_children.end(), remove_ordered(key)),
src.ordered_children.end());
child_list &dst = children[key];
unsigned before = dst.size();
dst.insert(dst.end(), i_src->second.begin(), i_src->second.end());
src.children.erase(i_src);
child_map::iterator i_dst = children.find(key);
for (unsigned j = before; j < dst.size(); ++j) {
ordered_children.push_back(child_pos(i_dst, j));
}
}
void config::recursive_clear_value(const std::string& key)
{
check_valid();

View file

@ -320,6 +320,12 @@ public:
const std::string &name, const std::string &value) const;
void clear_children(const std::string& key);
/**
* Moves all the children with tag @a key from @a src to this.
*/
void splice_children(config &src, const std::string &key);
void remove_child(const std::string& key, size_t index);
void recursive_clear_value(const std::string& key);

View file

@ -1489,15 +1489,10 @@ void game_controller::load_game_cfg(const bool force)
main_transaction.lock();
// clone and put the gfx rules aside so that we can prepend the add-on
// rules to them.
/* Put the gfx rules aside so that we can prepend the add-on
rules to them. */
config core_terrain_rules;
// FIXME: there should be a canned algorithm for cloning child_list objects,
// along with the memory their elements point to... little implementation detail.
foreach (config const &p_cfg, game_config_.child_range("terrain_graphics")) {
core_terrain_rules.add_child("terrain_graphics", p_cfg);
}
game_config_.clear_children("terrain_graphics");
core_terrain_rules.splice_children(game_config_, "terrain_graphics");
// load usermade add-ons
const std::string user_campaign_dir = get_addon_campaigns_dir();
@ -1567,7 +1562,7 @@ void game_controller::load_game_cfg(const bool force)
game_config_.clear_children("lua");
game_config_.merge_children("units");
game_config_.append(core_terrain_rules);
game_config_.splice_children(core_terrain_rules, "terrain_graphics");
config& hashes = game_config_.add_child("multiplayer_hashes");
foreach (const config &ch, game_config_.child_range("multiplayer")) {