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