IMP logo
IMP Reference Guide  develop.63b38c487d,2024/12/21
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-2022 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_, std::get<0>(p))) return false;
41  int ia = get_model()->get_attribute(key_, std::get<0>(p));
42  if (!get_model()->get_has_attribute(key_, std::get<1>(p))) return false;
43  int ib = get_model()->get_attribute(key_, std::get<1>(p));
44  return std::abs(ia - ib) == 1;
45  }
46 
47  protected:
48  virtual std::size_t do_get_contents_hash() const 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  virtual ParticleIndexPairs get_indexes() const override;
64  virtual ParticleIndexPairs get_range_indexes() const override;
65  virtual ModelObjectsTemp do_get_inputs() const override;
66  virtual ParticleIndexes get_all_possible_indexes() const override;
69 };
70 
72 
73 /** Check for whether the pair is a member of a specific
74  ConsecutivePairContainer. */
75 class IMPCONTAINEREXPORT ConsecutivePairFilter : public PairPredicate {
77 
78  public:
79  /** @param cpc the consecutive pair container that stores
80  filtered pairs
81  */
83 
84  virtual int get_value_index(Model *,
85  const ParticleIndexPair &pip) const
86  override {
87  return cpc_->get_contains(pip);
88  }
90  Model *m, const ParticleIndexes &pi) const override {
91  ModelObjectsTemp ret;
92  ret += IMP::get_particles(m, pi);
93  return ret;
94  }
97 };
98 
99 /** This is an ConsecutivePairContainer where each particle can only be on
100  one ExclusiveConsecutivePairContainer. The exclusivity makes the code
101  more efficient and allows one to use the ExclusiveConsecutivePairFilter,
102  which is way more efficient than using an InContainerPairFilter
103  with a ConsecutivePairContainer.*/
104 class IMPCONTAINEREXPORT ExclusiveConsecutivePairContainer
105  : public PairContainer {
106  friend class ExclusiveConsecutivePairFilter;
107  const ParticleIndexes ps_;
108  static IntKey get_exclusive_key() {
109  static IntKey k("exclusive consecutive numbering");
110  return k;
111  }
112  static ObjectKey get_exclusive_object_key() {
113  static ObjectKey k("exclusive consecutive container");
114  return k;
115  }
116  static bool get_contains(Model *m,
117  const ParticleIndexPair &pp) {
118  ObjectKey ok =
119  ExclusiveConsecutivePairContainer::get_exclusive_object_key();
120  bool has_eok_0 = m->get_has_attribute(ok, std::get<0>(pp));
121  bool has_eok_1= m->get_has_attribute(ok, std::get<1>(pp));
122  if ( !has_eok_0 || !has_eok_1 )
123  return false;
124  if (m->get_attribute(ok, std::get<0>(pp))
125  != m->get_attribute(ok, std::get<1>(pp))) {
126  return false;
127  }
128  IntKey k = ExclusiveConsecutivePairContainer::get_exclusive_key();
129  int ia = m->get_attribute(k, std::get<0>(pp));
130  int ib = m->get_attribute(k, std::get<1>(pp));
131  return std::abs(ia - ib) == 1;
132  }
133  void init();
134 
135  protected:
136  virtual std::size_t do_get_contents_hash() const override { return 0; }
137 
138  /**
139  Called by Object destructor - removes all keys associated with
140  the exclusive consecutive pair container, so it can be now added to
141  another exclusive consecutive pair container
142  */
143  virtual void do_destroy() override;
144 
145 
146  public:
147  //! apply to each item in container
148  template <class F>
149  void apply_generic(F *f) const {
150  for (unsigned int i = 1; i < ps_.size(); ++i) {
151  f->apply_index(get_model(),
152  ParticleIndexPair(ps_[i - 1], ps_[i]));
153  }
154  }
155 
157  std::string name =
158  "ExclusiveConsecutivePairContainer%1%");
159 
160  virtual ParticleIndexPairs get_indexes() const override;
161  virtual ParticleIndexPairs get_range_indexes() const override;
162  virtual ModelObjectsTemp do_get_inputs() const override;
163  virtual ParticleIndexes get_all_possible_indexes() const override;
166 };
167 
168 /** Check for whether the pair is a member of any
169  ExclusiveConsecutivePairContainer. */
170 class IMPCONTAINEREXPORT ExclusiveConsecutivePairFilter : public PairPredicate {
171  public:
173  : PairPredicate("ExclusiveConsecutivePairFilter %1%") {}
174 
175  virtual int get_value_index(Model *m,
176  const ParticleIndexPair &pip) const
177  override {
178  return ExclusiveConsecutivePairContainer::get_contains(m, pip);
179  }
181  Model *m, const ParticleIndexes &pi) const override {
182  ModelObjectsTemp ret;
183  ret += IMP::get_particles(m, pi);
184  return ret;
185  }
188 };
189 
190 IMPCONTAINER_END_NAMESPACE
191 
192 #endif /* IMPCONTAINER_CONSECUTIVE_PAIR_CONTAINER_H */
virtual ParticleIndexPairs get_range_indexes() const =0
A shared container for Pairs.
Definition: PairContainer.h:39
Macros for various classes.
A container for Singletons.
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)
Get the particles from a list of indexes.
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pi) const override
Overload this method to specify the inputs.
virtual int get_value_index(Model *m, const ParticleIndexPair &pip) const override
Compute the predicate and the derivative if needed.
#define IMP_PAIR_CONTAINER_METHODS(Name)
Definition: pair_macros.h:154
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pi) const override
Overload this method to specify the inputs.
A container for Pairs.
virtual void do_destroy()
Definition: Object.h:274
Define PairPredicate.
A smart pointer to a ref-counted Object that is a class member.
Definition: Pointer.h:143
virtual int get_value_index(Model *, const ParticleIndexPair &pip) const override
Compute the predicate and the derivative if needed.
Store a list of ParticleIndexPairs.
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing lists of object pointers.
Definition: object_macros.h:44
virtual ParticleIndexes get_all_possible_indexes() const =0
Get contained particles.
Abstract predicate function.
Definition: PairPredicate.h:31
virtual ParticleIndexPairs get_indexes() const =0
bool get_has_attribute(TypeKey attribute_key, ParticleIndex particle) const
return true if particle has attribute with the specified key
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
get the value of the particle attribute with the specified key
virtual ModelObjectsTemp do_get_inputs() const =0
#define IMP_PAIR_PREDICATE_METHODS(Name)
Define extra the functions needed for a PairPredicate.
Definition: pair_macros.h:78
Compile-time generic restraint and constraint support.