series.cpp 1.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Created by bvdmitri on 10.02.19.
//

#include "series.h"

#include <algorithm>
#include <fstream>
#include <iostream>

// trim from start
static inline std::string &ltrim(std::string &s) {
13
    s.erase(s.find_last_not_of(" \t\n\r\f\v") + 1);
14
15
16
17
18
    return s;
}

// trim from end
static inline std::string &rtrim(std::string &s) {
19
    s.erase(s.find_last_not_of(" \t\n\r\f\v") + 1);
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    return s;
}

// trim from both ends
static inline std::string &trim(std::string &s) {
    return ltrim(rtrim(s));
}

static inline bool is_file_exist(const std::string &file) {
    std::ifstream infile(file);
    return infile.good();
}

MatrixSeries::MatrixSeries(const std::string &file, const std::string &directory_prefix) : current(0) {
    std::ifstream input(file);
    std::string line;
    while (std::getline(input, line)) {
        if (line.empty()) {
            continue;
        }

        line = trim(line);
Dmitry Bagaev's avatar
Dmitry Bagaev committed
42
        std::string path = directory_prefix + line.substr(2);
43
44

        if (!is_file_exist(path)) {
Dmitry Bagaev's avatar
Dmitry Bagaev committed
45
            std::cerr << "[WARN] File " << path << " does not exist. Skipping..." << std::endl;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
            continue;
        }

        switch (line.at(0)) {
            case 'A':
                matrices.push_back(path);
                break;
            case 'b':
                rhss.push_back(path);
                break;
            default:
                std::cerr << "[WARN] Invalid line in matrix series configuration file: " << line << std::endl;
        }
    }
}

bool MatrixSeries::end() const {
    return current == matrices.size();
}

void MatrixSeries::restart() {
    current = 0;
}

std::pair<const char *, const char *> MatrixSeries::next() {
    const char *matrix = current < matrices.size() ? matrices.at(current).c_str() : nullptr;
    const char *rhs = current < rhss.size() ? rhss.at(current).c_str() : nullptr;
    current += 1;
    return std::make_pair(matrix, rhs);
}
Dmitry Bagaev's avatar
Dmitry Bagaev committed
76
77
78
79

int MatrixSeries::size() const {
    return matrices.size();
}