IMP logo
IMP Reference Guide  develop.d36d3deffb,2026/11/09
The Integrative Modeling Platform
PredicateSingletonsRestraint.h
Go to the documentation of this file.
1 // Autogenerated by ../../../../tmp/nightly-build-391/imp-20261109.develop.d36d3deffb/tools/build/make_containers.py
2 // from ../../../../tmp/nightly-build-391/imp-20261109.develop.d36d3deffb/tools/build/container_templates/container/PredicateClassnamesRestraint.h
3 // Do not edit - any changes will be lost!
4 
5 /**
6  * \file IMP/container/PredicateSingletonsRestraint.h
7  * \brief Apply a SingletonScore to each Singleton in a list.
8  *
9  * Copyright 2007-2026 IMP Inventors. All rights reserved.
10  *
11  */
12 
13 #ifndef IMPCONTAINER_PREDICATE_SINGLETONS_RESTRAINT_H
14 #define IMPCONTAINER_PREDICATE_SINGLETONS_RESTRAINT_H
15 
16 #include <IMP/container/container_config.h>
17 #include <IMP/container/internal/robin_map_cereal.h>
18 #include <IMP/container/internal/dense_hash_map_cereal.h>
19 #include <IMP/SingletonPredicate.h>
20 #include "generic.h"
21 #include <cereal/access.hpp>
22 #include <cereal/types/base_class.hpp>
23 #include <cereal/types/map.hpp>
24 
25 
26 #if IMP_CONTAINER_HAS_ROBIN_MAP==1
27 #define IMP_CONTAINER_PREDICATE_USE_ROBIN_MAP
28 #include <tsl/robin_map.h>
29 #elif IMP_CONTAINER_HAS_GOOGLE_DENSE_HASH_MAP==1
30 #define IMP_CONTAINER_PREDICATE_USE_GOOGLE_DENSE_HASH_MAP
31 #include <google/dense_hash_map>
32 #else
33 #define IMP_CONTAINER_PREDICATE_USE_KERNEL_MAP
34 #include <IMP/set_map_macros.h>
35 //#include <boost/unordered_map.hpp>
36 #endif
37 
38 #include <iostream>
39 
40 
41 IMPCONTAINER_BEGIN_NAMESPACE
42 
43 //! Applies a SingletonScore to each Singleton in a list based on a predicate
44 /** This restraint uses a passed predicate to choose which score to apply
45  to each tuple in the input container. The selections are cached, making it
46  substantially faster than using a core::TypedPairScore.
47 
48  \note The ordering of particles within a tuple may vary depending on the
49  input container used. You may need to call set_score() with several
50  different predicate values for different orderings.
51 */
52 class IMPCONTAINEREXPORT PredicateSingletonsRestraint : public Restraint {
55 #if IMP_CONTAINER_HAS_ROBIN_MAP==1
56  typedef tsl::robin_map<int, ParticleIndexes> t_lists_map;
57  typedef tsl::robin_map<int, PointerMember<SingletonScore>> t_score_map;
58 #elif IMP_CONTAINER_HAS_GOOGLE_DENSE_HASH_MAP==1
59  typedef google::dense_hash_map<int, ParticleIndexes> t_lists_map;
60  typedef google::dense_hash_map<int, PointerMember<SingletonScore>> t_score_map;
61 #else
62  typedef IMP_KERNEL_LARGE_UNORDERED_MAP<int, ParticleIndexes> t_lists_map;
63  typedef IMP_KERNEL_LARGE_UNORDERED_MAP<int, PointerMember<SingletonScore>> t_score_map;
64 #endif
65  mutable t_lists_map lists_;
66  t_score_map scores_;
67 
68  bool is_get_inputs_ignores_individual_scores_;
69  mutable std::size_t input_version_;
70  // bool is_unknown_score_set_;
71  bool error_on_unknown_;
72  PointerMember<SingletonScore> unknown_score_;
73 
74  friend class cereal::access;
75  template<class Archive> void serialize(Archive &ar) {
76  ar(cereal::base_class<Restraint>(this),
77  predicate_, input_, scores_,
78  is_get_inputs_ignores_individual_scores_,
79  error_on_unknown_, unknown_score_);
80  if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
81  // Force lists_ to be rebuilt (from scores_)
82  lists_.clear();
83  input_version_ = -1;
84  }
85  }
87 
88  // void update_lists_with_item(ParticleIndex const &it) const;
89  void update_lists_if_necessary() const;
90 
91  public:
94  std::string name =
95  "PredicateSingletonsRestraint %1%");
96 
98 #ifdef IMP_CONTAINER_PREDICATE_USE_GOOGLE_DENSE_HASH_MAP
99  lists_.set_empty_key(-1);
100  scores_.set_empty_key(-1);
101 #endif
102  }
103 
104  /** Apply the passed score to all pairs whose predicate values match
105  the passed value.*/
106  void set_score(int predicate_value, SingletonScore *score);
107 
108  /** Apply this score to any pair whose predicate value does not match
109  one passed to set_score().*/
110  void set_unknown_score(SingletonScore *score);
111 
112  /** By default, it is an error if the predicate returns a value that is
113  not known. If this is false, then they are silently skipped even
114  if set_unknown_score() was not called.
115  */
116  void set_is_complete(bool tf) { error_on_unknown_ = tf; }
117 
118  /** return the indexes of all particles for a given predicate value.*/
119  ParticleIndexes get_indexes(int predicate_value) const {
120  return lists_.find(predicate_value)->second;
121  }
122 
123  public:
124  void do_add_score_and_derivatives(IMP::ScoreAccumulator sa) const
125  override;
126  IMP::ModelObjectsTemp do_get_inputs() const override;
127 
128 #ifndef SWIG
129  //! use at own risk - for efficiency in case e.g. all scores use same inputs
130  //! If true, do_get_inputs() includes all inputs in input container
131  //! but not individual inputs from individual scores
133  is_get_inputs_ignores_individual_scores_= is_ignore;
134  }
135 #endif
137 
138  private:
139  /**
140  Returns the score for specified predicate based on set_score()
141 
142  Unknown scores:
143  If predicate was not added with set_score(), return the default unknown score
144  set by set_unknown_score() if applicable. If not, then if set_is_complete(false)
145  was called, return nullptr. Otherwise, undefined behavior (will fail when checks
146  are on)
147  */
148  inline SingletonScore*
149  get_score_for_predicate(int predicate) const;
150 
151 
153 };
154 
155 
156 #ifndef SWIG
158 PredicateSingletonsRestraint::get_score_for_predicate
159 (int predicate) const
160 {
161  SingletonScore* score;
162  t_score_map::const_iterator it= scores_.find(predicate);
163  if(it!=scores_.end()){
164  score= it->second;
165  } else {
166  IMP_USAGE_CHECK(!error_on_unknown_, // is_unknown_score_set_,
167  "unknown interaction type cannot be scored when error_on_unknown is true");
168  score = unknown_score_;
169  }
170  return score;
171 }
172 #endif
173 
174 
175 IMPCONTAINER_END_NAMESPACE
176 
177 #endif /* IMPCONTAINER_PREDICATE_SINGLETONS_RESTRAINT_H */
Various important functionality for implementing decorators.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Macros to choose the best set or map for different purposes.
Define SingletonPredicate.
virtual Restraints do_create_current_decomposition() const
Definition: Restraint.h:329
Abstract class for scoring object(s) of type ParticleIndex.
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
Definition: object_macros.h:95
A smart pointer to a ref-counted Object that is a class member.
Definition: Pointer.h:143
Class for adding up scores during ScoringFunction evaluation.
Applies a SingletonScore to each Singleton in a list based on a predicate.
Abstract predicate function.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
ParticleIndexes get_indexes(int predicate_value) const
virtual ModelObjectsTemp do_get_inputs() const =0
A restraint is a term in an IMP ScoringFunction.
Definition: Restraint.h:56