IMP  2.3.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-2014 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/base/Pointer.h>
16 #include <IMP/base/Value.h>
17 #include <IMP/base/ConstVector.h>
18 #include <algorithm>
19 #include <IMP/base/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 base::ConstVector<base::WeakPointer<kernel::Particle>,
35  kernel::Particle *> {
38  static const kernel::ParticlesTemp &get_sorted(kernel::ParticlesTemp &ps) {
39  std::sort(ps.begin(), ps.end());
40  return ps;
41  }
42 
43  public:
44 #ifndef IMP_DOXYGEN
45  // only use this if particles are sorted already
46  Subset(const kernel::ParticlesTemp &ps, bool) : P(ps.begin(), ps.end()) {
48  for (unsigned int i = 0; i < size(); ++i) {
49  IMP_CHECK_OBJECT(ps[i]);
50  }
51  for (unsigned int i = 1; i < ps.size(); ++i) {
52  IMP_INTERNAL_CHECK(ps[i - 1] < ps[i], "Particles not ordered");
53  }
54  }
55  }
56 #endif
57  Subset() {}
58  /** Construct a subset from a non-empty list of particles.
59  */
60  explicit Subset(kernel::ParticlesTemp ps) : P(get_sorted(ps)) {
61  IMP_USAGE_CHECK(!ps.empty(), "Do not create empty subsets");
63  std::sort(ps.begin(), ps.end());
64  IMP_USAGE_CHECK(std::unique(ps.begin(), ps.end()) == ps.end(),
65  "Duplicate particles in set");
66  for (unsigned int i = 0; i < ps.size(); ++i) {
67  IMP_CHECK_OBJECT(ps[i]);
68  }
69  }
70  }
71  kernel::Model *get_model() const { return operator[](0)->get_model(); }
72  std::string get_name() const;
73  bool get_contains(const Subset &o) const {
74  return std::includes(begin(), end(), o.begin(), o.end());
75  }
76 };
77 
79 IMP_SWAP(Subset);
80 
81 inline Subset get_union(const Subset &a, const Subset &b) {
83  set_union(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(pt));
84  return Subset(pt, true);
85 }
86 
87 inline Subset get_intersection(const Subset &a, const Subset &b) {
89  set_intersection(a.begin(), a.end(), b.begin(), b.end(),
90  std::back_inserter(pt));
91  if (pt.empty()) {
92  return Subset();
93  } else {
94  return Subset(pt, true);
95  }
96 }
97 
98 inline Subset get_difference(const Subset &a, const Subset &b) {
99  kernel::ParticlesTemp rs;
100  std::set_difference(a.begin(), a.end(), b.begin(), b.end(),
101  std::back_inserter(rs));
102  Subset ret(rs, true);
103  return ret;
104 }
105 
106 IMPDOMINO_END_NAMESPACE
107 
108 #endif /* IMPDOMINO_SUBSET_H */
#define IMP_IF_CHECK(level)
Execute the code block if a certain level checks are on.
Definition: check_macros.h:106
Store a list of kernel::ParticlesTemp.
A beyesian infererence-based sampler.
#define IMP_CHECK_OBJECT(obj)
Perform some basic validity checks on the object for memory debugging.
Definition: check_macros.h:282
IO support.
Represent a subset of the particles being optimized.
Definition: Subset.h:33
Import IMP/kernel/macros.h in the namespace.
Subset(kernel::ParticlesTemp ps)
Definition: Subset.h:60
Store an array of values of the same type.
Definition: ConstVector.h:27
#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:141
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
Class to handle individual model particles.
A nullptr-initialized pointer to an IMP Object.
Basic types used by IMP.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:170
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:73