iterator: move each iterator to their own unit

This commit is contained in:
Danny Robson 2019-03-18 16:18:27 +11:00
parent 020d3c0aaf
commit 340d873d3a
29 changed files with 1029 additions and 924 deletions

View File

@ -355,7 +355,18 @@ list (
introspection.hpp
io.cpp
io.hpp
iterator.hpp
iterator/constant.hpp
iterator/counting.hpp
iterator/dereference.hpp
iterator/discard.hpp
iterator/indices.hpp
iterator/infix.hpp
iterator/iota.hpp
iterator/numeric.hpp
iterator/referencing.hpp
iterator/transform.hpp
iterator/unequal.hpp
iterator/zip.hpp
job/fwd.hpp
job/dispatch.hpp
job/queue.cpp

View File

@ -8,7 +8,7 @@
#pragma once
#include "../iterator.hpp"
#include "../iterator/zip.hpp"
#include <iterator>
@ -49,7 +49,7 @@ namespace cruft {
{
CHECK_LE (init.size (), CapacityV);
for (auto &&[idx, src]: cruft::izip (init))
for (auto &&[idx, src]: cruft::iterator::izip (init))
m_data.objects[idx] = std::move (src);
}

View File

@ -8,7 +8,7 @@
#pragma once
#include "../iterator.hpp"
#include "../view.hpp"
#include <cstdlib>
#include <stdexcept>

View File

@ -172,7 +172,7 @@ cruft::operator<< (std::ostream &os, const bezier<N> &b)
os << "[";
std::transform (std::cbegin (b),
std::cend (b),
infix_iterator<value_type> (os, ", "),
iterator::infix_iterator<value_type> (os, ", "),
[] (auto i) { return +i; });
os << "]";

View File

@ -11,7 +11,7 @@
#define CRUFT_UTIL_CMDLINE_HPP
#include "introspection.hpp"
#include "iterator.hpp"
#include "iterator/infix.hpp"
#include <exception>
#include <functional>
@ -151,7 +151,7 @@ namespace cruft::cmdopt {
std::ostringstream os;
std::copy (std::cbegin (enum_traits<T>::names),
std::cend (enum_traits<T>::names),
infix_iterator<const char*> (os, "|"));
iterator::infix_iterator<const char*> (os, "|"));
return os.str ();
} ();
return EXAMPLE;

View File

@ -3,14 +3,13 @@
* 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 2016-2017 Danny Robson <danny@nerdcruft.net>
* Copyright 2016-2019 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_IOSTREAM
#define CRUFT_UTIL_IOSTREAM
#pragma once
#include "./traits.hpp"
#include "../iterator.hpp"
#include "../iterator/infix.hpp"
#include <cstddef>
#include <ostream>
@ -27,12 +26,10 @@ namespace cruft {
os << "[";
std::transform (std::cbegin (k),
std::cend (k),
infix_iterator<typename K::value_type> (os, ", "),
iterator::infix_iterator<typename K::value_type> (os, ", "),
[] (auto i) { return +i; });
os << "]";
return os;
}
}
#endif

View File

@ -14,6 +14,7 @@
#include "sphere.hpp"
#include "../coord/iostream.hpp"
#include "../iterator/infix.hpp"
///////////////////////////////////////////////////////////////////////////////
@ -62,7 +63,7 @@ template <typename T>
std::ostream&
cruft::geom::operator<< (std::ostream &os, frustum<T> val)
{
return os << "[ " << cruft::make_infix (val.planes) << " ]";
return os << "[ " << iterator::make_infix (val.planes) << " ]";
}

View File

@ -1,886 +0,0 @@
/*
* 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 "functor.hpp"
#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))
};
}
///////////////////////////////////////////////////////////////////////////
/// A numeric range that supplies a sequence of values.
///
/// The value range is specified as [closed-open). ie, The last value will
/// never be output by an iterator. This makes the object suitable as a
/// means of iterating over the indices of an integrable indexed container
/// (like std::vector)
///
/// Primarily used for integrating into other utilities (like izip).
template <typename ValueT = std::size_t>
class iota {
public:
using value_type = ValueT;
iota (value_type _total)
: m_total (_total)
{ ; }
class iterator {
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = ValueT const;
using difference_type = decltype (std::declval<value_type> () - std::declval<value_type> ());
using pointer = value_type*;
using reference = value_type&;
explicit iterator ()
: m_value {}
{ ; }
explicit iterator (value_type _value)
: m_value (_value)
{ ; }
pointer operator-> () const& noexcept { return &m_value; }
reference operator* () const& noexcept { return m_value; }
iterator& operator++ () noexcept { ++m_value; return *this; }
iterator& operator-- () noexcept { --m_value; return *this; }
constexpr difference_type
operator- (iterator const &rhs) const noexcept
{
return m_value - rhs.m_value;
}
constexpr bool operator!= (iterator const &rhs) const noexcept { return m_value != rhs.m_value; }
constexpr bool operator== (iterator const &rhs) const noexcept { return m_value == rhs.m_value; }
private:
ValueT m_value;
};
iterator begin (void) const { return iterator {}; }
iterator end (void) const { return iterator {m_total}; }
value_type size (void) const { return m_total; }
private:
value_type 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 zipped_iterator;
template <typename IteratorT, std::size_t ...Indices>
struct zipped_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;
using value_type = std::tuple<
decltype(
*std::get<Indices> (
std::declval<IteratorT> ()
)
)...
>;
using reference = value_type;
using pointer = value_type*;
zipped_iterator (IteratorT _iterators):
m_iterators (_iterators)
{ ; }
zipped_iterator&
operator++ (void)
{
(++std::get<Indices> (m_iterators), ...);
return *this;
}
zipped_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 zipped_iterator &rhs) const
{
return m_iterators == rhs.m_iterators;
}
bool
operator!= (const zipped_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 inner_t = std::tuple<decltype(std::begin (std::declval<StoreT> ()))...>;
using indices_t = std::make_index_sequence<sizeof...(StoreT)>;
using iterator = zipped_iterator<inner_t, indices_t>;
iterator begin (void)&
{
return iterator (
tuple::value::map (::cruft::functor::begin {}, m_store)
);
}
iterator begin (void) const&
{
return iterator (
tuple::value::map (::cruft::functor::begin {}, m_store)
);
}
iterator end (void)&
{
return iterator (
tuple::value::map (cruft::functor::end {}, m_store)
);
}
iterator end (void) const &
{
return iterator (
tuple::value::map (cruft::functor::end {}, m_store)
);
}
/// Returns the number of elements in the sequence.
decltype (auto)
size (void) const
{
// All stores should have the same size so we arbitrarily pick
// the first to query.
return std::get<0> (m_store).size ();
}
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>
decltype(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.
///
/// The supplied containers _must_ not change size while the izip object
/// is live. The size of the containers may be cached for efficiency.
///
/// eg, cruft::izip ("abc") ~= {{0,'a'},{1,'b'},{2,'c'}}
template <typename ...ContainerT>
decltype(auto)
izip (ContainerT&&... data)
{
// Assume that all containers are the same size and create a range
// that will supply integral indices over the first container.
//
// The call to `zip` will peform more rigorous tests on the sizes
// of the container collection.
return zip (
iota (::cruft::variadic::get<0> (data...).size ()),
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;
};
///////////////////////////////////////////////////////////////////////////
namespace iterator {
template <typename ValueT>
class constant {
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = ValueT;
using difference_type = std::size_t;
using pointer = value_type*;
using reference = value_type&;
constant () = delete;
constant (ValueT &&_value) noexcept
: m_value (std::move (_value))
{ ; }
constant (ValueT const &_value)
: m_value (_value)
{ ; }
constant (constant const &) = default;
constant& operator= (constant const &) = default;
constant (constant &&) = default;
constant& operator= (constant &&) = default;
ValueT const& operator* () const& { return m_value; }
ValueT & operator* () & { return m_value; }
ValueT const* operator-> () const & { return &m_value; }
ValueT * operator-> () & { return &m_value; }
constant& operator++ ( ) { return *this; }
constant& operator++ (int) { return *this; }
constant& operator-- ( ) { return *this; }
constant& operator-- (int) { return *this; }
private:
ValueT m_value;
};
}
};

53
iterator/constant.hpp Normal file
View File

@ -0,0 +1,53 @@
/*
* 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
///////////////////////////////////////////////////////////////////////////
namespace cruft::iterator {
template <typename ValueT>
class constant {
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = ValueT;
using difference_type = std::size_t;
using pointer = value_type*;
using reference = value_type&;
constant () = delete;
constant (ValueT &&_value) noexcept
: m_value (std::move (_value))
{ ; }
constant (ValueT const &_value)
: m_value (_value)
{ ; }
constant (constant const &) = default;
constant& operator= (constant const &) = default;
constant (constant &&) = default;
constant& operator= (constant &&) = default;
ValueT const& operator* () const& { return m_value; }
ValueT & operator* () & { return m_value; }
ValueT const* operator-> () const & { return &m_value; }
ValueT * operator-> () & { return &m_value; }
constant& operator++ ( ) { return *this; }
constant& operator++ (int) { return *this; }
constant& operator-- ( ) { return *this; }
constant& operator-- (int) { return *this; }
private:
ValueT m_value;
};
}

49
iterator/counting.hpp Normal file
View File

@ -0,0 +1,49 @@
/*
* 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
namespace cruft::iterator {
///////////////////////////////////////////////////////////////////////////
/// 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;
};
}

97
iterator/dereference.hpp Normal file
View File

@ -0,0 +1,97 @@
/*
* 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
namespace cruft::iterator {
///////////////////////////////////////////////////////////////////////////
/// 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;
};
}

26
iterator/discard.hpp Normal file
View File

@ -0,0 +1,26 @@
/*
* 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
namespace cruft::iterator {
///////////////////////////////////////////////////////////////////////////
/// 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; }
};
}

74
iterator/indices.hpp Normal file
View File

@ -0,0 +1,74 @@
/*
* 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
namespace cruft::iterator {
///////////////////////////////////////////////////////////////////////////
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>;
}

107
iterator/infix.hpp Normal file
View File

@ -0,0 +1,107 @@
/*
* 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 "../view.hpp"
namespace cruft::iterator {
///////////////////////////////////////////////////////////////////////////
/// 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});
}
}

76
iterator/iota.hpp Normal file
View File

@ -0,0 +1,76 @@
/*
* 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
namespace cruft::iterator {
///////////////////////////////////////////////////////////////////////////
/// A numeric range that supplies a sequence of values.
///
/// The value range is specified as [closed-open). ie, The last value will
/// never be output by an iterator. This makes the object suitable as a
/// means of iterating over the indices of an integrable indexed container
/// (like std::vector)
///
/// Primarily used for integrating into other utilities (like izip).
template <typename ValueT = std::size_t>
class iota {
public:
using value_type = ValueT;
iota (value_type _total)
: m_total (_total)
{ ; }
class iterator {
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = ValueT const;
using difference_type = decltype (std::declval<value_type> () - std::declval<value_type> ());
using pointer = value_type*;
using reference = value_type&;
explicit iterator ()
: m_value {}
{ ; }
explicit iterator (value_type _value)
: m_value (_value)
{ ; }
pointer operator-> () const& noexcept { return &m_value; }
reference operator* () const& noexcept { return m_value; }
iterator& operator++ () noexcept { ++m_value; return *this; }
iterator& operator-- () noexcept { --m_value; return *this; }
constexpr difference_type
operator- (iterator const &rhs) const noexcept
{
return m_value - rhs.m_value;
}
constexpr bool operator!= (iterator const &rhs) const noexcept { return m_value != rhs.m_value; }
constexpr bool operator== (iterator const &rhs) const noexcept { return m_value == rhs.m_value; }
private:
ValueT m_value;
};
iterator begin (void) const { return iterator {}; }
iterator end (void) const { return iterator {m_total}; }
value_type size (void) const { return m_total; }
private:
value_type m_total;
};
template <typename ValueT>
iota (ValueT) -> iota<ValueT>;
}

64
iterator/numeric.hpp Normal file
View File

@ -0,0 +1,64 @@
namespace cruft::iterator {
///////////////////////////////////////////////////////////////////////////
//
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))
};
}
}

57
iterator/referencing.hpp Normal file
View File

@ -0,0 +1,57 @@
/*
* 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
namespace cruft::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); }
};
}

79
iterator/transform.hpp Normal file
View File

@ -0,0 +1,79 @@
/*
* 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
namespace cruft::iterator {
///////////////////////////////////////////////////////////////////////////
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)...
);
}
};

88
iterator/unequal.hpp Normal file
View File

@ -0,0 +1,88 @@
/*
* 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
namespace cruft::iterator {
///////////////////////////////////////////////////////////////////////////
/// 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;
}
}

215
iterator/zip.hpp Normal file
View File

@ -0,0 +1,215 @@
/*
* 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 "iota.hpp"
#include "../tuple/value.hpp"
#include "../variadic.hpp"
#include "../functor.hpp"
namespace cruft::iterator {
///////////////////////////////////////////////////////////////////////////
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 zipped_iterator;
template <typename IteratorT, std::size_t ...Indices>
struct zipped_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;
using value_type = std::tuple<
decltype(
*std::get<Indices> (
std::declval<IteratorT> ()
)
)...
>;
using reference = value_type;
using pointer = value_type*;
zipped_iterator (IteratorT _iterators):
m_iterators (_iterators)
{ ; }
zipped_iterator&
operator++ (void)
{
(++std::get<Indices> (m_iterators), ...);
return *this;
}
zipped_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 zipped_iterator &rhs) const
{
return m_iterators == rhs.m_iterators;
}
bool
operator!= (const zipped_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 inner_t = std::tuple<decltype(std::begin (std::declval<StoreT> ()))...>;
using indices_t = std::make_index_sequence<sizeof...(StoreT)>;
using iterator = zipped_iterator<inner_t, indices_t>;
iterator begin (void)&
{
return iterator (
tuple::value::map (::cruft::functor::begin {}, m_store)
);
}
iterator begin (void) const&
{
return iterator (
tuple::value::map (::cruft::functor::begin {}, m_store)
);
}
iterator end (void)&
{
return iterator (
tuple::value::map (cruft::functor::end {}, m_store)
);
}
iterator end (void) const &
{
return iterator (
tuple::value::map (cruft::functor::end {}, m_store)
);
}
/// Returns the number of elements in the sequence.
decltype (auto)
size (void) const
{
// All stores should have the same size so we arbitrarily pick
// the first to query.
return std::get<0> (m_store).size ();
}
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>
decltype(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.
///
/// The supplied containers _must_ not change size while the izip object
/// is live. The size of the containers may be cached for efficiency.
///
/// eg, cruft::izip ("abc") ~= {{0,'a'},{1,'b'},{2,'c'}}
template <typename ...ContainerT>
decltype(auto)
izip (ContainerT&&... data)
{
// Assume that all containers are the same size and create a range
// that will supply integral indices over the first container.
//
// The call to `zip` will peform more rigorous tests on the sizes
// of the container collection.
return zip (
iota (::cruft::variadic::get<0> (data...).size ()),
std::forward<ContainerT> (data)...
);
}
}

View File

@ -9,7 +9,7 @@
#pragma once
#include "debug.hpp"
#include "iterator.hpp"
#include "iterator/zip.hpp"
#include "point.hpp"
#include <iterator>
@ -36,7 +36,7 @@ namespace cruft {
std::fill (std::begin (accum), std::end (accum), 0);
std::fill (std::begin (count), std::end (count), 0);
for (auto const& [j,p]: cruft::izip (src)) {
for (auto const& [j,p]: iterator::izip (src)) {
size_t bucket = 0;
for (size_t k = 1; k < dst.size (); ++k) {

View File

@ -9,7 +9,7 @@
#include "matrix.hpp"
#include "debug.hpp"
#include "iterator.hpp"
#include "iterator/infix.hpp"
#include "point.hpp"
#include <cstring>

View File

@ -6,13 +6,12 @@
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_MATRIX_HPP
#define CRUFT_UTIL_MATRIX_HPP
#pragma once
#include "point.hpp"
#include "range.hpp"
#include "vector.hpp"
#include "iterator.hpp"
#include "iterator/infix.hpp"
#include <cstdlib>
#include <ostream>
@ -542,10 +541,8 @@ namespace cruft {
std::copy (
std::cbegin (m),
std::cend (m),
cruft::infix_iterator<decltype(m[0])> (os, ", ")
iterator::infix_iterator<decltype(m[0])> (os, ", ")
);
return os << ']';
}
};
#endif

View File

@ -1,7 +1,6 @@
#include "../../tap.hpp"
#include "../../encode/base.hpp"
#include "../../iterator.hpp"
///////////////////////////////////////////////////////////////////////////////

View File

@ -1,6 +1,9 @@
#include "../tap.hpp"
#include "../iterator.hpp"
#include "../iterator/counting.hpp"
#include "../iterator/dereference.hpp"
#include "../iterator/iota.hpp"
#include "../iterator/zip.hpp"
#include <vector>
#include <array>
@ -10,7 +13,7 @@
static void
test_counting_output_iterator (cruft::TAP::logger &tap)
{
cruft::counting_output_iterator out;
cruft::iterator::counting_output_iterator out;
*out = 0; ++out;
*out = 1; ++out;
*out = 2; ++out;
@ -27,8 +30,8 @@ test_dereference_iterator (cruft::TAP::logger &tap)
int const* pointers[4] = { values + 0, values + 1, values + 2, values + 3 };
auto const sum = std::accumulate (
cruft::dereference_adapter (std::cbegin (pointers)),
cruft::dereference_adapter (std::cend (pointers)),
cruft::iterator::dereference_adapter (std::cbegin (pointers)),
cruft::iterator::dereference_adapter (std::cend (pointers)),
0
);
@ -40,7 +43,7 @@ test_dereference_iterator (cruft::TAP::logger &tap)
static void
test_iota (cruft::TAP::logger &tap)
{
cruft::iota seq (5);
cruft::iterator::iota seq (5);
auto const sum = std::accumulate (std::begin (seq), std::end (seq), 0u);
tap.expect_eq (sum, 4u+3u+2u+1u+0u, "iota summation %!", sum);
}
@ -62,7 +65,7 @@ main (int, char**)
char c_char[] = { '\0', 'b', 'c' };
bool success = true;
for (auto const &[i, v, a, c]: cruft::izip (v_int, a_float, c_char)) {
for (auto const &[i, v, a, c]: cruft::iterator::izip (v_int, a_float, c_char)) {
success = success &&
v_int[i] == v &&
cruft::equal (a_float[i], a) &&
@ -83,7 +86,7 @@ main (int, char**)
const std::array<unsigned,3> src { 0, 1, 2 };
std::array<unsigned,3> dst { 2, 0, 1 };
for (auto [a,b]: cruft::zip (src, dst))
for (auto [a,b]: cruft::iterator::zip (src, dst))
b = a;
tap.expect_eq (src, dst, "copy using structured bindings");

View File

@ -1,7 +1,7 @@
#include "tap.hpp"
#include "string.hpp"
#include "types.hpp"
#include "iterator.hpp"
#include "iterator/zip.hpp"
#include <vector>
@ -63,7 +63,7 @@ test_tokeniser (cruft::TAP::logger &tap)
};
cruft::view src { csv.c_str (), csv.size () };
for (auto [tok, expected]: cruft::zip (cruft::tokeniser (src, ','), TESTS)) {
for (auto [tok, expected]: cruft::iterator::zip (cruft::tokeniser (src, ','), TESTS)) {
tap.expect (equal (tok, expected.value), "%s", expected.message);
}
}

View File

@ -1,5 +1,4 @@
#include "io.hpp"
#include "iterator.hpp"
#include "string.hpp"
#include "view.hpp"
#include "cpp.hpp"

View File

@ -11,7 +11,7 @@
#include <memory>
#include "../iterator.hpp"
#include "../iterator/zip.hpp"
namespace cruft::comparator {
///////////////////////////////////////////////////////////////////////////
@ -55,7 +55,7 @@ namespace cruft::comparator {
constexpr
bool operator() (const T &a, const T &b) const
{
for (auto x: cruft::zip (a, b)) {
for (auto x: cruft::iterator::zip (a, b)) {
const auto &[i,j] = x; // BUG: clang-4.0: workaround for segfault.
if (i < j) return true;
if (i != j) return false;

View File

@ -20,7 +20,6 @@
#include "uri.hpp"
#include "debug.hpp"
#include "iterator.hpp"
#include <algorithm>
#include <iostream>