19 using entry_type = std::pair<std::string, T>;
20 using const_iterator =
typename std::vector<entry_type>::const_iterator;
21 using const_reverse_iterator =
typename std::vector<entry_type>::const_reverse_iterator;
24 return entries_.size();
28 return entries_.empty();
31 const T& get(
size_t index)
const {
32 return entries_[index].second;
35 std::string get_name(
size_t index)
const {
36 return entries_[index].first;
39 std::vector<std::string> get_names()
const {
40 std::vector<std::string> names;
41 names.reserve(entries_.size());
42 std::transform(entries_.begin(), entries_.end(), std::back_inserter(names), [](
const entry_type& entry) { return entry.first; });
46 bool add(
const std::string& name,
const T&
object) {
47 if(index_by_name_.find(name) != index_by_name_.end())
50 index_by_name_[name] = entries_.size();
51 entries_.push_back({ name,
object });
57 index_by_name_.clear();
60 const_iterator begin()
const noexcept {
61 return entries_.begin();
64 const_iterator end()
const noexcept {
65 return entries_.end();
68 const_reverse_iterator rbegin()
const noexcept {
69 return entries_.rbegin();
72 const_reverse_iterator rend()
const noexcept {
73 return entries_.rend();
76 const_iterator cbegin()
const noexcept {
80 const_iterator cend()
const noexcept {
84 const_reverse_iterator crbegin()
const noexcept {
88 const_reverse_iterator crend()
const noexcept {
92 const_iterator find(
const std::string& name)
const {
93 auto it = index_by_name_.find(name);
94 if(it != index_by_name_.end())
95 return entries_.begin() + it->second;
96 return entries_.end();
99 template<
typename UnaryPredicate>
100 const_iterator find_first(UnaryPredicate predicate)
const {
101 for(
auto it = entries_.begin(); it != entries_.end(); ++it) {
102 if(predicate(it->first, it->second))
105 return entries_.end();
108 template<
typename UnaryPredicate>
109 std::vector<const_iterator> find_all(UnaryPredicate predicate)
const {
110 std::vector<const_iterator> result;
111 for(
auto it = entries_.begin(); it != entries_.end(); ++it) {
112 if(predicate(it->first, it->second))
113 result.push_back(it);
119 std::vector<entry_type> entries_;
120 std::map<std::string, size_t> index_by_name_;