libcruft-util/iterator.hpp

778 lines
24 KiB
C++

/*
* 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/.
*
* Copyright 2010-2018 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include "types/traits.hpp"
#include "tuple/value.hpp"
#include "variadic.hpp"
#include "view.hpp"
#include <iterator>
template <typename Base>
class referencing_iterator {
protected:
typedef typename std::enable_if<
is_dereferencable<
typename Base::value_type
>::value,
typename Base::value_type
>::type base_value_type;
public:
typedef typename dereferenced_type<base_value_type>::type value_type ;
typedef typename Base::difference_type difference_type ;
typedef value_type& reference ;
typedef value_type* pointer;
typedef typename Base::iterator_category iterator_category;
protected:
Base m_base;
public:
explicit referencing_iterator (Base _base):
m_base (_base)
{ ; }
referencing_iterator& operator++() { ++m_base; return *this; }
referencing_iterator operator++(int) { auto val = *this; ++m_base; return val; }
bool operator== (const referencing_iterator<Base> &rhs) { return m_base == rhs.m_base; }
bool operator!= (const referencing_iterator<Base> &rhs) { return m_base != rhs.m_base; }
bool operator>= (const referencing_iterator<Base> &rhs) { return m_base >= rhs.m_base; }
bool operator<= (const referencing_iterator<Base> &rhs) { return m_base <= rhs.m_base; }
bool operator> (const referencing_iterator<Base> &rhs) { return m_base > rhs.m_base; }
bool operator< (const referencing_iterator<Base> &rhs) { return m_base < rhs.m_base; }
const value_type& operator*() const
{ return **m_base; }
reference operator*()
{ return **m_base; }
difference_type operator-(const referencing_iterator<Base>& rhs) const { return m_base - rhs.m_base; }
referencing_iterator<Base> operator-(int rhs) const { return referencing_iterator (m_base - rhs); }
referencing_iterator<Base> operator+(int rhs) const { return referencing_iterator (m_base + rhs); }
};
namespace cruft {
///////////////////////////////////////////////////////////////////////////
/// an output iterator that inserts a delimiter between successive
/// assignments
///
/// very useful for outputting comma seperated lists to an ostream, eg:
///
/// std::copy (
/// std::cbegin (container),
/// std::cend (container),
/// cruft::infix_iterator<value_type> (os, ", ")
/// );
template <
typename T,
class CharT = char,
class Traits = std::char_traits<CharT>
>
class infix_iterator : public std::iterator<std::output_iterator_tag, void, void, void, void> {
public:
using char_type = CharT;
using traits_type = Traits;
using ostream_type = std::basic_ostream<char_type, traits_type>;
infix_iterator (ostream_type& _output, const CharT *_delimiter):
m_output (_output),
m_delimiter (_delimiter)
{ ; }
infix_iterator&
operator= (T const &value)
{
if (!m_first)
m_output << m_delimiter;
m_output << value;
m_first = false;
return *this;
}
infix_iterator& operator* (void) { return *this; }
infix_iterator& operator++ (void) { return *this; }
infix_iterator& operator++ (int) { return *this; }
private:
bool m_first = true;
ostream_type &m_output;
const CharT *m_delimiter;
};
namespace detail {
template <typename ContainerT, typename CharT>
struct infix_t {
const ContainerT &_container;
const CharT *_delimiter;
};
template <typename ContainerT, typename CharT>
std::ostream&
operator<< (std::ostream &os, const infix_t<ContainerT,CharT> &val)
{
std::copy (std::cbegin (val._container),
std::cend (val._container),
infix_iterator<typename ContainerT::value_type> (os, val._delimiter));
return os;
}
};
/// a helper function that returns an object that will use a
/// cruft::infix_iterator to output a container's values to an ostream with
/// the given delimiter.
///
/// reduces boilerplate code required to output lists of things
///
/// std::cout << cruft::make_infix (container) << '\n';
template <typename ContainerT, typename CharT = char>
auto
make_infix (const ContainerT &_container, const CharT *_delimiter = ", ")
{
return detail::infix_t<ContainerT,CharT> { _container, _delimiter };
}
template <typename ValueT, size_t CountV>
auto
make_infix (const ValueT (&val)[CountV])
{
return make_infix (cruft::view {val});
}
///////////////////////////////////////////////////////////////////////////
//
template <typename IteratorT>
struct numeric_iterator : public std::iterator<
typename std::iterator_traits<IteratorT>::iterator_category,
decltype (+std::declval<typename std::iterator_traits<IteratorT>::value_type> ()),
typename std::iterator_traits<IteratorT>::difference_type,
typename std::iterator_traits<IteratorT>::pointer,
typename std::iterator_traits<IteratorT>::reference
> {
static_assert (std::is_arithmetic_v<typename std::iterator_traits<numeric_iterator>::value_type>);
explicit numeric_iterator (IteratorT _inner):
m_inner (_inner)
{ ; }
auto operator++ (void) { ++m_inner; return *this; }
auto
operator- (const numeric_iterator &rhs) const
{
return typename std::iterator_traits<IteratorT>::difference_type { m_inner - rhs.m_inner };
}
auto
operator* (void) const
{
return +*m_inner;
}
auto operator== (const numeric_iterator &rhs) const { return m_inner == rhs.m_inner; }
auto operator!= (const numeric_iterator &rhs) const { return m_inner != rhs.m_inner; }
private:
IteratorT m_inner;
};
//-------------------------------------------------------------------------
// convenience function that constructs a view of numeric_iterators for a
// provided container
template <typename ContainerT>
auto
numeric_view (ContainerT &data)
{
return cruft::view {
numeric_iterator (std::begin (data)),
numeric_iterator (std::end (data))
};
}
//-------------------------------------------------------------------------
template <typename ContainerT>
auto
numeric_view (const ContainerT &data)
{
return cruft::view {
numeric_iterator (std::begin (data)),
numeric_iterator (std::end (data))
};
}
///////////////////////////////////////////////////////////////////////////
template <typename ValueT = std::size_t>
class iota {
public:
using value_type = ValueT;
iota (ValueT _total)
: m_total (_total)
{ ; }
struct iterator {
iterator (ValueT _value)
: m_value (_value)
{ ; }
ValueT const* operator-> () const noexcept { return &m_value; }
ValueT const& operator* () const noexcept { return m_value; }
iterator& operator++ () noexcept { ++m_value; return *this; }
bool operator!= (iterator const &rhs) const noexcept { return m_value != rhs.m_value; }
ValueT m_value;
};
iterator begin (void) const { return iterator( 0); }
iterator end (void) const { return iterator(m_total); }
ValueT size (void) const { return m_total; }
private:
ValueT m_total;
};
template <typename ValueT>
iota (ValueT) -> iota<ValueT>;
///////////////////////////////////////////////////////////////////////////
template <typename ContainerT>
class indices {
public:
using value_type = std::size_t;
indices (const ContainerT &_container):
m_container (_container)
{ ; }
class iterator {
public:
using iterator_category = std::forward_iterator_tag;
iterator (value_type _index):
m_index (_index)
{ ; }
bool
operator!= (const iterator &rhs) const
{
return m_index != rhs.m_index;
}
bool
operator== (const iterator &rhs) const
{
return m_index == rhs.m_index;
}
iterator&
operator++ (void) &
{
++m_index;
return *this;
};
const value_type&
operator* (void) const&
{
return m_index;
}
private:
value_type m_index;
};
iterator begin (void) const { return iterator { value_type {0} }; }
iterator end (void) const { return iterator { m_container.size () }; }
constexpr auto size (void) const noexcept
{
return std::size (m_container);
}
private:
const ContainerT &m_container;
};
template <typename ContainerT>
indices (ContainerT const&) -> indices<ContainerT>;
///////////////////////////////////////////////////////////////////////////
namespace detail::zip {
/// A container that holds multiple iterators and returns a tuple of
/// their results when dereferenced.
///
/// \tparam IteratorT A tuple-like object that contains iterators
template <
typename IteratorT,
typename = std::make_index_sequence<std::tuple_size_v<IteratorT>>
>
struct iterator;
template <typename IteratorT, std::size_t ...Indices>
struct iterator<IteratorT, std::index_sequence<Indices...>> {
public:
// We can't declare ourselves as a forward_iterator because we're
// unable to supply references to our value_type when we get
// dereferenced unless we store the supplied values/references
// inside ourself.
//
// This complicates implementation too much given we have no
// pressing need for this functionality.
using iterator_category = std::input_iterator_tag;
using difference_type = std::ptrdiff_t;
iterator (IteratorT _iterators):
m_iterators (_iterators)
{ ; }
iterator&
operator++ (void)
{
(++std::get<Indices> (m_iterators), ...);
return *this;
}
iterator operator++ (int);
auto
operator* (void)
{
// We deliberately construct a tuple manually here to reduce
// the risk of dangling references. `forward_as_tuple` and
// `make_tuple` have resulted in references to locals in the
// past.
return std::tuple<
decltype(*std::get<Indices> (m_iterators))...
> (
*std::get<Indices> (m_iterators)...
);
}
bool
operator== (const iterator &rhs) const
{
return m_iterators == rhs.m_iterators;
}
bool
operator!= (const iterator &rhs) const
{
return !(*this == rhs);
}
private:
IteratorT m_iterators;
};
/// A simple container for multiple collections that returns a
/// wrapped multi-iterator for begin and end.
///
/// It is up to the user to ensure StoreT does not contain dangling
/// references.
///
/// \tparam StoreT A tuple-like object of collections (or references
/// to collections).
template <typename ...StoreT>
class collection {
public:
collection (StoreT&&... _store):
m_store (std::forward<StoreT> (_store)...)
{ ; }
using indices_t = std::make_index_sequence<sizeof...(StoreT)>;
using begin_t = std::tuple<decltype(std::begin (std::declval<StoreT> ()))...>;
using end_t = std::tuple<decltype(std::end (std::declval<StoreT> ()))...>;
auto begin (void)&
{
return iterator<begin_t, indices_t> (
tuple::value::map (
[] (auto &i) noexcept { return std::begin (i); },
m_store
)
);
}
auto end (void)&
{
return iterator<end_t, indices_t> (
tuple::value::map (
[] (auto &i) noexcept { return std::end (i); },
m_store
)
);
}
private:
std::tuple<StoreT...> m_store;
};
}
///------------------------------------------------------------------------
/// Takes a variable number of container arguments and returns an interable
/// object with a value_type that is a tuple of the each container's
/// value_type.
///
/// The returned iterator value_type is suitable for using in range-for
/// and structured bindings (and really, that's the entire point here).
///
/// eg, cruft::zip ({1,2,3}, {4,5,6}) ~= {{1,4},{2,5},{3,6}}
template <typename ...ContainerT>
auto
zip (ContainerT&&... data)
{
CHECK (((std::size (data) == std::size (variadic::get<0> (data...))) && ...));
return detail::zip::collection<ContainerT...> (
std::forward<ContainerT> (data)...
);
}
///------------------------------------------------------------------------
/// Takes a variable number of containers and returns a zipped iterable
/// object where the first of the iterator's value_types is the index of
/// that iterator. ie, It combines container offsets with value_types.
///
/// eg, cruft::izip ("abc") ~= {{0,'a'},{1,'b'},{2,'c'}}
template <typename ...ContainerT>
auto
izip (ContainerT&&... data)
{
indices idx (::cruft::variadic::get<0> (data...));
return zip (
std::move (idx),
std::forward<ContainerT> (data)...
);
}
///////////////////////////////////////////////////////////////////////////
/// an output iterator that always discards any parameters on assignment.
///
/// sometimes useful to pass to algorithms that generate useful results as
/// a return value, while not caring about the implicit OutputIterator
/// results.
struct discard_iterator : public std::iterator<std::output_iterator_tag, discard_iterator> {
template <typename T>
void operator= (const T&) { ; }
discard_iterator& operator++ ( ) { return *this; }
discard_iterator operator++ (int) { return *this; }
discard_iterator& operator* ( ) { return *this; }
};
///////////////////////////////////////////////////////////////////////////
/// an iterator that can be infinitely incremented but never assigned.
///
/// useful for iterator ranges where the begin iterator is an output
/// iterator and hence never reaches an end point (and where we don't want
/// to engineer the client code to account for this).
template <
typename ValueT,
typename CategoryT,
typename DistanceT,
typename PointerT,
typename ReferenceT
>
struct unequal_iterator {
using value_type = ValueT;
using iterator_category = CategoryT;
using difference_type = DistanceT;
using pointer = PointerT;
using reference = ReferenceT;
unequal_iterator& operator++ ( ) { return *this; }
unequal_iterator operator++ (int) { return *this; }
};
//-------------------------------------------------------------------------
template <typename ContainerT>
auto
make_unequal_iterator (const ContainerT&)
{
using t = typename std::iterator_traits<typename ContainerT::iterator>;
return unequal_iterator<
typename t::value_type,
typename t::iterator_category,
typename t::difference_type,
typename t::pointer,
typename t::reference
> {};
};
//-------------------------------------------------------------------------
template <
typename OtherT,
typename ValueT,
typename CategoryT,
typename DistanceT,
typename PointerT,
typename ReferenceT>
constexpr bool
operator== (
const unequal_iterator<ValueT,CategoryT,DistanceT,PointerT,ReferenceT>&,
const OtherT&
) {
return false;
}
//-------------------------------------------------------------------------
template <
typename OtherT,
typename ValueT,
typename CategoryT,
typename DistanceT,
typename PointerT,
typename ReferenceT>
constexpr bool
operator== (
const OtherT&,
const unequal_iterator<ValueT,CategoryT,DistanceT,PointerT,ReferenceT>&
) {
return false;
}
///////////////////////////////////////////////////////////////////////////
template <typename OutputIt, typename FunctionT>
OutputIt
_transform_by_block (
const cruft::view<OutputIt> &,
OutputIt cursor,
FunctionT &&
) {
return cursor;
}
//-------------------------------------------------------------------------
template <typename OutputIt, typename FunctionT, typename InputT, typename ...TailT>
OutputIt
_transform_by_block (
const cruft::view<OutputIt> &dst,
OutputIt cursor,
FunctionT &&func,
const InputT &_src,
TailT &&...tail
) {
auto remain = _src;
if (cursor != dst.begin ()) {
auto infill = std::distance (cursor, dst.end ());
if (remain.size () < static_cast<size_t> (infill)) {
return _transform_by_block (
dst,
std::copy_n (remain.begin (), remain.size (), cursor),
std::forward<FunctionT> (func),
std::forward<TailT> (tail)...
);
}
std::copy_n (remain.begin (), infill, cursor);
func (dst);
cursor = dst.begin ();
remain = { remain.begin () + infill, remain.end () };
}
while (remain.size () >= dst.size ()) {
std::copy_n (remain.begin (), dst.size (), dst.begin ());
func (dst);
remain = { remain.begin () + dst.size (), remain.end () };
}
return _transform_by_block (
dst,
std::copy (remain.begin (), remain.end (), cursor),
std::forward<FunctionT> (func),
std::forward<TailT> (tail)...
);
}
//-------------------------------------------------------------------------
template <typename OutputIt, typename FunctionT, typename ...Args>
OutputIt
transform_by_block (const cruft::view<OutputIt> &dst, FunctionT &&func, Args &&...src)
{
return _transform_by_block (
dst,
dst.begin (),
std::forward<FunctionT> (func),
std::forward<Args> (src)...
);
}
///////////////////////////////////////////////////////////////////////////
/// Counts the number of times the iterator is assigned to.
struct counting_output_iterator {
// An internal proxy value which is returned when the iterator is
// dereferenced. It increments the assignment value in the host
// iterator.
//
// The internals of this object are not a stable interface.
struct assignable {
assignable (std::size_t &_count)
: m_count (_count)
{ ; }
template <typename Arg>
void
operator= (Arg&&)
{
++m_count;
}
private:
std::size_t &m_count;
};
using value_type = assignable;
using iterator_category = std::output_iterator_tag;
using reference = assignable&;
counting_output_iterator& operator++ () { return *this; }
assignable operator* () { return assignable (m_count); }
/// Returns the number of times the iterator has been assigned to.
auto count (void) const { return m_count; }
private:
std::size_t m_count = 0;
};
///////////////////////////////////////////////////////////////////////////
/// Adapts a supplied iterator by dereferencing any operations that deal
/// with the underlying value_type of the supplied iterator.
template <typename IteratorT>
struct dereference_adapter {
//---------------------------------------------------------------------
using difference_type = typename std::iterator_traits<IteratorT>::difference_type;
using iterator_category = typename std::iterator_traits<IteratorT>::iterator_category;
using value_type = std::remove_reference_t<
decltype(
*std::declval<
typename std::iterator_traits<IteratorT>::value_type
> ()
)
>;
using pointer = value_type*;
using reference = value_type&;
//---------------------------------------------------------------------
dereference_adapter (IteratorT _cursor)
: m_cursor (_cursor)
{ ; }
//---------------------------------------------------------------------
// Trivial conditional operations
decltype(auto)
operator== (dereference_adapter const &rhs) const
{
return m_cursor == rhs.m_cursor;
}
decltype(auto)
operator!= (dereference_adapter const &rhs) const
{
return !(*this == rhs);
}
//---------------------------------------------------------------------
// Iterator movement operations
dereference_adapter&
operator++ ()
{
++m_cursor;
return *this;
}
difference_type
operator- (dereference_adapter const &rhs) const
{
return m_cursor - rhs.m_cursor;
}
auto
operator+ (difference_type offset) const
{
return dereference_adapter { m_cursor + offset };
}
//---------------------------------------------------------------------
// value_type operations
decltype(auto)
operator= (value_type const &rhs)
{
return **m_cursor = rhs;
}
decltype(auto) operator* () { return **m_cursor; }
decltype(auto) operator-> () { return **m_cursor; }
decltype(auto) operator* () const { return **m_cursor; }
decltype(auto) operator-> () const { return **m_cursor; }
private:
IteratorT m_cursor;
};
}