IMP logo
IMP Reference Guide  2.10.0
The Integrative Modeling Platform
Subset.h
Go to the documentation of this file.
1 /**
2  * \file IMP/domino/Subset.h
3  * \brief A Bayesian inference-based sampler.
4  *
5  * Copyright 2007-2018 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPDOMINO_SUBSET_H
10 #define IMPDOMINO_SUBSET_H
11 
12 #include <IMP/domino/domino_config.h>
13 #include "IMP/macros.h"
15 #include <IMP/Pointer.h>
16 #include <IMP/Value.h>
17 #include <IMP/ConstVector.h>
18 #include <algorithm>
19 #include <IMP/hash.h>
20 
21 IMPDOMINO_BEGIN_NAMESPACE
22 
23 //! Represent a subset of the particles being optimized.
24 /** Domino acts by dividing the particles being changed
25  into subsets and optimizing the subsets independently.
26  Each subset is represented using a Subset class. These
27  classes, like the Assignment classes, simply store
28  a constant list (in this case of particles). The list
29  is stored in sorted order. Their interface is more or
30  less that of a constant vector in C++ or
31  a constant list in Python.
32  */
33 class IMPDOMINOEXPORT Subset
34  : public IMP::ConstVector<IMP::WeakPointer<Particle>, Particle *> {
36  static const ParticlesTemp &get_sorted(ParticlesTemp &ps) {
37  std::sort(ps.begin(), ps.end());
38  return ps;
39  }
40 
41  public:
42 #ifndef IMP_DOXYGEN
43  // only use this if particles are sorted already
44  Subset(const ParticlesTemp &ps, bool) : P(ps.begin(), ps.end()) {
46  for (unsigned int i = 0; i < size(); ++i) {
47  IMP_CHECK_OBJECT(ps[i]);
48  }
49  for (unsigned int i = 1; i < ps.size(); ++i) {
50  IMP_INTERNAL_CHECK(ps[i - 1] < ps[i], "Particles not ordered");
51  }
52  }
53  }
54 #endif
55  Subset() {}
56  /** Construct a subset from a non-empty list of particles.
57  */
58  explicit Subset(ParticlesTemp ps) : P(get_sorted(ps)) {
59  IMP_USAGE_CHECK(!ps.empty(), "Do not create empty subsets");
61  std::sort(ps.begin(), ps.end());
62  IMP_USAGE_CHECK(std::unique(ps.begin(), ps.end()) == ps.end(),
63  "Duplicate particles in set");
64  for (unsigned int i = 0; i < ps.size(); ++i) {
65  IMP_CHECK_OBJECT(ps[i]);
66  }
67  }
68  }
69  Model *get_model() const { return operator[](0)->get_model(); }
70  std::string get_name() const;
71  bool get_contains(const Subset &o) const {
72  return std::includes(begin(), end(), o.begin(), o.end());
73  }
74 };
75 
77 IMP_SWAP(Subset);
78 
79 inline Subset get_union(const Subset &a, const Subset &b) {
80  ParticlesTemp pt;
81  set_union(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(pt));
82  return Subset(pt, true);
83 }
84 
85 inline Subset get_intersection(const Subset &a, const Subset &b) {
86  ParticlesTemp pt;
87  set_intersection(a.begin(), a.end(), b.begin(), b.end(),
88  std::back_inserter(pt));
89  if (pt.empty()) {
90  return Subset();
91  } else {
92  return Subset(pt, true);
93  }
94 }
95 
96 inline Subset get_difference(const Subset &a, const Subset &b) {
97  ParticlesTemp rs;
98  std::set_difference(a.begin(), a.end(), b.begin(), b.end(),
99  std::back_inserter(rs));
100  Subset ret(rs, true);
101  return ret;
102 }
103 
104 IMPDOMINO_END_NAMESPACE
105 
106 #endif /* IMPDOMINO_SUBSET_H */
#define IMP_IF_CHECK(level)
Execute the code block if a certain level checks are on.
Definition: check_macros.h:104
Store a list of ParticleIndexes.
Store an array of values of the same type.
#define IMP_CHECK_OBJECT(obj)
Perform some basic validity checks on the object for memory debugging.
Definition: check_macros.h:280
IO support.
Represent a subset of the particles being optimized.
Definition: Subset.h:33
Various general useful macros for IMP.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Definition: check_macros.h:139
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:72
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
Subset(ParticlesTemp ps)
Definition: Subset.h:58
A nullptr-initialized pointer to an IMP Object.
Basic types used by IMP.
Store an array of values of the same type.
Definition: ConstVector.h:27
Class to handle individual particles of a Model object.
Definition: Particle.h:41
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168