aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
multiDimArray.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48#ifndef GUM_MULTI_DIM_ARRAY_H
49#define GUM_MULTI_DIM_ARRAY_H
50
51#include <vector>
52
53#include <agrum/agrum.h>
54
56
57namespace gum {
58
59 template < typename GUM_ELEMENT >
60 class MultiDimBijArray;
61
62 // ===========================================================================
63 // === GUM_MULTI_DIM_ARRAY ===
64 // ===========================================================================
74 template < typename GUM_ELEMENT >
75 class MultiDimArray final: public MultiDimWithOffset< GUM_ELEMENT > {
76 public:
77 template < typename GUM_ELEMENTBIS >
78 friend class MultiDimBijArray;
79 // =========================================================================
81 // =========================================================================
83
90
100
110
117 ~MultiDimArray() override;
118
119 [[nodiscard]] MultiDimContainer< GUM_ELEMENT >* newFactory() const override;
120
122 // =========================================================================
124 // =========================================================================
126
130 void copyFrom(const MultiDimContainer< GUM_ELEMENT >& src) const override;
131
136 void apply(std::function< GUM_ELEMENT(GUM_ELEMENT) > f) const override;
137
143 GUM_ELEMENT reduce(std::function< GUM_ELEMENT(GUM_ELEMENT, GUM_ELEMENT) > f,
144 GUM_ELEMENT base) const override;
145
149 void add(const DiscreteVariable& v) override;
150
154 void erase(const DiscreteVariable& v) override;
155
159 Size realSize() const override;
160
164 void fill(const GUM_ELEMENT& d) const override;
165
169 const std::string& name() const override;
170
172 // =========================================================================
174 // =========================================================================
176
197 const GUM_ELEMENT& unsafeGet(Idx offset) const;
198
217 const GUM_ELEMENT& getByOffset(Idx offset) const;
218
238 void unsafeSet(Idx offset, const GUM_ELEMENT& val);
239
257 void setByOffset(Idx offset, const GUM_ELEMENT& val);
258
263 const GUM_ELEMENT* data() const noexcept override;
264
269 GUM_ELEMENT* data() noexcept override;
270
272
273 protected:
276 mutable std::vector< GUM_ELEMENT > values_;
277
278 void commitMultipleChanges_() final;
279
280 void commitMultipleChanges_(const GUM_ELEMENT&) final;
281
282 GUM_ELEMENT& get_(const Instantiation& i) const final;
283
284 void replace_(const DiscreteVariable* x, const DiscreteVariable* y) final;
285 };
286
287
288#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
289 extern template class MultiDimArray< double >;
290#endif
291
292
293} /* namespace gum */
294
296
297#endif /* GUM_MULTI_DIM_ARRAY_H */
Base class for discrete random variable.
Class for assigning/browsing values to tuples of discrete variables.
void commitMultipleChanges_() final
Synchronize content after MultipleChanges.
void add(const DiscreteVariable &v) override
Adds a variable.
const GUM_ELEMENT & unsafeGet(Idx offset) const
Returns the element stored in the multidimArray at a given offset.
GUM_ELEMENT reduce(std::function< GUM_ELEMENT(GUM_ELEMENT, GUM_ELEMENT) > f, GUM_ELEMENT base) const override
compute lfold for this container
void erase(const DiscreteVariable &v) override
Removes a variable.
void setByOffset(Idx offset, const GUM_ELEMENT &val)
Modifies the element stored in the multidimArray at a given offset.
friend class MultiDimBijArray
const GUM_ELEMENT * data() const noexcept override
Returns a pointer to the contiguous data buffer.
const std::string & name() const override
Returns the MultiDimArray name.
MultiDimContainer< GUM_ELEMENT > * newFactory() const override
Default constructor.
MultiDimArray(const MultiDimArray< GUM_ELEMENT > &src)
Copy constructor.
const GUM_ELEMENT & getByOffset(Idx offset) const
Returns the element stored in the multidimArray at a given offset.
void replace_(const DiscreteVariable *x, const DiscreteVariable *y) final
This is called by MultiDimContainer::replace() to proceed with the replacing between x and y.
std::vector< GUM_ELEMENT > values_
The true data : the values is mutable since we can change the value / in a const multiDimArray.
MultiDimArray()
Default constructor.
GUM_ELEMENT & get_(const Instantiation &i) const final
Return a data, given a Instantiation.
void fill(const GUM_ELEMENT &d) const override
Fills the MultiDimArray with the given value.
void copyFrom(const MultiDimContainer< GUM_ELEMENT > &src) const override
Copy from a other MultiDimContainer.
void unsafeSet(Idx offset, const GUM_ELEMENT &val)
Modifies the element stored in the multidimArray at a given offset.
void apply(std::function< GUM_ELEMENT(GUM_ELEMENT) > f) const override
Apply a function on every element of the container.
~MultiDimArray() override
Copy operator.
Size realSize() const override
Returns the real size of this MultiDimArray.
Decorator of a MultiDimArray, using a bijection over the variables.
Abstract base class for all multi dimensionnal containers.
MultiDimWithOffset()
Class constructor.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
Implementation of the MultiDimArray class.
Headers of the MultiDimWithOffset class.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.