aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
scheduleCliqueStoreMultiDim_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief an operator used by scheduled inferences to store tables into cliques
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 # include <agrum/agrum.h>
31 # include <sstream>
32 
33 namespace gum {
34 
35  /// default constructor
36  template < typename GUM_SCALAR >
37  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::ScheduleCliqueStoreMultiDim(
38  const ScheduleMultiDim< GUM_SCALAR >& table,
39  NodeProperty< Set< const MultiDimImplementation< GUM_SCALAR >* > >&
40  clique_tables,
41  NodeId clique) :
42  ScheduleOperation< GUM_SCALAR >(
43  ScheduleOperation< GUM_SCALAR >::Type::CLIQUE_STORE_MULTIDIM),
44  table__(table), tableSet__(&clique_tables), clique__(clique), args__(0) {
45  // for debugging purposes
46  GUM_CONSTRUCTOR(ScheduleCliqueStoreMultiDim);
47  }
48 
49  /// copy constructor
50  template < typename GUM_SCALAR >
51  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::ScheduleCliqueStoreMultiDim(
52  const ScheduleCliqueStoreMultiDim< GUM_SCALAR >& from) :
53  ScheduleOperation< GUM_SCALAR >(from),
54  table__(from.table__), tableSet__(from.tableSet__), clique__(from.clique__),
55  args__(0) {
56  // for debugging purposes
57  GUM_CONS_CPY(ScheduleCliqueStoreMultiDim);
58  }
59 
60  /// virtual copy constructor: creates a clone of the operation
61  template < typename GUM_SCALAR >
62  ScheduleCliqueStoreMultiDim< GUM_SCALAR >*
63  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::newFactory() const {
64  return new ScheduleCliqueStoreMultiDim< GUM_SCALAR >(*this);
65  }
66 
67  /// destructor
68  template < typename GUM_SCALAR >
69  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::~ScheduleCliqueStoreMultiDim() {
70  // for debugging purposes
71  GUM_DESTRUCTOR(ScheduleCliqueStoreMultiDim);
72 
73  if (args__) delete args__;
74  }
75 
76  /// copy operator
77  template < typename GUM_SCALAR >
78  ScheduleCliqueStoreMultiDim< GUM_SCALAR >&
79  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::operator=(
80  const ScheduleCliqueStoreMultiDim< GUM_SCALAR >& from) {
81  // avoid self assignment
82  if (&from != this) {
83  ScheduleOperation< GUM_SCALAR >::operator=(from);
84  table__ = from.table__;
85  tableSet__ = from.tableSet__;
86  clique__ = from.clique__;
87 
88  if (args__) {
89  args__->clear();
90  args__->insert(&table__);
91  }
92  }
93 
94  return *this;
95  }
96 
97  /// operator ==
98  template < typename GUM_SCALAR >
99  bool ScheduleCliqueStoreMultiDim< GUM_SCALAR >::operator==(
100  const ScheduleOperation< GUM_SCALAR >& op) const {
101  if (this->type() != op.type()) return false;
102 
103  const ScheduleCliqueStoreMultiDim< GUM_SCALAR >& real_op
104  = static_cast< const ScheduleCliqueStoreMultiDim< GUM_SCALAR >& >(op);
105  return ((table__ == real_op.table__) && (tableSet__ == real_op.tableSet__)
106  && (clique__ == real_op.clique__));
107  }
108 
109  /// operator !=
110  template < typename GUM_SCALAR >
111  bool ScheduleCliqueStoreMultiDim< GUM_SCALAR >::operator!=(
112  const ScheduleOperation< GUM_SCALAR >& op) const {
113  if (this->type() != op.type()) return true;
114 
115  const ScheduleCliqueStoreMultiDim< GUM_SCALAR >& real_op
116  = static_cast< const ScheduleCliqueStoreMultiDim< GUM_SCALAR >& >(op);
117  return ((table__ != real_op.table__) || (tableSet__ != real_op.tableSet__)
118  || (clique__ != real_op.clique__));
119  }
120 
121  /// executes the operation
122  template < typename GUM_SCALAR >
123  void ScheduleCliqueStoreMultiDim< GUM_SCALAR >::execute() {
124  const MultiDimImplementation< GUM_SCALAR >& multidim = table__.multiDim();
125 
126  if (!tableSet__->exists(clique__)) {
127  tableSet__->insert(clique__,
128  Set< const MultiDimImplementation< GUM_SCALAR >* >());
129  }
130 
131  tableSet__->operator[](clique__).insert(&multidim);
132  }
133 
134  /** @brief returns an estimation of the number of elementary operations
135  * needed to perform the ScheduleOperation */
136  template < typename GUM_SCALAR >
137  INLINE float ScheduleCliqueStoreMultiDim< GUM_SCALAR >::nbOperations() const {
138  return 1.0f;
139  }
140 
141  /// returns the memory consumption used during the operation
142  template < typename GUM_SCALAR >
143  INLINE std::pair< long, long >
144  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::memoryUsage() const {
145  return std::pair< long, long >(0, 0);
146  }
147 
148  /// returns the multidim to be stored
149  template < typename GUM_SCALAR >
150  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
151  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::multiDimArgs() const {
152  if (!args__) {
153  args__ = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
154  args__->insert(&table__);
155  }
156 
157  return *args__;
158  }
159 
160  /// returns the set of multidims that should be the result of the operation
161  template < typename GUM_SCALAR >
162  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
163  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::multiDimResults() const {
164  static Sequence< const ScheduleMultiDim< GUM_SCALAR >* > empty_seq;
165 # ifdef GUM_DEBUG_MODE
166  // for debugging purposes, we should inform the aGrUM's debugger that
167  // the static sequence used here will be removed at the end of the
168  // program's execution.
169  static bool first_time = true;
170 
171  if (first_time) {
172  first_time = false;
173  __debug__::inc_deletion__("Sequence",
174  __FILE__,
175  __LINE__,
176  "destructor of",
177  (void*)&empty_seq);
178  __debug__::inc_deletion__("SequenceImplementation",
179  __FILE__,
180  __LINE__,
181  "destructor of",
182  (void*)&empty_seq);
183  }
184 
185 # endif /* GUM_DEBUG_MODE */
186  return empty_seq;
187  }
188 
189  /// displays the content of the operation
190  template < typename GUM_SCALAR >
191  std::string ScheduleCliqueStoreMultiDim< GUM_SCALAR >::toString() const {
192  std::stringstream s;
193  s << "store ( " << table__.toString() << ", clique " << clique__ << " )";
194  return s.str();
195  }
196 
197 } /* namespace gum */
198 
199 #endif /* DOXYGEN_SHOULD_SKIP_THIS */