osrm-backend/include/util/integer_range.hpp

92 lines
3.2 KiB
C++
Raw Normal View History

2015-01-27 11:44:46 -05:00
#ifndef INTEGER_RANGE_HPP
#define INTEGER_RANGE_HPP
#include <boost/assert.hpp>
#include <boost/iterator/iterator_facade.hpp>
2015-01-27 11:44:46 -05:00
#include <type_traits>
#include <cstdint>
namespace osrm::util
2016-01-05 10:51:13 -05:00
{
2015-01-27 11:44:46 -05:00
// Ported from Boost.Range 1.56 due to required fix
// https://github.com/boostorg/range/commit/9e6bdc13ba94af4e150afae547557a2fbbfe3bf0
// Can be removed after dropping support of Boost < 1.56
template <typename Integer>
class integer_iterator : public boost::iterator_facade<integer_iterator<Integer>,
Integer,
boost::random_access_traversal_tag,
Integer>
{
2022-12-10 10:08:22 -05:00
using base_t = boost::iterator_facade<integer_iterator<Integer>,
Integer,
boost::random_access_traversal_tag,
Integer>;
public:
using value_type = typename base_t::value_type;
using difference_type = typename base_t::difference_type;
using reference = typename base_t::reference;
using iterator_category = std::random_access_iterator_tag;
integer_iterator() : m_value() {}
explicit integer_iterator(value_type x) : m_value(x) {}
private:
void increment() { ++m_value; }
void decrement() { --m_value; }
void advance(difference_type offset) { m_value += offset; }
bool equal(const integer_iterator &other) const { return m_value == other.m_value; }
reference dereference() const { return m_value; }
difference_type distance_to(const integer_iterator &other) const
{
return std::is_signed<value_type>::value ? (other.m_value - m_value)
: (other.m_value >= m_value)
? static_cast<difference_type>(other.m_value - m_value)
: -static_cast<difference_type>(m_value - other.m_value);
}
friend class ::boost::iterator_core_access;
value_type m_value;
};
// Warning: do not try to replace this with Boost's irange, as it is broken on Boost 1.55:
// auto r = boost::irange<unsigned int>(0, 15);
// std::cout << r.size() << std::endl;
// results in -4294967281. Latest Boost versions fix this, but we still support older ones.
2015-01-27 11:44:46 -05:00
template <typename Integer> class range
{
public:
using const_iterator = integer_iterator<Integer>;
using iterator = integer_iterator<Integer>;
2015-01-27 11:44:46 -05:00
range(Integer begin, Integer end) : iter(begin), last(end) {}
iterator begin() const noexcept { return iter; }
iterator end() const noexcept { return last; }
Integer front() const noexcept { return *iter; }
Integer back() const noexcept { return *last - 1; }
2016-01-05 06:04:04 -05:00
std::size_t size() const noexcept { return static_cast<std::size_t>(last - iter); }
2015-01-27 11:44:46 -05:00
private:
iterator iter;
iterator last;
2015-01-27 11:44:46 -05:00
};
// convenience function to construct an integer range with type deduction
template <typename Integer>
range<Integer>
irange(const Integer first,
const Integer last,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr) noexcept
2015-01-27 11:44:46 -05:00
{
return range<Integer>(first, last);
}
2022-12-20 12:00:11 -05:00
} // namespace osrm::util
2015-01-27 11:44:46 -05:00
#endif // INTEGER_RANGE_HPP