2015-10-19 12:06:23 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2015-10-19 12:06:23 +11:00
|
|
|
*
|
2019-08-06 14:31:48 +10:00
|
|
|
* Copyright 2015-2019 Danny Robson <danny@nerdcruft.net>
|
2015-10-19 12:06:23 +11:00
|
|
|
*/
|
|
|
|
|
2016-10-11 23:47:57 +11:00
|
|
|
#include <array>
|
2016-02-25 13:17:14 +11:00
|
|
|
#include <cstddef>
|
2015-10-19 12:06:23 +11:00
|
|
|
#include <iterator>
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::adapter {
|
2015-10-19 12:06:23 +11:00
|
|
|
namespace container {
|
2019-08-06 14:31:48 +10:00
|
|
|
/// An identity proxy for a referenced container.
|
|
|
|
///
|
|
|
|
/// Provided for symmetry with `reverse` and others, so as to allow a
|
|
|
|
/// common template interface.
|
|
|
|
template <typename ContainerT>
|
|
|
|
class identity {
|
|
|
|
public:
|
|
|
|
identity (ContainerT &_target)
|
|
|
|
: m_target (_target)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
decltype(auto) begin (void) & { return m_target.begin (); }
|
|
|
|
decltype(auto) end (void) & { return m_target.end (); }
|
|
|
|
|
|
|
|
decltype(auto) begin (void) const& { return m_target.begin (); }
|
|
|
|
decltype(auto) end (void) const& { return m_target.end (); }
|
|
|
|
|
|
|
|
decltype(auto) cbegin (void) const& { return m_target.cbegin (); }
|
|
|
|
decltype(auto) cend (void) const& { return m_target.cend (); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ContainerT &m_target;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Creates a reversed proxy for a referenced container.
|
|
|
|
///
|
|
|
|
/// The target container must remain an l-value.
|
|
|
|
///
|
|
|
|
/// It is the caller's job to ensure the targeted container remains
|
|
|
|
/// valid for the duration of the proxy's lifetime.
|
2015-10-19 12:06:23 +11:00
|
|
|
template <typename T>
|
2018-12-26 15:10:21 +11:00
|
|
|
class reverse {
|
|
|
|
public:
|
2017-05-23 12:50:51 +10:00
|
|
|
explicit reverse (T &_target):
|
2015-10-19 12:06:23 +11:00
|
|
|
m_target (_target)
|
|
|
|
{ ; }
|
|
|
|
|
2017-08-21 16:50:46 +10:00
|
|
|
auto begin (void) & { return m_target.rbegin (); }
|
|
|
|
auto end (void) & { return m_target.rend (); }
|
2015-10-19 12:06:23 +11:00
|
|
|
|
2017-08-21 16:50:46 +10:00
|
|
|
auto begin (void) const& { return m_target.rbegin (); }
|
|
|
|
auto end (void) const& { return m_target.rend (); }
|
2015-10-19 12:06:23 +11:00
|
|
|
|
2017-08-21 16:50:46 +10:00
|
|
|
auto cbegin (void) const& { return m_target.crbegin (); }
|
|
|
|
auto cend (void) const& { return m_target.crend (); }
|
2015-10-19 12:06:23 +11:00
|
|
|
|
|
|
|
private:
|
|
|
|
T &m_target;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
//---------------------------------------------------------------------
|
2017-08-21 16:51:03 +10:00
|
|
|
template <typename Container>
|
2018-12-26 15:10:21 +11:00
|
|
|
reverse (Container&) -> reverse<Container>;
|
2017-08-21 16:51:03 +10:00
|
|
|
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
/// Adapter's a container to return indices of the container's data
|
|
|
|
/// rather than the values themselves.
|
|
|
|
///
|
|
|
|
/// The targeted container must be an l-value.
|
|
|
|
///
|
|
|
|
/// It is the caller's job to ensure the targeted container remains
|
|
|
|
/// valid for the duration of the proxy's lifetime.
|
2015-10-19 12:06:23 +11:00
|
|
|
template <typename T>
|
2018-12-26 15:10:21 +11:00
|
|
|
class indices {
|
|
|
|
public:
|
2015-10-19 12:06:23 +11:00
|
|
|
using typename T::size_type;
|
|
|
|
|
2017-05-23 12:50:51 +10:00
|
|
|
explicit indices (T &_target):
|
2015-10-19 12:06:23 +11:00
|
|
|
m_target (_target)
|
|
|
|
{ ; }
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
size_type begin (void)& { return 0; }
|
|
|
|
size_type end (void)& { return m_target.size (); }
|
2015-10-19 12:06:23 +11:00
|
|
|
|
|
|
|
private:
|
2018-12-26 15:10:21 +11:00
|
|
|
T &m_target;
|
2015-10-19 12:06:23 +11:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
|
2015-10-19 12:06:23 +11:00
|
|
|
namespace iterator {
|
2018-12-26 15:10:21 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
/// Adapt's an iterator to return the n-th element of the tuple that
|
|
|
|
/// corresponds to the underlying iterator::value_type when
|
|
|
|
/// dereferenced.
|
|
|
|
template <size_t I, typename IteratorT>
|
2015-10-19 12:06:23 +11:00
|
|
|
struct scalar : public std::iterator<
|
2018-12-26 15:10:21 +11:00
|
|
|
typename std::iterator_traits<IteratorT>::iterator_category,
|
2015-10-19 12:06:23 +11:00
|
|
|
typename std::tuple_element<
|
|
|
|
I,
|
|
|
|
typename std::iterator_traits<
|
2018-12-26 15:10:21 +11:00
|
|
|
IteratorT
|
2015-10-19 12:06:23 +11:00
|
|
|
>::value_type
|
|
|
|
>::type,
|
2018-12-26 15:10:21 +11:00
|
|
|
typename std::iterator_traits<IteratorT>::difference_type
|
2015-10-19 12:06:23 +11:00
|
|
|
> {
|
|
|
|
public:
|
2018-12-26 15:10:21 +11:00
|
|
|
using inner_type = typename std::iterator_traits<IteratorT>::value_type;
|
|
|
|
|
2019-01-03 15:48:34 +11:00
|
|
|
using value_type = decltype (std::get<I> (std::declval<inner_type> ()));
|
2018-12-26 15:10:21 +11:00
|
|
|
using reference = value_type&;
|
2019-01-03 15:48:34 +11:00
|
|
|
using const_reference = value_type const&;
|
2015-10-19 12:06:23 +11:00
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
|
|
|
|
explicit scalar (IteratorT _inner):
|
2015-10-19 12:06:23 +11:00
|
|
|
m_inner (_inner)
|
|
|
|
{ ; }
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
|
2019-01-03 15:48:34 +11:00
|
|
|
const_reference operator* (void) const&
|
2015-10-19 12:06:23 +11:00
|
|
|
{ return std::get<I> (*m_inner); }
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
reference operator* (void)&
|
2015-10-19 12:06:23 +11:00
|
|
|
{ return std::get<I> (*m_inner); }
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
bool operator== (scalar<I,IteratorT> rhs) { return m_inner == rhs.m_inner; }
|
|
|
|
bool operator!= (scalar<I,IteratorT> rhs) { return m_inner != rhs.m_inner; }
|
2015-10-19 12:06:23 +11:00
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
|
|
|
|
scalar<I,IteratorT>& operator++ (void)
|
2015-10-19 12:06:23 +11:00
|
|
|
{ ++m_inner; return *this; }
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
|
2015-10-19 12:06:23 +11:00
|
|
|
private:
|
2018-12-26 15:10:21 +11:00
|
|
|
IteratorT m_inner;
|
2015-10-19 12:06:23 +11:00
|
|
|
};
|
|
|
|
}
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|