add unranged iterator extraction
This commit is contained in:
parent
23b0a79200
commit
e96f56a5da
11
libvmake/examples/input.cpp
Normal file
11
libvmake/examples/input.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "../vmake.hpp"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
vmake::output_n(cout, n, " ", vmake::transform(vmake::extract(istream_iterator<int>(cin)), [](int x) {
|
||||||
|
return x * x;
|
||||||
|
}));
|
||||||
|
return 0;
|
||||||
|
}
|
@ -99,12 +99,12 @@ struct empty_sequence {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<typename ContIt>
|
template<typename ContIt>
|
||||||
struct extractor {
|
struct ranged_iterator_extractor {
|
||||||
using result = typename std::remove_reference<decltype(*std::declval<ContIt>())>::type;
|
using result = typename std::remove_reference<decltype(*std::declval<ContIt>())>::type;
|
||||||
|
|
||||||
ContIt cur, end;
|
ContIt cur, end;
|
||||||
|
|
||||||
extractor(const ContIt &begin, const ContIt &end) : cur(begin), end(end) {}
|
ranged_iterator_extractor(const ContIt &begin, const ContIt &end) : cur(begin), end(end) {}
|
||||||
|
|
||||||
bool is_terminated() const noexcept {
|
bool is_terminated() const noexcept {
|
||||||
return cur == end;
|
return cur == end;
|
||||||
@ -116,6 +116,47 @@ struct extractor {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename ContIt>
|
||||||
|
struct iterator_extractor {
|
||||||
|
using result = typename std::remove_reference<decltype(*std::declval<ContIt>())>::type;
|
||||||
|
|
||||||
|
ContIt it;
|
||||||
|
bool added;
|
||||||
|
|
||||||
|
iterator_extractor(const ContIt &begin) : it(begin), added(false) {};
|
||||||
|
|
||||||
|
constexpr bool is_terminated() const noexcept {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator()() {
|
||||||
|
// WARN: use for strange iterators such as std::istream_iterator
|
||||||
|
// do NOT try to optimize this into *it++
|
||||||
|
if (added) return *++it;
|
||||||
|
added = true;
|
||||||
|
return *it;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
|
||||||
|
template<typename ContIt>
|
||||||
|
inline auto extract(const ContIt &begin, const ContIt &end) {
|
||||||
|
return details::ranged_iterator_extractor<ContIt>(begin, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename ContIt>
|
||||||
|
inline auto extract(const ContIt &begin) {
|
||||||
|
return details::iterator_extractor<ContIt>(begin);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename ContIt>
|
||||||
|
inline auto extract_n(const ContIt &begin, size_t n) {
|
||||||
|
return take(details::iterator_extractor<ContIt>(begin), n);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace details {
|
||||||
|
|
||||||
template<typename Func>
|
template<typename Func>
|
||||||
struct generator {
|
struct generator {
|
||||||
using result = typename std::result_of<Func()>::type;
|
using result = typename std::result_of<Func()>::type;
|
||||||
@ -236,11 +277,6 @@ inline OutputIt copy_n(OutputIt it, size_t n, Gen&& g) {
|
|||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ContIt>
|
|
||||||
inline auto extract(const ContIt &begin, const ContIt &end) {
|
|
||||||
return details::extractor<ContIt>(begin, end);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Gen1, typename Gen2>
|
template<typename Gen1, typename Gen2>
|
||||||
struct concator {
|
struct concator {
|
||||||
using result = typename Gen1::result;
|
using result = typename Gen1::result;
|
||||||
@ -308,7 +344,7 @@ inline auto output(OutputStream& out, Gen &&g) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename OutputStream, typename Gen>
|
template<typename OutputStream, typename Gen>
|
||||||
inline auto output_n(OutputStream& out, const char *delim, size_t n, Gen &&g) {
|
inline auto output_n(OutputStream& out, size_t n, const char *delim, Gen &&g) {
|
||||||
return copy_n(std::ostream_iterator<typename std::decay<Gen>::type::result>(out, delim), n, std::move(g));
|
return copy_n(std::ostream_iterator<typename std::decay<Gen>::type::result>(out, delim), n, std::move(g));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user