1 #include "selection_util.hpp"
2 #include <assert.h>
3
4 namespace horizon {
sel_count_type(const std::set<SelectableRef> & sel,ObjectType type)5 size_t sel_count_type(const std::set<SelectableRef> &sel, ObjectType type)
6 {
7 return std::count_if(sel.begin(), sel.end(), [type](const auto &a) { return a.type == type; });
8 }
9
sel_find_one(const std::set<SelectableRef> & sel,ObjectType type)10 const SelectableRef &sel_find_one(const std::set<SelectableRef> &sel, ObjectType type)
11 {
12 auto r = std::find_if(sel.begin(), sel.end(), [type](const auto &a) { return a.type == type; });
13 if (r == sel.end())
14 throw std::runtime_error("selectable not found");
15 return *r;
16 }
17
sel_find_exactly_one(const std::set<SelectableRef> & sel,ObjectType type)18 const SelectableRef *sel_find_exactly_one(const std::set<SelectableRef> &sel, ObjectType type)
19 {
20 const SelectableRef *s = nullptr;
21 for (const auto &it : sel) {
22 if (it.type == type) {
23 if (s == nullptr)
24 s = ⁢
25 else
26 return nullptr;
27 }
28 }
29 return s;
30 }
31
32 template <class T, class Comp, class Alloc, class Predicate>
discard_if(std::set<T,Comp,Alloc> & c,Predicate pred)33 void discard_if(std::set<T, Comp, Alloc> &c, Predicate pred)
34 {
35 for (auto it{c.begin()}, end{c.end()}; it != end;) {
36 if (pred(*it)) {
37 it = c.erase(it);
38 }
39 else {
40 ++it;
41 }
42 }
43 }
44
sel_erase_type(std::set<SelectableRef> & sel,ObjectType type)45 void sel_erase_type(std::set<SelectableRef> &sel, ObjectType type)
46 {
47 discard_if(sel, [type](const auto &a) { return a.type == type; });
48 }
49
50 } // namespace horizon
51