2015-02-04 11:07:32 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* Iterator over ranges of integers */
|
|
|
|
|
|
|
|
#ifndef mozilla_IntegerRange_h
|
|
|
|
#define mozilla_IntegerRange_h
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/ReverseIterator.h"
|
|
|
|
|
2020-03-28 16:57:14 +03:00
|
|
|
#include <type_traits>
|
|
|
|
|
2015-02-04 11:07:32 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename IntTypeT>
|
|
|
|
class IntegerIterator {
|
|
|
|
public:
|
|
|
|
template <typename IntType>
|
|
|
|
explicit IntegerIterator(IntType aCurrent) : mCurrent(aCurrent) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-02-04 11:07:32 +03:00
|
|
|
template <typename IntType>
|
2015-07-28 04:12:06 +03:00
|
|
|
explicit IntegerIterator(const IntegerIterator<IntType>& aOther)
|
2015-02-04 11:07:32 +03:00
|
|
|
: mCurrent(aOther.mCurrent) {}
|
|
|
|
|
2015-07-02 06:31:07 +03:00
|
|
|
IntTypeT operator*() const { return mCurrent; }
|
2015-02-04 11:07:32 +03:00
|
|
|
|
2015-03-23 22:06:00 +03:00
|
|
|
/* Increment and decrement operators */
|
2015-02-04 11:07:32 +03:00
|
|
|
|
|
|
|
IntegerIterator& operator++() {
|
|
|
|
++mCurrent;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
IntegerIterator& operator--() {
|
|
|
|
--mCurrent;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
IntegerIterator operator++(int) {
|
|
|
|
auto ret = *this;
|
|
|
|
++mCurrent;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
IntegerIterator operator--(int) {
|
|
|
|
auto ret = *this;
|
|
|
|
--mCurrent;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Comparison operators */
|
|
|
|
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
friend bool operator==(const IntegerIterator<IntType1>& aIter1,
|
|
|
|
const IntegerIterator<IntType2>& aIter2);
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
friend bool operator!=(const IntegerIterator<IntType1>& aIter1,
|
|
|
|
const IntegerIterator<IntType2>& aIter2);
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
friend bool operator<(const IntegerIterator<IntType1>& aIter1,
|
|
|
|
const IntegerIterator<IntType2>& aIter2);
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
friend bool operator<=(const IntegerIterator<IntType1>& aIter1,
|
|
|
|
const IntegerIterator<IntType2>& aIter2);
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
friend bool operator>(const IntegerIterator<IntType1>& aIter1,
|
|
|
|
const IntegerIterator<IntType2>& aIter2);
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
friend bool operator>=(const IntegerIterator<IntType1>& aIter1,
|
|
|
|
const IntegerIterator<IntType2>& aIter2);
|
|
|
|
|
|
|
|
private:
|
|
|
|
IntTypeT mCurrent;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
bool operator==(const IntegerIterator<IntType1>& aIter1,
|
|
|
|
const IntegerIterator<IntType2>& aIter2) {
|
|
|
|
return aIter1.mCurrent == aIter2.mCurrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
bool operator!=(const IntegerIterator<IntType1>& aIter1,
|
|
|
|
const IntegerIterator<IntType2>& aIter2) {
|
|
|
|
return aIter1.mCurrent != aIter2.mCurrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
bool operator<(const IntegerIterator<IntType1>& aIter1,
|
|
|
|
const IntegerIterator<IntType2>& aIter2) {
|
|
|
|
return aIter1.mCurrent < aIter2.mCurrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
bool operator<=(const IntegerIterator<IntType1>& aIter1,
|
|
|
|
const IntegerIterator<IntType2>& aIter2) {
|
|
|
|
return aIter1.mCurrent <= aIter2.mCurrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
bool operator>(const IntegerIterator<IntType1>& aIter1,
|
|
|
|
const IntegerIterator<IntType2>& aIter2) {
|
|
|
|
return aIter1.mCurrent > aIter2.mCurrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
bool operator>=(const IntegerIterator<IntType1>& aIter1,
|
|
|
|
const IntegerIterator<IntType2>& aIter2) {
|
|
|
|
return aIter1.mCurrent >= aIter2.mCurrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IntTypeT>
|
|
|
|
class IntegerRange {
|
|
|
|
public:
|
|
|
|
typedef IntegerIterator<IntTypeT> iterator;
|
|
|
|
typedef IntegerIterator<IntTypeT> const_iterator;
|
|
|
|
typedef ReverseIterator<IntegerIterator<IntTypeT>> reverse_iterator;
|
|
|
|
typedef ReverseIterator<IntegerIterator<IntTypeT>> const_reverse_iterator;
|
|
|
|
|
|
|
|
template <typename IntType>
|
|
|
|
explicit IntegerRange(IntType aEnd) : mBegin(0), mEnd(aEnd) {}
|
|
|
|
|
|
|
|
template <typename IntType1, typename IntType2>
|
|
|
|
IntegerRange(IntType1 aBegin, IntType2 aEnd) : mBegin(aBegin), mEnd(aEnd) {}
|
|
|
|
|
|
|
|
iterator begin() const { return iterator(mBegin); }
|
|
|
|
const_iterator cbegin() const { return begin(); }
|
|
|
|
iterator end() const { return iterator(mEnd); }
|
|
|
|
const_iterator cend() const { return end(); }
|
|
|
|
reverse_iterator rbegin() const { return reverse_iterator(mEnd); }
|
|
|
|
const_reverse_iterator crbegin() const { return rbegin(); }
|
|
|
|
reverse_iterator rend() const { return reverse_iterator(mBegin); }
|
|
|
|
const_reverse_iterator crend() const { return rend(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
IntTypeT mBegin;
|
|
|
|
IntTypeT mEnd;
|
|
|
|
};
|
|
|
|
|
2020-03-28 16:57:14 +03:00
|
|
|
template <typename T, bool = std::is_unsigned_v<T>>
|
2015-02-04 11:07:32 +03:00
|
|
|
struct GeqZero {
|
2019-05-07 16:29:17 +03:00
|
|
|
static bool isNonNegative(T t) { return t >= 0; }
|
2015-02-04 11:07:32 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct GeqZero<T, true> {
|
2019-05-07 16:29:17 +03:00
|
|
|
static bool isNonNegative(T t) { return true; }
|
2015-02-04 11:07:32 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <typename IntType>
|
|
|
|
detail::IntegerRange<IntType> IntegerRange(IntType aEnd) {
|
2020-03-28 16:57:15 +03:00
|
|
|
static_assert(std::is_integral_v<IntType>, "value must be integral");
|
2019-05-07 16:29:17 +03:00
|
|
|
MOZ_ASSERT(detail::GeqZero<IntType>::isNonNegative(aEnd),
|
2015-02-04 11:07:32 +03:00
|
|
|
"Should never have negative value here");
|
|
|
|
return detail::IntegerRange<IntType>(aEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IntType1, typename IntType2>
|
2017-01-06 17:22:53 +03:00
|
|
|
detail::IntegerRange<IntType2> IntegerRange(IntType1 aBegin, IntType2 aEnd) {
|
2020-03-28 16:57:15 +03:00
|
|
|
static_assert(std::is_integral_v<IntType1> && std::is_integral_v<IntType2>,
|
2015-07-02 06:31:07 +03:00
|
|
|
"values must both be integral");
|
2020-03-28 16:57:15 +03:00
|
|
|
static_assert(std::is_signed_v<IntType1> == std::is_signed_v<IntType2>,
|
2015-02-04 11:07:32 +03:00
|
|
|
"signed/unsigned mismatch");
|
|
|
|
MOZ_ASSERT(aEnd >= aBegin, "End value should be larger than begin value");
|
|
|
|
return detail::IntegerRange<IntType2>(aBegin, aEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_IntegerRange_h
|