General Purpose library for Freestanding C++ and POSIX systems
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

238 righe
5.7 KiB

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