General Purpose library for Freestanding C++ and POSIX systems
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

99 行
1.8 KiB

  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>
  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 operator T&()
  23. {
  24. return *data;
  25. }
  26. constexpr T& operator*(){
  27. return *data;
  28. }
  29. constexpr pointer_iterator operator++()
  30. {
  31. return pointer_iterator{++data};
  32. }
  33. constexpr pointer_iterator operator++(int)
  34. {
  35. return pointer_iterator{data++};
  36. }
  37. constexpr pointer_iterator operator--()
  38. {
  39. return pointer_iterator{--data};
  40. }
  41. constexpr pointer_iterator operator--(int)
  42. {
  43. return pointer_iterator{data--};
  44. }
  45. constexpr pointer_iterator operator+(const std::size_t offset)
  46. {
  47. return pointer_iterator{data+offset};
  48. }
  49. constexpr pointer_iterator operator+(const int offset)
  50. {
  51. return pointer_iterator{data+offset};
  52. }
  53. constexpr pointer_iterator operator-(const std::size_t offset)
  54. {
  55. return pointer_iterator{data-offset};
  56. }
  57. constexpr pointer_iterator operator-(const int offset)
  58. {
  59. return pointer_iterator{data-offset};
  60. }
  61. constexpr difference_type operator-(const pointer_iterator& oth) const
  62. {
  63. return (T*)data-(T*)oth.data;
  64. }
  65. constexpr bool operator==(const pointer_iterator& oth)
  66. {
  67. return data==oth.data;
  68. }
  69. constexpr bool operator!=(pointer_iterator& oth)
  70. {
  71. return data!=oth.data;
  72. }
  73. constexpr bool before_or_equal(const pointer_iterator& oth)
  74. {
  75. return reinterpret_cast<std::intptr_t>(data) <= reinterpret_cast<std::intptr_t>(oth.data);
  76. }
  77. constexpr bool operator<=(const pointer_iterator& oth)
  78. {
  79. return before_or_equal(oth);
  80. }
  81. };