IMP logo
IMP Reference Guide  2.5.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-2015 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 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 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  ParticleIndexPair(ps_[i - 1], ps_[i]));
57  }
58  }
59 
61  std::string name = "ConsecutivePairContainer%1%");
62 
63 #ifndef IMP_DOXYGEN
64  IMPCONTAINER_DEPRECATED_METHOD_DECL(2.5)
66  std::string name = "ConsecutivePairContainer%1%");
67 #endif
68 
70  virtual ParticleIndexPairs get_range_indexes() const IMP_OVERRIDE;
71  virtual ModelObjectsTemp do_get_inputs() const IMP_OVERRIDE;
72  virtual ParticleIndexes get_all_possible_indexes() const IMP_OVERRIDE;
73  IMP_PAIR_CONTAINER_METHODS(ConsecutivePairContainer);
74  IMP_OBJECT_METHODS(ConsecutivePairContainer);
75 };
76 
77 IMP_OBJECTS(ConsecutivePairContainer, ConsecutivePairContainers);
78 
79 /** Check for whether the pair is a member of a specific
80  ConsecutivePairContainer. */
81 class IMPCONTAINEREXPORT ConsecutivePairFilter : public PairPredicate {
83 
84  public:
85  /** @param cpc the consecutive pair container that stores
86  filtered pairs
87  */
88  ConsecutivePairFilter(ConsecutivePairContainer *cpc);
89 
90  virtual int get_value_index(Model *,
91  const ParticleIndexPair &pip) const
92  IMP_OVERRIDE {
93  return cpc_->get_contains(pip);
94  }
96  Model *m, const ParticleIndexes &pi) const IMP_OVERRIDE {
97  ModelObjectsTemp ret;
98  ret += IMP::get_particles(m, pi);
99  return ret;
100  }
103 };
104 
105 /** This is an ConsecutivePairContainer where each particle can only be on
106  one ExclusiveConsecutivePairContainer. The exclusivity makes the code
107  more efficient and allows one to use the ExclusiveConsecutivePairFilter,
108  which is way more efficient than using an InContainerPairFilter
109  with a ConsecutivePairContainer.*/
110 class IMPCONTAINEREXPORT ExclusiveConsecutivePairContainer
111  : public PairContainer {
112  friend class ExclusiveConsecutivePairFilter;
113  const ParticleIndexes ps_;
114  static IntKey get_exclusive_key() {
115  static IntKey k("exclusive consecutive numbering");
116  return k;
117  }
118  static ObjectKey get_exclusive_object_key() {
119  static ObjectKey k("exclusive consecutive container");
120  return k;
121  }
122  static bool get_contains(Model *m,
123  const ParticleIndexPair &pp) {
124  ObjectKey ok =
125  ExclusiveConsecutivePairContainer::get_exclusive_object_key();
126  bool has_eok_0 = m->get_has_attribute(ok, pp[0]);
127  bool has_eok_1= m->get_has_attribute(ok, pp[1]);
128  if ( !has_eok_0 || !has_eok_1 )
129  return false;
130  if (m->get_attribute(ok, pp[0]) != m->get_attribute(ok, pp[1])) {
131  return false;
132  }
133  IntKey k = ExclusiveConsecutivePairContainer::get_exclusive_key();
134  int ia = m->get_attribute(k, pp[0]);
135  int ib = m->get_attribute(k, pp[1]);
136  return std::abs(ia - ib) == 1;
137  }
138  void init();
139 
140  protected:
141  virtual std::size_t do_get_contents_hash() const IMP_OVERRIDE { return 0; }
142 
143  /**
144  Called by Object destructor - removes all keys associated with
145  the exclusive consecutive pair container, so it can be now added to
146  another exclusive consecutive pair container
147  */
148  virtual void do_destroy();
149 
150 
151  public:
152  //! apply to each item in container
153  template <class F>
154  void apply_generic(F *f) const {
155  for (unsigned int i = 1; i < ps_.size(); ++i) {
156  f->apply_index(get_model(),
157  ParticleIndexPair(ps_[i - 1], ps_[i]));
158  }
159  }
160 
162  std::string name =
163  "ExclusiveConsecutivePairContainer%1%");
164 
165 #ifndef IMP_DOXYGEN
166  IMPCONTAINER_DEPRECATED_METHOD_DECL(2.5)
168  std::string name =
170 #endif
171 
173  virtual ParticleIndexPairs get_range_indexes() const IMP_OVERRIDE;
174  virtual ModelObjectsTemp do_get_inputs() const IMP_OVERRIDE;
175  virtual ParticleIndexes get_all_possible_indexes() const IMP_OVERRIDE;
176  IMP_PAIR_CONTAINER_METHODS(ExclusiveConsecutivePairContainer);
177  IMP_OBJECT_METHODS(ExclusiveConsecutivePairContainer);
178 };
179 
180 /** Check for whether the pair is a member of any
181  ExclusiveConsecutivePairContainer. */
182 class IMPCONTAINEREXPORT ExclusiveConsecutivePairFilter : public PairPredicate {
183  public:
185  : PairPredicate("ExclusiveConsecutivePairFilter %1%") {}
186 
187  virtual int get_value_index(Model *m,
188  const ParticleIndexPair &pip) const
189  IMP_OVERRIDE {
190  return ExclusiveConsecutivePairContainer::get_contains(m, pip);
191  }
193  Model *m, const ParticleIndexes &pi) const IMP_OVERRIDE {
194  ModelObjectsTemp ret;
195  ret += IMP::get_particles(m, pi);
196  return ret;
197  }
200 };
201 
202 IMPCONTAINER_END_NAMESPACE
203 
204 #endif /* IMPCONTAINER_CONSECUTIVE_PAIR_CONTAINER_H */
A shared container for Pairs.
Definition: PairContainer.h:37
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pi) const
Macros for various classes.
A container for Singletons.
A class to store an fixed array of same-typed values.
Definition: Array.h:33
void apply_generic(F *f) const
apply to each item in container
void apply_generic(F *f) const
apply to each item in container
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
A container which contains all consecutive particle pairs from an input list.
ParticlesTemp get_particles(Model *m, const ParticleIndexes &ps)
#define IMP_PAIR_CONTAINER_METHODS(Name)
Definition: pair_macros.h:117
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:72
A container for Pairs.
virtual void do_destroy()
Definition: Object.h:231
Define PairPredicate.
A smart pointer to a ref-counted Object that is a class member.
Definition: Pointer.h:146
Store a list of ParticleIndexPairs.
virtual int get_value_index(Model *m, const ParticleIndexPair &pip) const
Compute the predicate and the derivative if needed.
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing sets of objects.
Definition: object_macros.h:42
Abstract predicate function.
Definition: PairPredicate.h:32
virtual int get_value_index(Model *, const ParticleIndexPair &pip) const
Compute the predicate and the derivative if needed.
virtual ParticleIndexPairs get_indexes() const =0
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pi) const
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.
#define IMP_PAIR_PREDICATE_METHODS(Name)
Define extra the functions needed for a PairPredicate.
Definition: pair_macros.h:47
Various important functionality for implementing decorators.