IMP  2.1.0
The Integrative Modeling Platform
ConsecutivePairContainer.h
Go to the documentation of this file.
1 /**
2  * \file IMP/container/ConsecutivePairContainer.h
3  * \brief Return all pairs from a SingletonContainer
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPCONTAINER_CONSECUTIVE_PAIR_CONTAINER_H
9 #define IMPCONTAINER_CONSECUTIVE_PAIR_CONTAINER_H
10 
11 #include <IMP/container/container_config.h>
12 #include <IMP/generic.h>
13 #include <IMP/PairContainer.h>
14 #include <IMP/PairPredicate.h>
15 #include <IMP/SingletonContainer.h>
17 #include <IMP/base/map.h>
18 #include <IMP/pair_macros.h>
19 
20 IMPCONTAINER_BEGIN_NAMESPACE
21 
22 //! A container which contains all consecutive pairs from an input list
23 // of particles
24 /** If it is assumed that each particle is in at most one such container,
25  then ExclusiveConsecutivePairContainer should be used instead,
26  since it is faster when doing certain computations.
27 
28  Also see ConsecutivePairFilter.
29 */
30 class IMPCONTAINEREXPORT ConsecutivePairContainer : public PairContainer {
31  friend class ConsecutivePairFilter;
32  const kernel::ParticleIndexes ps_;
33  IntKey key_;
34  /**
35  add the key of this container as an attribute to all particles
36  if there might be ovrlaps - create a different keys for each instance
37  */
38  void init();
39 
40  bool get_contains(const kernel::ParticleIndexPair &p) const {
41  if (!get_model()->get_has_attribute(key_, p[0])) return false;
42  int ia = get_model()->get_attribute(key_, p[0]);
43  if (!get_model()->get_has_attribute(key_, p[1])) return false;
44  int ib = get_model()->get_attribute(key_, p[1]);
45  return std::abs(ia - ib) == 1;
46  }
47 
48  public:
49  //! apply to each item in container
50  template <class F>
51  void apply_generic(F *f) const {
52  for (unsigned int i = 1; i < ps_.size(); ++i) {
53  f->apply_index(get_model(),
54  kernel::ParticleIndexPair(ps_[i - 1], ps_[i]));
55  }
56  }
57  //! Get the individual particles from the passed SingletonContainer
59  std::string name = "ConsecutivePairContainer%1%");
60  virtual kernel::ParticleIndexPairs get_indexes() const IMP_OVERRIDE;
61  virtual kernel::ParticleIndexPairs get_range_indexes() const IMP_OVERRIDE;
62  virtual kernel::ModelObjectsTemp do_get_inputs() const IMP_OVERRIDE;
63  virtual kernel::ParticleIndexes get_all_possible_indexes() const IMP_OVERRIDE;
64  virtual void do_before_evaluate() IMP_OVERRIDE;
66  IMP_OBJECT_METHODS(ConsecutivePairContainer);
67 };
68 
69 IMP_OBJECTS(ConsecutivePairContainer, ConsecutivePairContainers);
70 
71 /** Check for whether the pair is a member of a specific
72  ConsecutivePairContainer. */
73 class IMPCONTAINEREXPORT ConsecutivePairFilter : public PairPredicate {
75 
76  public:
77  ConsecutivePairFilter(ConsecutivePairContainer *cpc);
78 
80  const kernel::ParticleIndexPair &pip) const
81  IMP_OVERRIDE {
82  return cpc_->get_contains(pip);
83  }
85  kernel::Model *m, const kernel::ParticleIndexes &pi) const IMP_OVERRIDE {
87  ret += IMP::get_particles(m, pi);
88  return ret;
89  }
92 };
93 
94 /** This is an ConsecutivePairContainer where each particle can only be on
95  one ExclusiveConsecutivePairContainer. The exclusivity makes the code
96  more efficient and allows one to use the ExclusiveConsecutivePairFilter,
97  which is way more efficient than using an InContainerPairFilter
98  with a ConsecutivePairContainer.*/
99 class IMPCONTAINEREXPORT ExclusiveConsecutivePairContainer
100  : public PairContainer {
101  friend class ExclusiveConsecutivePairFilter;
102  const kernel::ParticleIndexes ps_;
103  static IntKey get_exclusive_key() {
104  static IntKey k("exclusive consecutive numbering");
105  return k;
106  }
107  static ObjectKey get_exclusive_object_key() {
108  static ObjectKey k("exclusive consecutive container");
109  return k;
110  }
111  static bool get_contains(kernel::Model *m,
112  const kernel::ParticleIndexPair &pp) {
113  ObjectKey ok =
114  ExclusiveConsecutivePairContainer::get_exclusive_object_key();
115  if (!m->get_has_attribute(ok, pp[0]) || !m->get_has_attribute(ok, pp[1]))
116  return false;
117  if (m->get_attribute(ok, pp[0]) != m->get_attribute(ok, pp[1])) {
118  return false;
119  }
120  IntKey k = ExclusiveConsecutivePairContainer::get_exclusive_key();
121  int ia = m->get_attribute(k, pp[0]);
122  int ib = m->get_attribute(k, pp[1]);
123  return std::abs(ia - ib) == 1;
124  }
125  void init();
126 
127  public:
128  //! apply to each item in container
129  template <class F>
130  void apply_generic(F *f) const {
131  for (unsigned int i = 1; i < ps_.size(); ++i) {
132  f->apply_index(get_model(),
133  kernel::ParticleIndexPair(ps_[i - 1], ps_[i]));
134  }
135  }
136 
137  //! Get the individual particles from the passed SingletonContainer
139  std::string name =
140  "ExclusiveConsecutivePairContainer%1%");
141  virtual kernel::ParticleIndexPairs get_indexes() const IMP_OVERRIDE;
142  virtual kernel::ParticleIndexPairs get_range_indexes() const IMP_OVERRIDE;
143  virtual kernel::ModelObjectsTemp do_get_inputs() const IMP_OVERRIDE;
144  virtual kernel::ParticleIndexes get_all_possible_indexes() const IMP_OVERRIDE;
145  virtual void do_before_evaluate() IMP_OVERRIDE;
147  IMP_OBJECT_METHODS(ExclusiveConsecutivePairContainer);
148 };
149 
150 /** Check for whether the pair is a member of any
151  ExclusiveConsecutivePairContainer. */
152 class IMPCONTAINEREXPORT ExclusiveConsecutivePairFilter : public PairPredicate {
153  public:
155  : PairPredicate("ExclusiveConsecutivePairFilter %1%") {}
156 
158  const kernel::ParticleIndexPair &pip) const
159  IMP_OVERRIDE {
160  return ExclusiveConsecutivePairContainer::get_contains(m, pip);
161  }
163  kernel::Model *m, const kernel::ParticleIndexes &pi) const IMP_OVERRIDE {
165  ret += IMP::get_particles(m, pi);
166  return ret;
167  }
170 };
171 
172 IMPCONTAINER_END_NAMESPACE
173 
174 #endif /* IMPCONTAINER_CONSECUTIVE_PAIR_CONTAINER_H */
ParticleIndexes get_indexes(const ParticlesTemp &ps)
virtual int get_value_index(kernel::Model *, const kernel::ParticleIndexPair &pip) const
Compute the predicate and the derivative if needed.
A base class for Keys.
Definition: kernel/Key.h:46
Import IMP/kernel/pair_macros.h in the namespace.
IMP::base::Vector< IMP::base::WeakPointer< kernel::ModelObject > > ModelObjectsTemp
Import IMP/kernel/SingletonContainer.h in the namespace.
void apply_generic(F *f) const
apply to each item in container
void apply_generic(F *f) const
apply to each item in container
A smart pointer to a ref-counted Object that is a class memeber.
Definition: base/Pointer.h:146
ParticlesTemp get_particles(kernel::Model *m, const ParticleIndexes &ps)
A container which contains all consecutive pairs from an input list.
#define IMP_PAIR_CONTAINER_METHODS(Name)
A shared container for Pairs.
virtual ParticleIndexPairs get_indexes() const =0
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
A class to store an fixed array of same-typed values.
Definition: base/Array.h:33
virtual int get_value_index(kernel::Model *m, const kernel::ParticleIndexPair &pip) const
Compute the predicate and the derivative if needed.
Import IMP/kernel/PairContainer.h in the namespace.
Import IMP/kernel/PairPredicate.h in the namespace.
virtual kernel::ModelObjectsTemp do_get_inputs(kernel::Model *m, const kernel::ParticleIndexes &pi) const
#define IMP_PAIR_PREDICATE_METHODS(Name)
Define extra the functions needed for a PairPredicate.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
virtual kernel::ModelObjectsTemp do_get_inputs(kernel::Model *m, const kernel::ParticleIndexes &pi) const
Store a list of ParticlePairsTemp.
Abstract predicate function.
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing sets of objects.
IMP::kernel::PairPredicate PairPredicate
Declare an efficient stl-compatible map.
Class for storing model, its restraints, constraints, and particles.
Import IMP/kernel/generic.h in the namespace.