Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Creates node representing virus of given id by constructing it from one parent.
- // Throws VirusAlreadyCreated if virus of given id already exists.
- // Throws VirusNotFound if given parent doesn't exist.
- void create(typename Virus::id_type const &id, typename Virus::id_type const &parent_id)
- {
- if (exists(id)) {
- throw VirusAlreadyCreated();
- }
- if (!exists(parent_id)) {
- throw VirusNotFound();
- }
- // Creates new virus as a pair of id and smart pointer to its wrapper to be inserted to virus_map:
- std::pair<typename Virus::id_type, std::shared_ptr<VirusGenealogyWrapper<Virus>>>
- n(id, std::make_shared<VirusGenealogyWrapper<Virus> >(id));
- // Inserts new pair into virus_map;
- virus_map.insert(n); // strong guarantee, if fails, nothing changes
- // Inserts new parent into the copy of id's parents set
- std::set<std::shared_ptr<VirusGenealogyWrapper<Virus>>> parents(n.second->parents);
- parents.insert(virus_map.find(parent_id)->second);
- // Inserts new child into the copy of id's parent's children set:
- std::set<std::shared_ptr<VirusGenealogyWrapper<Virus>>> parent_children(virus_map.find(parent_id)->second->children);
- parent_children.insert(virus_map.find(id)->second);
- // If none of above threw the exception, it means real sets can now be updated by no-throw swap:
- n.second->parents.swap(parents);
- virus_map.find(parent_id)->second->children.swap(parent_children);
- }
Add Comment
Please, Sign In to add comment