General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
5.2 KiB

  1. #pragma once
  2. #include <gp/utils/iterators/iterator_properties.hpp>
  3. #include <cstddef>
  4. #include <cstdint>
  5. // TODO: Specify the concept of an iterator
  6. namespace gp {
  7. /**
  8. * @brief An abstraction of a pointer to iterate against, in both normal and reverse order
  9. *
  10. * @tparam T The type of data pointed by the iterator
  11. * @tparam sign the direction in which data is scrutinized, should be either 1 or -1, behaviour for other value is left undefined
  12. */
  13. template<typename T, int sign = 1>
  14. struct pointer_iterator final
  15. {
  16. T* data; /**< the only data field of the class */
  17. typedef T value_type; /**< The type of which a reference will be returned on dereferencing */
  18. typedef std::size_t difference_type; /**< The type of the substraction of two pointers */
  19. static constexpr iterator_type_t iterator_type = iterator_type_t::contiguous_iterator; /**< @see iterator_type_t */
  20. /**
  21. * @brief Generates an empty iterator
  22. */
  23. constexpr pointer_iterator()
  24. : data{nullptr}
  25. {}
  26. constexpr pointer_iterator(const pointer_iterator& oth)
  27. : data{oth.data}
  28. {}
  29. /**
  30. * @brief Generates an iterator from any pointer
  31. */
  32. constexpr pointer_iterator(T* ptr)
  33. : data{ptr}
  34. {}
  35. /**
  36. * @brief Dereference unary operator
  37. *
  38. * @return constexpr T& returns a reference to the pointed value
  39. */
  40. constexpr T& operator*() const
  41. {
  42. return *data;
  43. }
  44. constexpr pointer_iterator& operator++()
  45. {
  46. data += sign;
  47. return *this;
  48. }
  49. constexpr pointer_iterator operator++(int)
  50. {
  51. auto p = *this;
  52. data += sign;
  53. return p;
  54. }
  55. constexpr pointer_iterator& operator--()
  56. {
  57. data -= sign;
  58. return *this;
  59. }
  60. constexpr pointer_iterator operator--(int)
  61. {
  62. auto p = *this;
  63. data -= sign;
  64. return p;
  65. }
  66. constexpr pointer_iterator operator+(const std::size_t offset) const
  67. {
  68. return pointer_iterator{data+sign*offset};
  69. }
  70. constexpr pointer_iterator operator+(const int offset) const
  71. {
  72. return pointer_iterator{data+sign*offset};
  73. }
  74. constexpr pointer_iterator operator-(const std::size_t offset) const
  75. {
  76. return pointer_iterator{data-sign*offset};
  77. }
  78. constexpr pointer_iterator operator-(const int offset) const
  79. {
  80. return pointer_iterator{data-sign*offset};
  81. }
  82. constexpr difference_type operator-(const pointer_iterator& oth) const
  83. {
  84. return ((T*)data-(T*)oth.data)*sign;
  85. }
  86. constexpr bool operator==(const pointer_iterator oth) const
  87. {
  88. return data==oth.data;
  89. }
  90. constexpr bool operator!=(pointer_iterator oth) const
  91. {
  92. return data!=oth.data;
  93. }
  94. constexpr bool before_or_equal(const pointer_iterator oth) const
  95. {
  96. return reinterpret_cast<std::intptr_t>(data) <= reinterpret_cast<std::intptr_t>(oth.data);
  97. }
  98. constexpr bool operator<=(const pointer_iterator oth) const
  99. {
  100. return before_or_equal(oth);
  101. }
  102. };
  103. /**
  104. * @brief An identical twin to the pointer_iterator, but which dereference to a const reference
  105. *
  106. * @see pointer_iterator
  107. * @tparam T @see pointer_iterator
  108. * @tparam sign @see pointer_iterator
  109. */
  110. template<typename T, int sign = 1>
  111. struct const_pointer_iterator final
  112. {
  113. const T* data; /**< @see pointer_iterator */
  114. typedef T value_type; /**< @see pointer_iterator */
  115. typedef std::size_t difference_type; /**< @see pointer_iterator */
  116. static constexpr iterator_type_t iterator_type = iterator_type_t::contiguous_iterator; /**< @see pointer_iterator */
  117. constexpr const_pointer_iterator(const const_pointer_iterator& oth)
  118. : data{oth.data}
  119. {}
  120. /**
  121. * @brief @see pointer_iterator
  122. */
  123. constexpr const_pointer_iterator(const T* ptr)
  124. : data{ptr}
  125. {}
  126. /**
  127. * @brief Dereferencing returns a const version of what a pointer_iterator would return
  128. */
  129. constexpr const T& operator*() const
  130. {
  131. return *data;
  132. }
  133. constexpr const_pointer_iterator& operator++()
  134. {
  135. data += sign;
  136. return *this;
  137. }
  138. constexpr const_pointer_iterator operator++(int)
  139. {
  140. auto p = data;
  141. data += sign;
  142. return const_pointer_iterator{p};
  143. }
  144. constexpr const_pointer_iterator& operator--()
  145. {
  146. data -= sign;
  147. return *this;
  148. }
  149. constexpr const_pointer_iterator operator--(int)
  150. {
  151. auto p = data;
  152. data -= sign;
  153. return const_pointer_iterator{p};
  154. }
  155. constexpr const_pointer_iterator operator+(const std::size_t offset) const
  156. {
  157. return const_pointer_iterator{data+sign*offset};
  158. }
  159. constexpr const_pointer_iterator operator+(const int offset) const
  160. {
  161. return const_pointer_iterator{data+sign*offset};
  162. }
  163. constexpr const_pointer_iterator operator-(const std::size_t offset) const
  164. {
  165. return const_pointer_iterator{data-sign*offset};
  166. }
  167. constexpr const_pointer_iterator operator-(const int offset)
  168. {
  169. return const_pointer_iterator{data-sign*offset};
  170. }
  171. constexpr difference_type operator-(const const_pointer_iterator& oth) const
  172. {
  173. return ((T*)data-(T*)oth.data)*sign;
  174. }
  175. constexpr bool operator==(const const_pointer_iterator oth) const
  176. {
  177. return data==oth.data;
  178. }
  179. constexpr bool operator!=(const_pointer_iterator oth) const
  180. {
  181. return data!=oth.data;
  182. }
  183. constexpr bool before_or_equal(const const_pointer_iterator oth) const
  184. {
  185. return reinterpret_cast<std::intptr_t>(data) <= reinterpret_cast<std::intptr_t>(oth.data);
  186. }
  187. constexpr bool operator<=(const const_pointer_iterator oth) const
  188. {
  189. return before_or_equal(oth);
  190. }
  191. };
  192. }