aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
projectionRegister4MultiDim_tpl.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#pragma once
42
43
51
53#ifndef DOXYGEN_SHOULD_SKIP_THIS
54
55# include <agrum/agrum.h>
56
58
59namespace gum {
60
61 // adds a new entry into the register
62 template < typename GUM_ELEMENT >
64 std::string_view projection_name,
65 std::string_view type_multidim,
67 // insert the new entry
68 ProjectionSet* theset;
69
70 if (!_set_.exists(projection_name)) {
71 theset = _set_.insert(std::string{projection_name}, new ProjectionSet).second;
72# ifdef GUM_DEBUG_MODE
73 // for debugging purposes, we should inform the aGrUM's debugger that
74 // the hashtable contained within the ProjectionRegister4MultiDim will be
75 // removed at the end of the program's execution.
76 __debug__::_inc_deletion_("HashTable", __FILE__, __LINE__, "destructor of", (void*)theset);
77# endif /* GUM_DEBUG_MODE */
78 } else {
79 theset = _set_[projection_name];
80 }
81
82 theset->insert(std::string{type_multidim}, newFunction);
83 }
84
85 // removes a given entry from the register
86 template < typename GUM_ELEMENT >
87 void ProjectionRegister4MultiDim< GUM_ELEMENT >::erase(std::string_view projection_name,
88 std::string_view type_multidim) {
89 if (!_set_.exists(projection_name)) return;
90
91 ProjectionSet* theset = _set_[projection_name];
92
93 theset->erase(type_multidim);
94 }
95
96 // indicates whether a given entry exists in the register
97 template < typename GUM_ELEMENT >
98 bool ProjectionRegister4MultiDim< GUM_ELEMENT >::exists(std::string_view projection_name,
99 std::string_view type_multidim) const {
100 if (!_set_.exists(projection_name)) return false;
101
102 const ProjectionSet& theset = *(_set_[projection_name]);
103 return theset.exists(type_multidim);
104 }
105
108 template < typename GUM_ELEMENT >
110 ProjectionRegister4MultiDim< GUM_ELEMENT >::get(std::string_view projection_name,
111 std::string_view type_multidim) const {
112 const ProjectionSet& theset = *(_set_[projection_name]);
113 return theset[type_multidim];
114 }
115
116 // a named constructor that constructs one and only one Register per data
117 // type
118 template < typename GUM_ELEMENT >
121 // Here, this initialization is thread-safe due to Meyer’s Singleton property
122 static ProjectionRegister4MultiDim container;
123
124# ifdef GUM_DEBUG_MODE
125 static std::atomic_flag first = ATOMIC_FLAG_INIT;
126 if (!first.test_and_set()) {
127 // for debugging purposes, we should inform the aGrUM's debugger that
128 // the hashtable contained within the ProjectionRegister4MultiDim will be
129 // removed at the end of the program's execution.
130 __debug__::_inc_deletion_("HashTable",
131 __FILE__,
132 __LINE__,
133 "destructor of",
134 (void*)&container._set_);
135 }
136# endif /* GUM_DEBUG_MODE */
137
138 return container;
139 }
140
141 // Default constructor: creates an empty register
142 template < typename GUM_ELEMENT >
144
145 // destructor
146 template < typename GUM_ELEMENT >
148 // remove all the sets
149 for (typename HashTable< std::string, ProjectionSet* >::iterator_safe iter = _set_.beginSafe();
150 iter != _set_.endSafe();
151 ++iter)
152 delete iter.val();
153 }
154
155 // a function to more easily register new projection functions in MultiDims
156 template < typename GUM_ELEMENT >
158 std::string_view projection_name,
159 std::string_view type_multidim,
162 type_multidim,
163 function);
164 }
165
166} /* namespace gum */
167
168#endif /* DOXYGEN_SHOULD_SKIP_THIS */
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Definition hashTable.h:655
A container for registering projection functions on multiDimImplementations, i.e.,...
ProjectionPtr get(std::string_view projection_name, std::string_view type_multidim) const
returns the specialized projection operator assigned to a given type of MultiDimImplementation
ProjectionRegister4MultiDim()
Default constructor: creates an empty register.
void insert(std::string_view projection_name, std::string_view type_multidim, ProjectionPtr newFunction)
adds a new entry into the register
void erase(std::string_view projection_name, std::string_view type_multidim)
removes a given entry from the register
static ProjectionRegister4MultiDim & Register()
a named constructor that constructs one and only one Register per data type
MultiDimImplementation< GUM_ELEMENT > *(*)(const MultiDimImplementation< GUM_ELEMENT > *, const gum::VariableSet &) ProjectionPtr
the type of functions used by the register
bool exists(std::string_view projection_name, std::string_view type_multidim) const
indicates whether a given entry exists in the register
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
void registerProjection(std::string_view projection_name, std::string_view type_multidim, typename ProjectionRegister4MultiDim< GUM_ELEMENT >::ProjectionPtr function)
A function to more easily register new projection functions in MultiDims.
A container for registering projection functions on multiDimImplementations.