index_based_iterator.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "base/assertion.h"
  9. namespace base {
  10. template <typename Container>
  11. class index_based_iterator {
  12. public:
  13. using iterator_category = std::random_access_iterator_tag;
  14. using value_type = typename Container::value_type;
  15. using difference_type = typename Container::difference_type;
  16. using pointer = std::conditional_t<
  17. std::is_const_v<Container>,
  18. typename Container::const_pointer,
  19. typename Container::pointer>;
  20. using reference = std::conditional_t<
  21. std::is_const_v<Container>,
  22. typename Container::const_reference,
  23. typename Container::reference>;
  24. using base_type = std::conditional_t<
  25. std::is_const_v<Container>,
  26. typename Container::const_iterator,
  27. typename Container::iterator>;
  28. index_based_iterator(Container *container, base_type impl)
  29. : _container(container)
  30. , _index(impl - _container->begin()) {
  31. }
  32. reference operator*() const {
  33. return *(_container->begin() + _index);
  34. }
  35. pointer operator->() const {
  36. return std::addressof(**this);
  37. }
  38. index_based_iterator &operator++() {
  39. ++_index;
  40. return *this;
  41. }
  42. index_based_iterator operator++(int) {
  43. auto copy = *this;
  44. ++*this;
  45. return copy;
  46. }
  47. index_based_iterator &operator--() {
  48. --_index;
  49. return *this;
  50. }
  51. index_based_iterator operator--(int) {
  52. auto copy = *this;
  53. --*this;
  54. return copy;
  55. }
  56. index_based_iterator &operator+=(difference_type offset) {
  57. _index += offset;
  58. return *this;
  59. }
  60. index_based_iterator operator+(difference_type offset) const {
  61. auto copy = *this;
  62. copy += offset;
  63. return copy;
  64. }
  65. index_based_iterator &operator-=(difference_type offset) {
  66. _index -= offset;
  67. return *this;
  68. }
  69. index_based_iterator operator-(difference_type offset) const {
  70. auto copy = *this;
  71. copy -= offset;
  72. return copy;
  73. }
  74. difference_type operator-(const index_based_iterator &other) const {
  75. return _index - other._index;
  76. }
  77. reference operator[](difference_type offset) const {
  78. return *(*this + offset);
  79. }
  80. bool operator==(const index_based_iterator &other) const {
  81. Expects(_container == other._container);
  82. return _index == other._index;
  83. }
  84. bool operator!=(const index_based_iterator &other) const {
  85. Expects(_container == other._container);
  86. return _index != other._index;
  87. }
  88. bool operator<(const index_based_iterator &other) const {
  89. Expects(_container == other._container);
  90. return _index < other._index;
  91. }
  92. bool operator>(const index_based_iterator &other) const {
  93. return other < *this;
  94. }
  95. bool operator<=(const index_based_iterator &other) const {
  96. return !(other < *this);
  97. }
  98. bool operator>=(const index_based_iterator &other) const {
  99. return !(*this < other);
  100. }
  101. base_type base() const {
  102. return _container->begin() + _index;
  103. }
  104. private:
  105. Container *_container = nullptr;
  106. difference_type _index = 0;
  107. };
  108. template <typename Container>
  109. index_based_iterator<Container> index_based_begin(Container &container) {
  110. return { &container, std::begin(container) };
  111. }
  112. template <typename Container>
  113. index_based_iterator<Container> index_based_end(Container &container) {
  114. return { &container, std::end(container) };
  115. }
  116. } // namespace base