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>
24 #include <boost/serialization/access.hpp>
30 IMPCORE_BEGIN_NAMESPACE
59 return parent_ == o.parent_;
85 for (
unsigned int i = 0; i < children.size(); ++i) {
93 do_setup_particle(m, pi,
get_indexes(children), traits);
97 void update_changed_trigger()
const {
103 traits, get_default_traits());
123 if (
get_model()->get_has_attribute(get_decorator_traits().get_parent_key(),
133 unsigned int get_number_of_children()
const {
144 Hierarchy get_child(
unsigned int i)
const {
145 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
147 get_decorator_traits().get_children_key(),
149 get_decorator_traits());
152 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
156 ParticleIndexes get_children_indexes()
const {
162 return ParticleIndexes();
165 GenericHierarchies get_children()
const {
166 GenericHierarchies ret(get_number_of_children());
167 for (
unsigned int i = 0; i < ret.size(); ++i) {
168 ret[i] = get_child(i);
172 void remove_child(
unsigned int i) {
173 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
174 Hierarchy c = get_child(i);
175 ParticleIndexes &pis =
get_model()->access_attribute(
177 pis.erase(pis.begin() + i);
179 c.get_particle_index());
180 update_changed_trigger();
182 void remove_child(Hierarchy h) { remove_child(h.get_child_index()); }
183 void clear_children() {
184 ParticleIndexes &pis =
get_model()->access_attribute(
186 for (
unsigned int i = 0; i < pis.size(); ++i) {
192 update_changed_trigger();
194 void add_child(Hierarchy h)
const {
198 ->access_attribute(get_decorator_traits().get_children_key(),
200 .push_back(h.get_particle_index());
204 ParticleIndexes(1, h.get_particle_index()));
208 update_changed_trigger();
210 void add_child_at(Hierarchy h,
unsigned int pos) {
214 ParticleIndexes &pis =
get_model()->access_attribute(
216 pis.insert(pis.begin() + pos, h.get_particle_index());
220 ParticleIndexes(1, h.get_particle_index()));
224 update_changed_trigger();
227 int get_child_index()
const;
229 static const HierarchyTraits &get_default_traits();
231 HierarchyTraits get_traits() {
return get_decorator_traits(); }
244 virtual bool operator()(
Hierarchy p) = 0;
267 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
269 template <
class H,
class F,
class Out,
bool Slice = false>
272 Gather(F f, Out out) : f_(f), out_(out) {}
273 bool operator()(H p) {
277 if (Slice)
return false;
282 Out get_out()
const {
return out_; }
298 template <
class HD,
class F>
300 std::deque<HD> stack;
304 HD cur = stack.front();
308 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
309 stack.push_back(cur.get_child(i));
312 }
while (!stack.empty());
321 template <
class HD,
class F>
327 HD cur = stack.back();
331 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
332 stack.push_back(cur.get_child(i));
335 }
while (!stack.empty());
364 template <
class HD,
class F>
366 typename F::result_type data) {
367 typedef std::pair<typename F::result_type, HD> DP;
368 std::deque<DP> stack;
369 stack.push_back(DP(data, d));
372 DP cur = stack.front();
374 typename F::result_type r = f(cur.second.get_particle(), cur.first);
376 for (
int i = cur.second.get_number_of_children() - 1; i >= 0; --i) {
377 stack.push_back(std::make_pair(r, cur.second.get_child(i)));
379 }
while (!stack.empty());
388 template <
class HD,
class F>
390 typedef std::pair<typename F::result_type, HD> DP;
392 stack.push_back(DP(data, d));
395 DP cur = stack.back();
397 typename F::result_type r = f(cur.second, cur.first);
399 for (
int i = cur.second.get_number_of_children() - 1; i >= 0; --i) {
400 stack.push_back(DP(r, cur.second.get_child(i)));
402 }
while (!stack.empty());
412 IMP_PRINT_TREE(out,
Hierarchy, h, n.get_number_of_children(), n.get_child,
436 friend class boost::serialization::access;
438 template<
class Archive>
void serialize(Archive &ar,
const unsigned int) {
459 template <
class H,
class Out,
class F>
461 internal::Gather<H, F, Out>
gather(f, out);
463 return gather.get_out();
473 template <
class H,
class Out,
class F>
475 internal::Gather<H, F, Out, true>
gather(f, out);
477 return gather.get_out();
484 template <
class H,
class Out,
class K,
class V>
486 internal::Gather<H, internal::MatchAttribute<K, V>, Out>
gather(
487 internal::MatchAttribute<K, V>(k, v), out);
489 return gather.get_out();
496 template <
class H,
class Out,
class K0,
class V0,
class K1,
class V1>
498 internal::Gather<H, internal::MatchAttributes<K0, V0, K1, V1>, Out>
gather(
499 internal::MatchAttributes<K0, V0, K1, V1>(k0, v0, k1, v1), out);
501 return gather.get_out();
508 template <
class HD,
class F>
510 if (f(h.get_particle()))
return h;
515 HD cur = stack.back();
518 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
519 HD hd = cur.get_child(i);
520 if (f(hd.get_particle())) {
526 }
while (!stack.empty());
536 IMPCOREEXPORT GenericHierarchies
get_leaves(Hierarchy mhd);
541 IMPCOREEXPORT GenericHierarchies
get_internal(Hierarchy mhd);
557 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.
bool operator()(Hierarchy) override
Increment the counter.
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)
remove particle attribute with the specied key
Index< ParticleIndexTag > ParticleIndex
#define IMP_DECORATOR_TRAITS_SETUP_1(Name, FirstArgumentType,first_argument_name)
A more IMP-like version of the std::vector.
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.
virtual bool operator()(Hierarchy p) override
Return true if the traversal should visit this node's children.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
F visit_depth_first_with_data(HD d, F f, typename F::result_type data)
Apply functor F to each particle, traversing the hierarchy depth first.
Hierarchy get_root(Hierarchy h)
Return the root of the hierarchy.
Hierarchy get_parent() const
void add_attribute(TypeKey attribute_key, ParticleIndex particle, Type value)
add particle atribute with the specied key and initial value
F visit_breadth_first_with_data(HD d, F f, typename F::result_type data)
Apply functor F to each particle, traversing the hierarchy breadth first.
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.
Helper macros for implementing Decorators.
A smart pointer to a ref-counted Object that is a class member.
#define IMP_DECORATOR_TRAITS_SETUP_0(Name)
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.
Interface to specialized Particle types (e.g. atoms)
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
#define IMP_DECORATOR_WITH_TRAITS_METHODS(Name, Parent, TraitsType,traits_name, default_traits)
void show(Hierarchy h, std::ostream &out=std::cout)
Print out a molecular hierarchy.
A nullptr-initialized pointer to an IMP Object.
unsigned int get_count() const
Return how many nodes have been visited.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
A decorator for helping deal with a generalized hierarchy.
void set_trigger_updated(TriggerKey tk)
Update the given trigger.
Hierarchies get_leaves(Hierarchy h)
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
get the value of the particle attribute with the specified key
ParticleIndexes get_indexes(const ParticlesTemp &ps)
Get the indexes from a list of particles.