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/internal/utility.h>
23 #include <boost/tuple/tuple.hpp>
29 IMPCORE_BEGIN_NAMESPACE
58 return parent_ == o.parent_;
84 for (
unsigned int i = 0; i < children.size(); ++i) {
92 do_setup_particle(m, pi,
get_indexes(children), traits);
97 traits, get_default_traits());
113 if (
get_model()->get_has_attribute(get_decorator_traits().get_parent_key(),
123 unsigned int get_number_of_children()
const {
134 Hierarchy get_child(
unsigned int i)
const {
135 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
137 get_decorator_traits().get_children_key(),
139 get_decorator_traits());
142 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
146 ParticleIndexes get_children_indexes()
const {
152 return ParticleIndexes();
155 GenericHierarchies get_children()
const {
156 GenericHierarchies ret(get_number_of_children());
157 for (
unsigned int i = 0; i < ret.size(); ++i) {
158 ret[i] = get_child(i);
162 void remove_child(
unsigned int i) {
163 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
164 Hierarchy c = get_child(i);
165 ParticleIndexes &pis =
get_model()->access_attribute(
167 pis.erase(pis.begin() + i);
169 c.get_particle_index());
171 void remove_child(Hierarchy h) { remove_child(h.get_child_index()); }
172 void clear_children() {
173 ParticleIndexes &pis =
get_model()->access_attribute(
175 for (
unsigned int i = 0; i < pis.size(); ++i) {
182 void add_child(Hierarchy h)
const {
186 ->access_attribute(get_decorator_traits().get_children_key(),
188 .push_back(h.get_particle_index());
192 ParticleIndexes(1, h.get_particle_index()));
197 void add_child_at(Hierarchy h,
unsigned int pos) {
201 ParticleIndexes &pis =
get_model()->access_attribute(
203 pis.insert(pis.begin() + pos, h.get_particle_index());
207 ParticleIndexes(1, h.get_particle_index()));
213 int get_child_index()
const;
215 static const HierarchyTraits &get_default_traits();
217 HierarchyTraits get_traits() {
return get_decorator_traits(); }
230 virtual bool operator()(
Hierarchy p) = 0;
253 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
255 template <
class H,
class F,
class Out,
bool Slice = false>
258 Gather(F f, Out out) : f_(f), out_(out) {}
259 bool operator()(H p) {
263 if (Slice)
return false;
268 Out get_out()
const {
return out_; }
284 template <
class HD,
class F>
286 std::deque<HD> stack;
290 HD cur = stack.front();
294 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
295 stack.push_back(cur.get_child(i));
298 }
while (!stack.empty());
307 template <
class HD,
class F>
313 HD cur = stack.back();
317 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
318 stack.push_back(cur.get_child(i));
321 }
while (!stack.empty());
350 template <
class HD,
class F>
352 typedef std::pair<typename F::result_type, HD> DP;
353 std::deque<DP> stack;
354 stack.push_back(DP(i, d));
357 DP cur = stack.front();
359 typename F::result_type r = f(cur.second.get_particle(), cur.first);
361 for (
int i = cur.second.get_number_of_children() - 1; i >= 0; --i) {
362 stack.push_back(std::make_pair(r, cur.second.get_child(i)));
364 }
while (!stack.empty());
373 template <
class HD,
class F>
375 typedef std::pair<typename F::result_type, HD> DP;
377 stack.push_back(DP(i, d));
380 DP cur = stack.back();
382 typename F::result_type r = f(cur.second, cur.first);
384 for (
int i = cur.second.get_number_of_children() - 1; i >= 0; --i) {
385 stack.push_back(DP(r, cur.second.get_child(i)));
387 }
while (!stack.empty());
397 IMP_PRINT_TREE(out,
Hierarchy, h, n.get_number_of_children(), n.get_child,
439 template <
class H,
class Out,
class F>
441 internal::Gather<H, F, Out>
gather(f, out);
443 return gather.get_out();
453 template <
class H,
class Out,
class F>
455 internal::Gather<H, F, Out, true>
gather(f, out);
457 return gather.get_out();
464 template <
class H,
class Out,
class K,
class V>
466 internal::Gather<H, internal::MatchAttribute<K, V>, Out>
gather(
467 internal::MatchAttribute<K, V>(k, v), out);
469 return gather.get_out();
476 template <
class H,
class Out,
class K0,
class V0,
class K1,
class V1>
478 internal::Gather<H, internal::MatchAttributes<K0, V0, K1, V1>, Out>
gather(
479 internal::MatchAttributes<K0, V0, K1, V1>(k0, v0, k1, v1), out);
481 return gather.get_out();
488 template <
class HD,
class F>
490 if (f(h.get_particle()))
return h;
495 HD cur = stack.back();
498 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
499 HD hd = cur.get_child(i);
500 if (f(hd.get_particle())) {
506 }
while (!stack.empty());
516 IMPCOREEXPORT GenericHierarchies
get_leaves(Hierarchy mhd);
521 IMPCOREEXPORT GenericHierarchies
get_internal(Hierarchy mhd);
537 IMPCORE_END_NAMESPACE
F visit_breadth_first(HD d, F f)
Apply the visitor to each particle, breadth first.
The base class for decorators.
A base class for modifiers of ParticlesTemp.
A visitor for traversal of a hierarchy.
A Modifier on ParticlesTemp.
ParticleIndex get_particle_index() const
Returns the particle index decorated by this decorator.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
static bool get_is_setup(Model *, ParticleIndex, HierarchyTraits=Hierarchy::get_default_traits())
F visit_depth_first(HD d, F &f)
Apply functor F to each particle, traversing the hierarchy depth first.
Model * get_model() const
Returns the Model containing the particle.
Storage of a model, its restraints, constraints and particles.
HD find_breadth_first(HD h, F f)
Find the first node which matches some criteria.
void remove_attribute(TypeKey attribute_key, ParticleIndex particle)
Index< ParticleIndexTag > ParticleIndex
#define IMP_DECORATOR_TRAITS_SETUP_1(Name, FirstArgumentType,first_argument_name)
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.
GenericHierarchies get_leaves(Hierarchy mhd)
Get all the leaves of the bit of hierarchy.
GenericHierarchies get_all_descendants(Hierarchy mhd)
Get all the particles in the subtree.
A visitor which applies a modifier to each Particle in a hierarchy.
Class for storing model, its restraints, constraints, and particles.
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.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Hierarchy get_parent() const
void add_attribute(TypeKey attribute_key, ParticleIndex particle, Type value)
Out gather_slice(H h, F f, Out out)
Gather all the uppermost particles in the hierarchy that meet some criteria.
A simple functor to count the number of particles in a hierarchy.
Define the type for a type of hierarchy.
Various general useful macros for IMP.
A smart pointer to a ref-counted Object that is a class member.
#define IMP_DECORATOR_TRAITS_SETUP_0(Name)
bool operator()(Hierarchy)
Increment the counter.
Out gather(H h, F f, Out out)
Gather all the particles in the hierarchy that 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.
GenericHierarchies get_internal(Hierarchy mhd)
Get all the non-leaves of the bit of hierarchy.
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator to display each node.
#define IMP_DECORATOR_WITH_TRAITS_METHODS(Name, Parent, TraitsType,traits_name, default_traits)
A nullptr-initialized pointer to an IMP Object.
unsigned int get_count() const
Return how many nodes have been visited.
Hierarchy get_root(Hierarchy h)
Return the root of the hierarchy.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
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.
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
ParticleIndexes get_indexes(const ParticlesTemp &ps)
virtual bool operator()(Hierarchy p)
Return true if the traversal should visit this node's children.