Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shared_field_iterator.h
Go to the documentation of this file.
1 #ifndef gamgee__shared_field_iterator__guard
2 #define gamgee__shared_field_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 
27 template<class VALUE_TYPE>
28 class SharedFieldIterator : public std::iterator<std::random_access_iterator_tag, VALUE_TYPE> {
29  public:
30 
36  m_body {nullptr},
37  m_current_data_ptr {nullptr},
38  m_original_data_ptr{nullptr},
39  m_end_data_ptr{nullptr},
40  m_bytes_per_value{0},
42  m_is_current_pointee_cached{false}
43  {}
44 
54  explicit SharedFieldIterator(const std::shared_ptr<bcf1_t>& body, uint8_t* data_ptr, uint8_t* end_ptr, const uint8_t bytes_per_value, const utils::VariantFieldType& type) :
55  m_body {body},
56  m_current_data_ptr {data_ptr},
57  m_original_data_ptr {data_ptr},
58  m_end_data_ptr {end_ptr},
59  m_bytes_per_value {bytes_per_value},
60  m_type {type},
61  m_is_current_pointee_cached{false}
62  {}
63 
72  explicit SharedFieldIterator(const std::shared_ptr<bcf1_t>& body, uint8_t* data_ptr, const uint8_t bytes_per_value, const utils::VariantFieldType& type): SharedFieldIterator(body, data_ptr, nullptr, bytes_per_value, type)
73  {}
74 
75  SharedFieldIterator(const SharedFieldIterator& other) = default;
76  SharedFieldIterator(SharedFieldIterator&& other) = default;
77  SharedFieldIterator& operator=(const SharedFieldIterator& other) = default;
79  bool operator==(const SharedFieldIterator& other) { return (m_body == other.m_body && m_current_data_ptr == other.m_current_data_ptr); }
81  bool operator!=(const SharedFieldIterator& other) { return !(*this == other); }
82  bool operator<(const SharedFieldIterator& other) { return m_body == other.m_body && m_current_data_ptr < other.m_current_data_ptr;}
83  bool operator>(const SharedFieldIterator& other) { return m_body == other.m_body && m_current_data_ptr > other.m_current_data_ptr;}
84  bool operator<=(const SharedFieldIterator& other) { return m_body == other.m_body && m_current_data_ptr <= other.m_current_data_ptr;}
85  bool operator>=(const SharedFieldIterator& other) { return m_body == other.m_body && m_current_data_ptr >= other.m_current_data_ptr;}
86 
87  VALUE_TYPE operator*() const noexcept {
88  if(m_is_current_pointee_cached)
89  return m_current_data_value;
90  else
91  return convert_from_byte_array(m_current_data_ptr, 0);
92  }
93 
94  /*
95  * @brief - operator* is const function, hence cannot set m_current_data_value and m_is_current_pointee_cached
96  */
97  VALUE_TYPE read_and_cache_current_pointee() noexcept {
98  m_current_data_value = *(*this);
99  m_is_current_pointee_cached = true;
100  return m_current_data_value;
101  }
102 
105  m_current_data_ptr += n * m_bytes_per_value;
106  m_is_current_pointee_cached = false;
107  m_current_data_ptr = utils::cache_and_advance_to_end_if_necessary(m_current_data_ptr, m_end_data_ptr, *this);
108  return *this;
109  }
110 
113  m_current_data_ptr -= n * m_bytes_per_value;
114  m_is_current_pointee_cached = false;
115  return *this;
116  }
117 
125  m_current_data_ptr += m_bytes_per_value;
126  m_is_current_pointee_cached = false;
127  m_current_data_ptr = utils::cache_and_advance_to_end_if_necessary(m_current_data_ptr, m_end_data_ptr, *this);
128  return *this;
129  }
130 
138  const auto tmp = SharedFieldIterator(*this);
139  operator++();
140  return tmp;
141  }
142 
143 
151  m_current_data_ptr -= m_bytes_per_value;
152  m_is_current_pointee_cached = false;
153  return *this;
154  }
155 
163  const auto tmp = SharedFieldIterator(*this);
164  operator--();
165  return tmp;
166  }
172  int32_t operator-(const SharedFieldIterator& first) const {
173  return static_cast<int32_t>(m_current_data_ptr - first.m_current_data_ptr)/m_bytes_per_value;
174  }
175 
183  VALUE_TYPE operator[](const uint32_t index) const {
184  return (index == 0 ? *(*this) : convert_from_byte_array(m_current_data_ptr, index));
185  }
186 
187  private:
188  std::shared_ptr<bcf1_t> m_body;
189  const uint8_t* m_current_data_ptr;
190  const uint8_t* m_original_data_ptr;
191  const uint8_t* m_end_data_ptr;
192  uint8_t m_bytes_per_value;
194  VALUE_TYPE m_current_data_value;
195  bool m_is_current_pointee_cached;
196 
197  VALUE_TYPE convert_from_byte_array(const uint8_t* data_ptr, int index) const;
198 };
199 
203 template<> inline
204 int32_t SharedFieldIterator<int32_t>::convert_from_byte_array(const uint8_t* data_ptr, int index) const {
205  return utils::convert_data_to_integer(data_ptr, index, m_bytes_per_value, m_type);
206 }
207 
211 template<> inline
212 float SharedFieldIterator<float>::convert_from_byte_array(const uint8_t* data_ptr, int index) const {
213  return utils::convert_data_to_float(data_ptr, index, m_bytes_per_value, m_type);
214 }
215 
219 template<> inline
220 std::string SharedFieldIterator<std::string>::convert_from_byte_array(const uint8_t* data_ptr, int index) const {
221  return utils::convert_data_to_string(data_ptr, index, m_bytes_per_value, m_type);
222 }
223 
224 };
225 
226 #endif // gamgee__shared_field_iterator__guard
bool operator<=(const SharedFieldIterator &other)
not greater than other
Definition: shared_field_iterator.h:84
VariantFieldType
an enumeration of the types in htslib for the format field values
Definition: variant_field_type.h:16
bool operator==(const SharedFieldIterator &other)
Definition: shared_field_iterator.h:80
int32_t operator-(const SharedFieldIterator &first) const
difference between two iterators as an integer.
Definition: shared_field_iterator.h:172
SharedFieldIterator()
default constructor of an empty iterator
Definition: shared_field_iterator.h:35
SharedFieldIterator operator--(int) noexcept
Postfix decrement. Move back to the previous sample.
Definition: shared_field_iterator.h:162
SharedFieldIterator(const std::shared_ptr< bcf1_t > &body, uint8_t *data_ptr, const uint8_t bytes_per_value, const utils::VariantFieldType &type)
Constructor with bcf1_t structure and start pointer of the vector/array.
Definition: shared_field_iterator.h:72
SharedFieldIterator operator++(int) noexcept
Postfix increment. Advances to the next sample.
Definition: shared_field_iterator.h:137
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
SharedFieldIterator & operator--()
advances to the previous value
Definition: shared_field_iterator.h:150
SharedFieldIterator & operator=(const SharedFieldIterator &other)=default
standard copy assignment operator creates a new iterator pointing to the same underlying data ...
bool operator>=(const SharedFieldIterator &other)
not smaller than other
Definition: shared_field_iterator.h:85
SharedFieldIterator & operator+=(const int n)
Definition: shared_field_iterator.h:104
bool operator>(const SharedFieldIterator &other)
not smaller than other neither equal to other
Definition: shared_field_iterator.h:83
SharedFieldIterator & operator-=(const int n)
Definition: shared_field_iterator.h:112
VALUE_TYPE operator*() const noexcept
Definition: shared_field_iterator.h:87
bool operator<(const SharedFieldIterator &other)
an operator is greater/less than another iterator if it is pointing to a previous element in the Shar...
Definition: shared_field_iterator.h:82
void iterator(const char *fname)
Definition: test-vcf-api.c:248
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
VALUE_TYPE operator[](const uint32_t index) const
random access to the value of a given index for reading or writing
Definition: shared_field_iterator.h:183
Definition: exceptions.h:9
VALUE_TYPE read_and_cache_current_pointee() noexcept
Definition: shared_field_iterator.h:97
bool operator!=(const SharedFieldIterator &other)
the negation of SharedFieldIterator::operator==()
Definition: shared_field_iterator.h:81
SharedFieldIterator & operator++() noexcept
advances to the next value
Definition: shared_field_iterator.h:124
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
iterator for SharedField objects.
Definition: shared_field_iterator.h:28
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
SharedFieldIterator(const std::shared_ptr< bcf1_t > &body, uint8_t *data_ptr, uint8_t *end_ptr, const uint8_t bytes_per_value, const utils::VariantFieldType &type)
Constructor with bcf1_t structure and start and end pointers of the vector/array. ...
Definition: shared_field_iterator.h:54