IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
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>
17#include <boost/unordered_map.hpp>
18#include <IMP/pair_macros.h>
19
20IMPCONTAINER_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*/
29class 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;
67 IMP_PAIR_CONTAINER_METHODS(ConsecutivePairContainer);
68 IMP_OBJECT_METHODS(ConsecutivePairContainer);
69};
70
72
73/** Check for whether the pair is a member of a specific
74 ConsecutivePairContainer. */
75class 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 {
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.*/
104class 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;
164 IMP_PAIR_CONTAINER_METHODS(ExclusiveConsecutivePairContainer);
165 IMP_OBJECT_METHODS(ExclusiveConsecutivePairContainer);
166};
167
168/** Check for whether the pair is a member of any
169 ExclusiveConsecutivePairContainer. */
170class IMPCONTAINEREXPORT ExclusiveConsecutivePairFilter : public PairPredicate {
171 public:
172 ExclusiveConsecutivePairFilter()
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 {
183 ret += IMP::get_particles(m, pi);
184 return ret;
185 }
187 IMP_OBJECT_METHODS(ExclusiveConsecutivePairFilter);
188};
189
190IMPCONTAINER_END_NAMESPACE
191
192#endif /* IMPCONTAINER_CONSECUTIVE_PAIR_CONTAINER_H */
Store a list of ParticleIndexPairs.
A container for Pairs.
Define PairPredicate.
A container for Singletons.
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
A container which contains all consecutive particle pairs from an input list.
virtual ParticleIndexPairs get_indexes() const override
void apply_generic(F *f) const
apply to each item in container
virtual ParticleIndexPairs get_range_indexes() const override
virtual ModelObjectsTemp do_get_inputs() const override
virtual ParticleIndexes get_all_possible_indexes() const override
Get contained particles.
ConsecutivePairFilter(ConsecutivePairContainer *cpc)
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 *, const ParticleIndexPair &pip) const override
Compute the predicate and the derivative if needed.
virtual ParticleIndexPairs get_range_indexes() const override
void apply_generic(F *f) const
apply to each item in container
virtual ParticleIndexPairs get_indexes() const override
virtual ParticleIndexes get_all_possible_indexes() const override
Get contained particles.
virtual ModelObjectsTemp do_get_inputs() const override
virtual int get_value_index(Model *m, const ParticleIndexPair &pip) const override
Compute the predicate and the derivative if needed.
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pi) const override
Overload this method to specify the inputs.
Compile-time generic restraint and constraint support.
IMP::Vector< IMP::Pointer< ConsecutivePairContainer > > ConsecutivePairContainers
ParticlesTemp get_particles(Model *m, const ParticleIndexes &ps)
Get the particles from a list of indexes.
IMP::Vector< ParticleIndexPair, std::allocator< ParticleIndexPair > > ParticleIndexPairs
Definition base_types.h:208
Vector< ParticleIndex, std::allocator< ParticleIndex > > ParticleIndexes
A list of particles, each identified by its ParticleIndex.
Definition base_types.h:199
Array< 2, ParticleIndex > ParticleIndexPair
An ordered pair of particles, each identified by its ParticleIndex.
Definition base_types.h:202
Key< 4 > ObjectKey
The type used to identify an Object attribute.
Definition base_types.h:48
Key< 1 > IntKey
The type used to identify int attributes in the Particles.
Definition base_types.h:36
IMP::Vector< IMP::WeakPointer< ModelObject > > ModelObjectsTemp
Definition base_types.h:122
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing lists of object pointers.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Macros for various classes.
#define IMP_PAIR_CONTAINER_METHODS(Name)
#define IMP_PAIR_PREDICATE_METHODS(Name)
Define extra the functions needed for a PairPredicate.
A smart pointer to a ref-counted Object that is a class member.
Definition Pointer.h:144