IMP  2.0.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 {
32  friend class ConsecutivePairFilter;
33  const ParticleIndexes ps_;
34  IntKey key_;
35  /**
36  add the key of this container as an attribute to all particles
37  if there might be ovrlaps - create a different keys for each instance
38  */
39  void init();
40 
41  bool get_contains(const ParticleIndexPair &p) const {
42  if (!get_model()->get_has_attribute(key_, p[0])) return false;
43  int ia= get_model()->get_attribute(key_, p[0]);
44  if (!get_model()->get_has_attribute(key_, p[1])) return false;
45  int ib= get_model()->get_attribute(key_, p[1]);
46  return std::abs(ia-ib)==1;
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  ParticleIndexPair(ps_[i-1], ps_[i]));
55  }
56  }
57  //! Get the individual particles from the passed SingletonContainer
58  ConsecutivePairContainer(const ParticlesTemp &ps,
59  std::string name="ConsecutivePairContainer%1%");
60 
62 };
63 
65 
66 
67 /** Check for whether the pair is a member of a specific
68  ConsecutivePairContainer. */
69 class IMPCONTAINEREXPORT ConsecutivePairFilter:
70  public PairPredicate {
72  public:
75  IMP_UNUSED(m);
76  return cpc_->get_contains(pi);
77  },{
78  ModelObjectsTemp ret;
79  ret+= IMP::get_particles(m, pi);
80  return ret;
81  });
82 };
83 
84 /** This is an ConsecutivePairContainer where each particle can only be on
85  one ExclusiveConsecutivePairContainer. The exclusivity makes the code
86  more efficient and allows one to use the ExclusiveConsecutivePairFilter,
87  which is way more efficient than using an InContainerPairFilter
88  with a ConsecutivePairContainer.*/
89 class IMPCONTAINEREXPORT ExclusiveConsecutivePairContainer :
90 public PairContainer
91 {
92  friend class ExclusiveConsecutivePairFilter;
93  const ParticleIndexes ps_;
94  static IntKey get_exclusive_key() {
95  static IntKey k("exclusive consecutive numbering");
96  return k;
97  }
98  static ObjectKey get_exclusive_object_key() {
99  static ObjectKey k("exclusive consecutive container");
100  return k;
101  }
102  static bool get_contains(Model *m, const ParticleIndexPair &pp) {
103  ObjectKey ok= ExclusiveConsecutivePairContainer::get_exclusive_object_key();
104  if (!m->get_has_attribute(ok, pp[0])
105  || !m->get_has_attribute(ok, pp[1])) return false;
106  if (m->get_attribute(ok, pp[0]) != m->get_attribute(ok, pp[1])) {
107  return false;
108  }
109  IntKey k= ExclusiveConsecutivePairContainer::get_exclusive_key();
110  int ia= m->get_attribute(k, pp[0]);
111  int ib= m->get_attribute(k, pp[1]);
112  return std::abs(ia-ib)==1;
113  }
114  void init();
115  public:
116  //! apply to each item in container
117  template <class F>
118  void apply_generic(F* f) const {
119  for (unsigned int i=1; i< ps_.size(); ++i) {
120  f->apply_index(get_model(),
121  ParticleIndexPair(ps_[i-1], ps_[i]));
122  }
123  }
124 
125  //! Get the individual particles from the passed SingletonContainer
126  ExclusiveConsecutivePairContainer(const ParticlesTemp &ps,
127  std::string name="ExclusiveConsecutivePairContainer%1%");
128 
130 };
131 
132 /** Check for whether the pair is a member of any
133  ExclusiveConsecutivePairContainer. */
134 class IMPCONTAINEREXPORT ExclusiveConsecutivePairFilter:
135  public PairPredicate {
136  public:
138  PairPredicate("ExclusiveConsecutivePairFilter %1%"){}
140  return ExclusiveConsecutivePairContainer
141  ::get_contains(m, pi);
142  },{
143  ModelObjectsTemp ret;
144  ret+= IMP::get_particles(m, pi);
145  return ret;
146  });
147 };
148 
149 
150 
151 IMPCONTAINER_END_NAMESPACE
152 
153 #endif /* IMPCONTAINER_CONSECUTIVE_PAIR_CONTAINER_H */