Utopia 2
Framework for studying models of complex & adaptive systems.
|
Collection of classes and functions for custom iteration over ranges. More...
Namespaces | |
namespace | Utopia::Itertools |
Tools for iterating over collections. | |
Classes | |
class | Utopia::Itertools::ZipIterator< Iters > |
Iterator over an arbitrary number of collections. More... | |
class | Utopia::Itertools::zip< Containers > |
A range defined by instances of ZipIterator. More... | |
Functions | |
template<typename Adaptor , typename... Containers> | |
auto | Utopia::Itertools::adapt_zip (Adaptor &&adaptor, Containers &... containers) |
Return a zip iterator built from an adapter applied to containers. | |
Collection of classes and functions for custom iteration over ranges.
This improves the standard C++ abilities to iterate over collections of elements.
This implementation mirrors the Python zip
function. It provides an iterator over arbitrarily many heterogeneous collections. The Utopia::Itertools::zip class stores references to the containers inserted into it and supplies methods for retrieving the respective Utopia::Itertools::ZipIterator.
Zip iterators obey the C++ standard interface for iterators. They also adapt their functionality on the lowest level iterator category of all containers inserted into the object. For example, inserting an std::vector
and an std::list
into Utopia::Itertools::zip will yield a bidirectional zip iterator.
Dereferencing this iterator returns an std::tuple
containing references to the respective objects of the underlying containers. They can be captured using structured bindings. Use auto&
for capturing the references is not necessary and might even be illegal in some situations. Extracting single values via std::get
, however, obeys regular value/reference syntax.
Zip iterators can be used in range-based and regular for loops. Additionally, Utopia provides overloads for ostream
std::operator<<() to write the contents of a tuple into the command line. This works if the objects themselves can be written into the same stream.
Use the free function Utopia::adapt_zip() to build a ZipIterator from an iterator adaptor and a set of containers.
auto Utopia::Itertools::adapt_zip | ( | Adaptor && | adaptor, |
Containers &... | containers | ||
) |
Return a zip iterator built from an adapter applied to containers.
Use this function to apply STL iterator adaptors to a pack of containers and build a ZipIterator from the resulting pack of iterators.
adaptor | The callable iterator adaptor to apply to all containers |
container | The containers to build the ZipIterator from |