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_value_iterator.h
Go to the documentation of this file.
1 #ifndef gamgee__individual_field_value_iterator__guard
2 #define gamgee__individual_field_value_iterator__guard
3 
4 #include "../utils/variant_field_type.h"
5 
6 #include "htslib/vcf.h"
7 
8 #include <iterator>
9 #include <memory>
10 
11 namespace gamgee {
12 
30 template<class VALUE_TYPE>
31 class IndividualFieldValueIterator : public std::iterator<std::random_access_iterator_tag, VALUE_TYPE> {
32  public:
33 
44  explicit IndividualFieldValueIterator(const std::shared_ptr<bcf1_t>& body, uint8_t* data_ptr, uint8_t* end_ptr, const uint8_t num_bytes, const utils::VariantFieldType& type) :
45  m_body {body},
46  m_current_data_ptr {data_ptr},
47  m_original_data_ptr {data_ptr},
48  m_end_data_ptr {end_ptr},
49  m_num_bytes {num_bytes},
50  m_type {type},
51  m_is_current_pointee_cached {false}
52  {}
53 
63  explicit IndividualFieldValueIterator(const std::shared_ptr<bcf1_t>& body, uint8_t* data_ptr, const uint8_t num_bytes, const utils::VariantFieldType& type): IndividualFieldValueIterator(body, data_ptr, nullptr, num_bytes, type)
64  {}
65 
70  m_body {other.m_body},
71  m_current_data_ptr {other.m_current_data_ptr},
72  m_original_data_ptr {other.m_original_data_ptr},
73  m_end_data_ptr {other.m_end_data_ptr},
74  m_num_bytes {other.m_num_bytes},
75  m_type {other.m_type},
76  m_current_data_value {other.m_current_data_value},
77  m_is_current_pointee_cached {other.m_is_current_pointee_cached}
78  {}
79 
84  m_body {std::move(other.m_body)},
85  m_current_data_ptr {other.m_current_data_ptr},
86  m_original_data_ptr {other.m_original_data_ptr},
87  m_end_data_ptr {other.m_end_data_ptr},
88  m_num_bytes {other.m_num_bytes},
89  m_type {other.m_type},
90  m_current_data_value {other.m_current_data_value},
91  m_is_current_pointee_cached {other.m_is_current_pointee_cached}
92  {}
93 
98  if (&this == other)
99  return *this;
100  m_body = std::move(other.m_body);
101  m_current_data_ptr = other.m_current_data_ptr;
102  m_original_data_ptr = other.m_original_data_ptr;
103  m_end_data_ptr = other.m_end_data_ptr;
104  m_num_bytes = other.m_num_bytes;
105  m_type = other.m_type;
106  m_current_data_value = other.m_current_data_value;
107  m_is_current_pointee_cached = other.m_is_current_pointee_cached;
108  return *this;
109  }
110 
111 
116  if (&this == other)
117  return *this;
118  m_body = std::move(other.m_body);
119  m_current_data_ptr = other.m_current_data_ptr;
120  m_original_data_ptr = other.m_original_data_ptr;
121  m_end_data_ptr = other.m_end_data_ptr;
122  m_num_bytes = other.m_num_bytes;
123  m_type = other.m_type;
124  m_current_data_value = other.m_current_data_value;
125  m_is_current_pointee_cached = other.m_is_current_pointee_cached;
126  return *this;
127  }
128 
133  m_current_data_ptr += n * m_num_bytes;
134  m_is_current_pointee_cached = false;
135  m_current_data_ptr = utils::cache_and_advance_to_end_if_necessary(m_current_data_ptr, m_end_data_ptr, *this);
136  return *this;
137  }
138 
143  m_current_data_ptr -= n * m_num_bytes;
144  m_is_current_pointee_cached = false;
145  return *this;
146  }
147 
151  bool operator==(const IndividualFieldValueIterator& other) { return (m_body == other.m_body && m_current_data_ptr == other.m_current_data_ptr); }
152 
157  return !(*this == other);
158  }
159 
165  return m_body == other.m_body && m_current_data_ptr < other.m_current_data_ptr;
166  }
167 
172  return m_body == other.m_body && m_current_data_ptr > other.m_current_data_ptr;
173  }
174 
179  return m_body == other.m_body && m_current_data_ptr <= other.m_current_data_ptr;
180  }
181 
186  return m_body == other.m_body && m_current_data_ptr >= other.m_current_data_ptr;
187  }
188 
193  VALUE_TYPE operator*() const noexcept {
194  if(m_is_current_pointee_cached)
195  return m_current_data_value;
196  else
197  return convert_from_byte_array(m_current_data_ptr, 0);
198  }
199 
200  /*
201  * @brief - operator* is const function, hence cannot set m_current_data_value and m_is_current_pointee_cached
202  */
203  VALUE_TYPE read_and_cache_current_pointee() noexcept {
204  m_current_data_value = *(*this);
205  m_is_current_pointee_cached = true;
206  return m_current_data_value;
207  }
215  m_current_data_ptr += m_num_bytes;
216  m_is_current_pointee_cached = false;
217  m_current_data_ptr = utils::cache_and_advance_to_end_if_necessary(m_current_data_ptr, m_end_data_ptr, *this);
218  return *this;
219  }
220 
228  auto const tmp = IndividualFieldValueIterator(*this);
229  operator++();
230  return tmp;
231  }
239  m_current_data_ptr -= m_num_bytes;
240  m_is_current_pointee_cached = false;
241  return *this;
242  }
243 
251  auto const tmp = IndividualFieldValueIterator(*this);
252  operator--();
253  return tmp;
254  }
262  VALUE_TYPE operator[](const uint32_t index) const {
263  //if index == 0, use the cached value logic in operator*
264  return (index == 0 ? *(*this) : convert_from_byte_array(m_current_data_ptr, index));
265  }
266 
267  private:
268  std::shared_ptr<bcf1_t> m_body;
269  const uint8_t* m_current_data_ptr;
270  const uint8_t* m_original_data_ptr;
271  const uint8_t* m_end_data_ptr;
272  uint8_t m_num_bytes;
274  VALUE_TYPE m_current_data_value;
275  bool m_is_current_pointee_cached;
276 
277  VALUE_TYPE convert_from_byte_array(const uint8_t* data_ptr, int index) const;
278 };
279 
283 template<> inline
284 int32_t IndividualFieldValueIterator<int32_t>::convert_from_byte_array(const uint8_t* data_ptr, int index) const {
285  return utils::convert_data_to_integer(data_ptr, index, m_num_bytes, m_type);
286 }
287 
291 template<> inline
292 float IndividualFieldValueIterator<float>::convert_from_byte_array(const uint8_t* data_ptr, int index) const {
293  return utils::convert_data_to_float(data_ptr, index, m_num_bytes, m_type);
294 }
295 
299 template<> inline
300 std::string IndividualFieldValueIterator<std::string>::convert_from_byte_array(const uint8_t* data_ptr, int index) const {
301  return utils::convert_data_to_string(data_ptr, index, m_num_bytes, m_type);
302 }
303 
304 };
305 
306 #endif // gamgee__individual_field_value_iterator__guard
VariantFieldType
an enumeration of the types in htslib for the format field values
Definition: variant_field_type.h:16
IndividualFieldValueIterator operator++(int) noexcept
postfix operator to advance iterator to the next position
Definition: individual_field_value_iterator.h:227
bool operator==(const IndividualFieldValueIterator &other)
two iterators are equal if they are in exactly the same state (pointing at the same location in memor...
Definition: individual_field_value_iterator.h:151
IndividualFieldValueIterator(IndividualFieldValueIterator &&other) noexcept
copy constructor is only meant for internal STL functions use. It makes a shallow copy of the underly...
Definition: individual_field_value_iterator.h:83
IndividualFieldValueIterator operator--(int) noexcept
postfix operator to retreat iterator to the previous position
Definition: individual_field_value_iterator.h:250
std::string convert_data_to_string(const uint8_t *data_ptr, const int index, const uint8_t num_bytes_per_value, const VariantFieldType &type)
converts the value in an index from the byte array into string
Definition: variant_field_type.cpp:77
IndividualFieldValueIterator & operator=(IndividualFieldValueIterator &&other) noexcept
safely moves the data from one VariantField to a new one without making any copies ...
Definition: individual_field_value_iterator.h:115
IndividualFieldValueIterator & operator+=(const int n)
simple compound assignment operation for random advances (back/forward) to the iterator ...
Definition: individual_field_value_iterator.h:132
IndividualFieldValueIterator & operator--()
advances to the previous sample
Definition: individual_field_value_iterator.h:238
IndividualFieldValueIterator(const std::shared_ptr< bcf1_t > &body, uint8_t *data_ptr, uint8_t *end_ptr, const uint8_t num_bytes, const utils::VariantFieldType &type)
Constructor with bcf1_t structure and start and end pointers of the array/vector. ...
Definition: individual_field_value_iterator.h:44
bool operator>(const IndividualFieldValueIterator &other)
an operator is greater/less than another iterator if it is pointing to a previous element (sample) in...
Definition: individual_field_value_iterator.h:171
IndividualFieldValueIterator & operator++() noexcept
advances to the next sample
Definition: individual_field_value_iterator.h:214
VALUE_TYPE operator[](const uint32_t index) const
random access to the value of a given index for reading or writing
Definition: individual_field_value_iterator.h:262
void iterator(const char *fname)
Definition: test-vcf-api.c:248
IndividualFieldValueIterator(const IndividualFieldValueIterator &other)
copy constructor is only meant for internal STL functions use. It makes a shallow copy of the underly...
Definition: individual_field_value_iterator.h:69
const uint8_t * cache_and_advance_to_end_if_necessary(const uint8_t *current_ptr, const uint8_t *end_ptr, ITER &it)
advances current ptr to end of the vector if the current element is bcf_*_vector_end ...
Definition: utils.h:130
IndividualFieldValueIterator & operator-=(const int n)
simple compound assignment operation for random advances (back/forward) to the iterator ...
Definition: individual_field_value_iterator.h:142
Definition: exceptions.h:9
IndividualFieldValueIterator & operator=(const IndividualFieldValueIterator &other)
copy constructor is only meant for internal STL functions use. It makes a shallow copy of the underly...
Definition: individual_field_value_iterator.h:97
bool operator>=(const IndividualFieldValueIterator &other)
an operator is greater/less than another iterator if it is pointing to a previous element (sample) in...
Definition: individual_field_value_iterator.h:185
VALUE_TYPE read_and_cache_current_pointee() noexcept
Definition: individual_field_value_iterator.h:203
iterator for FormatFieldGenericValue objects.
Definition: individual_field_value_iterator.h:31
bool operator<=(const IndividualFieldValueIterator &other)
an operator is greater/less than another iterator if it is pointing to a previous element (sample) in...
Definition: individual_field_value_iterator.h:178
IndividualFieldValueIterator(const std::shared_ptr< bcf1_t > &body, uint8_t *data_ptr, const uint8_t num_bytes, const utils::VariantFieldType &type)
Constructor with bcf1_t structure and start pointer of the array/vector.
Definition: individual_field_value_iterator.h:63
int32_t convert_data_to_integer(const uint8_t *data_ptr, const int index, const uint8_t num_bytes_per_value, const VariantFieldType &type)
converts the value in an index from the byte array into int32_t
Definition: variant_field_type.cpp:11
float convert_data_to_float(const uint8_t *data_ptr, const int index, const uint8_t num_bytes_per_value, const VariantFieldType &type)
converts the value in an index from the byte array into float
Definition: variant_field_type.cpp:41
bool operator!=(const IndividualFieldValueIterator &other)
the oposite check of IndividualFieldValueIterator::operator==()
Definition: individual_field_value_iterator.h:156
bool operator<(const IndividualFieldValueIterator &other)
an operator is greater/less than another iterator if it is pointing to a previous element (sample) in...
Definition: individual_field_value_iterator.h:164
VALUE_TYPE operator*() const noexcept
direct access to the value of the current sample
Definition: individual_field_value_iterator.h:193