All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
read_rows.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2015
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
15 #ifndef PAAL_READ_ROWS_HPP
16 #define PAAL_READ_ROWS_HPP
17 
18 #include "paal/utils/functors.hpp"
20 
21 #include <boost/range/size.hpp>
22 #include <boost/range/algorithm/copy.hpp>
23 #include <boost/range/istream_range.hpp>
24 
25 #include <cassert>
26 #include <istream>
27 #include <string>
28 #include <sstream>
29 #include <vector>
30 #include <list>
31 
32 namespace paal {
33 
45 template <typename CoordinateType,
46  typename RowType = std::vector<CoordinateType>,
47  typename ShouldIgnoreBadRow = utils::always_true>
48 void read_rows(std::istream &input_stream,
49  std::vector<RowType> &rows,
50  std::size_t row_size,
51  std::size_t max_rows_to_read,
52  ShouldIgnoreBadRow &&should_ignore_bad_row = ShouldIgnoreBadRow{}) {
53  std::string line;
54 
55  RowType row;
56  row.reserve(row_size);
57 
58  while ((max_rows_to_read--) && std::getline(input_stream, line)) {
59  row.clear();
60  std::stringstream row_stream(line);
61 
62  boost::copy(boost::istream_range<CoordinateType>(row_stream), std::back_inserter(row));
63 
64  if((!row_stream.bad() && row_size == boost::size(row)) || !should_ignore_bad_row(line)) {
65  rows.emplace_back(row);
66  }
67  }
68 }
69 
83 template <typename CoordinateType,
84  typename RowType = std::vector<CoordinateType>,
85  typename ShouldIgnoreBadRow = utils::always_true,
86  typename FailureMessage = utils::failure_message>
87 void read_rows_first_row_size(std::istream &input_stream,
88  std::vector<RowType> &rows,
89  std::size_t max_rows_to_read,
90  ShouldIgnoreBadRow &&should_ignore_bad_row = ShouldIgnoreBadRow{},
91  FailureMessage &&failure_message = FailureMessage{}) {
92  if(!input_stream.good()) {
93  failure_message("Input stream is broken");
94  }
95 
96  read_rows<CoordinateType>(input_stream, rows, 0, 1, utils::always_false{});
97 
98  if(rows.empty()) {
99  failure_message("Empty input data");
100  }
101  std::size_t const row_size = boost::size(rows.front());
102  if(row_size <= 0) {
103  failure_message("Empty first row");
104  }
105 
106  read_rows<CoordinateType>(input_stream, rows, row_size, max_rows_to_read - 1, std::forward<ShouldIgnoreBadRow>(should_ignore_bad_row));
107 }
108 
109 }
110 
111 #endif /* PAAL_READ_ROWS_HPP */
void read_rows_first_row_size(std::istream &input_stream, std::vector< RowType > &rows, std::size_t max_rows_to_read, ShouldIgnoreBadRow &&should_ignore_bad_row=ShouldIgnoreBadRow{}, FailureMessage &&failure_message=FailureMessage{})
reads up to max_rows_to_read rows, size is determine by first row
Definition: read_rows.hpp:87
functor return false
Definition: functors.hpp:222
void read_rows(std::istream &input_stream, std::vector< RowType > &rows, std::size_t row_size, std::size_t max_rows_to_read, ShouldIgnoreBadRow &&should_ignore_bad_row=ShouldIgnoreBadRow{})
reads up to max_rows_to_read rows of size row_size
Definition: read_rows.hpp:48
This file contains set of simple useful functors or functor adapters.
Functor prints failure message.
functor return true
Definition: functors.hpp:227