view: allow multiple iterator types

This commit is contained in:
Danny Robson 2017-12-22 12:34:25 +11:00
parent cfe1185621
commit 8d317fd41e

View File

@ -29,18 +29,18 @@
#include <stdexcept> #include <stdexcept>
namespace util { namespace util {
template <typename IteratorT> template <typename IteratorA, typename IteratorB = IteratorA>
struct view { struct view {
public: public:
//--------------------------------------------------------------------- //---------------------------------------------------------------------
using value_type = typename std::iterator_traits< using value_type = typename std::iterator_traits<
remove_restrict_t<IteratorT> remove_restrict_t<IteratorA>
>::value_type; >::value_type;
//--------------------------------------------------------------------- //---------------------------------------------------------------------
constexpr constexpr
view (const IteratorT &first, const IteratorT &last) noexcept: view (const IteratorA &first, const IteratorB &last) noexcept:
m_begin (first), m_begin (first),
m_end (last) m_end (last)
{ ; } { ; }
@ -114,24 +114,24 @@ namespace util {
m_begin = rhs.m_begin; m_begin = rhs.m_begin;
m_end = rhs.m_end; m_end = rhs.m_end;
rhs.m_begin = IteratorT{}; rhs.m_begin = IteratorA{};
rhs.m_end = IteratorT{}; rhs.m_end = IteratorB{};
return *this; return *this;
}; };
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
constexpr IteratorT begin (void) noexcept { return m_begin; } constexpr IteratorA begin (void) noexcept { return m_begin; }
constexpr IteratorT end (void) noexcept { return m_end; } constexpr IteratorB end (void) noexcept { return m_end; }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
constexpr const IteratorT begin (void) const noexcept { return cbegin (); } constexpr const IteratorA begin (void) const noexcept { return cbegin (); }
constexpr const IteratorT end (void) const noexcept { return cend (); } constexpr const IteratorB end (void) const noexcept { return cend (); }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
constexpr const IteratorT cbegin (void) const noexcept { return m_begin; } constexpr const IteratorA cbegin (void) const noexcept { return m_begin; }
constexpr const IteratorT cend (void) const noexcept { return m_end; } constexpr const IteratorB cend (void) const noexcept { return m_end; }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
auto data (void) { return begin (); } auto data (void) { return begin (); }
@ -181,8 +181,8 @@ namespace util {
private: private:
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
IteratorT m_begin; IteratorA m_begin;
IteratorT m_end; IteratorB m_end;
}; };
@ -230,11 +230,11 @@ namespace util {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
template <typename IteratorT> template <typename IteratorA, typename IteratorB>
auto auto
make_view (IteratorT first, IteratorT last) make_view (IteratorA first, IteratorB last)
{ {
return view<IteratorT> {first, last}; return view<IteratorA, IteratorB> {first, last};
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------