aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
operatorRegister4MultiDim_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& operation_name,
63 const std::string& type1,
64 const std::string& type2,
66 // insert the new entry
67 OperatorSet* theset;
68
69 if (!_set_.exists(operation_name)) {
70 theset = _set_.insert(operation_name, new OperatorSet).second;
71# ifdef GUM_DEBUG_MODE
72 // for debugging purposes, we should inform the aGrUM's debugger that
73 // the hashtable contained within the OperatorRegister4MultiDim will be
74 // removed at the end of the program's execution.
75 __debug__::_inc_deletion_("HashTable", __FILE__, __LINE__, "destructor of", (void*)theset);
76# endif /* GUM_DEBUG_MODE */
77 } else {
78 theset = _set_[operation_name];
79 }
80
81 std::pair< std::string, std::string > thepair(type1, type2);
82
83 theset->insert(thepair, newFunction);
84 }
85
86 // removes a given entry from the register
87 template < typename GUM_SCALAR >
88 void OperatorRegister4MultiDim< GUM_SCALAR >::erase(const std::string& operation_name,
89 const std::string& type1,
90 const std::string& type2) {
91 if (!_set_.exists(operation_name)) return;
92
93 OperatorSet* theset = _set_[operation_name];
94
95 theset->erase(std::pair< std::string, std::string >(type1, type2));
96 }
97
98 // indicates whether a given entry exists in the register
99 template < typename GUM_SCALAR >
100 INLINE bool OperatorRegister4MultiDim< GUM_SCALAR >::exists(const std::string& operation_name,
101 const std::string& type1,
102 const std::string& type2) const {
103 if (!_set_.exists(operation_name)) return false;
104 const OperatorSet& theset = *(_set_[operation_name]);
105 const std::pair< std::string, std::string > key(type1, type2);
106 return theset.exists(key);
107 }
108
111 template < typename GUM_SCALAR >
113 OperatorRegister4MultiDim< GUM_SCALAR >::get(const std::string& operation_name,
114 const std::string& type1,
115 const std::string& type2) const {
116 const OperatorSet& theset = *(_set_[operation_name]);
117 const std::pair< std::string, std::string > key(type1, type2);
118 return theset[key];
119 }
120
121 // a named constructor that constructs one and only one Register per data
122 // type
123 template < typename GUM_SCALAR >
125 // Here, this initialization is thread-safe due to Meyer’s Singleton property
127
128# ifdef GUM_DEBUG_MODE
129 static std::atomic_flag first = ATOMIC_FLAG_INIT;
130 if (!first.test_and_set()) {
131 // for debugging purposes, we should inform the aGrUM's debugger that
132 // the hashtable contained within the OperatorRegister4MultiDim will be
133 // removed at the end of the program's execution.
134 __debug__::_inc_deletion_("HashTable",
135 __FILE__,
136 __LINE__,
137 "destructor of",
138 (void*)&container._set_);
139 }
140# endif /* GUM_DEBUG_MODE */
141
142 return container;
143 }
144
145 // Default constructor: creates an empty register
146 template < typename GUM_SCALAR >
148
149 // destructor
150 template < typename GUM_SCALAR >
152 // remove all the sets
153 for (typename HashTable< std::string, OperatorSet* >::iterator_safe iter = _set_.beginSafe();
154 iter != _set_.endSafe();
155 ++iter)
156 delete iter.val();
157 }
158
159 // a function to more easily register new operators in MultiDims
160 template < typename GUM_SCALAR >
161 void registerOperator(const std::string& operation_name,
162 const std::string& type1,
163 const std::string& type2,
166 type1,
167 type2,
168 function);
169 }
170
171} /* namespace gum */
172
173#endif /* DOXYGEN_SHOULD_SKIP_THIS */
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Definition hashTable.h:652
A container for registering binary functions on multiDimImplementations.
~OperatorRegister4MultiDim()
Destructor.
void insert(const std::string &operation_name, const std::string &type1, const std::string &type2, OperatorPtr newFunction)
adds a new entry into the register
MultiDimImplementation< GUM_SCALAR > *(*)(const MultiDimImplementation< GUM_SCALAR > *, const MultiDimImplementation< GUM_SCALAR > *) OperatorPtr
The type of functions used by the register.
void erase(const std::string &operation_name, const std::string &type1, const std::string &type2)
removes a given entry from the register
OperatorRegister4MultiDim()
Default constructor: creates an empty register.
OperatorPtr get(const std::string &operation_name, const std::string &type1, const std::string &type2) const
returns the specialized operator assigned to a given pair of MultiDimImplementations
static OperatorRegister4MultiDim< GUM_SCALAR > & Register()
A named constructor that constructs one and only one Register per data type.
bool exists(const std::string &operation_name, const std::string &type1, const std::string &type2) const
Indicates whether a given entry exists in the register.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
void registerOperator(const std::string &operation_name, const std::string &type1, const std::string &type2, typename OperatorRegister4MultiDim< GUM_SCALAR >::OperatorPtr function)
A function to more easily register new operators in MultiDims.
A container for registering binary functions on multiDimImplementations.