29 #include <boost/algorithm/string.hpp>
30 #include <boost/lexical_cast.hpp>
31 #include <boost/tokenizer.hpp>
50 size_t comment_pos = line.
find(comment);
51 if (comment_pos != std::string::npos) {
52 line = line.
substr(0, comment_pos);
76 {
"bool",
typeid(bool)},
77 {
"boolean",
typeid(bool)},
79 {
"int",
typeid(int32_t)},
80 {
"long",
typeid(int64_t)},
81 {
"int32",
typeid(int32_t)},
82 {
"int64",
typeid(int64_t)},
84 {
"float",
typeid(float)},
85 {
"double",
typeid(double)},
98 {
"[int+]",
typeid(NdArray<int32_t>)},
99 {
"[long+]",
typeid(NdArray<int64_t>)},
100 {
"[int32+]",
typeid(NdArray<int32_t>)},
101 {
"[int64+]",
typeid(NdArray<int64_t>)},
102 {
"[float+]",
typeid(NdArray<float>)},
103 {
"[double+]",
typeid(NdArray<double>)},
108 if (p->first == keyword) {
125 if (boost::starts_with(line, comment)) {
128 boost::replace_all(line, comment,
"");
130 if (boost::starts_with(line,
"Column:")) {
138 if (descriptions.
count(name) != 0) {
144 if (!boost::starts_with(token,
"(") && token !=
"-") {
151 if (boost::starts_with(token,
"(")) {
158 if (line_stream && token ==
"-") {
162 while (line_stream) {
163 desc << token <<
' ';
167 boost::trim(desc_str);
194 if (boost::starts_with(line, comment)) {
197 boost::replace_all(line, comment,
"");
202 if (boost::starts_with(line,
"Column:")) {
206 auto space_i = temp.
find(
' ');
208 temp = temp.
substr(0, space_i);
210 desc_names.emplace_back(
std::move(temp));
218 if (!last_comment.empty()) {
222 while (line_stream) {
223 names.push_back(token);
226 if (names.size() != columns_number) {
233 if (desc_names.size() != 0 && desc_names.size() != columns_number) {
234 logger.
warn() <<
"Number of column descriptions does not matches the number"
235 <<
" of the columns";
240 if (names.size() < columns_number) {
241 for (
size_t i = names.size() + 1; i <= columns_number; ++i) {
247 for (
auto name : names) {
248 if (!set.insert(name).second) {
257 template <
typename T>
260 boost::char_separator<char> sep{
","};
261 boost::tokenizer<boost::char_separator<char>> tok{str, sep};
262 for (
auto& s : tok) {
268 template <
typename T>
269 NdArray<T> convertStringToNdArray(
const std::string& str) {
272 }
else if (str[0] !=
'<') {
276 auto closing_char = str.
find(
'>');
277 if (closing_char == std::string::npos) {
281 auto shape_str = str.
substr(1, closing_char - 1);
282 auto shape_i = convertStringToVector<int32_t>(shape_str);
283 auto data = convertStringToVector<T>(str.
substr(closing_char + 1));
287 return NdArray<T>(shape_u, data);
296 if (value ==
"true" || value ==
"t" || value ==
"yes" || value ==
"y" || value ==
"1") {
298 }
else if (value ==
"false" || value ==
"f" || value ==
"no" || value ==
"n" || value ==
"0") {
304 {
typeid(int32_t), boost::lexical_cast<int32_t, const std::string&>},
305 {
typeid(int64_t), boost::lexical_cast<int64_t, const std::string&>},
307 {
typeid(float), boost::lexical_cast<float, const std::string&>},
308 {
typeid(double), boost::lexical_cast<double, const std::string&>},
310 {
typeid(
std::string), boost::lexical_cast<std::string, const std::string&>},
330 return i->second(value);
331 }
catch (boost::bad_lexical_cast
const&) {
341 size_t comment_pos = line.
find(comment);
342 if (comment_pos != std::string::npos) {
343 line = line.
substr(0, comment_pos);
359 size_t comment_pos = line.
find(comment);
360 if (comment_pos != std::string::npos) {
361 line = line.
substr(0, comment_pos);
T back_inserter(T... args)
static Logging getLogger(const std::string &name="")
void warn(const std::string &logMessage)
NdArray(const std::vector< size_t > &shape_)
boost::variant< bool, int32_t, int64_t, float, double, std::string, std::vector< bool >, std::vector< int32_t >, std::vector< int64_t >, std::vector< float >, std::vector< double >, NdArray::NdArray< int32_t >, NdArray::NdArray< int64_t >, NdArray::NdArray< float >, NdArray::NdArray< double > > cell_type
The possible cell types.
This class gets a stream as argument during construction and when it is deleted it sets the position ...
T forward_as_tuple(T... args)
std::type_index keywordToType(const std::string &keyword)
bool hasNextRow(std::istream &in, const std::string &comment)
std::map< std::string, ColumnDescription > autoDetectColumnDescriptions(std::istream &in, const std::string &comment)
Reads the column descriptions of the given stream.
size_t countColumns(std::istream &in, const std::string &comment)
Returns the number of whitespace separated tokens of the first non commented line.
std::vector< std::string > autoDetectColumnNames(std::istream &in, const std::string &comment, size_t columns_number)
Reads the column names of the given stream.
Row::cell_type convertToCellType(const std::string &value, std::type_index type)
Converts the given value to a Row::cell_type of the given type.
const std::vector< std::pair< std::string, std::type_index > > KeywordTypeMap
std::size_t countRemainingRows(std::istream &in, const std::string &comment)
const std::map< std::type_index, std::function< Row::cell_type(const std::string &)> > sCellConverter
static Elements::Logging logger
std::string quoted(const std::string &str)