1 | #ifndef theplu_yat_utility_cigar_iterator |
---|
2 | #define theplu_yat_utility_cigar_iterator |
---|
3 | |
---|
4 | // $Id: CigarIterator.h 3372 2015-02-11 01:31:03Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2014 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 3 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "Cigar.h" |
---|
26 | |
---|
27 | #include <boost/cstdint.hpp> |
---|
28 | #include <boost/iterator/iterator_facade.hpp> |
---|
29 | #include <boost/iterator/iterator_categories.hpp> |
---|
30 | |
---|
31 | #include <cstddef> // for size_t |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace utility { |
---|
36 | |
---|
37 | /** |
---|
38 | \brief Iterator over a CIGAR |
---|
39 | |
---|
40 | A CIGAR string is typically represented as a compact array, i.e., |
---|
41 | an array \c MMMMMMDDMMMM is represented as \c 6M2D4M. This class |
---|
42 | is a proxy that allows iterating over a CIGAR as though the array |
---|
43 | was \c MMMMMMDDMMMM. |
---|
44 | |
---|
45 | CigarIterator is a \readable_iterator (not mutable) and |
---|
46 | models a \bidirectional_traversal_iterator. Its value_type is \c |
---|
47 | uint8_t. Since no \c uint8_t is stored in CIGAR, the dereference |
---|
48 | operator calculates the CIGAR element to return and returns by |
---|
49 | value. CigarIterator is therefore an \input_iterator. |
---|
50 | |
---|
51 | \since New in yat 0.13 |
---|
52 | */ |
---|
53 | template<typename BASE> |
---|
54 | class CigarIterator |
---|
55 | : public boost::iterator_facade< |
---|
56 | CigarIterator<BASE>, uint8_t |
---|
57 | , boost::bidirectional_traversal_tag, const uint8_t> |
---|
58 | { |
---|
59 | public: |
---|
60 | /** |
---|
61 | \brief Default constructor |
---|
62 | */ |
---|
63 | CigarIterator(void); |
---|
64 | |
---|
65 | /** |
---|
66 | Construct an iterator that points to element offset in \a p. If |
---|
67 | \a p corresponds to \c 5M and offset is 2, the iterator points to |
---|
68 | the third \c M. |
---|
69 | */ |
---|
70 | explicit CigarIterator(BASE p, size_t offset=0); |
---|
71 | |
---|
72 | /** |
---|
73 | \return underlying iterator |
---|
74 | */ |
---|
75 | BASE base(void) const; |
---|
76 | private: |
---|
77 | friend class boost::iterator_core_access; |
---|
78 | |
---|
79 | BASE base_; |
---|
80 | size_t index_; |
---|
81 | |
---|
82 | void decrement(void); |
---|
83 | uint8_t dereference(void) const; |
---|
84 | bool equal(const CigarIterator& other) const; |
---|
85 | void increment(void); |
---|
86 | |
---|
87 | // using compiler generated copy |
---|
88 | //CigarIterator(const CigarIterator& other); |
---|
89 | //CigarIterator& operator=(const CigarIterator&); |
---|
90 | }; |
---|
91 | |
---|
92 | |
---|
93 | //// implementation ///////////// |
---|
94 | |
---|
95 | template<typename BASE> |
---|
96 | CigarIterator<BASE>::CigarIterator(void) {} |
---|
97 | |
---|
98 | |
---|
99 | template<typename BASE> |
---|
100 | CigarIterator<BASE>::CigarIterator(BASE b, size_t x) |
---|
101 | : base_(b), index_(x) {} |
---|
102 | |
---|
103 | |
---|
104 | template<typename BASE> |
---|
105 | BASE CigarIterator<BASE>::base(void) const |
---|
106 | { |
---|
107 | return base_; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | template<typename BASE> |
---|
112 | void CigarIterator<BASE>::decrement(void) |
---|
113 | { |
---|
114 | if (index_) |
---|
115 | --index_; |
---|
116 | else { |
---|
117 | --base_; |
---|
118 | index_ = bam_cigar_oplen(*base_)-1; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | template<typename BASE> |
---|
124 | uint8_t CigarIterator<BASE>::dereference(void) const |
---|
125 | { |
---|
126 | return bam_cigar_op(*base_); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | template<typename BASE> |
---|
131 | bool CigarIterator<BASE>::equal(const CigarIterator& other) const |
---|
132 | { |
---|
133 | return base_==other.base_ && index_==other.index_; |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | template<typename BASE> |
---|
138 | void CigarIterator<BASE>::increment(void) |
---|
139 | { |
---|
140 | if (++index_ == bam_cigar_oplen(*base_)) { |
---|
141 | index_=0; |
---|
142 | ++base_; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | }}} |
---|
147 | #endif |
---|