source: trunk/yat/utility/CigarIterator.h @ 3368

Last change on this file since 3368 was 3368, checked in by Peter, 9 years ago

add missing includes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1#ifndef theplu_yat_utility_cigar_iterator
2#define theplu_yat_utility_cigar_iterator
3
4// $Id: CigarIterator.h 3368 2015-02-10 06:06:41Z 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
33namespace theplu {
34namespace yat {
35namespace 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 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    : base_(NULL) {}
98
99
100  template<typename BASE>
101  CigarIterator<BASE>::CigarIterator(BASE b, size_t x)
102    : base_(b), index_(x) {}
103
104
105  template<typename BASE>
106  BASE CigarIterator<BASE>::base(void) const
107  {
108    return base_;
109  }
110
111
112  template<typename BASE>
113  void CigarIterator<BASE>::decrement(void)
114  {
115    if (index_)
116      --index_;
117    else {
118      --base_;
119      index_ = bam_cigar_oplen(*base_)-1;
120    }
121  }
122
123
124  template<typename BASE>
125  uint8_t CigarIterator<BASE>::dereference(void) const
126  {
127    return bam_cigar_op(*base_);
128  }
129
130
131  template<typename BASE>
132  bool CigarIterator<BASE>::equal(const CigarIterator& other) const
133  {
134    return base_==other.base_ && index_==other.index_;
135  }
136
137
138  template<typename BASE>
139  void CigarIterator<BASE>::increment(void)
140  {
141    if (++index_ == bam_cigar_oplen(*base_)) {
142      index_=0;
143      ++base_;
144    }
145  }
146
147}}}
148#endif
Note: See TracBrowser for help on using the repository browser.