aGrUM  0.16.0
DBCell_tpl.h
Go to the documentation of this file.
1 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 namespace gum {
31 
32  namespace learning {
33 
34 
35  // checks whether a string correspond to a missing value
36  template < template < typename > class ALLOC >
37  INLINE bool DBCell::isMissing(
38  const std::string& str,
39  const std::vector< std::string, ALLOC< std::string > >& missingVals) {
40  for (auto missing : missingVals) {
41  if (str == missing) return true;
42  }
43  return false;
44  }
45 
46  // returns the best type to store a given element encoded as a string
47  template < template < typename > class ALLOC >
49  const std::string& str,
50  const std::vector< std::string, ALLOC< std::string > >& missingVals) {
51  if (isMissing(str, missingVals)) return EltType::MISSING;
52  if (isInteger(str)) return EltType::INTEGER;
53  if (isReal(str)) return EltType::REAL;
54  return EltType::STRING;
55  }
56 
57 
58  // returns the DBCell with the best type for an element encoded as a string
59  template < template < typename > class ALLOC >
61  const std::string& str,
62  const std::vector< std::string, ALLOC< std::string > >& missingVals) {
63  if (isMissing(str, missingVals)) return DBCell();
64  if (isInteger(str)) return DBCell(std::stoi(str));
65  if (isReal(str)) return DBCell(std::stof(str));
66 
67  return DBCell(str);
68  }
69 
70 
72  template < template < typename > class ALLOC >
73  std::string DBCell::toString(
74  const std::vector< std::string, ALLOC< std::string > >& missingVals) const {
75  switch (__type) {
76  case EltType::STRING: 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: return std::to_string(__val_integer);
85 
86  case EltType::MISSING:
87  if (missingVals.size())
88  return missingVals[0];
89  else
90  GUM_ERROR(UndefinedElement, "no missing value symbol found");
91 
92  default:
93  GUM_ERROR(NotImplementedYet, "type not supported by DBCell toString");
94  }
95  }
96 
97 
98  } /* namespace learning */
99 
100 } /* namespace gum */
101 
102 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
bool isMissing() const
indicates whether the cell contains a missing value
static bool isInteger(const std::string &str)
determines whether a string corresponds precisely to an integer
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
static DBCell bestDBCell(const std::string &str, const std::vector< std::string, ALLOC< std::string > > &missingVals)
returns the DBCell with the best type for an element encoded as a string
std::string to_string(const Formula &f)
Definition: formula_inl.h:499
static bool isReal(const std::string &str)
determine whether a string corresponds precisely to a real number
static EltType bestType(const std::string &str, const std::vector< std::string, ALLOC< std::string > > &missingVals)
returns the best type to store a given element encoded as a string
std::string toString(const std::vector< std::string, ALLOC< std::string > > &missingVals) const
returns the content of the DBCell as a string, whatever its type
EltType
the set of types possibly taken by the last element read
Definition: DBCell.h:75
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
DBCell()
default constructor (ontains a missing value)