IntegerRange.h 5.7 KB

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