#ifndef INTEGER_RANGE_HPP #define INTEGER_RANGE_HPP #include #include #include namespace osrm { namespace util { // Warning: do not try to replace this with Boost's irange, as it is broken on Boost 1.55: // auto r = boost::irange(0, 15); // std::cout << r.size() << std::endl; // results in -4294967281. Latest Boost versions fix this, but we still support older ones. template class range { private: const Integer last; Integer iter; public: range(Integer start, Integer end) noexcept : last(end), iter(start) { BOOST_ASSERT_MSG(start <= end, "backwards counting ranges not suppoted"); static_assert(std::is_integral::value, "range type must be integral"); } // Iterable functions const range &begin() const noexcept { return *this; } const range &end() const noexcept { return *this; } Integer front() const noexcept { return iter; } Integer back() const noexcept { return last - 1; } std::size_t size() const noexcept { return static_cast(last - iter); } // Iterator functions bool operator!=(const range &) const noexcept { return iter < last; } void operator++() noexcept { ++iter; } Integer operator*() const noexcept { return iter; } }; // convenience function to construct an integer range with type deduction template range irange(const Integer first, const Integer last, typename std::enable_if::value>::type * = 0) noexcept { return range(first, last); } } } #endif // INTEGER_RANGE_HPP