2015-01-06 14:40:56 +01:00
|
|
|
#ifndef array_view_hh_INCLUDED
|
|
|
|
#define array_view_hh_INCLUDED
|
2012-02-03 20:13:02 +01:00
|
|
|
|
|
|
|
#include <initializer_list>
|
2015-06-24 14:40:18 +02:00
|
|
|
#include <iterator>
|
2012-02-03 20:13:02 +01:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-01-06 14:40:56 +01:00
|
|
|
// An ArrayView provides a typed, non owning view of a memory
|
2012-02-03 20:13:02 +01:00
|
|
|
// range with an interface similar to std::vector.
|
|
|
|
template<typename T>
|
2015-01-06 14:40:56 +01:00
|
|
|
class ArrayView
|
2012-02-03 20:13:02 +01:00
|
|
|
{
|
|
|
|
public:
|
2013-04-09 20:05:40 +02:00
|
|
|
using size_t = std::size_t;
|
|
|
|
|
2015-03-09 14:21:43 +01:00
|
|
|
constexpr ArrayView()
|
2012-02-03 20:13:02 +01:00
|
|
|
: m_pointer(nullptr), m_size(0) {}
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
constexpr ArrayView(T& oneval)
|
2012-02-03 20:13:02 +01:00
|
|
|
: m_pointer(&oneval), m_size(1) {}
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
constexpr ArrayView(T* pointer, size_t size)
|
2012-02-03 20:13:02 +01:00
|
|
|
: m_pointer(pointer), m_size(size) {}
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
constexpr ArrayView(T* begin, T* end)
|
2012-02-03 20:13:02 +01:00
|
|
|
: m_pointer(begin), m_size(end - begin) {}
|
|
|
|
|
2015-02-06 23:32:25 +01:00
|
|
|
template<size_t N>
|
2015-03-09 14:48:41 +01:00
|
|
|
constexpr ArrayView(T(&array)[N]) : m_pointer(array), m_size(N) {}
|
2015-02-06 23:32:25 +01:00
|
|
|
|
2019-01-24 11:02:07 +01:00
|
|
|
template<typename Container,
|
|
|
|
typename = std::enable_if_t<sizeof(decltype(*std::declval<Container>().data())) == sizeof(T)>>
|
|
|
|
constexpr ArrayView(const Container& c)
|
|
|
|
: m_pointer(c.data()), m_size(c.size()) {}
|
2012-02-03 20:13:02 +01:00
|
|
|
|
2015-03-09 14:21:43 +01:00
|
|
|
constexpr ArrayView(const std::initializer_list<T>& v)
|
2012-02-03 20:13:02 +01:00
|
|
|
: m_pointer(v.begin()), m_size(v.size()) {}
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
constexpr T* pointer() const { return m_pointer; }
|
2015-03-09 14:21:43 +01:00
|
|
|
constexpr size_t size() const { return m_size; }
|
2015-05-14 20:05:41 +02:00
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2015-03-09 14:48:41 +01:00
|
|
|
constexpr T& operator[](size_t n) const { return *(m_pointer + n); }
|
2012-02-03 20:13:02 +01:00
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
constexpr T* begin() const { return m_pointer; }
|
|
|
|
constexpr T* end() const { return m_pointer+m_size; }
|
2012-02-03 20:13:02 +01:00
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
using reverse_iterator = std::reverse_iterator<T*>;
|
2015-03-09 14:21:43 +01:00
|
|
|
constexpr reverse_iterator rbegin() const { return reverse_iterator(m_pointer+m_size); }
|
|
|
|
constexpr reverse_iterator rend() const { return reverse_iterator(m_pointer); }
|
2014-08-14 21:35:03 +02:00
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
constexpr T& front() const { return *m_pointer; }
|
|
|
|
constexpr T& back() const { return *(m_pointer + m_size - 1); }
|
2012-02-03 20:13:02 +01:00
|
|
|
|
2015-03-09 14:21:43 +01:00
|
|
|
constexpr bool empty() const { return m_size == 0; }
|
2012-02-03 20:13:02 +01:00
|
|
|
|
2016-11-20 12:15:15 +01:00
|
|
|
constexpr ArrayView subrange(size_t first, size_t count = -1) const
|
2012-02-03 20:13:02 +01:00
|
|
|
{
|
2016-11-20 12:15:15 +01:00
|
|
|
return ArrayView(m_pointer + std::min(first, m_size),
|
|
|
|
std::min(count, m_size - std::min(first, m_size)));
|
2012-02-03 20:13:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-03-09 14:48:41 +01:00
|
|
|
T* m_pointer;
|
2015-04-13 00:48:11 +02:00
|
|
|
size_t m_size;
|
2012-02-03 20:13:02 +01:00
|
|
|
};
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
template<typename T>
|
|
|
|
using ConstArrayView = ArrayView<const T>;
|
|
|
|
|
2016-09-19 10:10:41 +02:00
|
|
|
template<typename T>
|
|
|
|
bool operator==(ArrayView<T> lhs, ArrayView<T> rhs)
|
|
|
|
{
|
|
|
|
if (lhs.size() != rhs.size())
|
|
|
|
return false;
|
|
|
|
for (int i = 0; i < lhs.size(); ++i)
|
|
|
|
{
|
|
|
|
if (lhs[i] != rhs[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool operator!=(ArrayView<T> lhs, ArrayView<T> rhs)
|
|
|
|
{
|
|
|
|
return not (lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2012-02-03 20:13:02 +01:00
|
|
|
}
|
|
|
|
|
2015-01-06 14:40:56 +01:00
|
|
|
#endif // array_view_hh_INCLUDED
|