9 #ifndef IMPCORE_HIERARCHY_H
10 #define IMPCORE_HIERARCHY_H
12 #include <IMP/core/core_config.h>
13 #include "internal/hierarchy_helpers.h"
20 #include <IMP/kernel/internal/utility.h>
23 #include <boost/tuple/tuple.hpp>
29 IMPCORE_BEGIN_NAMESPACE
59 return parent_ == o.parent_;
85 for (
unsigned int i = 0; i < children.size(); ++i) {
98 traits, get_default_traits());
114 if (
get_model()->get_has_attribute(get_decorator_traits().get_parent_key(),
115 get_particle_index())) {
117 get_decorator_traits().get_parent_key(), get_particle_index());
124 unsigned int get_number_of_children()
const {
126 get_decorator_traits().get_children_key(), get_particle_index())) {
129 get_particle_index())
135 Hierarchy get_child(
unsigned int i)
const {
136 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
138 get_decorator_traits().get_children_key(),
139 get_particle_index())[i],
140 get_decorator_traits());
143 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
145 get_decorator_traits().get_children_key(),
146 get_particle_index())[i];
148 GenericHierarchies get_children()
const {
149 GenericHierarchies ret(get_number_of_children());
150 for (
unsigned int i = 0; i < ret.size(); ++i) {
151 ret[i] = get_child(i);
155 void remove_child(
unsigned int i) {
156 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
157 Hierarchy c = get_child(i);
158 kernel::ParticleIndexes &pis =
get_model()->access_attribute(
159 get_decorator_traits().get_children_key(), get_particle_index());
160 pis.erase(pis.begin() + i);
162 c.get_particle_index());
164 void remove_child(Hierarchy h) { remove_child(h.get_child_index()); }
165 void clear_children() {
166 kernel::ParticleIndexes &pis =
get_model()->access_attribute(
167 get_decorator_traits().get_children_key(), get_particle_index());
168 for (
unsigned int i = 0; i < pis.size(); ++i) {
173 get_particle_index());
175 void add_child(Hierarchy h)
const {
177 get_decorator_traits().get_children_key(), get_particle_index())) {
179 ->access_attribute(get_decorator_traits().get_children_key(),
180 get_particle_index())
181 .push_back(h.get_particle_index());
184 get_decorator_traits().get_children_key(), get_particle_index(),
185 kernel::ParticleIndexes(1, h.get_particle_index()));
188 h.get_particle_index(), get_particle_index());
190 void add_child_at(Hierarchy h,
unsigned int pos) {
193 get_decorator_traits().get_children_key(), get_particle_index())) {
194 kernel::ParticleIndexes &pis =
get_model()->access_attribute(
195 get_decorator_traits().get_children_key(), get_particle_index());
196 pis.insert(pis.begin() + pos, h.get_particle_index());
199 get_decorator_traits().get_children_key(), get_particle_index(),
200 kernel::ParticleIndexes(1, h.get_particle_index()));
203 h.get_particle_index(), get_particle_index());
206 int get_child_index()
const;
208 static const HierarchyTraits &get_default_traits();
210 HierarchyTraits get_traits() {
return get_decorator_traits(); }
223 virtual bool operator()(
Hierarchy p) = 0;
240 sm_->apply_index(p.
get_model(), p.get_particle_index());
246 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
248 template <
class H,
class F,
class Out,
bool Slice = false>
251 Gather(F f, Out out) : f_(f), out_(out) {}
252 bool operator()(H p) {
256 if (Slice)
return false;
261 Out get_out()
const {
return out_; }
277 template <
class HD,
class F>
279 std::deque<HD> stack;
283 HD cur = stack.front();
287 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
288 stack.push_back(cur.get_child(i));
291 }
while (!stack.empty());
300 template <
class HD,
class F>
306 HD cur = stack.back();
310 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
311 stack.push_back(cur.get_child(i));
314 }
while (!stack.empty());
343 template <
class HD,
class F>
345 typedef std::pair<typename F::result_type, HD> DP;
346 std::deque<DP> stack;
347 stack.push_back(DP(i, d));
350 DP cur = stack.front();
352 typename F::result_type r = f(cur.second.get_particle(), cur.first);
354 for (
int i = cur.second.get_number_of_children() - 1; i >= 0; --i) {
355 stack.push_back(std::make_pair(r, cur.second.get_child(i)));
357 }
while (!stack.empty());
366 template <
class HD,
class F>
368 typedef std::pair<typename F::result_type, HD> DP;
370 stack.push_back(DP(i, d));
373 DP cur = stack.back();
375 typename F::result_type r = f(cur.second, cur.first);
377 for (
int i = cur.second.get_number_of_children() - 1; i >= 0; --i) {
378 stack.push_back(DP(r, cur.second.get_child(i)));
380 }
while (!stack.empty());
391 IMP_PRINT_TREE(out,
Hierarchy, h, n.get_number_of_children(), n.get_child,
424 template <
class H,
class Out,
class F>
426 internal::Gather<H, F, Out>
gather(f, out);
428 return gather.get_out();
438 template <
class H,
class Out,
class F>
440 internal::Gather<H, F, Out, true>
gather(f, out);
442 return gather.get_out();
449 template <
class H,
class Out,
class K,
class V>
451 internal::Gather<H, internal::MatchAttribute<K, V>, Out>
gather(
452 internal::MatchAttribute<K, V>(k, v), out);
454 return gather.get_out();
461 template <
class H,
class Out,
class K0,
class V0,
class K1,
class V1>
463 internal::Gather<H, internal::MatchAttributes<K0, V0, K1, V1>, Out>
gather(
464 internal::MatchAttributes<K0, V0, K1, V1>(k0, v0, k1, v1), out);
466 return gather.get_out();
473 template <
class HD,
class F>
475 if (f(h.get_particle()))
return h;
480 HD cur = stack.back();
483 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
484 HD hd = cur.get_child(i);
485 if (f(hd.get_particle())) {
491 }
while (!stack.empty());
501 IMPCOREEXPORT GenericHierarchies
get_leaves(Hierarchy mhd);
506 IMPCOREEXPORT GenericHierarchies
get_internal(Hierarchy mhd);
522 IMPCORE_END_NAMESPACE
F visit_breadth_first(HD d, F f)
Apply the visitor to each particle, breadth first.
Import IMP/kernel/Decorator.h in the namespace.
Hierarchies get_leaves(Hierarchy h)
A visitor for traversal of a hierarchy.
Import IMP/kernel/SingletonModifier.h in the namespace.
ParticleIndexes get_indexes(const ParticlesTemp &ps)
A nullptr-initialized pointer to an IMP Object.
A smart pointer to a ref-counted Object that is a class memeber.
HD find_breadth_first(HD h, F f)
Find the first node which matches some criteria.
Model * get_model() const
Returns the Model containing the particle.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
F visit_depth_first_with_data(HD d, F f, typename F::result_type i)
Apply functor F to each particle, traversing the hierarchy depth first.
void remove_attribute(TypeKey attribute_key, ParticleIndex particle)
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
#define IMP_DECORATOR_WITH_TRAITS_METHODS(Name, Parent, TraitsType,traits_name, default_traits)
GenericHierarchies get_all_descendants(Hierarchy mhd)
Get all the particles in the subtree.
#define IMP_DECORATOR_TRAITS_SETUP_1(Name, FirstArgumentType,first_argument_name)
A base class for modifiers of ParticlesTemp.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
A which applies a singleton modifier to each kernel::Particle in a hierarchy.
Out gather_by_attributes(H h, K0 k0, V0 v0, K1 k1, V1 v1, Out out)
Gather all the particles in the hierarchy which match on two attributes.
Hierarchy get_root(Hierarchy h)
Return the root of the hierarchy.
Hierarchy get_parent() const
Out gather_slice(H h, F f, Out out)
Gather all the particles in the hierarchy which meet some criteria.
F visit_depth_first(HD d, F f)
Apply functor F to each particle, traversing the hierarchy depth first.
A simple functor to count the number of particles in a hierarchy.
Define the type for a type of hierarchy.
Import IMP/kernel/decorator_macros.h in the namespace.
bool operator()(Hierarchy)
Increment the counter.
#define IMP_DECORATOR_TRAITS_SETUP_0(Name)
Out gather(H h, F f, Out out)
Gather all the particles in the hierarchy which meet some criteria.
Out gather_by_attribute(H h, K k, V v, Out out)
Gather all the particles in the hierarchy which match on an attribute.
Storage of a model, its restraints, constraints and particles.
GenericHierarchies get_internal(Hierarchy mhd)
Get all the non-leaves of the bit of hierarchy.
Classes to handle individual model particles.
base::Index< ParticleIndexTag > ParticleIndex
void show(Hierarchy h, std::ostream &out=std::cout)
Print out a molecular hierarchy.
unsigned int get_count() const
Return how many nodes have been visited.
void add_attribute(TypeKey attribute_key, ParticleIndex particle, Type value)
static bool get_is_setup(kernel::Model *, kernel::ParticleIndex, HierarchyTraits=Hierarchy::get_default_traits())
A decorator for helping deal with a hierarchy.
F visit_breadth_first_with_data(HD d, F f, typename F::result_type i)
Apply functor F to each particle, traversing the hierarchy breadth first.
virtual bool operator()(Hierarchy p)
Return true if the traversal should visit this node's children.
Class for storing model, its restraints, constraints, and particles.