aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
scheduleCliqueStoreMultiDim_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 >* > >& clique_tables,
40  NodeId clique) :
41  ScheduleOperation< GUM_SCALAR >(ScheduleOperation< GUM_SCALAR >::Type::CLIQUE_STORE_MULTIDIM),
42  _table_(table), _tableSet_(&clique_tables), _clique_(clique), _args_(0) {
43  // for debugging purposes
44  GUM_CONSTRUCTOR(ScheduleCliqueStoreMultiDim);
45  }
46 
47  /// copy constructor
48  template < typename GUM_SCALAR >
49  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::ScheduleCliqueStoreMultiDim(
50  const ScheduleCliqueStoreMultiDim< GUM_SCALAR >& from) :
51  ScheduleOperation< GUM_SCALAR >(from),
52  _table_(from._table_), _tableSet_(from._tableSet_), _clique_(from._clique_), _args_(0) {
53  // for debugging purposes
54  GUM_CONS_CPY(ScheduleCliqueStoreMultiDim);
55  }
56 
57  /// virtual copy constructor: creates a clone of the operation
58  template < typename GUM_SCALAR >
59  ScheduleCliqueStoreMultiDim< GUM_SCALAR >*
60  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::newFactory() const {
61  return new ScheduleCliqueStoreMultiDim< GUM_SCALAR >(*this);
62  }
63 
64  /// destructor
65  template < typename GUM_SCALAR >
66  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::~ScheduleCliqueStoreMultiDim() {
67  // for debugging purposes
68  GUM_DESTRUCTOR(ScheduleCliqueStoreMultiDim);
69 
70  if (_args_) delete _args_;
71  }
72 
73  /// copy operator
74  template < typename GUM_SCALAR >
75  ScheduleCliqueStoreMultiDim< GUM_SCALAR >& ScheduleCliqueStoreMultiDim< GUM_SCALAR >::operator=(
76  const ScheduleCliqueStoreMultiDim< GUM_SCALAR >& from) {
77  // avoid self assignment
78  if (&from != this) {
79  ScheduleOperation< GUM_SCALAR >::operator=(from);
80  _table_ = from._table_;
81  _tableSet_ = from._tableSet_;
82  _clique_ = from._clique_;
83 
84  if (_args_) {
85  _args_->clear();
86  _args_->insert(&_table_);
87  }
88  }
89 
90  return *this;
91  }
92 
93  /// operator ==
94  template < typename GUM_SCALAR >
95  bool ScheduleCliqueStoreMultiDim< GUM_SCALAR >::operator==(
96  const ScheduleOperation< GUM_SCALAR >& op) const {
97  if (this->type() != op.type()) return false;
98 
99  const ScheduleCliqueStoreMultiDim< GUM_SCALAR >& real_op
100  = static_cast< const ScheduleCliqueStoreMultiDim< GUM_SCALAR >& >(op);
101  return ((_table_ == real_op._table_) && (_tableSet_ == real_op._tableSet_)
102  && (_clique_ == real_op._clique_));
103  }
104 
105  /// operator !=
106  template < typename GUM_SCALAR >
107  bool ScheduleCliqueStoreMultiDim< GUM_SCALAR >::operator!=(
108  const ScheduleOperation< GUM_SCALAR >& op) const {
109  if (this->type() != op.type()) return true;
110 
111  const ScheduleCliqueStoreMultiDim< GUM_SCALAR >& real_op
112  = static_cast< const ScheduleCliqueStoreMultiDim< GUM_SCALAR >& >(op);
113  return ((_table_ != real_op._table_) || (_tableSet_ != real_op._tableSet_)
114  || (_clique_ != real_op._clique_));
115  }
116 
117  /// executes the operation
118  template < typename GUM_SCALAR >
119  void ScheduleCliqueStoreMultiDim< GUM_SCALAR >::execute() {
120  const MultiDimImplementation< GUM_SCALAR >& multidim = _table_.multiDim();
121 
122  if (!_tableSet_->exists(_clique_)) {
123  _tableSet_->insert(_clique_, Set< const MultiDimImplementation< GUM_SCALAR >* >());
124  }
125 
126  _tableSet_->operator[](_clique_).insert(&multidim);
127  }
128 
129  /** @brief returns an estimation of the number of elementary operations
130  * needed to perform the ScheduleOperation */
131  template < typename GUM_SCALAR >
132  INLINE float ScheduleCliqueStoreMultiDim< GUM_SCALAR >::nbOperations() const {
133  return 1.0f;
134  }
135 
136  /// returns the memory consumption used during the operation
137  template < typename GUM_SCALAR >
138  INLINE std::pair< long, long > ScheduleCliqueStoreMultiDim< GUM_SCALAR >::memoryUsage() const {
139  return std::pair< long, long >(0, 0);
140  }
141 
142  /// returns the multidim to be stored
143  template < typename GUM_SCALAR >
144  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
145  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::multiDimArgs() const {
146  if (!_args_) {
147  _args_ = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
148  _args_->insert(&_table_);
149  }
150 
151  return *_args_;
152  }
153 
154  /// returns the set of multidims that should be the result of the operation
155  template < typename GUM_SCALAR >
156  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
157  ScheduleCliqueStoreMultiDim< GUM_SCALAR >::multiDimResults() const {
158  static Sequence< const ScheduleMultiDim< GUM_SCALAR >* > empty_seq;
159 # ifdef GUM_DEBUG_MODE
160  // for debugging purposes, we should inform the aGrUM's debugger that
161  // the static sequence used here will be removed at the end of the
162  // program's execution.
163  static bool first_time = true;
164 
165  if (first_time) {
166  first_time = false;
167  __debug__::_inc_deletion_("Sequence", __FILE__, __LINE__, "destructor of", (void*)&empty_seq);
168  __debug__::_inc_deletion_("SequenceImplementation",
169  __FILE__,
170  __LINE__,
171  "destructor of",
172  (void*)&empty_seq);
173  }
174 
175 # endif /* GUM_DEBUG_MODE */
176  return empty_seq;
177  }
178 
179  /// displays the content of the operation
180  template < typename GUM_SCALAR >
181  std::string ScheduleCliqueStoreMultiDim< GUM_SCALAR >::toString() const {
182  std::stringstream s;
183  s << "store ( " << _table_.toString() << ", clique " << _clique_ << " )";
184  return s.str();
185  }
186 
187 } /* namespace gum */
188 
189 #endif /* DOXYGEN_SHOULD_SKIP_THIS */