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.

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