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