removedアダプタ
Boost.Filesystem v3調べ物メモ - Faith and Brave - C++で遊ぼう
removedアダプタは僕も欲しいかも・・・!
#ifndef HWM_REMOVED_HPP_ #define HWM_REMOVED_HPP_ #include <pstade/oven/detail/base_to_adaptor.hpp> #include <pstade/oven/detail/filter_iterator.hpp> #include <pstade/result_of.hpp> #include <pstade/oven/range_value.hpp> #include <pstade/oven/iter_range.hpp> namespace hwm { namespace removed_detail { using namespace pstade; using namespace pstade::oven; template<class Range, class Predicate> struct base { struct not_helper { typedef bool result_type; not_helper(Predicate pred) : pred_(pred) {} template<typename T> result_type operator()(T const &t) const { return !(pred_(t)); } Predicate pred_; }; typedef not_helper not_t; typedef detail::filter_iterator< typename pass_by_value<not_t>::type, typename range_iterator<Range>::type > iter_t; typedef iter_range<iter_t> const result_type; result_type operator()(Range& rng, Predicate& pred) const { PSTADE_CONCEPT_ASSERT((SinglePass<Range>)); return aux(boost::begin(rng), boost::end(rng), pred); } template< class Iterator > result_type aux(Iterator first, Iterator last, Predicate& pred) const { return result_type(iter_t(not_t(pred), first, last), iter_t(not_t(pred), last, last)); } }; } //namespace removed_detail PSTADE_OVEN_BASE_TO_ADAPTOR(removed, (removed_detail::base<_, _>)) } //namespace hwm #endif //HWM_REMOVED_HPP_
#include <iostream> #include <vector> #include <boost/tuple/tuple_io.hpp> #include <pstade/oven/foreach.hpp> #include <pstade/oven/io.hpp> #include <pstade/oven/regular.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> #include "removed.hpp" int main() { namespace bll = boost::lambda; using namespace pstade::oven; int v[] = {5, 6, 7, 8, 9}; PSTADE_OVEN_FOREACH(elem, v|hwm::removed(regular(bll::_1 < 7))) { std::cout << elem << std::endl; } std::cout << std::endl; struct pred { typedef bool result_type; result_type operator()(int i) const { return i >= 7; } }; std::cout << (v|hwm::removed(pred())) << std::endl; std::cout << std::endl; struct binary_pred { typedef bool result_type; result_type operator()(int i, int j) const { return i < j; } }; PSTADE_OVEN_FOREACH(elem, v|hwm::removed(bll::bind(binary_pred(), bll::_1, 8))) { std::cout << elem << std::endl; } }
7 8 9 {5,6} 8 9
安直にT_make_filteredをpstade::result_ofにかけてたら複雑すぎってmsvcに怒られました><;
そういったこともあってpstade/oven/filtered.hppの中身をほどんどコピーした感じ・・・
あと、Predicateは参照で持ってたら実行時に落ちたので参照やめてます。(←安直)
使ってみて感じたことは、あんまりremoved Rangeアダプタって直感的じゃないような・・・いや、慣れてないからかな?