Bug 1456906 [wpt PR 10641] - Subtract scrollbar in PerpendicularContainingBlockLogica...
[gecko.git] / mfbt / IntegerRange.h
blobb6e21be7c1313582270f012482c544c7f0f50b70
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/. */
7 /* Iterator over ranges of integers */
9 #ifndef mozilla_IntegerRange_h
10 #define mozilla_IntegerRange_h
12 #include "mozilla/Assertions.h"
13 #include "mozilla/ReverseIterator.h"
14 #include "mozilla/TypeTraits.h"
16 namespace mozilla {
18 namespace detail {
20 template<typename IntTypeT>
21 class IntegerIterator
23 public:
24 template<typename IntType>
25 explicit IntegerIterator(IntType aCurrent)
26 : mCurrent(aCurrent) { }
28 template<typename IntType>
29 explicit IntegerIterator(const IntegerIterator<IntType>& aOther)
30 : mCurrent(aOther.mCurrent) { }
32 IntTypeT operator*() const { return mCurrent; }
34 /* Increment and decrement operators */
36 IntegerIterator& operator++() { ++mCurrent; return *this; }
37 IntegerIterator& operator--() { --mCurrent; return *this; }
38 IntegerIterator operator++(int) { auto ret = *this; ++mCurrent; return ret; }
39 IntegerIterator operator--(int) { auto ret = *this; --mCurrent; return ret; }
41 /* Comparison operators */
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 template<typename IntType1, typename IntType2>
50 friend bool operator<(const IntegerIterator<IntType1>& aIter1,
51 const IntegerIterator<IntType2>& aIter2);
52 template<typename IntType1, typename IntType2>
53 friend bool operator<=(const IntegerIterator<IntType1>& aIter1,
54 const IntegerIterator<IntType2>& aIter2);
55 template<typename IntType1, typename IntType2>
56 friend bool operator>(const IntegerIterator<IntType1>& aIter1,
57 const IntegerIterator<IntType2>& aIter2);
58 template<typename IntType1, typename IntType2>
59 friend bool operator>=(const IntegerIterator<IntType1>& aIter1,
60 const IntegerIterator<IntType2>& aIter2);
62 private:
63 IntTypeT mCurrent;
66 template<typename IntType1, typename IntType2>
67 bool operator==(const IntegerIterator<IntType1>& aIter1,
68 const IntegerIterator<IntType2>& aIter2)
70 return aIter1.mCurrent == aIter2.mCurrent;
73 template<typename IntType1, typename IntType2>
74 bool operator!=(const IntegerIterator<IntType1>& aIter1,
75 const IntegerIterator<IntType2>& aIter2)
77 return aIter1.mCurrent != aIter2.mCurrent;
80 template<typename IntType1, typename IntType2>
81 bool operator<(const IntegerIterator<IntType1>& aIter1,
82 const IntegerIterator<IntType2>& aIter2)
84 return aIter1.mCurrent < aIter2.mCurrent;
87 template<typename IntType1, typename IntType2>
88 bool operator<=(const IntegerIterator<IntType1>& aIter1,
89 const IntegerIterator<IntType2>& aIter2)
91 return aIter1.mCurrent <= aIter2.mCurrent;
94 template<typename IntType1, typename IntType2>
95 bool operator>(const IntegerIterator<IntType1>& aIter1,
96 const IntegerIterator<IntType2>& aIter2)
98 return aIter1.mCurrent > aIter2.mCurrent;
101 template<typename IntType1, typename IntType2>
102 bool operator>=(const IntegerIterator<IntType1>& aIter1,
103 const IntegerIterator<IntType2>& aIter2)
105 return aIter1.mCurrent >= aIter2.mCurrent;
108 template<typename IntTypeT>
109 class IntegerRange
111 public:
112 typedef IntegerIterator<IntTypeT> iterator;
113 typedef IntegerIterator<IntTypeT> const_iterator;
114 typedef ReverseIterator<IntegerIterator<IntTypeT>> reverse_iterator;
115 typedef ReverseIterator<IntegerIterator<IntTypeT>> const_reverse_iterator;
117 template<typename IntType>
118 explicit IntegerRange(IntType aEnd)
119 : mBegin(0), mEnd(aEnd) { }
121 template<typename IntType1, typename IntType2>
122 IntegerRange(IntType1 aBegin, IntType2 aEnd)
123 : mBegin(aBegin), mEnd(aEnd) { }
125 iterator begin() const { return iterator(mBegin); }
126 const_iterator cbegin() const { return begin(); }
127 iterator end() const { return iterator(mEnd); }
128 const_iterator cend() const { return end(); }
129 reverse_iterator rbegin() const { return reverse_iterator(mEnd); }
130 const_reverse_iterator crbegin() const { return rbegin(); }
131 reverse_iterator rend() const { return reverse_iterator(mBegin); }
132 const_reverse_iterator crend() const { return rend(); }
134 private:
135 IntTypeT mBegin;
136 IntTypeT mEnd;
139 template<typename T, bool = IsUnsigned<T>::value>
140 struct GeqZero
142 static bool check(T t) {
143 return t >= 0;
147 template<typename T>
148 struct GeqZero<T, true>
150 static bool check(T t) {
151 return true;
155 } // namespace detail
157 template<typename IntType>
158 detail::IntegerRange<IntType>
159 IntegerRange(IntType aEnd)
161 static_assert(IsIntegral<IntType>::value, "value must be integral");
162 MOZ_ASSERT(detail::GeqZero<IntType>::check(aEnd),
163 "Should never have negative value here");
164 return detail::IntegerRange<IntType>(aEnd);
167 template<typename IntType1, typename IntType2>
168 detail::IntegerRange<IntType2>
169 IntegerRange(IntType1 aBegin, IntType2 aEnd)
171 static_assert(IsIntegral<IntType1>::value && IsIntegral<IntType2>::value,
172 "values must both be integral");
173 static_assert(IsSigned<IntType1>::value == IsSigned<IntType2>::value,
174 "signed/unsigned mismatch");
175 MOZ_ASSERT(aEnd >= aBegin, "End value should be larger than begin value");
176 return detail::IntegerRange<IntType2>(aBegin, aEnd);
179 } // namespace mozilla
181 #endif // mozilla_IntegerRange_h