-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultiFileIterator.h
144 lines (121 loc) · 3.84 KB
/
MultiFileIterator.h
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
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
// -*- c++ -*-
/**
*
* LightStringGraph
*
* Lightweight String Graph Construction.
*
* Copyright (C) 2013, 2014 Stefano Beretta, Yuri Pirola, Marco Previtali
*
* Distributed under the terms of the GNU General Public License (or the Lesser
* GPL).
*
* This file is part of LightStringGraph.
*
* LighStringGraph is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LightStringGraph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LightStringGraph. If not, see <http://www.gnu.org/licenses/>.
*
**/
#ifndef MULTI_FILE_ITERATOR_H
#define MULTI_FILE_ITERATOR_H
#include <vector>
#include <string>
#include "types.h"
#include "util.h"
#include "SingleFileIterator.h"
template <typename TFileValue,
typename TFile,
typename TValue=TFileValue,
typename TReader=read_value_t<TFile, TFileValue> >
class MultiFileIterator
{
private:
typedef SingleFileIterator<TFileValue, TFile, TValue, TReader> file_iterator_t;
typedef std::vector< file_iterator_t* > file_iterators_t;
file_iterators_t _iterators;
typename file_iterators_t::const_iterator _current_iterator;
BWTPosition _current_position;
bool _terminated;
public:
// Constructors
MultiFileIterator(const std::vector< std::string >& filenames);
// Destructor
~MultiFileIterator() {
for (typename file_iterators_t::const_iterator it= _iterators.begin();
it != _iterators.end();
++it) {
delete *it;
}
}
MultiFileIterator& operator++() {
_FAIL_IF_DBG(_terminated);
++_current_position;
++(**_current_iterator);
while (!_terminated && (*_current_iterator)->terminated()) {
++_current_iterator;
_terminated |= (_current_iterator == _iterators.end());
}
return *this;
}
TValue operator*() const {
_FAIL_IF_DBG(_terminated);
return ***_current_iterator;
}
BWTPosition get_position() const {
return _current_position;
}
void reset() {
for (typename file_iterators_t::const_iterator it= _iterators.begin();
it != _iterators.end();
++it) {
(**it).reset();
}
_current_iterator= _iterators.begin();
_terminated= (_current_iterator == _iterators.end());
while (!_terminated && (*_current_iterator)->terminated()) {
++_current_iterator;
_terminated |= (_current_iterator == _iterators.end());
}
_current_position= 0;
}
bool terminated() const {
return _terminated;
}
private:
// no need of copy ctor nor assignment operator
MultiFileIterator();
MultiFileIterator(const MultiFileIterator<TFileValue,TFile,TValue,TReader>& );
MultiFileIterator& operator=(const MultiFileIterator<TFileValue,TFile,TValue,TReader>& );
};
#include "util.h"
template <typename TFileValue, typename TFile, typename TValue, typename TReader>
MultiFileIterator<TFileValue,TFile,TValue,TReader>:: \
MultiFileIterator(const std::vector< std::string >& filenames)
: _current_position(0)
{
if (filenames.empty()) {
_FAIL_AND_LOG("Can't initialize a MultiFileIterator without filenames.");
}
for (std::vector<std::string>::const_iterator it= filenames.begin();
it != filenames.end();
++it) {
_iterators.push_back(new file_iterator_t(*it));
}
_current_iterator= _iterators.begin();
_terminated= (_current_iterator == _iterators.end());
while (!_terminated && (*_current_iterator)->terminated()) {
++_current_iterator;
_terminated |= (_current_iterator == _iterators.end());
}
}
#endif