osrm-backend/include/util/vector_view.hpp

271 lines
7.8 KiB
C++
Raw Normal View History

#ifndef UTIL_VECTOR_VIEW_HPP
#define UTIL_VECTOR_VIEW_HPP
2013-09-17 08:23:06 -04:00
2017-04-01 21:00:03 -04:00
#include "util/exception.hpp"
2017-04-01 21:25:55 -04:00
#include "util/log.hpp"
2016-05-23 20:13:32 -04:00
#include "storage/shared_memory_ownership.hpp"
2017-01-18 14:40:38 -05:00
#include <boost/assert.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/iterator/reverse_iterator.hpp>
#include <climits>
2016-02-05 10:45:36 -05:00
#include <cstddef>
2013-09-23 12:01:30 -04:00
#include <algorithm>
2013-09-17 08:23:06 -04:00
#include <iterator>
2014-05-07 12:39:16 -04:00
#include <type_traits>
2016-02-05 10:45:36 -05:00
#include <utility>
2016-05-27 15:05:04 -04:00
#include <vector>
2013-09-17 08:23:06 -04:00
namespace osrm::util
2016-01-05 10:51:13 -05:00
{
2017-01-18 14:40:38 -05:00
template <typename DataT>
class VectorViewIterator : public boost::iterator_facade<VectorViewIterator<DataT>,
DataT,
2018-04-06 09:09:52 -04:00
boost::random_access_traversal_tag,
DataT &>
2014-05-07 12:39:16 -04:00
{
2022-12-10 10:08:22 -05:00
using base_t = boost::iterator_facade<VectorViewIterator<DataT>,
DataT,
boost::random_access_traversal_tag,
DataT &>;
2014-05-07 12:39:16 -04:00
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;
2013-09-17 08:23:06 -04:00
explicit VectorViewIterator() : m_value(nullptr) {}
explicit VectorViewIterator(DataT *x) : m_value(x) {}
2017-01-18 14:40:38 -05:00
private:
void increment() { ++m_value; }
void decrement() { --m_value; }
void advance(difference_type offset) { m_value += offset; }
bool equal(const VectorViewIterator &other) const { return m_value == other.m_value; }
2017-01-18 14:40:38 -05:00
reference dereference() const { return *m_value; }
difference_type distance_to(const VectorViewIterator &other) const
{
2017-01-18 14:40:38 -05:00
return other.m_value - m_value;
}
2017-01-18 14:40:38 -05:00
friend class ::boost::iterator_core_access;
DataT *m_value;
};
template <typename DataT> class vector_view
2014-05-07 12:39:16 -04:00
{
private:
DataT *m_ptr;
2013-09-17 08:23:06 -04:00
std::size_t m_size;
2014-05-07 12:39:16 -04:00
public:
using value_type = DataT;
using iterator = VectorViewIterator<DataT>;
using const_iterator = VectorViewIterator<const DataT>;
2017-01-18 14:40:38 -05:00
using reverse_iterator = boost::reverse_iterator<iterator>;
vector_view() : m_ptr(nullptr), m_size(0) {}
2013-09-23 12:01:30 -04:00
vector_view(DataT *ptr, std::size_t size) : m_ptr(ptr), m_size(size) {}
2013-09-17 08:23:06 -04:00
2016-05-01 07:48:56 -04:00
void reset(DataT *ptr, std::size_t size)
{
m_ptr = ptr;
m_size = size;
}
void reset(void *ptr, std::size_t size)
{
m_ptr = reinterpret_cast<DataT *>(ptr);
m_size = size;
}
2014-05-07 12:39:16 -04:00
DataT &at(const std::size_t index) { return m_ptr[index]; }
2013-09-23 12:01:30 -04:00
2014-05-07 12:39:16 -04:00
const DataT &at(const std::size_t index) const { return m_ptr[index]; }
2017-01-18 14:40:38 -05:00
auto begin() const { return iterator(m_ptr); }
2013-09-23 12:01:30 -04:00
2017-01-18 14:40:38 -05:00
auto end() const { return iterator(m_ptr + m_size); }
2013-09-17 08:23:06 -04:00
auto cbegin() const { return const_iterator(m_ptr); }
auto cend() const { return const_iterator(m_ptr + m_size); }
2017-01-18 14:40:38 -05:00
auto rbegin() const { return reverse_iterator(iterator(m_ptr + m_size)); }
2017-01-18 14:40:38 -05:00
auto rend() const { return reverse_iterator(iterator(m_ptr)); }
2013-09-17 08:23:06 -04:00
std::size_t size() const { return m_size; }
2018-03-22 14:26:40 -04:00
void resize(const size_t size)
{
if (size > m_size)
{
throw util::exception("Trying to resize a view to a larger size.");
}
m_size = size;
}
bool empty() const { return 0 == size(); }
2014-05-07 12:39:16 -04:00
DataT &operator[](const unsigned index)
{
2013-09-17 08:23:06 -04:00
BOOST_ASSERT_MSG(index < m_size, "invalid size");
return m_ptr[index];
}
2013-09-23 12:01:30 -04:00
2014-05-07 12:39:16 -04:00
const DataT &operator[](const unsigned index) const
{
2013-09-23 12:01:30 -04:00
BOOST_ASSERT_MSG(index < m_size, "invalid size");
return m_ptr[index];
}
2017-03-07 08:14:10 -05:00
const DataT &front() const
{
BOOST_ASSERT_MSG(m_size > 0, "invalid size");
return m_ptr[0];
}
2017-03-07 08:14:10 -05:00
const DataT &back() const
{
BOOST_ASSERT_MSG(m_size > 0, "invalid size");
return m_ptr[m_size - 1];
}
auto data() const { return m_ptr; }
2017-04-01 21:25:55 -04:00
template <typename T> friend void swap(vector_view<T> &, vector_view<T> &) noexcept;
2013-09-17 08:23:06 -04:00
};
template <> class vector_view<bool>
2014-05-07 12:39:16 -04:00
{
public:
using Word = std::uint64_t;
2014-05-07 12:39:16 -04:00
private:
static constexpr std::size_t WORD_BITS = CHAR_BIT * sizeof(Word);
Word *m_ptr;
std::size_t m_size;
2017-06-15 10:52:14 -04:00
2014-05-07 12:39:16 -04:00
public:
2017-04-01 21:00:03 -04:00
using value_type = bool;
2017-06-15 10:52:14 -04:00
struct reference
{
reference &operator=(bool value)
{
*m_ptr = (*m_ptr & ~mask) | (static_cast<unsigned>(value) * mask);
return *this;
}
operator bool() const { return (*m_ptr) & mask; }
bool operator==(const reference &other) const
{
return other.m_ptr == m_ptr && other.mask == mask;
}
friend std::ostream &operator<<(std::ostream &os, const reference &rhs)
{
return os << static_cast<bool>(rhs);
}
Word *m_ptr;
const Word mask;
2017-06-15 10:52:14 -04:00
};
2017-04-01 21:00:03 -04:00
vector_view() : m_ptr(nullptr), m_size(0) {}
vector_view(Word *ptr, std::size_t size) : m_ptr(ptr), m_size(size) {}
2014-05-07 12:39:16 -04:00
bool at(const std::size_t index) const
{
2017-01-18 14:40:38 -05:00
BOOST_ASSERT_MSG(index < m_size, "invalid size");
const std::size_t bucket = index / WORD_BITS;
// Note: ordering of bits here should match packBits in storage/serialization.hpp
// so that directly mmap-ing data is possible
const auto offset = index % WORD_BITS;
BOOST_ASSERT(WORD_BITS > offset);
return m_ptr[bucket] & (static_cast<Word>(1) << offset);
}
void reset(std::uint64_t *ptr, std::size_t size)
2018-03-22 14:26:40 -04:00
{
m_ptr = ptr;
m_size = size;
}
2018-03-22 14:26:40 -04:00
void resize(const size_t size)
{
if (size > m_size)
{
throw util::exception("Trying to resize a view to a larger size.");
}
m_size = size;
}
2016-05-01 07:48:56 -04:00
std::size_t size() const { return m_size; }
bool empty() const { return 0 == size(); }
bool operator[](const std::size_t index) const { return at(index); }
reference operator[](const std::size_t index)
2017-06-15 10:52:14 -04:00
{
BOOST_ASSERT(index < m_size);
const auto bucket = index / WORD_BITS;
// Note: ordering of bits here should match packBits in storage/serialization.hpp
// so that directly mmap-ing data is possible
const auto offset = index % WORD_BITS;
BOOST_ASSERT(WORD_BITS > offset);
return reference{m_ptr + bucket, static_cast<Word>(1) << offset};
2017-06-15 10:52:14 -04:00
}
2017-04-03 04:28:46 -04:00
template <typename T> friend void swap(vector_view<T> &, vector_view<T> &) noexcept;
friend std::ostream &operator<<(std::ostream &os, const vector_view<bool> &rhs)
{
for (std::size_t i = 0; i < rhs.size(); ++i)
{
os << (i > 0 ? " " : "") << rhs.at(i);
}
return os;
}
};
// Both vector_view<T> and the vector_view<bool> specializations share this impl.
2017-04-03 04:28:46 -04:00
template <typename DataT> void swap(vector_view<DataT> &lhs, vector_view<DataT> &rhs) noexcept
{
std::swap(lhs.m_ptr, rhs.m_ptr);
std::swap(lhs.m_size, rhs.m_size);
}
template <typename DataT, storage::Ownership Ownership>
2017-04-10 04:35:52 -04:00
using InternalOrExternalVector =
typename std::conditional<Ownership == storage::Ownership::External,
2020-06-17 06:16:21 -04:00
std::vector<DataT>,
2017-04-10 04:35:52 -04:00
std::vector<DataT>>::type;
template <typename DataT, storage::Ownership Ownership>
using ViewOrVector = typename std::conditional<Ownership == storage::Ownership::View,
vector_view<DataT>,
InternalOrExternalVector<DataT, Ownership>>::type;
2017-06-15 10:52:14 -04:00
// We can use this for compile time assertions
template <typename ValueT, typename VectorT>
struct is_view_or_vector
: std::integral_constant<bool,
std::is_same<std::vector<ValueT>, VectorT>::value ||
std::is_same<util::vector_view<ValueT>, VectorT>::value>
{
};
} // namespace osrm
2016-01-05 10:51:13 -05:00
#endif // SHARED_MEMORY_VECTOR_WRAPPER_HPP