15 #ifndef PALL_READ_SVM_HPP
16 #define PALL_READ_SVM_HPP
44 template <
typename RowsRange,
typename RowRefExtractor>
45 void resize_rows(RowsRange &&rows, RowRefExtractor row_ref_extractor,
46 std::size_t new_size) {
47 for (
auto &row : rows) {
48 row_ref_extractor(row).resize(new_size);
61 template <
typename CoordinatesType,
62 typename ResultType = int,
63 typename FeatureIdType = std::size_t>
65 CoordinatesType m_coordinates;
67 bool m_has_out_of_bounds_feature_id =
false;
72 svm_row(FeatureIdType dimensions = 1) : m_coordinates(dimensions) {}
95 row.nullify_coordinates();
97 if (!row.read_result(row_stream)) {
101 while (row_stream.good()) {
102 row.read_single_feature(row_stream);
105 row.fail_stream_if_out_of_bound_features(row_stream);
116 void nullify_coordinates() {
117 auto size = m_coordinates.size();
118 m_coordinates.clear();
119 m_coordinates.resize(size);
122 std::istream& read_result(std::istream &row_stream) {
124 if (!(row_stream >> result)) {
127 m_result = (result == 1) ? 1 : 0;
132 std::istream& read_single_feature(std::istream &row_stream) {
133 FeatureIdType feature_id;
134 read_feature_id(row_stream, feature_id);
135 if (!row_stream.good()) {
139 if (!skip_exact_character(row_stream,
':')) {
143 coordinate_t coordinate;
144 if (!(row_stream >> coordinate)) {
148 save(feature_id, coordinate);
152 std::istream& read_feature_id(std::istream &stream, FeatureIdType &feature_id)
const {
153 stream >> feature_id;
154 if (stream.fail() && stream.eof()) {
155 flip_state(stream, std::ios::failbit);
160 void flip_state(std::istream &stream,
const std::ios::iostate &state)
const {
161 stream.clear(stream.rdstate() ^ state);
164 std::istream& skip_exact_character(std::istream &stream,
char character)
const {
167 if (!stream.good() || c != character) {
168 stream.setstate(std::ios::failbit);
173 void save(FeatureIdType feature_id, coordinate_t coordinate) {
174 if (m_coordinates.size() <= feature_id) {
175 m_has_out_of_bounds_feature_id =
true;
176 m_coordinates.resize(feature_id + 1);
178 m_coordinates[feature_id] = coordinate;
181 void fail_stream_if_out_of_bound_features(std::istream &stream)
const {
182 if (m_has_out_of_bounds_feature_id) {
183 stream.setstate(std::ios::failbit);
203 template <
typename RowType,
204 typename ResultType = int,
207 std::size_t &max_dimensions,
208 std::vector<std::tuple<RowType, ResultType>> &points,
209 std::size_t max_points_to_read,
210 ShouldIgnoreBadRow &&should_ignore_bad_row = ShouldIgnoreBadRow{}) {
211 assert(input_stream.good());
213 detail::svm_row<RowType, ResultType> row{max_dimensions};
215 while ((max_points_to_read--) && std::getline(input_stream, line)) {
216 std::stringstream row_stream(line);
218 if (row_stream || !should_ignore_bad_row(line)) {
219 assign_max(max_dimensions, row.get_coordinates().size());
220 points.emplace_back(row.get_coordinates(),
236 template <
typename RowType,
237 typename ResultType =
int>
239 assert(input_stream.good());
241 using point_with_result_t = std::tuple<RowType, ResultType>;
243 std::size_t max_dimensions = 0;
244 std::size_t max_points_to_read = 1;
245 std::vector<point_with_result_t> points;
246 while (input_stream.good()) {
247 read_svm(input_stream, max_dimensions, points, max_points_to_read);
251 return std::make_tuple(points, max_dimensions);
typename boost::range_value< Range >::type range_to_elem_t
for given range returns type of its element
friend std::istream & operator>>(std::istream &row_stream, svm_row &row)
reads svm row of format:
CoordinatesType const & get_coordinates() const
coordinates getter
This file contains set of simple useful functors or functor adapters.
class that can read single svm row
void assign_max(T &t, const T &u)
ResultType const & get_result() const
result getter
svm_row(FeatureIdType dimensions=1)
constructor
void resize_rows(RowsRange &&rows, RowRefExtractor row_ref_extractor, std::size_t new_size)
resize rows to have equal sizes
functor for std::tuple::get<I>
void read_svm(std::istream &input_stream, std::size_t &max_dimensions, std::vector< std::tuple< RowType, ResultType >> &points, std::size_t max_points_to_read, ShouldIgnoreBadRow &&should_ignore_bad_row=ShouldIgnoreBadRow{})
detail