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

Last change on this file since 3336 was 3336, checked in by Peter, 7 years ago

closes #816

  • 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 3336 2014-10-24 23:27:45Z 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 <boost/cstdint.hpp>
26#include <boost/iterator/iterator_facade.hpp>
27#include <boost/iterator/iterator_categories.hpp>
28
29namespace theplu {
30namespace yat {
31namespace utility {
32
33  /**
34     \brief Iterator over a CIGAR
35
36     A CIGAR string is typically represented as a compact array, i.e.,
37     an array \c MMMMMMDDMMMM is represented as \c 6M2D4M. This class
38     is a proxy that allows iterating over a CIGAR as though the array
39     was \c MMMMMMDDMMMM.
40
41     CigarIterator is a \readable_iterator (not mutable) and and
42     models a \bidirectional_traversal_iterator. Its value_type is \c
43     uint8_t. Since no \c uint8_t is stored in CIGAR, the dereference
44     operator calculates the CIGAR element to return and returns by
45     value. CigarIterator is therefore an \input_iterator.
46
47     \since New in yat 0.13
48   */
49  template<typename BASE>
50  class CigarIterator
51    : public boost::iterator_facade<
52    CigarIterator<BASE>, uint8_t
53    , boost::bidirectional_traversal_tag, const uint8_t>
54  {
55  public:
56    /**
57       \brief Default constructor
58     */
59    CigarIterator(void);
60
61    /**
62       Construct an iterator that points to element offset in \a p. If
63       \a p corresponds to \c 5M and offset is 2, the iterator points to
64       the third \c M.
65     */
66    explicit CigarIterator(BASE p, size_t offset=0);
67
68    /**
69       \return underlying iterator
70     */
71    BASE base(void) const;
72  private:
73    friend class boost::iterator_core_access;
74
75    BASE base_;
76    size_t index_;
77
78    void decrement(void);
79    uint8_t dereference(void) const;
80    bool equal(const CigarIterator& other) const;
81    void increment(void);
82
83    // using compiler generated copy
84    //CigarIterator(const CigarIterator& other);
85    //CigarIterator& operator=(const CigarIterator&);
86  };
87
88
89  //// implementation  /////////////
90
91  template<typename BASE>
92  CigarIterator<BASE>::CigarIterator(void)
93    : base_(NULL) {}
94
95
96  template<typename BASE>
97  CigarIterator<BASE>::CigarIterator(BASE b, size_t x)
98    : base_(b), index_(x) {}
99
100
101  template<typename BASE>
102  BASE CigarIterator<BASE>::base(void) const
103  {
104    return base_;
105  }
106
107
108  template<typename BASE>
109  void CigarIterator<BASE>::decrement(void)
110  {
111    if (index_)
112      --index_;
113    else {
114      --base_;
115      index_ = bam_cigar_oplen(*base_)-1;
116    }
117  }
118
119
120  template<typename BASE>
121  uint8_t CigarIterator<BASE>::dereference(void) const
122  {
123    return bam_cigar_op(*base_);
124  }
125
126
127  template<typename BASE>
128  bool CigarIterator<BASE>::equal(const CigarIterator& other) const
129  {
130    return base_==other.base_ && index_==other.index_;
131  }
132
133
134  template<typename BASE>
135  void CigarIterator<BASE>::increment(void)
136  {
137    if (++index_ == bam_cigar_oplen(*base_)) {
138      index_=0;
139      ++base_;
140    }
141  }
142
143}}}
144#endif
Note: See TracBrowser for help on using the repository browser.