IMP  2.3.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-2014 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 <boost/unordered_map.hpp>
18 #include <IMP/pair_macros.h>
19 
20 IMPCONTAINER_BEGIN_NAMESPACE
21 
22 //! A container which contains all consecutive particle pairs from an input list
23 /** If it is assumed that each particle is in at most one such container,
24  then ExclusiveConsecutivePairContainer should be used instead,
25  since it is faster when doing certain computations.
26 
27  Also see ConsecutivePairFilter.
28 */
29 class IMPCONTAINEREXPORT ConsecutivePairContainer : public PairContainer {
30  friend class ConsecutivePairFilter;
31  const kernel::ParticleIndexes ps_;
32  IntKey key_;
33  /**
34  add the key of this container as an attribute to all particles
35  if there might be overlaps - create a different key for each instance
36  */
37  void init();
38 
39  bool get_contains(const kernel::ParticleIndexPair &p) const {
40  if (!get_model()->get_has_attribute(key_, p[0])) return false;
41  int ia = get_model()->get_attribute(key_, p[0]);
42  if (!get_model()->get_has_attribute(key_, p[1])) return false;
43  int ib = get_model()->get_attribute(key_, p[1]);
44  return std::abs(ia - ib) == 1;
45  }
46 
47  protected:
48  virtual std::size_t do_get_contents_hash() const IMP_OVERRIDE { return 0; }
49 
50  public:
51  //! apply to each item in container
52  template <class F>
53  void apply_generic(F *f) const {
54  for (unsigned int i = 1; i < ps_.size(); ++i) {
55  f->apply_index(get_model(),
56  kernel::ParticleIndexPair(ps_[i - 1], ps_[i]));
57  }
58  }
59  //! Get the individual particles from the passed SingletonContainer
61  std::string name = "ConsecutivePairContainer%1%");
63  virtual kernel::ParticleIndexPairs get_range_indexes() const IMP_OVERRIDE;
64  virtual kernel::ModelObjectsTemp do_get_inputs() const IMP_OVERRIDE;
65  virtual kernel::ParticleIndexes get_all_possible_indexes() const IMP_OVERRIDE;
67  IMP_OBJECT_METHODS(ConsecutivePairContainer);
68 };
69 
70 IMP_OBJECTS(ConsecutivePairContainer, ConsecutivePairContainers);
71 
72 /** Check for whether the pair is a member of a specific
73  ConsecutivePairContainer. */
74 class IMPCONTAINEREXPORT ConsecutivePairFilter : public PairPredicate {
76 
77  public:
78  /** @param cpc the consecutive pair container that stores
79  filtered pairs
80  */
81  ConsecutivePairFilter(ConsecutivePairContainer *cpc);
82 
84  const kernel::ParticleIndexPair &pip) const
85  IMP_OVERRIDE {
86  return cpc_->get_contains(pip);
87  }
89  kernel::Model *m, const kernel::ParticleIndexes &pi) const IMP_OVERRIDE {
91  ret += IMP::get_particles(m, pi);
92  return ret;
93  }
96 };
97 
98 /** This is an ConsecutivePairContainer where each particle can only be on
99  one ExclusiveConsecutivePairContainer. The exclusivity makes the code
100  more efficient and allows one to use the ExclusiveConsecutivePairFilter,
101  which is way more efficient than using an InContainerPairFilter
102  with a ConsecutivePairContainer.*/
103 class IMPCONTAINEREXPORT ExclusiveConsecutivePairContainer
104  : public PairContainer {
105  friend class ExclusiveConsecutivePairFilter;
106  const kernel::ParticleIndexes ps_;
107  static IntKey get_exclusive_key() {
108  static IntKey k("exclusive consecutive numbering");
109  return k;
110  }
111  static ObjectKey get_exclusive_object_key() {
112  static ObjectKey k("exclusive consecutive container");
113  return k;
114  }
115  static bool get_contains(kernel::Model *m,
116  const kernel::ParticleIndexPair &pp) {
117  ObjectKey ok =
118  ExclusiveConsecutivePairContainer::get_exclusive_object_key();
119  bool has_eok_0 = m->get_has_attribute(ok, pp[0]);
120  bool has_eok_1= m->get_has_attribute(ok, pp[1]);
121  if ( !has_eok_0 || !has_eok_1 )
122  return false;
123  if (m->get_attribute(ok, pp[0]) != m->get_attribute(ok, pp[1])) {
124  return false;
125  }
126  IntKey k = ExclusiveConsecutivePairContainer::get_exclusive_key();
127  int ia = m->get_attribute(k, pp[0]);
128  int ib = m->get_attribute(k, pp[1]);
129  return std::abs(ia - ib) == 1;
130  }
131  void init();
132 
133  protected:
134  virtual std::size_t do_get_contents_hash() const IMP_OVERRIDE { return 0; }
135 
136  /**
137  Called by Object destructor - removes all keys associated with
138  the exclusive consecutive pair container, so it can be now added to
139  another exclusive consecutive pair container
140  */
141  virtual void do_destroy();
142 
143 
144  public:
145  //! apply to each item in container
146  template <class F>
147  void apply_generic(F *f) const {
148  for (unsigned int i = 1; i < ps_.size(); ++i) {
149  f->apply_index(get_model(),
150  kernel::ParticleIndexPair(ps_[i - 1], ps_[i]));
151  }
152  }
153 
154  //! Get the individual particles from the passed SingletonContainer
156  std::string name =
157  "ExclusiveConsecutivePairContainer%1%");
159  virtual kernel::ParticleIndexPairs get_range_indexes() const IMP_OVERRIDE;
160  virtual kernel::ModelObjectsTemp do_get_inputs() const IMP_OVERRIDE;
161  virtual kernel::ParticleIndexes get_all_possible_indexes() const IMP_OVERRIDE;
163  IMP_OBJECT_METHODS(ExclusiveConsecutivePairContainer);
164 };
165 
166 /** Check for whether the pair is a member of any
167  ExclusiveConsecutivePairContainer. */
168 class IMPCONTAINEREXPORT ExclusiveConsecutivePairFilter : public PairPredicate {
169  public:
171  : PairPredicate("ExclusiveConsecutivePairFilter %1%") {}
172 
174  const kernel::ParticleIndexPair &pip) const
175  IMP_OVERRIDE {
176  return ExclusiveConsecutivePairContainer::get_contains(m, pip);
177  }
179  kernel::Model *m, const kernel::ParticleIndexes &pi) const IMP_OVERRIDE {
181  ret += IMP::get_particles(m, pi);
182  return ret;
183  }
186 };
187 
188 IMPCONTAINER_END_NAMESPACE
189 
190 #endif /* IMPCONTAINER_CONSECUTIVE_PAIR_CONTAINER_H */
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 member.
Definition: Pointer.h:147
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
ParticlesTemp get_particles(kernel::Model *m, const ParticleIndexes &ps)
A container which contains all consecutive particle pairs from an input list.
#define IMP_PAIR_CONTAINER_METHODS(Name)
virtual kernel::ParticleIndexPairs get_indexes() const =0
A shared container for Pairs.
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
A class to store an fixed array of same-typed values.
Definition: 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.
virtual kernel::ModelObjectsTemp do_get_inputs(kernel::Model *m, const kernel::ParticleIndexes &pi) const
Store a list of kernel::ParticlePairsTemp.
Abstract predicate function.
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing sets of objects.
Definition: object_macros.h:52
IMP::kernel::PairPredicate PairPredicate
virtual void do_destroy()
Definition: Object.h:231
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:73
Import IMP/kernel/generic.h in the namespace.