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