2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-11-16 02:21:26 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_VECTOR_VIEW_H
|
|
|
|
|
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
template<class T> struct vector_view;
|
2018-03-12 19:24:31 +01:00
|
|
|
|
2020-08-21 02:29:28 +02:00
|
|
|
template<class T> bool empty(const vector_view<T> &) noexcept;
|
|
|
|
template<class T> size_t size(const vector_view<T> &) noexcept;
|
|
|
|
template<class T> T *data(const vector_view<T> &) noexcept;
|
2017-11-16 02:21:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-21 12:01:40 +02:00
|
|
|
/// Template to represent a contiguous vector or array in a generic way.
|
2017-11-16 02:21:26 +01:00
|
|
|
template<class T>
|
|
|
|
struct ircd::vector_view
|
|
|
|
{
|
|
|
|
using value_type = T;
|
|
|
|
using pointer = value_type *;
|
|
|
|
using reference = value_type &;
|
|
|
|
using difference_type = size_t;
|
|
|
|
using iterator = value_type *;
|
|
|
|
using const_iterator = const value_type *;
|
|
|
|
|
2021-03-16 02:36:08 +01:00
|
|
|
pointer _data { nullptr };
|
|
|
|
pointer _stop { nullptr };
|
2017-11-16 02:21:26 +01:00
|
|
|
|
|
|
|
public:
|
2022-07-13 00:43:53 +02:00
|
|
|
constexpr pointer data() const noexcept { return _data; }
|
|
|
|
constexpr size_t size() const noexcept { return std::distance(_data, _stop); }
|
|
|
|
constexpr bool empty() const noexcept { return !size(); }
|
2017-11-16 02:21:26 +01:00
|
|
|
|
2020-08-21 02:29:28 +02:00
|
|
|
const_iterator begin() const noexcept { return data(); }
|
|
|
|
const_iterator end() const noexcept { return _stop; }
|
|
|
|
const_iterator cbegin() noexcept { return data(); }
|
|
|
|
const_iterator cend() noexcept { return _stop; }
|
|
|
|
iterator begin() noexcept { return data(); }
|
|
|
|
iterator end() noexcept { return _stop; }
|
2017-11-16 02:21:26 +01:00
|
|
|
|
2019-08-15 06:55:49 +02:00
|
|
|
// Bounds check in debug only.
|
2021-03-16 02:36:08 +01:00
|
|
|
reference operator[](const size_t &pos) const noexcept
|
2017-11-16 02:21:26 +01:00
|
|
|
{
|
2019-08-15 06:55:49 +02:00
|
|
|
assert(pos < size());
|
2017-11-16 02:21:26 +01:00
|
|
|
return *(data() + pos);
|
|
|
|
}
|
|
|
|
|
2019-08-15 06:55:49 +02:00
|
|
|
// Bounds check at runtime.
|
2021-03-16 02:36:08 +01:00
|
|
|
reference at(const size_t &pos) const
|
2017-11-16 02:21:26 +01:00
|
|
|
{
|
|
|
|
if(unlikely(pos >= size()))
|
2019-08-15 06:55:49 +02:00
|
|
|
throw std::out_of_range
|
|
|
|
{
|
|
|
|
"vector_view::range_check"
|
|
|
|
};
|
2017-11-16 02:21:26 +01:00
|
|
|
|
|
|
|
return operator[](pos);
|
|
|
|
}
|
|
|
|
|
2021-03-16 02:36:08 +01:00
|
|
|
reference back() const
|
2019-06-07 02:02:39 +02:00
|
|
|
{
|
|
|
|
return at(size() - 1);
|
|
|
|
}
|
|
|
|
|
2021-03-16 02:36:08 +01:00
|
|
|
reference front() const
|
2019-06-07 02:02:39 +02:00
|
|
|
{
|
|
|
|
return at(0);
|
|
|
|
}
|
|
|
|
|
2022-07-13 00:43:53 +02:00
|
|
|
constexpr
|
2021-03-16 02:39:03 +01:00
|
|
|
vector_view(const pointer __restrict__ start, const pointer __restrict__ stop)
|
2020-08-21 02:29:28 +02:00
|
|
|
noexcept
|
2017-11-16 02:21:26 +01:00
|
|
|
:_data{start}
|
|
|
|
,_stop{stop}
|
|
|
|
{}
|
|
|
|
|
2022-07-13 00:43:53 +02:00
|
|
|
constexpr
|
2021-03-16 02:39:03 +01:00
|
|
|
vector_view(const pointer __restrict__ start, const size_t size)
|
2020-08-21 02:29:28 +02:00
|
|
|
noexcept
|
2017-11-16 02:21:26 +01:00
|
|
|
:vector_view(start, start + size)
|
|
|
|
{}
|
|
|
|
|
2022-07-13 00:43:53 +02:00
|
|
|
constexpr
|
2021-03-09 03:33:15 +01:00
|
|
|
vector_view(const vector_view<value_type> &start, const size_t &size)
|
|
|
|
noexcept
|
|
|
|
:vector_view(start.data(), std::min(start.size(), size))
|
|
|
|
{}
|
|
|
|
|
2022-07-13 00:43:53 +02:00
|
|
|
constexpr
|
2017-11-16 02:21:26 +01:00
|
|
|
vector_view(const std::initializer_list<value_type> &list)
|
2020-08-21 02:29:28 +02:00
|
|
|
noexcept
|
2017-11-16 02:21:26 +01:00
|
|
|
:vector_view(std::begin(list), std::end(list))
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class U,
|
|
|
|
class A>
|
|
|
|
vector_view(const std::vector<U, A> &v)
|
2020-08-21 02:29:28 +02:00
|
|
|
noexcept
|
2017-11-16 02:21:26 +01:00
|
|
|
:vector_view(v.data(), v.size())
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class U,
|
|
|
|
class A>
|
|
|
|
vector_view(std::vector<U, A> &v)
|
2020-08-21 02:29:28 +02:00
|
|
|
noexcept
|
2017-11-16 02:21:26 +01:00
|
|
|
:vector_view(v.data(), v.size())
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
2022-07-13 00:43:53 +02:00
|
|
|
constexpr
|
2021-03-16 02:39:03 +01:00
|
|
|
vector_view(value_type (&__restrict__ buffer)[SIZE])
|
2020-08-21 02:29:28 +02:00
|
|
|
noexcept
|
2017-11-16 02:21:26 +01:00
|
|
|
:vector_view(buffer, SIZE)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class U,
|
|
|
|
size_t SIZE>
|
2022-07-13 00:43:53 +02:00
|
|
|
constexpr
|
2017-11-16 02:21:26 +01:00
|
|
|
vector_view(const std::array<U, SIZE> &array)
|
2020-08-21 02:29:28 +02:00
|
|
|
noexcept
|
2018-12-27 02:13:08 +01:00
|
|
|
:vector_view(const_cast<pointer>(array.data()), array.size())
|
2017-11-16 02:21:26 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
2022-07-13 00:43:53 +02:00
|
|
|
constexpr
|
2017-11-16 02:21:26 +01:00
|
|
|
vector_view(std::array<value_type, SIZE> &array)
|
2020-08-21 02:29:28 +02:00
|
|
|
noexcept
|
2017-11-16 02:21:26 +01:00
|
|
|
:vector_view(array.data(), array.size())
|
|
|
|
{}
|
|
|
|
|
2020-04-22 05:01:15 +02:00
|
|
|
// Required for reasonable implicit const conversion of value_type.
|
2022-07-13 00:43:53 +02:00
|
|
|
constexpr
|
2020-04-22 05:01:15 +02:00
|
|
|
vector_view(const vector_view<typename std::remove_const<value_type>::type> &v)
|
2020-08-21 02:29:28 +02:00
|
|
|
noexcept
|
2020-04-22 05:01:15 +02:00
|
|
|
:vector_view(v.data(), v.size())
|
|
|
|
{}
|
|
|
|
|
2020-08-21 02:29:28 +02:00
|
|
|
vector_view() noexcept = default;
|
2022-05-04 21:21:27 +02:00
|
|
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-copy"
|
2020-08-21 02:29:28 +02:00
|
|
|
vector_view &operator=(const vector_view &) noexcept = default;
|
2022-05-04 21:21:27 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2017-11-16 02:21:26 +01:00
|
|
|
};
|
2018-03-12 19:24:31 +01:00
|
|
|
|
|
|
|
template<class T>
|
2020-08-21 02:29:28 +02:00
|
|
|
inline T *
|
2018-03-12 19:24:31 +01:00
|
|
|
ircd::data(const vector_view<T> &v)
|
2020-08-21 02:29:28 +02:00
|
|
|
noexcept
|
2018-03-12 19:24:31 +01:00
|
|
|
{
|
|
|
|
return v.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2020-08-21 02:29:28 +02:00
|
|
|
inline size_t
|
2018-03-12 19:24:31 +01:00
|
|
|
ircd::size(const vector_view<T> &v)
|
2020-08-21 02:29:28 +02:00
|
|
|
noexcept
|
2018-03-12 19:24:31 +01:00
|
|
|
{
|
|
|
|
return v.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2020-08-21 02:29:28 +02:00
|
|
|
inline bool
|
2018-03-12 19:24:31 +01:00
|
|
|
ircd::empty(const vector_view<T> &v)
|
2020-08-21 02:29:28 +02:00
|
|
|
noexcept
|
2018-03-12 19:24:31 +01:00
|
|
|
{
|
|
|
|
return v.empty();
|
|
|
|
}
|