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