aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
DBCell_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief The inlined implementation of DBCells
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef DOXYGEN_SHOULD_SKIP_THIS
28 
29 namespace gum {
30 
31  namespace learning {
32 
33 
34  // checks whether a string correspond to a missing value
35  template < template < typename > class ALLOC >
36  INLINE bool
37  DBCell::isMissing(const std::string& str,
38  const std::vector< std::string, ALLOC< std::string > >& missingVals) {
39  for (auto missing: missingVals) {
40  if (str == missing) return true;
41  }
42  return false;
43  }
44 
45  // returns the best type to store a given element encoded as a string
46  template < template < typename > class ALLOC >
48  DBCell::bestType(const std::string& str,
49  const std::vector< std::string, ALLOC< std::string > >& missingVals) {
50  if (isMissing(str, missingVals)) return EltType::MISSING;
51  if (isInteger(str)) return EltType::INTEGER;
52  if (isReal(str)) return EltType::REAL;
53  return EltType::STRING;
54  }
55 
56 
57  // returns the DBCell with the best type for an element encoded as a string
58  template < template < typename > class ALLOC >
60  DBCell::bestDBCell(const std::string& str,
61  const std::vector< std::string, ALLOC< std::string > >& missingVals) {
62  if (isMissing(str, missingVals)) return DBCell();
63  if (isInteger(str)) return DBCell(std::stoi(str));
64  if (isReal(str)) return DBCell(std::stof(str));
65 
66  return DBCell(str);
67  }
68 
69 
70  /// returns the content of the DBCell as a string, whatever its type
71  template < template < typename > class ALLOC >
72  std::string
73  DBCell::toString(const std::vector< std::string, ALLOC< std::string > >& missingVals) const {
74  switch (_type_) {
75  case EltType::STRING:
76  return _strings_().first(_val_index_);
77 
78  case EltType::REAL: {
79  char buffer[100];
80  sprintf(buffer, "%g", _val_real_);
81  return std::string(buffer);
82  }
83 
84  case EltType::INTEGER:
85  return std::to_string(_val_integer_);
86 
87  case EltType::MISSING:
88  if (missingVals.size())
89  return missingVals[0];
90  else
91  GUM_ERROR(UndefinedElement, "no missing value symbol found")
92 
93  default:
94  GUM_ERROR(NotImplementedYet, "type not supported by DBCell toString")
95  }
96  }
97 
98 
99  } /* namespace learning */
100 
101 } /* namespace gum */
102 
103 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)