IntegerRange.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* Iterator over ranges of integers */
  6. #ifndef mozilla_IntegerRange_h
  7. #define mozilla_IntegerRange_h
  8. #include "mozilla/Assertions.h"
  9. #include "mozilla/ReverseIterator.h"
  10. #include "mozilla/TypeTraits.h"
  11. namespace mozilla {
  12. namespace detail {
  13. template<typename IntTypeT>
  14. class IntegerIterator
  15. {
  16. public:
  17. template<typename IntType>
  18. explicit IntegerIterator(IntType aCurrent)
  19. : mCurrent(aCurrent) { }
  20. template<typename IntType>
  21. explicit IntegerIterator(const IntegerIterator<IntType>& aOther)
  22. : mCurrent(aOther.mCurrent) { }
  23. IntTypeT operator*() const { return mCurrent; }
  24. /* Increment and decrement operators */
  25. IntegerIterator& operator++() { ++mCurrent; return *this; }
  26. IntegerIterator& operator--() { --mCurrent; return *this; }
  27. IntegerIterator operator++(int) { auto ret = *this; ++mCurrent; return ret; }
  28. IntegerIterator operator--(int) { auto ret = *this; --mCurrent; return ret; }
  29. /* Comparison operators */
  30. template<typename IntType1, typename IntType2>
  31. friend bool operator==(const IntegerIterator<IntType1>& aIter1,
  32. const IntegerIterator<IntType2>& aIter2);
  33. template<typename IntType1, typename IntType2>
  34. friend bool operator!=(const IntegerIterator<IntType1>& aIter1,
  35. const IntegerIterator<IntType2>& aIter2);
  36. template<typename IntType1, typename IntType2>
  37. friend bool operator<(const IntegerIterator<IntType1>& aIter1,
  38. const IntegerIterator<IntType2>& aIter2);
  39. template<typename IntType1, typename IntType2>
  40. friend bool operator<=(const IntegerIterator<IntType1>& aIter1,
  41. const IntegerIterator<IntType2>& aIter2);
  42. template<typename IntType1, typename IntType2>
  43. friend bool operator>(const IntegerIterator<IntType1>& aIter1,
  44. const IntegerIterator<IntType2>& aIter2);
  45. template<typename IntType1, typename IntType2>
  46. friend bool operator>=(const IntegerIterator<IntType1>& aIter1,
  47. const IntegerIterator<IntType2>& aIter2);
  48. private:
  49. IntTypeT mCurrent;
  50. };
  51. template<typename IntType1, typename IntType2>
  52. bool operator==(const IntegerIterator<IntType1>& aIter1,
  53. const IntegerIterator<IntType2>& aIter2)
  54. {
  55. return aIter1.mCurrent == aIter2.mCurrent;
  56. }
  57. template<typename IntType1, typename IntType2>
  58. bool operator!=(const IntegerIterator<IntType1>& aIter1,
  59. const IntegerIterator<IntType2>& aIter2)
  60. {
  61. return aIter1.mCurrent != aIter2.mCurrent;
  62. }
  63. template<typename IntType1, typename IntType2>
  64. bool operator<(const IntegerIterator<IntType1>& aIter1,
  65. const IntegerIterator<IntType2>& aIter2)
  66. {
  67. return aIter1.mCurrent < aIter2.mCurrent;
  68. }
  69. template<typename IntType1, typename IntType2>
  70. bool operator<=(const IntegerIterator<IntType1>& aIter1,
  71. const IntegerIterator<IntType2>& aIter2)
  72. {
  73. return aIter1.mCurrent <= aIter2.mCurrent;
  74. }
  75. template<typename IntType1, typename IntType2>
  76. bool operator>(const IntegerIterator<IntType1>& aIter1,
  77. const IntegerIterator<IntType2>& aIter2)
  78. {
  79. return aIter1.mCurrent > aIter2.mCurrent;
  80. }
  81. template<typename IntType1, typename IntType2>
  82. bool operator>=(const IntegerIterator<IntType1>& aIter1,
  83. const IntegerIterator<IntType2>& aIter2)
  84. {
  85. return aIter1.mCurrent >= aIter2.mCurrent;
  86. }
  87. template<typename IntTypeT>
  88. class IntegerRange
  89. {
  90. public:
  91. typedef IntegerIterator<IntTypeT> iterator;
  92. typedef IntegerIterator<IntTypeT> const_iterator;
  93. typedef ReverseIterator<IntegerIterator<IntTypeT>> reverse_iterator;
  94. typedef ReverseIterator<IntegerIterator<IntTypeT>> const_reverse_iterator;
  95. template<typename IntType>
  96. explicit IntegerRange(IntType aEnd)
  97. : mBegin(0), mEnd(aEnd) { }
  98. template<typename IntType1, typename IntType2>
  99. IntegerRange(IntType1 aBegin, IntType2 aEnd)
  100. : mBegin(aBegin), mEnd(aEnd) { }
  101. iterator begin() const { return iterator(mBegin); }
  102. const_iterator cbegin() const { return begin(); }
  103. iterator end() const { return iterator(mEnd); }
  104. const_iterator cend() const { return end(); }
  105. reverse_iterator rbegin() const { return reverse_iterator(mEnd); }
  106. const_reverse_iterator crbegin() const { return rbegin(); }
  107. reverse_iterator rend() const { return reverse_iterator(mBegin); }
  108. const_reverse_iterator crend() const { return rend(); }
  109. private:
  110. IntTypeT mBegin;
  111. IntTypeT mEnd;
  112. };
  113. template<typename T, bool = IsUnsigned<T>::value>
  114. struct GeqZero
  115. {
  116. static bool check(T t) {
  117. return t >= 0;
  118. }
  119. };
  120. template<typename T>
  121. struct GeqZero<T, true>
  122. {
  123. static bool check(T t) {
  124. return true;
  125. }
  126. };
  127. } // namespace detail
  128. template<typename IntType>
  129. detail::IntegerRange<IntType>
  130. MakeRange(IntType aEnd)
  131. {
  132. static_assert(IsIntegral<IntType>::value, "value must be integral");
  133. MOZ_ASSERT(detail::GeqZero<IntType>::check(aEnd),
  134. "Should never have negative value here");
  135. return detail::IntegerRange<IntType>(aEnd);
  136. }
  137. template<typename IntType1, typename IntType2>
  138. detail::IntegerRange<IntType2>
  139. MakeRange(IntType1 aBegin, IntType2 aEnd)
  140. {
  141. static_assert(IsIntegral<IntType1>::value && IsIntegral<IntType2>::value,
  142. "values must both be integral");
  143. static_assert(IsSigned<IntType1>::value == IsSigned<IntType2>::value,
  144. "signed/unsigned mismatch");
  145. MOZ_ASSERT(aEnd >= aBegin, "End value should be larger than begin value");
  146. return detail::IntegerRange<IntType2>(aBegin, aEnd);
  147. }
  148. } // namespace mozilla
  149. #endif // mozilla_IntegerRange_h