Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F94208212
aka_zip_iterator.hh
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Dec 4, 18:13
Size
10 KB
Mime Type
text/x-c++
Expires
Fri, Dec 6, 18:13 (1 d, 15 h)
Engine
blob
Format
Raw Data
Handle
22757939
Attached To
rAKA akantu
aka_zip_iterator.hh
View Options
/**
* @file aka_zip_iterator.hh
*
* @author Nicolas Richart
*
* @date creation jeu déc 12 2019
*
* @brief A Documented file.
*
* @section LICENSE
*
* Copyright (©) 2010-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne)
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* Akantu is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Akantu is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Akantu. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#include "aka_compatibilty_with_cpp_standard.hh"
#include "aka_tuple_tools.hh"
/* -------------------------------------------------------------------------- */
#include <iterator>
#include <tuple>
#include <utility>
/* -------------------------------------------------------------------------- */
#ifndef AKA_ZIP_ITERATOR_HH
#define AKA_ZIP_ITERATOR_HH
#ifndef AKANTU_ITERATORS_NAMESPACE
#define AKANTU_ITERATORS_NAMESPACE akantu
#endif
namespace AKANTU_ITERATORS_NAMESPACE {
/* -------------------------------------------------------------------------- */
namespace iterators {
namespace details {
template <bool enable> struct CopyAssignmentEnabler {};
template <> struct CopyAssignmentEnabler<false> {
CopyAssignmentEnabler() = default;
CopyAssignmentEnabler(const CopyAssignmentEnabler &) = default;
CopyAssignmentEnabler(CopyAssignmentEnabler &&) = default;
CopyAssignmentEnabler & operator=(const CopyAssignmentEnabler &) = delete;
CopyAssignmentEnabler & operator=(CopyAssignmentEnabler &&) = default;
};
template <bool enable> struct MoveAssignmentEnabler {};
template <> struct MoveAssignmentEnabler<false> {
MoveAssignmentEnabler() = default;
MoveAssignmentEnabler(const MoveAssignmentEnabler &) = default;
MoveAssignmentEnabler(MoveAssignmentEnabler &&) = default;
MoveAssignmentEnabler & operator=(const MoveAssignmentEnabler &) = delete;
MoveAssignmentEnabler & operator=(MoveAssignmentEnabler &&) = default;
};
} // namespace details
/* ------------------------------------------------------------------------ */
template <class... Iterators>
class ZipIterator
: public details::CopyAssignmentEnabler<
aka::conjunction<std::is_copy_assignable<Iterators>...,
std::is_copy_constructible<Iterators>...>::value>,
public details::MoveAssignmentEnabler<
aka::conjunction<std::is_move_assignable<Iterators>...,
std::is_move_constructible<Iterators>...>::value> {
private:
using tuple_t = std::tuple<Iterators...>;
public:
using value_type =
std::tuple<typename std::iterator_traits<Iterators>::value_type...>;
using difference_type = std::common_type_t<
typename std::iterator_traits<Iterators>::difference_type...>;
using pointer =
std::tuple<typename std::iterator_traits<Iterators>::pointer...>;
using reference =
std::tuple<typename std::iterator_traits<Iterators>::reference...>;
using iterator_category = // std::input_iterator_tag;
std::common_type_t<
typename std::iterator_traits<Iterators>::iterator_category...>;
using nb_iterators = std::tuple_size<tuple_t>;
public:
explicit ZipIterator(tuple_t iterators) : iterators(std::move(iterators)) {}
/* ---------------------------------------------------------------------- */
template <class iterator_category_ = iterator_category,
std::enable_if_t<aka::is_iterator_category_at_least<
iterator_category_,
std::bidirectional_iterator_tag>::value> * = nullptr>
ZipIterator & operator--() {
tuple::foreach ([](auto && it) { --it; }, iterators);
return *this;
}
template <class iterator_category_ = iterator_category,
std::enable_if_t<aka::is_iterator_category_at_least<
iterator_category_,
std::bidirectional_iterator_tag>::value> * = nullptr>
ZipIterator operator--(int) {
auto cpy = *this;
this->operator--();
return cpy;
}
// input iterator ++it
ZipIterator & operator++() {
tuple::foreach ([](auto && it) { ++it; }, iterators);
return *this;
}
// input iterator it++
ZipIterator operator++(int) {
auto cpy = *this;
this->operator++();
return cpy;
}
// input iterator it != other_it
bool operator!=(const ZipIterator & other) const {
// return tuple::are_not_equal(iterators, other.iterators);
return std::get<0>(iterators) !=
std::get<0>(other.iterators); // helps the compiler to optimize
}
// input iterator dereference *it
decltype(auto) operator*() {
return tuple::transform([](auto && it) -> decltype(auto) { return *it; },
iterators);
}
template <class iterator_category_ = iterator_category,
std::enable_if_t<aka::is_iterator_category_at_least<
iterator_category_,
std::random_access_iterator_tag>::value> * = nullptr>
difference_type operator-(const ZipIterator & other) {
return std::get<0>(this->iterators) - std::get<0>(other.iterators);
}
// random iterator it[idx]
template <class iterator_category_ = iterator_category,
std::enable_if_t<aka::is_iterator_category_at_least<
iterator_category_,
std::random_access_iterator_tag>::value> * = nullptr>
decltype(auto) operator[](std::size_t idx) {
return tuple::transform(
[idx](auto && it) -> decltype(auto) { return it[idx]; }, iterators);
}
// random iterator it + n
template <class iterator_category_ = iterator_category,
std::enable_if_t<aka::is_iterator_category_at_least<
iterator_category_,
std::random_access_iterator_tag>::value> * = nullptr>
decltype(auto) operator+(std::size_t n) {
return ZipIterator(std::forward<tuple_t>(tuple::transform(
[n](auto && it) -> decltype(auto) { return it + n; }, iterators)));
}
// random iterator it - n
template <class iterator_category_ = iterator_category,
std::enable_if_t<aka::is_iterator_category_at_least<
iterator_category_,
std::random_access_iterator_tag>::value> * = nullptr>
decltype(auto) operator-(std::size_t n) {
return ZipIterator(std::forward<tuple_t>(tuple::transform(
[n](auto && it) -> decltype(auto) { return it - n; }, iterators)));
}
template <
class iterator_category_ = iterator_category,
std::enable_if_t<aka::is_iterator_category_at_least<
iterator_category_, std::forward_iterator_tag>::value> * = nullptr>
bool operator==(const ZipIterator & other) const {
return not tuple::are_not_equal(iterators, other.iterators);
}
private:
tuple_t iterators;
};
} // namespace iterators
/* -------------------------------------------------------------------------- */
template <class... Iterators>
decltype(auto) zip_iterator(std::tuple<Iterators...> && iterators_tuple) {
auto zip = iterators::ZipIterator<Iterators...>(
std::forward<decltype(iterators_tuple)>(iterators_tuple));
return zip;
}
/* -------------------------------------------------------------------------- */
namespace containers {
template <class... Containers> class ZipContainer {
using containers_t = std::tuple<Containers...>;
public:
explicit ZipContainer(Containers &&... containers)
: containers(std::forward<Containers>(containers)...) {}
decltype(auto) begin() const {
return zip_iterator(
tuple::transform([](auto && c) { return c.begin(); },
std::forward<containers_t>(containers)));
}
decltype(auto) end() const {
return zip_iterator(
tuple::transform([](auto && c) { return c.end(); },
std::forward<containers_t>(containers)));
}
decltype(auto) begin() {
return zip_iterator(
tuple::transform([](auto && c) { return c.begin(); },
std::forward<containers_t>(containers)));
}
decltype(auto) end() {
return zip_iterator(
tuple::transform([](auto && c) { return c.end(); },
std::forward<containers_t>(containers)));
}
private:
containers_t containers;
};
} // namespace containers
/* -------------------------------------------------------------------------- */
template <class... Containers> decltype(auto) zip(Containers &&... conts) {
return containers::ZipContainer<Containers...>(
std::forward<Containers>(conts)...);
}
template <class... zip_container_t>
decltype(auto) make_zip_cat(zip_container_t &&... cont) {
return make_transform_adaptor(
zip(std::forward<zip_container_t>(cont)...),
[](auto && value) { return tuple::flatten(value); });
}
} // namespace AKANTU_ITERATORS_NAMESPACE
namespace std {
template <typename... Its>
struct iterator_traits<
::AKANTU_ITERATORS_NAMESPACE::iterators::ZipIterator<Its...>> {
private:
using iterator_type =
typename ::AKANTU_ITERATORS_NAMESPACE::iterators::ZipIterator<Its...>;
public:
using iterator_category = typename iterator_type::iterator_category;
using value_type = typename iterator_type::value_type;
using difference_type = typename iterator_type::difference_type;
using pointer = typename iterator_type::pointer;
using reference = typename iterator_type::reference;
};
} // namespace std
#endif /* AKA_ZIP_ITERATOR_HH */
Event Timeline
Log In to Comment