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.

199 lines
3.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #pragma once
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. enum class iterator_type_t{
  5. contiguous_iterator,
  6. non_contiguous_iterator,
  7. lazy_iterator
  8. };
  9. template<typename T, int sign = 1>
  10. struct pointer_iterator final
  11. {
  12. T* data;
  13. typedef T value_type;
  14. typedef std::size_t difference_type;
  15. static constexpr iterator_type_t iterator_type = iterator_type_t::contiguous_iterator;
  16. constexpr pointer_iterator()
  17. : data{nullptr}
  18. {}
  19. constexpr pointer_iterator(const pointer_iterator& oth)
  20. : data{oth.data}
  21. {}
  22. constexpr pointer_iterator(T* ptr)
  23. : data{ptr}
  24. {}
  25. constexpr T& operator*()
  26. {
  27. return *data;
  28. }
  29. constexpr pointer_iterator& operator++()
  30. {
  31. data += sign;
  32. return *this;
  33. }
  34. constexpr pointer_iterator operator++(int)
  35. {
  36. auto p = *this;
  37. data += sign;
  38. return p;
  39. }
  40. constexpr pointer_iterator& operator--()
  41. {
  42. data -= sign;
  43. return *this;
  44. }
  45. constexpr pointer_iterator operator--(int)
  46. {
  47. auto p = *this;
  48. data -= sign;
  49. return p;
  50. }
  51. constexpr pointer_iterator operator+(const std::size_t offset)
  52. {
  53. return pointer_iterator{data+sign*offset};
  54. }
  55. constexpr pointer_iterator operator+(const int offset)
  56. {
  57. return pointer_iterator{data+sign*offset};
  58. }
  59. constexpr pointer_iterator operator-(const std::size_t offset)
  60. {
  61. return pointer_iterator{data-sign*offset};
  62. }
  63. constexpr pointer_iterator operator-(const int offset)
  64. {
  65. return pointer_iterator{data-sign*offset};
  66. }
  67. constexpr difference_type operator-(const pointer_iterator& oth) const
  68. {
  69. return ((T*)data-(T*)oth.data)*sign;
  70. }
  71. constexpr bool operator==(const pointer_iterator oth)
  72. {
  73. return data==oth.data;
  74. }
  75. constexpr bool operator!=(pointer_iterator oth)
  76. {
  77. return data!=oth.data;
  78. }
  79. constexpr bool before_or_equal(const pointer_iterator oth)
  80. {
  81. return reinterpret_cast<std::intptr_t>(data) <= reinterpret_cast<std::intptr_t>(oth.data);
  82. }
  83. constexpr bool operator<=(const pointer_iterator oth)
  84. {
  85. return before_or_equal(oth);
  86. }
  87. };
  88. template<typename T, int sign = 1>
  89. struct const_pointer_iterator final
  90. {
  91. const T* data;
  92. typedef T value_type;
  93. typedef std::size_t difference_type;
  94. static constexpr iterator_type_t iterator_type = iterator_type_t::contiguous_iterator;
  95. constexpr const_pointer_iterator(const const_pointer_iterator& oth)
  96. : data{oth.data}
  97. {}
  98. constexpr const_pointer_iterator(const T* ptr)
  99. : data{ptr}
  100. {}
  101. constexpr const T& operator*()
  102. {
  103. return *data;
  104. }
  105. constexpr const_pointer_iterator& operator++()
  106. {
  107. data += sign;
  108. return *this;
  109. }
  110. constexpr const_pointer_iterator operator++(int)
  111. {
  112. auto p = data;
  113. data += sign;
  114. return const_pointer_iterator{p};
  115. }
  116. constexpr const_pointer_iterator& operator--()
  117. {
  118. data -= sign;
  119. return *this;
  120. }
  121. constexpr const_pointer_iterator operator--(int)
  122. {
  123. auto p = data;
  124. data -= sign;
  125. return const_pointer_iterator{p};
  126. }
  127. constexpr const_pointer_iterator operator+(const std::size_t offset)
  128. {
  129. return const_pointer_iterator{data+sign*offset};
  130. }
  131. constexpr const_pointer_iterator operator+(const int offset)
  132. {
  133. return const_pointer_iterator{data+sign*offset};
  134. }
  135. constexpr const_pointer_iterator operator-(const std::size_t offset)
  136. {
  137. return const_pointer_iterator{data-sign*offset};
  138. }
  139. constexpr const_pointer_iterator operator-(const int offset)
  140. {
  141. return const_pointer_iterator{data-sign*offset};
  142. }
  143. constexpr difference_type operator-(const const_pointer_iterator& oth) const
  144. {
  145. return ((T*)data-(T*)oth.data)*sign;
  146. }
  147. constexpr bool operator==(const const_pointer_iterator oth)
  148. {
  149. return data==oth.data;
  150. }
  151. constexpr bool operator!=(const_pointer_iterator oth)
  152. {
  153. return data!=oth.data;
  154. }
  155. constexpr bool before_or_equal(const const_pointer_iterator oth)
  156. {
  157. return reinterpret_cast<std::intptr_t>(data) <= reinterpret_cast<std::intptr_t>(oth.data);
  158. }
  159. constexpr bool operator<=(const const_pointer_iterator oth)
  160. {
  161. return before_or_equal(oth);
  162. }
  163. };