IMP  2.0.1
The Integrative Modeling Platform
Subset.h
Go to the documentation of this file.
1 /**
2  * \file IMP/domino/Subset.h
3  * \brief A beyesian infererence-based sampler.
4  *
5  * Copyright 2007-2013 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<Particle>,
35  Particle*> {
37  static const ParticlesTemp &get_sorted(ParticlesTemp &ps) {
38  std::sort(ps.begin(), ps.end());
39  return ps;
40  }
41  public:
42 #ifndef IMP_DOXYGEN
43  // only use this if particles are sorted already
44  Subset(const ParticlesTemp &ps, bool):
45  P(ps.begin(), ps.end()) {
47  for (unsigned int i=0; i< size(); ++i) {
48  IMP_CHECK_OBJECT(ps[i]);
49  }
50  for (unsigned int i=1; i< ps.size(); ++i) {
51  IMP_INTERNAL_CHECK(ps[i-1] < ps[i], "Particles not ordered");
52  }
53  }
54  }
55 #endif
56  Subset(){}
57  /** Construct a subset from a non-empty list of particles.
58  */
59  explicit Subset(ParticlesTemp ps): P(get_sorted(ps)) {
60  IMP_USAGE_CHECK(!ps.empty(), "Do not create empty subsets");
62  std::sort(ps.begin(), ps.end());
63  IMP_USAGE_CHECK(std::unique(ps.begin(), ps.end()) == ps.end(),
64  "Duplicate particles in set");
65  for (unsigned int i=0; i< ps.size(); ++i) {
66  IMP_CHECK_OBJECT(ps[i]);
67  }
68  }
69  }
70  Model *get_model() const {
71  return operator[](0)->get_model();
72  }
73  std::string get_name() const;
74  bool get_contains(const Subset &o) const {
75  return std::includes(begin(), end(), o.begin(), o.end());
76  }
77 };
78 
80 IMP_SWAP(Subset);
81 
82 inline
83 Subset get_union(const Subset &a, const Subset &b) {
84  ParticlesTemp pt;
85  set_union(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(pt));
86  return Subset(pt, true);
87 }
88 
89 inline
90 Subset get_intersection(const Subset &a, const Subset &b) {
91  ParticlesTemp pt;
92  set_intersection(a.begin(), a.end(), b.begin(), b.end(),
93  std::back_inserter(pt));
94  if (pt.empty()) {
95  return Subset();
96  } else {
97  return Subset(pt, true);
98  }
99 }
100 
101 
102 inline Subset get_difference(const Subset &a, const Subset &b) {
103  ParticlesTemp rs;
104  std::set_difference(a.begin(), a.end(),
105  b.begin(), b.end(),
106  std::back_inserter(rs));
107  Subset ret(rs, true);
108  return ret;
109 }
110 
111 IMPDOMINO_END_NAMESPACE
112 
113 #endif /* IMPDOMINO_SUBSET_H */