2 #include <score/model/IdentifiedObject.hpp>
3 #include <score/tools/std/IndirectContainer.hpp>
5 #include <ossia/detail/hash_map.hpp>
15 template <
class Element,
class Model = Element,
bool Order = false>
31 template <
typename Element,
typename Model>
32 requires std::is_base_of_v<IdentifiedObject<Model>, Element>
36 using model_type = Model;
37 using order_t = std::list<Element*>;
39 = ossia::hash_map<Id<Model>, std::pair<Element*, typename order_t::iterator>>;
43 using value_type = Element;
58 for(
auto elt : m_order)
64 OSSIA_INLINE
auto& ordered() INLINE_EXPORT {
return m_order; }
68 return score::make_indirect_iterator(this->m_order.begin());
72 return score::make_indirect_iterator(this->m_order.rbegin());
76 return score::make_indirect_iterator(this->m_order.cbegin());
80 return score::make_indirect_iterator(this->m_order.end());
84 return score::make_indirect_iterator(this->m_order.rend());
88 return score::make_indirect_iterator(this->m_order.cend());
91 OSSIA_INLINE std::size_t size()
const INLINE_EXPORT {
return m_map.size(); }
93 bool empty()
const INLINE_EXPORT {
return m_map.empty(); }
95 std::vector<Element*> as_vec()
const INLINE_EXPORT
97 return std::vector<Element*>(m_order.begin(), m_order.end());
105 void insert(value_type* t) INLINE_EXPORT
107 SCORE_ASSERT(m_map.find(t->id()) == m_map.end());
108 m_order.push_front(t);
109 m_map.insert({t->id(), {t, m_order.begin()}});
112 void remove(
typename map_t::iterator it) INLINE_EXPORT
116 if(it != this->m_map.end())
118 m_order.erase(it->second.second);
122 void remove(
typename map_t::const_iterator it) INLINE_EXPORT
126 if(it != this->m_map.end())
128 m_order.erase(it->second.second);
133 void remove(
const Id<Model>&
id) INLINE_EXPORT { remove(m_map.find(
id)); }
135 void clear() INLINE_EXPORT
146 auto it = this->m_map.find(
id);
147 if(it != this->m_map.end())
149 return score::make_indirect_iterator(
150 (
typename order_t::const_iterator)it->second.second);
154 return score::make_indirect_iterator(this->m_order.end());
158 Element& at(
const Id<Model>&
id)
const INLINE_EXPORT
162 SCORE_ASSERT(
id.m_ptr->parent() == this->m_map.find(
id)->second.first->parent());
163 return safe_cast<Element&>(*
id.m_ptr);
165 auto item = this->m_map.find(
id);
166 SCORE_ASSERT(item != this->m_map.end());
168 id.m_ptr = item->second.first;
169 return safe_cast<Element&>(*item->second.first);
176 template <
typename Element,
typename Model>
177 requires std::is_base_of_v<IdentifiedObject<Model>, Element>
181 using model_type = Model;
182 using map_t = ossia::hash_map<Id<Model>, Element*>;
185 using value_type = Element;
199 for(
const auto& elt : m_map)
207 return score::make_indirect_map_iterator(this->m_map.begin());
217 return score::make_indirect_map_iterator(this->m_map.cbegin());
221 return score::make_indirect_map_iterator(this->m_map.end());
231 return score::make_indirect_map_iterator(this->m_map.cend());
234 OSSIA_INLINE std::size_t size()
const INLINE_EXPORT {
return m_map.size(); }
236 OSSIA_INLINE
bool empty()
const INLINE_EXPORT {
return m_map.empty(); }
238 std::vector<Element*> as_vec()
const INLINE_EXPORT
240 std::vector<Element*> e;
241 e.reserve(m_map.size());
242 for(
auto& [key, val] : m_map)
252 e.reserve(m_map.size());
253 for(
auto& [key, val] : m_map)
260 void insert(value_type* t) INLINE_EXPORT
262 SCORE_ASSERT(m_map.find(t->id()) == m_map.end());
263 m_map.insert({t->id(), t});
266 void remove(
typename map_t::iterator it) INLINE_EXPORT
269 if(it != this->m_map.end())
274 void remove(
typename map_t::const_iterator it) INLINE_EXPORT
277 if(it != this->m_map.end())
283 void remove(
const Id<Model>&
id) INLINE_EXPORT { remove(m_map.find(
id)); }
285 void clear() INLINE_EXPORT
295 auto it = this->m_map.find(
id);
296 if(it != this->m_map.end())
298 return score::make_indirect_map_iterator(it);
302 return score::make_indirect_map_iterator(this->m_map.end());
306 Element& at(
const Id<Model>&
id)
const INLINE_EXPORT
310 SCORE_ASSERT(
id.m_ptr->parent() == this->m_map.find(
id)->second->parent());
311 return safe_cast<Element&>(*
id.m_ptr);
313 auto item = this->m_map.find(
id);
314 SCORE_ASSERT(item != this->m_map.end());
316 id.m_ptr = item->second;
317 return safe_cast<Element&>(*item->second);
324 template <
typename Element,
typename Model>
329 using model_type = Model;
330 ossia::hash_map<Id<Model>, Element*> m_map;
332 std::vector<Element*> as_vec() const INLINE_EXPORT
334 std::vector<Element*> v;
335 const auto N = m_map.size();
339 v.push_back(e.second);
344 OSSIA_INLINE
auto begin() const INLINE_EXPORT
346 return score::make_indirect_map_iterator(this->m_map.begin());
348 OSSIA_INLINE
auto rbegin() const INLINE_EXPORT
350 return score::make_indirect_map_iterator(this->m_map.begin());
352 OSSIA_INLINE
auto cbegin() const INLINE_EXPORT
354 return score::make_indirect_map_iterator(this->m_map.cbegin());
356 OSSIA_INLINE
auto end() const INLINE_EXPORT
358 return score::make_indirect_map_iterator(this->m_map.end());
360 OSSIA_INLINE
auto rend() const INLINE_EXPORT
362 return score::make_indirect_map_iterator(this->m_map.end());
364 OSSIA_INLINE
auto cend() const INLINE_EXPORT
366 return score::make_indirect_map_iterator(this->m_map.cend());
369 auto find(
const Id<Model>&
id)
const INLINE_EXPORT
371 return score::make_indirect_map_iterator(this->m_map.find(
id));
374 void insert(Element* t) INLINE_EXPORT
376 SCORE_ASSERT(m_map.find(t->id()) == m_map.end());
377 m_map.insert({t->id(), t});
380 void erase(
const Id<Model>&
id) INLINE_EXPORT
382 auto it = m_map.find(
id);
383 if(it != m_map.end())
385 auto ptr = it->second;
391 void remove_all() INLINE_EXPORT
400 auto& at(
const Id<Model>&
id)
const INLINE_EXPORT
402 auto item = this->m_map.find(
id);
403 SCORE_ASSERT(item != this->m_map.end());
404 return *item->second;
A map to access child objects through their id.
Definition: IdentifiedObjectMap.hpp:16
The id_base_t class.
Definition: Identifier.hpp:57
Definition: IndirectContainer.hpp:129
Definition: IndirectContainer.hpp:10
Definition: IndirectContainer.hpp:89