external_source.hpp 4.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
// Copyright (c) 2020 INRA Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef ORG_VLEPROJECT_IRRITATOR_EXTERNAL_SOURCE_2021
#define ORG_VLEPROJECT_IRRITATOR_EXTERNAL_SOURCE_2021

#include <irritator/core.hpp>

#include <array>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
11
#include <filesystem>
12
13
14
15
#include <fstream>

namespace irt::source {

Gauthier Quesnel's avatar
Gauthier Quesnel committed
16
17
18
19
20
21
22
23
24
25
26
struct constant
{
    double value;

    bool operator()(external_source& src)
    {
        src.index = 0;
        return true;
    }
};

27
28
29
struct binary_file
{
    std::array<char, 1024 * 1024> buffer;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
30
    std::filesystem::path file_path;
31
32
33
34
35
36
37
38
    std::ifstream ifs;
    sz buffer_size = 0;
    bool use_rewind = false;

    binary_file() = default;

    bool init(external_source& src)
    {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
39
40
41
42
43
44
45
        if (!ifs) {
            ifs.open(file_path);

            if (!ifs)
                return false;
        }

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
76
77
78
79
80
81
82
83
        if (!read(src))
            return false;
    }

    bool operator()(external_source& src)
    {
        if (!ifs.good() && !use_rewind)
            return false;

        if (!ifs.good())
            ifs.seekg(0);

        if (!read(src))
            return false;

        return true;
    }

private:
    bool read(external_source& src)
    {
        ifs.read(buffer.data(), std::size(buffer));
        buffer_size = ifs.gcount();

        if (buffer_size % 8 != 0)
            return false;

        src.data = reinterpret_cast<double*>(buffer.data());
        src.index = 0;
        src.size = buffer_size / 8;

        return true;
    }
};

struct text_file
{
    std::array<double, 1024 * 1024 / 8> buffer;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
84
    std::filesystem::path file_path;
85
86
87
88
89
90
91
    std::ifstream ifs;
    bool use_rewind = false;

    text_file() = default;

    bool init(external_source& src)
    {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
92
93
94
95
96
97
98
        if (!ifs) {
            ifs.open(file_path);

            if (!ifs)
                return false;
        }

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
        if (!read(src))
            return false;
    }

    bool operator()(external_source& src)
    {
        if (!ifs.good() && !use_rewind)
            return false;

        if (!ifs.good())
            ifs.seekg(0);

        if (!read(src))
            return false;

        return true;
    }

private:
    bool read(external_source& src)
    {
        size_t i = 0;

        for (; i < std::size(buffer) && ifs.good(); ++i) {
            if (!(ifs >> buffer[i]))
                break;
        }

        src.data = buffer.data();
        src.index = 0;
        src.size = i;

        return true;
    }
};

struct random_source
{
    double* buffer = nullptr;
    sz size = 0;
    bool use_rewind;

    random_source() = default;

    ~random_source() noexcept
    {
        if (buffer)
            g_free_fn(buffer);
    }

    template<typename RandomGenerator, typename Distribution>
    bool init(const sz size_, RandomGenerator& gen, Distribution& dist) noexcept
    {
        if (!size_)
            return false;

        if (buffer)
            g_free_fn(buffer);

        size = 0;
        buffer = g_alloc_fn(sizeof(double) * size_);

        if (buffer) {
            std::generate_n(buffer, size, (*dist)(*gen));

            size = size_;
            return true;
        }

        return false;
    }

    bool operator()(external_source& src)
    {
        if (!use_rewind)
            return false;

        size = 0;

        return true;
    }
};

enum class random_file_type
{
    binary,
    text,
};

template<typename RandomGenerator, typename Distribution>
inline int
generate_random_file(std::ostream& os,
                     RandomGenerator& gen,
                     Distribution& dist,
                     const std::size_t size,
                     const random_file_type type) noexcept
{
    switch (type) {
    case random_file_type::text: {
        if (!os)
            return -1;

        for (std::size_t sz = 0; sz < size; ++sz)
            if (!(os << dist(gen) << '\n'))
                return -2;
    } break;

    case random_file_type::binary: {
        if (!os)
            return -1;

        for (std::size_t sz = 0; sz < size; ++sz) {
            const double value = dist(gen);
            os.write(reinterpret_cast<const char*>(&value), sizeof(value));
        }
    } break;
    }

    return 0;
}

} // namespace irt::source

#endif