indirect_range_iterator.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 namespace illumina { namespace interop { namespace util
13 {
16  {
17  public:
25  {
26  advance();
27  return *this;
28  }
31  void operator()()
32  {
33  reset();
34  }
39  operator bool() const
40  {
41  return !is_done();
42  }
43 
44  public:
47  virtual void reset()=0;
50  virtual void advance()=0;
55  virtual bool is_done()const=0;
56  };
57 
63  {
64  public:
71  m_iterator_1(it1), m_iterator_2(it2)
72  {
73  }
81  template<int N>
82  chain_range_iterator(abstract_range_iterator* (&iterators)[N], size_t current=0, size_t dummy=0)
83  {
84  (void)dummy;
85  m_iterator_1 = iterators[current];
86  if( (current+2) < N )
87  m_iterator_2 = new chain_range_iterator(iterators, current+1, 0);
88  else m_iterator_2 = iterators[current+1];
89  }
92  {
93  delete m_iterator_1;
94  delete m_iterator_2;
95  }
96 
97  public:
104  abstract_range_iterator(), m_iterator_1(other.m_iterator_1), m_iterator_2(other.m_iterator_2)
105  {
106  other.m_iterator_1 = 0;
107  other.m_iterator_2 = 0;
108  }
116  {
117  delete m_iterator_1;
118  delete m_iterator_2;
119  m_iterator_1 = other.m_iterator_1;
120  m_iterator_2 = other.m_iterator_2;
121  other.m_iterator_1 = 0;
122  other.m_iterator_2 = 0;
123  return *this;
124  }
125 
126  public:
129  void reset()
130  {
131  INTEROP_ASSERT(m_iterator_1 != 0);
132  INTEROP_ASSERT(m_iterator_2 != 0);
133  if(m_iterator_2 == 0 || m_iterator_1 == 0) return;
134  m_iterator_1->reset();
135  m_iterator_2->reset();
136  }
139  void advance()
140  {
141  INTEROP_ASSERT(m_iterator_1 != 0);
142  INTEROP_ASSERT(m_iterator_2 != 0);
143  if(m_iterator_2 == 0 || m_iterator_1 == 0) return;
144  m_iterator_1->advance();
145  if(m_iterator_1->is_done())
146  {
147 
148  m_iterator_2->advance();
149  if(!m_iterator_2->is_done()) m_iterator_1->reset();
150  }
151  }
156  bool is_done()const
157  {
158  INTEROP_ASSERT(m_iterator_2 != 0);
159  if(m_iterator_2 == 0 || m_iterator_1 == 0) return true;
160  return m_iterator_1->is_done() && m_iterator_2->is_done();
161  }
162 
163  private:
164  mutable abstract_range_iterator* m_iterator_1;
165  mutable abstract_range_iterator* m_iterator_2;
166  };
169  template<typename T, typename Storage=T>
171  {
172  public:
177  indirect_range_iterator(T &rval) : m_value(&rval),
178  m_begin(static_cast<Storage>(rval)),
179  m_end(static_cast<Storage>(rval))
180  {
181  reset();
182  }
190  indirect_range_iterator(T &rval, const T beg, const T end, const bool reset_state=true) : m_value(&rval),
191  m_begin(static_cast<Storage>(beg)),
192  m_end(static_cast<Storage>(end))
193  {
194  if(reset_state) reset();
195  }
198 
199  public:
202  void reset()
203  {
204  (*m_value) = static_cast<T>(m_begin);
205  }
208  void advance()
209  {
210  const Storage sval = static_cast<Storage>(*m_value)+1;
211  (*m_value) = static_cast<T>(sval);
212  }
213 
218  bool is_done() const
219  {
220  return (*m_value) >= static_cast<T>(m_end);
221  }
222 
223  private:
224  T* m_value;
225  Storage m_begin;
226  Storage m_end;
227  };
228 
229 }}}
Definition: indirect_range_iterator.h:170
Definition: indirect_range_iterator.h:15
virtual ~abstract_range_iterator()
Definition: indirect_range_iterator.h:19
Definition: enum_description.h:15
chain_range_iterator(abstract_range_iterator *it1=0, abstract_range_iterator *it2=0)
Definition: indirect_range_iterator.h:70
chain_range_iterator & operator=(const chain_range_iterator &other)
Definition: indirect_range_iterator.h:115
Definition: indirect_range_iterator.h:62
bool is_done() const
Definition: indirect_range_iterator.h:218
virtual ~indirect_range_iterator()
Definition: indirect_range_iterator.h:197
virtual ~chain_range_iterator()
Definition: indirect_range_iterator.h:91
#define INTEROP_ASSERT(TST)
Definition: assert.h:21
void advance()
Definition: indirect_range_iterator.h:208
void operator()()
Definition: indirect_range_iterator.h:31
void advance()
Definition: indirect_range_iterator.h:139
abstract_range_iterator & operator++()
Definition: indirect_range_iterator.h:24
indirect_range_iterator(T &rval, const T beg, const T end, const bool reset_state=true)
Definition: indirect_range_iterator.h:190
chain_range_iterator(abstract_range_iterator *(&iterators)[N], size_t current=0, size_t dummy=0)
Definition: indirect_range_iterator.h:82
Definition: enums.h:301
void reset()
Definition: indirect_range_iterator.h:129
chain_range_iterator(const chain_range_iterator &other)
Definition: indirect_range_iterator.h:103
void reset()
Definition: indirect_range_iterator.h:202
indirect_range_iterator(T &rval)
Definition: indirect_range_iterator.h:177
bool is_done() const
Definition: indirect_range_iterator.h:156