IMP logo
IMP Reference Guide  develop.63b38c487d,2024/12/21
The Integrative Modeling Platform
singleton_predicates.h
Go to the documentation of this file.
1 // Autogenerated by ../../../../tmp/nightly-build-465/imp-20241221.develop.63b38c487d/tools/build/make_containers.py
2 // from ../../../../tmp/nightly-build-465/imp-20241221.develop.63b38c487d/tools/build/container_templates/core/classname_predicates.h
3 // Do not edit - any changes will be lost!
4 
5 /**
6  * \file IMP/core/singleton_predicates.h
7  * \brief Define some predicates.
8  *
9  * Copyright 2007-2023 IMP Inventors. All rights reserved.
10  */
11 
12 #ifndef IMPCORE_SINGLETON_PREDICATES_H
13 #define IMPCORE_SINGLETON_PREDICATES_H
14 
15 #include <IMP/core/core_config.h>
16 #include <IMP/core/Typed.h>
17 #include <IMP/SingletonPredicate.h>
18 #include <IMP/singleton_macros.h>
19 #include <boost/random.hpp>
20 #include "internal/container_helpers.h"
21 #include <cereal/access.hpp>
22 #include <cereal/types/base_class.hpp>
23 
24 IMPCORE_BEGIN_NAMESPACE
25 
26 //! Always return a constant value.
27 class IMPCOREEXPORT ConstantSingletonPredicate : public SingletonPredicate {
28  int v_;
29 
30  friend class cereal::access;
31  template<class Archive> void serialize(Archive &ar) {
32  ar(cereal::base_class<SingletonPredicate>(this), v_);
33  }
35 
36  public:
38  std::string name = "ConstSingletonPredicate%1%");
39 
41 
42  virtual int get_value_index(Model *, ParticleIndex) const
43  override {
44  return v_;
45  }
47  Model *, const ParticleIndexes &) const override {
48  return ModelObjectsTemp();
49  }
52 };
53 
54 //! Return a unique predicate value for each unordered set of ParticleTypes
55 /** When applied to a set of particles that are decorated with the
56  Typed decorator, this returns a value that is a function of all of
57  the types. The order of the particles does not matter.
58  */
60  : public SingletonPredicate {
61  friend class cereal::access;
62  template<class Archive> void serialize(Archive &ar) {
63  ar(cereal::base_class<SingletonPredicate>(this));
64  }
66 
67  public:
68  UnorderedTypeSingletonPredicate(std::string name =
69  "UnorderedTypeSingletonPredicate%1%");
70  virtual int get_value_index(Model *m, ParticleIndex pi) const
71  override {
72  return internal::get_type_hash(m, pi);
73  }
75  Model *m, const ParticleIndexes &pis) const override {
76  ModelObjectsTemp ret;
77  ret += IMP::get_particles(m, pis);
78  return ret;
79  }
82 };
83 
84 //! Return a unique predicate value for each ordered set of ParticleTypes
85 /** When applied to a set of particles that are decorated with the
86  Typed decorator, this returns a value that is a function of all of
87  the types. The order of the particles matters.
88  */
89 class IMPCOREEXPORT OrderedTypeSingletonPredicate : public SingletonPredicate {
90  private:
91  mutable int const* cached_particle_type_ids_table_;
92  mutable int cached_n_particle_types_;
93  public:
94  OrderedTypeSingletonPredicate(std::string name =
95  "OrderedTypeSingletonPredicate%1%");
96  //! Compute the predicate for specified types
97  int get_value(const core::ParticleTypes &types) {
98  return internal::get_ordered_type_hash(types);
99  }
100  //! Compute the predicate for types of specific pi
101  virtual int get_value_index(Model *m, ParticleIndex pi) const
102  override {
103  return internal::get_ordered_type_hash(m, pi);
104  }
105 
106  //! Setup for a batch of calls to get_value_index_in_batch()
107  //! (used for improving performance)
109  override{
110  cached_particle_type_ids_table_=
111  m->IMP::internal::IntAttributeTable::access_attribute_data(Typed::get_type_key());
112  cached_n_particle_types_= ParticleType::get_number_unique();
113  };
114 
115  //! Same as get_value_index, but with optimizations
116  //! for a batch of calls. Call setup_for_get_value_index_in_batch()
117  //! right before calling a batch of those, otherwise unexpected behavior.
118  virtual int get_value_index_in_batch(Model* m, ParticleIndex pi) const
119  override{
120  IMP_UNUSED(m);
121  return internal::get_ordered_type_hash( pi,
122  cached_particle_type_ids_table_,
123  cached_n_particle_types_);
124  }
125 
127  Model *m, const ParticleIndexes &pis) const override {
128  ModelObjectsTemp ret;
129  ret += IMP::get_particles(m, pis);
130  return ret;
131  }
134 };
135 
136 //! Return true (1) if all members of the tuple are the same.
137 class IMPCOREEXPORT AllSameSingletonPredicate : public SingletonPredicate {
138  friend class cereal::access;
139  template<class Archive> void serialize(Archive &ar) {
140  ar(cereal::base_class<SingletonPredicate>(this));
141  }
143 
144  public:
145  AllSameSingletonPredicate(std::string name = "AllSameSingletonPredicate%1%");
146  virtual int get_value_index(Model *m, ParticleIndex pi) const
147  override {
148  return internal::get_all_same(m, pi);
149  }
151  Model *, const ParticleIndexes &) const override {
152  return ModelObjectsTemp();
153  }
156 };
157 
158 //! Return true (1) with a fixed probability.
159 class IMPCOREEXPORT CoinFlipSingletonPredicate : public SingletonPredicate {
160  double p_;
161  mutable boost::random::uniform_real_distribution<double> rng_;
162 
163  public:
164  CoinFlipSingletonPredicate(double p, std::string name =
165  "CoinFlipSingletonPredicate%1%");
166  virtual int get_value_index(Model *, ParticleIndex) const
167  override {
168  if (rng_(random_number_generator) < p_)
169  return 1;
170  else
171  return 0;
172  }
174  Model *, const ParticleIndexes &) const override {
175  return ModelObjectsTemp();
176  }
179 };
180 
181 IMPCORE_END_NAMESPACE
182 
183 #endif /* IMPCORE_SINGLETON_PREDICATES_H */
virtual int get_value_index(Model *, ParticleIndex) const override
Compute the predicate and the derivative if needed.
virtual int get_value_index(Model *m, ParticleIndex pi) const override
Compute the predicate and the derivative if needed.
int get_value(const core::ParticleTypes &types)
Compute the predicate for specified types.
virtual void setup_for_get_value_index_in_batch(Model *m) const override
virtual ModelObjectsTemp do_get_inputs(Model *, const ParticleIndexes &) const override
Overload this method to specify the inputs.
Always return a constant value.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
virtual int get_value_index_in_batch(Model *m, ParticleIndex pi) const override
virtual ModelObjectsTemp do_get_inputs(Model *, const ParticleIndexes &) const override
Overload this method to specify the inputs.
Macros for various classes.
ParticlesTemp get_particles(Model *m, const ParticleIndexes &ps)
Get the particles from a list of indexes.
Return a unique predicate value for each ordered set of ParticleTypes.
virtual int get_value_index(Model *, ParticleIndex) const override
Compute the predicate and the derivative if needed.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
A particle with a user-defined type.
Define SingletonPredicate.
IMP::Vector< IMP::WeakPointer< ModelObject > > ModelObjectsTemp
Definition: base_types.h:106
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
#define IMP_UNUSED(variable)
Return true (1) with a fixed probability.
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
Definition: object_macros.h:95
virtual int get_value_index(Model *m, ParticleIndex pi) const override
Compute the predicate for types of specific pi.
virtual int get_value_index(Model *m, ParticleIndex pi) const override
Compute the predicate and the derivative if needed.
#define IMP_SINGLETON_PREDICATE_METHODS(Name)
Define extra the functions needed for a SingletonPredicate.
virtual ModelObjectsTemp do_get_inputs(Model *, const ParticleIndexes &) const override
Overload this method to specify the inputs.
Return a unique predicate value for each unordered set of ParticleTypes.
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const override
Overload this method to specify the inputs.
Abstract predicate function.
Return true (1) if all members of the tuple are the same.
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const override
Overload this method to specify the inputs.
RandomNumberGenerator random_number_generator
A shared non-GPU random number generator.