Last active
August 29, 2015 14:00
-
-
Save compnerd/6f50ef4545eed05e429b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* vim: set et ft=cpp.doxygen sts=2 sw=2 ts=8 : */ | |
/** | |
* Copyright © 2014 Saleem Abdulrasool <[email protected]> | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions are met: | |
* | |
* 1. Redistributions of source code must retain the above copyright notice, | |
* this list of conditions and the following disclaimer. | |
* | |
* 2. Redistributions in binary form must reproduce the above copyright notice, | |
* this list of conditions and the following disclaimer in the documentation | |
* and/or other materials provided with the distribution. | |
* | |
* 3. The name of the author may not be used to endorse or promote products | |
* derived from this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED | |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
**/ | |
#include "zip.hh" | |
#include <iostream> | |
#include <vector> | |
int main(int argc, char *argv[]) { | |
char chars[] = { 'a', 'b', 'c' }; | |
std::vector<int> ints = { 1, 2, 3 }; | |
for (const auto & items : zip(chars, ints)) | |
std::cout << std::get<0>(items) << ", " << std::get<1>(items) << '\n'; | |
return EXIT_SUCCESS; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* vim: set et ft=cpp.doxygen sts=2 sw=2 ts=8 : */ | |
/** | |
* Copyright © 2014 Saleem Abdulrasool <[email protected]> | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions are met: | |
* | |
* 1. Redistributions of source code must retain the above copyright notice, | |
* this list of conditions and the following disclaimer. | |
* | |
* 2. Redistributions in binary form must reproduce the above copyright notice, | |
* this list of conditions and the following disclaimer in the documentation | |
* and/or other materials provided with the distribution. | |
* | |
* 3. The name of the author may not be used to endorse or promote products | |
* derived from this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED | |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
**/ | |
#ifndef zip_hh | |
#define zip_hh | |
#include <iterator> | |
#include <memory> | |
#include <numeric> | |
#include <tuple> | |
#include <type_traits> | |
#if __cplusplus >= 201400l | |
#include <utility> | |
#endif | |
// TODO(compnerd) make this interface more compatible with C++14 | |
// make_integer_sequence | |
#if __cplusplus < 201400l | |
template <size_t ...> | |
struct sequence { }; | |
template <size_t position_, size_t ... subsequence_> | |
struct generate : generate<position_ - 1u, position_ - 1u, subsequence_...> { }; | |
template <size_t ... sequence_> | |
struct generate<0u, sequence_...> { | |
typedef sequence<sequence_...> sequence; | |
}; | |
#endif | |
namespace details { | |
template <typename> | |
struct iterable; | |
template <template <typename T_, typename Allocator_ = std::allocator<T_>> | |
class container_, typename type_> | |
struct iterable<container_<type_>> { | |
typedef container_<type_> type; | |
typedef typename container_<type_>::const_iterator iterator; | |
typedef typename std::remove_reference<type_>::type value_type; | |
}; | |
template <typename type_, size_t bound_> | |
struct iterable<type_[bound_]> { | |
typedef type_ type[bound_]; | |
typedef const type_ * iterator; | |
typedef typename std::remove_reference<type_>::type value_type; | |
}; | |
template <typename ... iterable_> | |
class zipped { | |
const std::tuple<const typename iterable<iterable_>::type & ...> iterables_; | |
public: | |
class const_iterator | |
: public std::iterator<std::forward_iterator_tag, | |
const std::tuple<const typename iterable<iterable_>::value_type & ...>> { | |
friend class zipped; | |
std::tuple<typename iterable<iterable_>::iterator ...> iterators_; | |
const_iterator(typename iterable<iterable_>::iterator ... iterators) | |
: iterators_(iterators...) { } | |
const_iterator(std::tuple<typename iterable<iterable_>::type & ...> &) = delete; | |
const_iterator() = delete; | |
template <size_t ... sequence_> | |
const std::tuple<const typename iterable<iterable_>::value_type & ...> | |
operator *(sequence<sequence_...>) const { | |
return std::make_tuple(std::cref(*std::get<sequence_>(iterators_))...); | |
} | |
template <size_t ... sequence_> | |
void advance(sequence<sequence_...>) { | |
static_cast<void>(std::initializer_list<int>{ | |
[&]{ ++std::get<sequence_>(iterators_); return 0; }()... | |
}); | |
} | |
template <size_t ... sequence_> | |
bool equals(const const_iterator & rhs, sequence<sequence_...>) const { | |
auto il = std::initializer_list<int>{ | |
[&]{ return std::get<sequence_>(iterators_) == std::get<sequence_>(rhs.iterators_); }()... | |
}; | |
return std::accumulate(std::begin(il), std::end(il), 0) == sizeof...(sequence_); | |
} | |
public: | |
bool operator == (const const_iterator & rhs) const { | |
return equals(rhs, typename generate<sizeof...(iterable_)>::sequence()); | |
} | |
bool operator != (const const_iterator & rhs) const { | |
return not operator == (rhs); | |
} | |
const_iterator & operator ++ () { | |
advance(typename generate<sizeof...(iterable_)>::sequence()); | |
return *this; | |
} | |
const_iterator operator ++ (int) { | |
const_iterator tmp = *this; | |
operator ++ (); | |
return tmp; | |
} | |
const std::tuple<const typename iterable<iterable_>::value_type & ...> | |
operator * () const { | |
return operator *(typename generate<sizeof...(iterable_)>::sequence()); | |
} | |
}; | |
private: | |
template <size_t ... sequence_> | |
const_iterator begin(sequence<sequence_...>) const { | |
return const_iterator(std::begin(std::get<sequence_>(iterables_))...); | |
} | |
template <size_t ... sequence_> | |
const_iterator end(sequence<sequence_...>) const { | |
return const_iterator(std::end(std::get<sequence_>(iterables_))...); | |
} | |
public: | |
zipped(const typename iterable<iterable_>::type & ... iterables) | |
: iterables_(iterables...) { } | |
zipped(const typename iterable<iterable_>::type && ... iterables) | |
: iterables_(iterables...) { } | |
~zipped() noexcept = default; | |
zipped & operator = (const zipped &) = delete; | |
const_iterator begin() const { | |
return begin(typename generate<sizeof...(iterable_)>::sequence()); | |
} | |
const_iterator end() const { | |
// TODO(compnerd) truncate the iterators to the minimal width of the | |
// iterables | |
return end(typename generate<sizeof...(iterable_)>::sequence()); | |
} | |
}; | |
} | |
template <typename ... iterable_> | |
details::zipped<iterable_...> zip(iterable_ && ... iterables) { | |
return details::zipped<iterable_...>{ std::forward<iterable_>(iterables)... }; | |
} | |
template <typename ... iterable_> | |
details::zipped<iterable_...> zip(iterable_ & ... iterables) { | |
return details::zipped<iterable_...>{ iterables... }; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment