-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_queue.hpp
107 lines (87 loc) · 2.68 KB
/
file_queue.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef FILE_QUEUE_H_
#define FILE_QUEUE_H_
#include <fstream>
#include "mer_op.hpp"
#include "imove_signature.hpp"
#include "common.hpp"
template<typename mer_op_type>
struct queue_elt {
typedef mer_op_type mer_op_t;
typedef typename mer_op_type::mer_t mer_t;
typedef imove_sig_type<mer_op_type> imove_sig_t;
size_t index;
std::vector<mer_t> fms;
imove_sig_t ims;
};
template<typename mer_op_type>
std::istream& operator>>(std::istream& is, queue_elt<mer_op_type>& elt) {
elt.fms.resize(mer_op_type::nb_fmoves);
elt.ims.clear();
// Index
is >> elt.index;
if(is.peek() != '\t') goto parse_failure;
if(!is.good()) return is;
// FMs
size_t value;
for(size_t i = 0; i < elt.fms.size(); ++i) {
is >> value;
elt.fms[i] = value;
}
if(is.peek() != '\t') goto parse_failure;
if(!is.good()) return is;
// NB IMs
size_t nb_ims;
is >> nb_ims;
if(is.peek() != '\t') goto parse_failure;
if(!is.good()) return is;
// IMs
elt.ims.resize(nb_ims);
for(size_t i = 0; i < nb_ims; ++i)
is >> elt.ims[i];
return is;
parse_failure:
is.setstate(std::ios::badbit);
return is;
}
template<typename mer_op_type>
std::ostream& operator<<(std::ostream& os, const queue_elt<mer_op_type>& elt) {
return os << elt.index << '\t'
<< joinT<size_t>(elt.fms, ' ') << '\t'
<< elt.ims.size() << '\t'
<< join(elt.ims, ' ');
}
// Queue made by reading/writing a file from "both ends".
template<typename mer_op_type>
struct comp_queue {
typedef mer_op_type mer_op_t;
typedef typename mer_op_type::mer_t mer_t;
typedef imove_sig_type<mer_op_type> imove_sig_t;
typedef queue_elt<mer_op_type> queue_elt_t;
std::ofstream right; // Must open writing end first
std::ifstream left;
comp_queue(const char* path)
: right(path, std::ios::out | std::ios::trunc)
, left(path)
{
if(!left.good())
throw std::runtime_error("Failed to open queue file for reading");
if(!right.good())
throw std::runtime_error("Failed to open queue file for writing");
}
bool empty() { return left.tellg() == right.tellp(); }
// Read and return first element, advancing by 1 element. Returns false if
// not successful.
bool dequeue(queue_elt_t& elt) {
if(empty()) return false;
left >> elt;
if(left.peek() != '\n') return false;
if(!left.good()) return false;
left.get(); // Skip new line
return left.good();
}
bool enqueue(const queue_elt_t& elt) {
right << elt << std::endl; // std::flush;
return right.good();
}
};
#endif // FILE_QUEUE_H_