Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
individual_field_iterator.h
Go to the documentation of this file.
1 #ifndef gamgee__individual_field_iterator__guard
2 #define gamgee__individual_field_iterator__guard
3 
4 #include "../utils/utils.h"
5 
6 #include "htslib/vcf.h"
7 
8 #include<iterator>
9 
10 namespace gamgee {
11 
12 
27 template<class TYPE>
28 class IndividualFieldIterator : public std::iterator<std::random_access_iterator_tag, TYPE> {
29  public:
30 
38  IndividualFieldIterator(const std::shared_ptr<bcf1_t>& body, bcf_fmt_t* format_ptr, bool end_iterator = false) :
39  m_body {body},
40  m_format_ptr {format_ptr},
41  m_data_ptr {end_iterator ? format_ptr->p + m_format_ptr->size * m_body->n_sample : format_ptr->p}
42  {}
43 
53  m_body {other.m_body},
54  m_format_ptr {other.m_format_ptr},
55  m_data_ptr {other.m_data_ptr}
56  {}
57 
63  m_body {std::move(other.m_body)},
64  m_format_ptr {other.m_format_ptr},
65  m_data_ptr {other.m_data_ptr}
66  {}
67 
72  m_body = other.m_body;
73  m_format_ptr = other.m_format_ptr;
74  m_data_ptr = other.m_data_ptr;
75  return *this;
76  }
77 
82  m_body = std::move(other.m_body);
83  m_format_ptr = other.m_format_ptr;
84  m_data_ptr = other.m_data_ptr;
85  return *this;
86  }
87 
93  IndividualFieldIterator& operator+=(const int n) noexcept {
94  m_data_ptr += n * m_format_ptr->size;
95  return *this;
96  }
97 
101  IndividualFieldIterator& operator-=(const int n) noexcept {
102  m_data_ptr -= n * m_format_ptr->size;
103  return *this;
104  }
105 
109  bool operator==(const IndividualFieldIterator& other) {
110  return m_body == other.m_body && m_data_ptr == other.m_data_ptr;
111  }
112 
116  bool operator!=(const IndividualFieldIterator& other) {
117  return m_body != other.m_body || m_data_ptr != other.m_data_ptr;
118  }
119 
124  bool operator<(const IndividualFieldIterator& other) {
125  return m_body == other.m_body && m_data_ptr < other.m_data_ptr;
126  }
127 
131  bool operator>(const IndividualFieldIterator& other) {
132  return m_body == other.m_body && m_data_ptr > other.m_data_ptr;
133  }
134 
138  bool operator<=(const IndividualFieldIterator& other) {
139  return m_body == other.m_body && m_data_ptr <= other.m_data_ptr;
140  }
141 
145  bool operator>=(const IndividualFieldIterator& other) {
146  return m_body == other.m_body && m_data_ptr >= other.m_data_ptr;
147  }
148 
153  TYPE operator*() const noexcept {
154  return TYPE{m_body, m_format_ptr, m_data_ptr};
155  }
156 
164  operator+=(1);
165  return *this;
166  }
167 
175  auto const tmp = IndividualFieldIterator(*this);
176  operator++();
177  return tmp;
178  }
179 
187  operator-=(1);
188  return *this;
189  }
190 
198  auto const tmp = IndividualFieldIterator(*this);
199  operator--();
200  return tmp;
201  }
202 
210  TYPE operator[](const uint32_t sample) const {
211  uint32_t curr_sample_idx = (uint32_t)(((uint64_t)(m_data_ptr - m_format_ptr->p))/(m_format_ptr->size));
212  utils::check_max_boundary(curr_sample_idx + sample, m_body->n_sample);
213  return TYPE{m_body, m_format_ptr, m_data_ptr + (sample * m_format_ptr->size)};
214  }
215 
222  int32_t operator-(const IndividualFieldIterator<TYPE>& first) const {
223  return static_cast<int32_t>(m_data_ptr - first.m_data_ptr)/m_format_ptr->size;
224  }
225 
226  private:
227  std::shared_ptr<bcf1_t> m_body;
228  bcf_fmt_t* m_format_ptr;
229  uint8_t* m_data_ptr;
230 
231 };
232 
233 }
234 
235 #endif // gamgee__individual_field_iterator__guard
bool operator<(const IndividualFieldIterator &other)
an operator is greater/less than another iterator if it is pointing to a previous element (sample) in...
Definition: individual_field_iterator.h:124
IndividualFieldIterator(IndividualFieldIterator &&other) noexcept
safely moves the data from one VariantField to a new one without making any copies ...
Definition: individual_field_iterator.h:62
uint8_t * p
Definition: vcf.h:139
void check_max_boundary(const uint32_t index, const uint32_t size, const std::string &prefix_msg)
checks that an index is greater than or equal to size
Definition: utils.h:63
IndividualFieldIterator & operator--() noexcept
Prefix increment. Reverses to the previous sample.
Definition: individual_field_iterator.h:186
bool operator>(const IndividualFieldIterator &other)
an operator is greater/less than another iterator if it is pointing to a previous element (sample) in...
Definition: individual_field_iterator.h:131
bool operator!=(const IndividualFieldIterator &other)
the oposite check of IndividualFieldIterator::operator==()
Definition: individual_field_iterator.h:116
TYPE operator*() const noexcept
direct access to the value of the current sample
Definition: individual_field_iterator.h:153
TYPE operator[](const uint32_t sample) const
random access to the value of a given sample for reading or writing
Definition: individual_field_iterator.h:210
bool operator==(const IndividualFieldIterator &other)
two iterators are equal if they are in exactly the same state (pointing at the same location in memor...
Definition: individual_field_iterator.h:109
bool operator<=(const IndividualFieldIterator &other)
an operator is greater/less than another iterator if it is pointing to a previous element (sample) in...
Definition: individual_field_iterator.h:138
IndividualFieldIterator & operator=(IndividualFieldIterator &&other) noexcept
safely moves the data from one VariantField to a new one without making any copies ...
Definition: individual_field_iterator.h:81
IndividualFieldIterator & operator+=(const int n) noexcept
simple compound assignment operation for random advances (back/forward) to the iterator ...
Definition: individual_field_iterator.h:93
bool operator>=(const IndividualFieldIterator &other)
an operator is greater/less than another iterator if it is pointing to a previous element (sample) in...
Definition: individual_field_iterator.h:145
int32_t operator-(const IndividualFieldIterator< TYPE > &first) const
Difference between two iterators as an integer.
Definition: individual_field_iterator.h:222
IndividualFieldIterator & operator=(const IndividualFieldIterator &other)
copy constructor is only meant for internal STL functions use. It makes a shallow copy of the underly...
Definition: individual_field_iterator.h:71
IndividualFieldIterator & operator++() noexcept
Prefix increment. Advances to the next sample.
Definition: individual_field_iterator.h:163
IndividualFieldIterator(const IndividualFieldIterator &other)
copy constructor is only meant for internal STL functions use. It makes a shallow copy of the underly...
Definition: individual_field_iterator.h:52
Definition: vcf.h:136
IndividualFieldIterator & operator-=(const int n) noexcept
simple compound assignment operation for random advances (back/forward) to the iterator ...
Definition: individual_field_iterator.h:101
void iterator(const char *fname)
Definition: test-vcf-api.c:248
IndividualFieldIterator operator--(int) noexcept
Postfix decrement. Reverses to the previous sample.
Definition: individual_field_iterator.h:197
IndividualFieldIterator operator++(int) noexcept
Postfix increment. Advances to the next sample.
Definition: individual_field_iterator.h:174
Definition: exceptions.h:9
iterator for VariantField objects.
Definition: individual_field_iterator.h:28
int size
Definition: vcf.h:138
IndividualFieldIterator(const std::shared_ptr< bcf1_t > &body, bcf_fmt_t *format_ptr, bool end_iterator=false)
simple constructor used by VariantField to create an iterator
Definition: individual_field_iterator.h:38