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 <cereal/access.hpp>
29#include <cereal/access.hpp>
30#include <cereal/types/base_class.hpp>
32IMPCORE_BEGIN_NAMESPACE
50class IMPCOREEXPORT HierarchyTraits {
54 friend class cereal::access;
55 template<
class Archive>
void serialize(Archive &ar) {
56 ar(children_, parent_);
65 bool operator==(
const HierarchyTraits &o)
const {
66 return parent_ == o.parent_;
86class IMPCOREEXPORT Hierarchy :
public Decorator {
87 friend class cereal::access;
88 template<
class Archive>
void serialize(Archive &ar) {
89 ar(cereal::base_class<Decorator>(
this), traits_);
97 for (
unsigned int i = 0; i < children.size(); ++i) {
98 m->add_attribute(traits.get_parent_key(), children[i], pi);
100 m->add_attribute(traits.get_children_key(), pi, children);
103 const ParticlesTemp &children,
105 do_setup_particle(m, pi,
get_indexes(children), traits);
109 void update_changed_trigger()
const {
135 if (
get_model()->get_has_attribute(get_decorator_traits().get_parent_key(),
139 return Hierarchy(
get_model(), VALUE, get_decorator_traits());
145 unsigned int get_number_of_children()
const {
146 if (get_model()->get_has_attribute(
147 get_decorator_traits().get_children_key(), get_particle_index())) {
149 ->get_attribute(get_decorator_traits().get_children_key(),
150 get_particle_index())
156 Hierarchy get_child(
unsigned int i)
const {
157 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
158 return Hierarchy(get_model(), get_model()->get_attribute(
159 get_decorator_traits().get_children_key(),
160 get_particle_index())[i],
161 get_decorator_traits());
164 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
165 return get_model()->get_attribute(get_decorator_traits().get_children_key(),
166 get_particle_index())[i];
169 if (get_model()->get_has_attribute(
170 get_decorator_traits().get_children_key(), get_particle_index())) {
171 return get_model()->get_attribute(
172 get_decorator_traits().get_children_key(), get_particle_index());
177 GenericHierarchies get_children()
const {
178 GenericHierarchies ret(get_number_of_children());
179 for (
unsigned int i = 0; i < ret.size(); ++i) {
180 ret[i] = get_child(i);
184 void remove_child(
unsigned int i) {
185 IMP_USAGE_CHECK(i < get_number_of_children(),
"Invalid child requested");
186 Hierarchy c = get_child(i);
188 get_decorator_traits().get_children_key(), get_particle_index());
189 pis.erase(pis.begin() + i);
190 get_model()->remove_attribute(get_decorator_traits().get_parent_key(),
191 c.get_particle_index());
192 update_changed_trigger();
194 void remove_child(Hierarchy h) { remove_child(h.get_child_index()); }
195 void clear_children() {
197 get_decorator_traits().get_children_key(), get_particle_index());
198 for (
unsigned int i = 0; i < pis.size(); ++i) {
199 get_model()->remove_attribute(get_decorator_traits().get_parent_key(),
202 get_model()->remove_attribute(get_decorator_traits().get_children_key(),
203 get_particle_index());
204 update_changed_trigger();
206 void add_child(Hierarchy h)
const {
207 if (get_model()->get_has_attribute(
208 get_decorator_traits().get_children_key(), get_particle_index())) {
210 ->access_attribute(get_decorator_traits().get_children_key(),
211 get_particle_index())
212 .push_back(h.get_particle_index());
214 get_model()->add_attribute(
215 get_decorator_traits().get_children_key(), get_particle_index(),
218 get_model()->add_attribute(get_decorator_traits().get_parent_key(),
219 h.get_particle_index(), get_particle_index());
220 update_changed_trigger();
222 void add_child_at(Hierarchy h,
unsigned int pos) {
224 if (get_model()->get_has_attribute(
225 get_decorator_traits().get_children_key(), get_particle_index())) {
227 get_decorator_traits().get_children_key(), get_particle_index());
228 pis.insert(pis.begin() + pos, h.get_particle_index());
230 get_model()->add_attribute(
231 get_decorator_traits().get_children_key(), get_particle_index(),
234 get_model()->add_attribute(get_decorator_traits().get_parent_key(),
235 h.get_particle_index(), get_particle_index());
236 update_changed_trigger();
252class IMPCOREEXPORT HierarchyVisitor {
254 HierarchyVisitor() {}
257 virtual ~HierarchyVisitor() {}
267class IMPCOREEXPORT ModifierVisitor :
public HierarchyVisitor {
273 sm_->apply_index(p.get_model(), p.get_particle_index());
279#if !defined(SWIG) && !defined(IMP_DOXYGEN)
281template <
class H,
class F,
class Out,
bool Slice = false>
284 Gather(F f, Out out) : f_(f), out_(out) {}
285 bool operator()(H p) {
289 if (Slice)
return false;
294 Out get_out()
const {
return out_; }
310template <
class HD,
class F>
312 std::deque<HD> stack;
316 HD cur = stack.front();
320 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
321 stack.push_back(cur.get_child(i));
324 }
while (!stack.empty());
333template <
class HD,
class F>
339 HD cur = stack.back();
343 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
344 stack.push_back(cur.get_child(i));
347 }
while (!stack.empty());
376template <
class HD,
class F>
378 typename F::result_type data) {
379 typedef std::pair<typename F::result_type, HD> DP;
380 std::deque<DP> stack;
381 stack.push_back(DP(data, d));
384 DP cur = stack.front();
386 typename F::result_type r = f(cur.second.get_particle(), cur.first);
388 for (
int i = cur.second.get_number_of_children() - 1; i >= 0; --i) {
389 stack.push_back(std::make_pair(r, cur.second.get_child(i)));
391 }
while (!stack.empty());
400template <
class HD,
class F>
402 typedef std::pair<typename F::result_type, HD> DP;
404 stack.push_back(DP(data, d));
407 DP cur = stack.back();
409 typename F::result_type r = f(cur.second, cur.first);
411 for (
int i = cur.second.get_number_of_children() - 1; i >= 0; --i) {
412 stack.push_back(DP(r, cur.second.get_child(i)));
414 }
while (!stack.empty());
424 IMP_PRINT_TREE(out,
Hierarchy, h, n.get_number_of_children(), n.get_child,
434struct HierarchyCounter :
public HierarchyVisitor {
435 HierarchyCounter() : ct_(0) {}
448 friend class cereal::access;
450 template<
class Archive>
void serialize(Archive &ar) {
471template <
class H,
class Out,
class F>
473 internal::Gather<H, F, Out>
gather(f, out);
485template <
class H,
class Out,
class F>
487 internal::Gather<H, F, Out, true>
gather(f, out);
496template <
class H,
class Out,
class K,
class V>
498 internal::Gather<H, internal::MatchAttribute<K, V>, Out>
gather(
499 internal::MatchAttribute<K, V>(k, v), out);
508template <
class H,
class Out,
class K0,
class V0,
class K1,
class V1>
510 internal::Gather<H, internal::MatchAttributes<K0, V0, K1, V1>, Out>
gather(
511 internal::MatchAttributes<K0, V0, K1, V1>(k0, v0, k1, v1), out);
520template <
class HD,
class F>
522 if (f(h.get_particle()))
return h;
527 HD cur = stack.back();
530 for (
int i = cur.get_number_of_children() - 1; i >= 0; --i) {
531 HD hd = cur.get_child(i);
532 if (f(hd.get_particle())) {
538 }
while (!stack.empty());
563 while (h.get_parent()) {
The base class for decorators.
Storage of a model, its restraints, constraints and particles.
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
A nullptr-initialized pointer to an IMP Object.
A Modifier on ParticlesTemp.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
ParticleIndex get_particle_index() const
Returns the particle index decorated by this decorator.
Model * get_model() const
Returns the Model containing the particle.
Class for storing model, its restraints, constraints, and particles.
A base class for modifiers of ParticlesTemp.
A more IMP-like version of the std::vector.
Define the type for a type of hierarchy.
HierarchyTraits(std::string name)
Create a HierarchyTraits with the given name.
virtual bool operator()(Hierarchy p)=0
Return true if the traversal should visit this node's children.
A decorator for helping deal with a generalized hierarchy.
static TriggerKey get_changed_key()
The key used to signal to the Model that the Hierarchy has changed.
Hierarchy get_parent() const
static const HierarchyTraits & get_default_traits()
Get the default hierarchy traits.
int get_child_index() const
Return i such that get_parent().get_child(i) == this.
static bool get_is_setup(Model *, ParticleIndex, HierarchyTraits=Hierarchy::get_default_traits())
A visitor which applies a modifier to each Particle in a hierarchy.
virtual bool operator()(Hierarchy p) override
Return true if the traversal should visit this node's children.
Helper macros for implementing Decorators.
#define IMP_DECORATOR_TRAITS_SETUP_1(Name, FirstArgumentType, first_argument_name)
#define IMP_DECORATOR_TRAITS_SETUP_0(Name)
#define IMP_DECORATOR_WITH_TRAITS_METHODS(Name, Parent, TraitsType, traits_name, default_traits)
Out gather_slice(H h, F f, Out out)
Gather all the uppermost particles in the hierarchy that meet some criteria.
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator to display each node.
HD find_breadth_first(HD h, F f)
Find the first node which matches some criteria.
F visit_depth_first(HD d, F &f)
Apply functor F to each particle, traversing the hierarchy depth first.
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.
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.
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(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.
F visit_breadth_first(HD d, F f)
Apply the visitor to each particle, breadth first.
GenericHierarchies get_all_descendants(Hierarchy mhd)
Get all the particles in the subtree.
IMP::Vector< HierarchyTraits > HierarchyTraitsList
Hierarchy get_root(Hierarchy h)
Return the root of the hierarchy.
GenericHierarchies get_internal(Hierarchy mhd)
Get all the non-leaves of the bit of hierarchy.
GenericHierarchies get_leaves(Hierarchy mhd)
Get all the leaves of the bit of hierarchy.
IMP::Vector< HierarchyCounter > HierarchyCounters
Index< ParticleIndexTag > ParticleIndex
Vector< ParticleIndex, std::allocator< ParticleIndex > > ParticleIndexes
A list of particles, each identified by its ParticleIndex.
ParticleIndexes get_indexes(const ParticlesTemp &ps)
Get the indexes from a list of particles.
Key< 3 > ParticleIndexKey
The type used to identify a particle attribute in the Particles.
Key< 11 > TriggerKey
The type used to identify changed model properties.
Key< 6 > ParticleIndexesKey
The type used to identify a particle attribute in the Particles.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
A smart pointer to a ref-counted Object that is a class member.
A simple functor to count the number of particles in a hierarchy.
unsigned int get_count() const
Return how many nodes have been visited.
bool operator()(Hierarchy) override
Increment the counter.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.