aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBTranslator4RangeVariable_inl.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
48
49#include <cstdio>
50#include <limits>
51#include <utility>
52#include <vector>
53
56
57#ifndef DOXYGEN_SHOULD_SKIP_THIS
58
59namespace gum {
60
61 namespace learning {
62
65 GUM_DESTRUCTOR(DBTranslator4RangeVariable);
66 }
67
69 INLINE std::string
71 try {
72 return this->back_dico_.second(translated_val.discr_val);
73 } catch (Exception const&) {
74 // check if this is a missing value
75 if (translated_val.discr_val == std::numeric_limits< std::size_t >::max()) {
76 if (!_nonint_missing_symbol_.empty()) return _nonint_missing_symbol_;
77 if (this->missing_symbols_.empty()) return *(this->missing_symbols_.begin());
78 }
79
80 GUM_ERROR(UnknownLabelInDatabase,
81 "The back translation of \"" << translated_val.discr_val
82 << "\" could not be found");
83 }
84 }
85
87 INLINE HashTable< std::size_t, std::size_t > DBTranslator4RangeVariable::reorder() {
88 // assign to each label the index it had before reordering
89 const auto& labels = _variable_.labels();
90 const std::size_t size = labels.size();
91 std::vector< std::pair< std::size_t, std::string > > xlabels;
92 xlabels.reserve(size);
93 bool modifications = false;
94 for (std::size_t i = std::size_t(0); i < size; ++i) {
95 const std::size_t old_val = this->back_dico_.first(labels[i]);
96 xlabels.push_back(std::make_pair(old_val, labels[i]));
97 if (old_val != i) modifications = true;
98 }
99
100
101 // if there were no modification, return an empty update hashtable
102 if (!modifications) { return HashTable< std::size_t, std::size_t >(); }
103
104 // create the hashTable corresponding to the mapping from the old
105 // indices to the new one
106 this->back_dico_.clear();
107 HashTable< std::size_t, std::size_t > mapping((Size)size);
108 for (std::size_t i = std::size_t(0); i < size; ++i) {
109 mapping.insert(xlabels[i].first, i);
110 this->back_dico_.insert(i, xlabels[i].second);
111 }
112
113 return mapping;
114 }
115
117 INLINE std::size_t DBTranslator4RangeVariable::domainSize() const {
118 return _variable_.domainSize();
119 }
120
122 INLINE const RangeVariable* DBTranslator4RangeVariable::variable() const { return &_variable_; }
123
126 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
127 }
128
129
130 } /* namespace learning */
131
132} /* namespace gum */
133
134
135#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The class representing the original values of the cells of databases.
The databases' cell translators for range variables.
virtual const RangeVariable * variable() const final
returns the variable stored into the translator
DBTranslator4RangeVariable(const std::vector< std::string > &missing_symbols, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max())
default constructor without any initial variable
virtual DBTranslatedValue missingValue() const final
returns the translation of a missing value
virtual std::size_t domainSize() const final
returns the domain size of a variable corresponding to the translations
virtual std::string translateBack(const DBTranslatedValue translated_val) const final
returns the original value for a given translation
virtual HashTable< std::size_t, std::size_t > reorder() final
performs a reordering of the dictionary and returns a mapping from the old translated values to the n...
virtual ~DBTranslator4RangeVariable()
destructor
Bijection< std::size_t, std::string > back_dico_
the bijection relating back translated values and their original strings.
Set< std::string > missing_symbols_
the set of missing symbols
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
The union class for storing the translated values in learning databases.