#include "alternative_registry.h" #include #include namespace alternatives { namespace { bool canonical_slots(Group& group, std::set& links, std::set& slot_names) { for (const auto& slot : group.slots) { if (!valid_id(slot.name)) { return false; } if (!valid_path(slot.link)) { return false; } if (!slot_names.insert(slot.name).second) { return false; } if (!links.insert(slot.link).second) { return false; } } std::sort(group.slots.begin(), group.slots.end(), [](const Slot& a, const Slot& b) { return a.name < b.name; }); return true; } bool canonical_attachments(Candidate& candidate, const std::set& slots) { if (candidate.attachments.size() > 16) { return false; } std::set attached; for (const auto& attachment : candidate.attachments) { if (!valid_id(attachment.slot)) { return false; } if (!slots.count(attachment.slot)) { return false; } if (!attached.insert(attachment.slot).second) { return false; } if (!valid_path(attachment.target)) { return false; } } std::sort(candidate.attachments.begin(), candidate.attachments.end(), [](const Attachment& a, const Attachment& b) { return a.slot < b.slot; }); return true; } bool canonical_candidates(Group& group, const std::set& slots) { std::set paths; for (auto& candidate : group.candidates) { if (!valid_path(candidate.path)) { return false; } if (!paths.insert(candidate.path).second) { return false; } if (!canonical_attachments(candidate, slots)) { return false; } } switch (group.mode) { case Mode::automatic: if (!group.pin.empty()) { return false; } break; case Mode::manual: if (!paths.count(group.pin)) { return false; } break; default: return false; } std::sort(group.candidates.begin(), group.candidates.end(), [](const Candidate& a, const Candidate& b) { return a.path < b.path; }); return true; } bool canonical_group(Group& group, std::set& links, std::size_t& candidates, std::size_t& slots) { if (!valid_id(group.name)) { return false; } if (!valid_path(group.master_link)) { return false; } if (!links.insert(group.master_link).second) { return false; } if (group.candidates.size() > 64) { return false; } if (group.slots.size() > 16) { return false; } candidates += group.candidates.size(); slots += group.slots.size(); if (candidates > 256 || slots > 256) { return false; } std::set slot_names; if (!canonical_slots(group, links, slot_names)) { return false; } return canonical_candidates(group, slot_names); } } Error compile_world(const std::vector& groups, World& output) { std::vector canonical = groups; output.groups.clear(); if (canonical.size() > 64) { return Error::invalid; } std::set names; std::set links; std::size_t candidates = 0; std::size_t slots = 0; for (auto& group : canonical) { if (!names.insert(group.name).second) { return Error::invalid; } if (!canonical_group(group, links, candidates, slots)) { return Error::invalid; } } std::sort(canonical.begin(), canonical.end(), [](const Group& a, const Group& b) { return a.name < b.name; }); output.groups = std::move(canonical); return Error::none; } } // namespace alternatives