1 #ifndef BFG_KMER_ITERATOR_HPP 2 #define BFG_KMER_ITERATOR_HPP 15 class KmerIterator :
public std::iterator<std::input_iterator_tag, std::pair<Kmer, int>, int> {
20 KmerIterator(
const char *s) : s_(s), p_(), invalid_(
false) { find_next(-1,-1,
false);}
29 bool operator!=(
const KmerIterator& o) {
return !this->operator==(o);}
31 std::pair<Kmer, int>& operator*();
32 std::pair<Kmer, int> *operator->();
36 void find_next(
size_t i,
size_t j,
bool last_valid);
39 std::pair<Kmer, int> p_;
48 KmerHashIterator(
const char* _s,
const int _length,
const int _k) : s(_s), n(_length), k(_k), hf(HF(_k)), p_(0,-1), invalid(
true) {
50 if ((_s != NULL) || (n >= k)) {
57 KmerHashIterator() : s(NULL), n(0), k(0), hf(HF(0)), p_(0,-1), invalid(
true) {}
63 if (invalid || o.invalid)
return invalid && o.invalid;
64 return s==o.s && n==o.n && k==o.k && p_.first==o.p_.first && p_.second==o.p_.second;
71 if (invalid)
return *
this;
75 if (p_.second >= n - k + 1 || s[p_.second + k - 1] ==
'\0') {
78 p_ = std::make_pair(0,-1);
84 int j = p_.second + k - 1;
88 while (j >= p_.second) {
92 if ((c ==
'A') || (c ==
'C') || (c ==
'G') || (c ==
'T')) j--;
95 p_.second += j - p_.second + 1;
97 if (p_.second >= n - k + 1 || s[p_.second + k - 1] ==
'\0') {
100 p_ = std::make_pair(0,-1);
104 j = p_.second + k - 1;
108 hf.init(&s[p_.second]);
114 if ((c ==
'A') || (c ==
'C') || (c ==
'G') || (c ==
'T')) hf.update(s[j-k], s[j]);
119 if (p_.second >= n - k + 1 || s[p_.second + k - 1] ==
'\0') {
122 p_ = std::make_pair(0,-1);
126 j = p_.second + k - 1;
128 while (j >= p_.second) {
132 if ((c ==
'A') || (c ==
'C') || (c ==
'G') || (c ==
'T')) j--;
135 p_.second += j - p_.second + 1;
137 if (p_.second >= n - k + 1 || s[p_.second + k - 1] ==
'\0') {
140 p_ = std::make_pair(0,-1);
144 j = p_.second + k - 1;
148 hf.init(&s[p_.second]);
152 p_.first = hf.hash();
168 size_t next_pos = p_.second + length;
170 while (!invalid && p_.second < next_pos) operator++();
175 std::pair<uint64_t, int>& operator*() {
return p_; }
177 std::pair<uint64_t, int>* operator->() {
return &(operator*()); }
183 std::pair<uint64_t, int> p_;
187 #endif // BFG_KMER_ITERATOR_HPP
Definition: KmerIterator.hpp:44
Definition: KmerIterator.hpp:15