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(),
124 unsigned int get_number_of_children()
const {
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(),
140 get_decorator_traits());
143 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
147 kernel::ParticleIndexes get_children_indexes()
const {
153 return kernel::ParticleIndexes();
156 GenericHierarchies get_children()
const {
157 GenericHierarchies ret(get_number_of_children());
158 for (
unsigned int i = 0; i < ret.size(); ++i) {
159 ret[i] = get_child(i);
163 void remove_child(
unsigned int i) {
164 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
165 Hierarchy c = get_child(i);
166 kernel::ParticleIndexes &pis =
get_model()->access_attribute(
168 pis.erase(pis.begin() + i);
170 c.get_particle_index());
172 void remove_child(Hierarchy h) { remove_child(h.get_child_index()); }
173 void clear_children() {
174 kernel::ParticleIndexes &pis =
get_model()->access_attribute(
176 for (
unsigned int i = 0; i < pis.size(); ++i) {
183 void add_child(Hierarchy h)
const {
187 ->access_attribute(get_decorator_traits().get_children_key(),
189 .push_back(h.get_particle_index());
193 kernel::ParticleIndexes(1, h.get_particle_index()));
198 void add_child_at(Hierarchy h,
unsigned int pos) {
202 kernel::ParticleIndexes &pis =
get_model()->access_attribute(
204 pis.insert(pis.begin() + pos, h.get_particle_index());
208 kernel::ParticleIndexes(1, h.get_particle_index()));
214 int get_child_index()
const;
216 static const HierarchyTraits &get_default_traits();
218 HierarchyTraits get_traits() {
return get_decorator_traits(); }
231 virtual bool operator()(
Hierarchy p) = 0;
254 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
256 template <
class H,
class F,
class Out,
bool Slice = false>
259 Gather(F f, Out out) : f_(f), out_(out) {}
260 bool operator()(H p) {
264 if (Slice)
return false;
269 Out get_out()
const {
return out_; }
285 template <
class HD,
class F>
287 std::deque<HD> stack;
291 HD cur = stack.front();
295 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
296 stack.push_back(cur.get_child(i));
299 }
while (!stack.empty());
308 template <
class HD,
class F>
314 HD cur = stack.back();
318 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
319 stack.push_back(cur.get_child(i));
322 }
while (!stack.empty());
351 template <
class HD,
class F>
353 typedef std::pair<typename F::result_type, HD> DP;
354 std::deque<DP> stack;
355 stack.push_back(DP(i, d));
358 DP cur = stack.front();
360 typename F::result_type r = f(cur.second.get_particle(), cur.first);
362 for (
int i = cur.second.get_number_of_children() - 1; i >= 0; --i) {
363 stack.push_back(std::make_pair(r, cur.second.get_child(i)));
365 }
while (!stack.empty());
374 template <
class HD,
class F>
376 typedef std::pair<typename F::result_type, HD> DP;
378 stack.push_back(DP(i, d));
381 DP cur = stack.back();
383 typename F::result_type r = f(cur.second, cur.first);
385 for (
int i = cur.second.get_number_of_children() - 1; i >= 0; --i) {
386 stack.push_back(DP(r, cur.second.get_child(i)));
388 }
while (!stack.empty());
399 IMP_PRINT_TREE(out,
Hierarchy, h, n.get_number_of_children(), n.get_child,
431 template <
class H,
class Out,
class F>
433 internal::Gather<H, F, Out>
gather(f, out);
435 return gather.get_out();
445 template <
class H,
class Out,
class F>
447 internal::Gather<H, F, Out, true>
gather(f, out);
449 return gather.get_out();
456 template <
class H,
class Out,
class K,
class V>
458 internal::Gather<H, internal::MatchAttribute<K, V>, Out>
gather(
459 internal::MatchAttribute<K, V>(k, v), out);
461 return gather.get_out();
468 template <
class H,
class Out,
class K0,
class V0,
class K1,
class V1>
470 internal::Gather<H, internal::MatchAttributes<K0, V0, K1, V1>, Out>
gather(
471 internal::MatchAttributes<K0, V0, K1, V1>(k0, v0, k1, v1), out);
473 return gather.get_out();
480 template <
class HD,
class F>
482 if (f(h.get_particle()))
return h;
487 HD cur = stack.back();
490 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
491 HD hd = cur.get_child(i);
492 if (f(hd.get_particle())) {
498 }
while (!stack.empty());
508 IMPCOREEXPORT GenericHierarchies
get_leaves(Hierarchy mhd);
513 IMPCOREEXPORT GenericHierarchies
get_internal(Hierarchy mhd);
529 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.
A visitor for traversal of a hierarchy.
Import IMP/kernel/SingletonModifier.h in the namespace.
ParticleIndex get_particle_index() const
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)
GenericHierarchies get_leaves(Hierarchy mhd)
Get all the leaves of the bit of hierarchy.
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 kernel::ParticlesTemp.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
A visitor which applies a 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_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.
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator as to display each node.
base::Index< ParticleIndexTag > ParticleIndex
unsigned int get_count() const
Return how many nodes have been visited.
Hierarchy get_root(Hierarchy h)
Return the root of the hierarchy.
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.