簡體   English   中英

boost :: adapters :: transformed后跟boost :: adapters :: filtered調用函數兩次

[英]boost::adaptors::transformed followed by boost::adaptors::filtered calls function twice

我正在嘗試將boost::adaptors::transformed (讓我們稱之為map )鏈接到boost::adaptors::filtered (讓我們稱之為filter ) - 想法是映射一個返回“Maybe”的fun (在我的例子中, std::pair<bool, T> )在一個范圍內,只輸出部分結果。 我的第一個實施:

define BOOST_RESULT_OF_USE_DECLTYPE // enable lambda arguments for Boost.Range
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>

struct OnlyEven
{
    typedef int argument_type;
    typedef std::pair<bool, int> result_type;
    result_type operator()(argument_type x) const
    {
        std::cout << "fun: " << x << std::endl;
        return std::make_pair(x % 2 == 0, x);
    }
} only_even;

int main(int argc, char* argv[])
{
    auto map = boost::adaptors::transformed;
    auto filter = boost::adaptors::filtered;
    int v[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    auto s = v | map(only_even) | filter([](std::pair<bool, int> x)->bool{ return x.first; });
    for (auto i : s) {}
    return 0;
}

當我運行這個時,我得到:

fun: 1
fun: 2
fun: 2
fun: 3
fun: 4
fun: 4
fun: 5
fun: 6
fun: 6
fun: 7
fun: 8
fun: 8
fun: 9
fun: 10
fun: 10

每次predicatetrue ,都會調用兩次fun 這是預期的行為嗎? 我做錯了什么,或者這是Boost中的一個錯誤(我使用的是1.48)?

編輯 :我在Boost的主干版本上嘗試了這個,它仍然會發生。

第一次在傳遞給過濾器時調用它 - 在增量期間。

第二次在取消引用期間在基於范圍的內容中調用它。 它不會緩存結果。

即,只是通過范圍:

++++++++++boost::begin(s);

給出

fun: 1
fun: 2
fun: 3
fun: 4
fun: 5
fun: 6
fun: 7
fun: 8
fun: 9
fun: 10

檢查filter_iterator的實現(過濾基於它)。 它不做任何緩存。

如果轉型很昂貴怎么辦?

過濾不要使用知識輸入來自哪里。

緩存結果將需要大小過濾的迭代器。 試想一下應該存儲緩存結果的位置。 它應該被復制到filter過濾器的某個成員中。

因此,基本上,在用於緩存的空間和解除引用的計數之間存在折衷。


編輯:我已經做了cached_iterator的概念驗證,它緩存了取消引用的結果,並在每次推進時使其無效。 另外,我已經制作了相應的范圍適配器。

它是如何使用的:

auto s = v | transformed(only_even) | cached | reversed | cached | flt | flt | flt | flt | flt | flt;

您應該將緩存放在要緩存結果的鏈中。

現場演示

#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/adaptor/map.hpp>
#include <boost/range/algorithm.hpp>

#include <iostream>
#include <ostream>

// ____________________________________________________________________________________________ //

#include <boost/iterator/iterator_adaptor.hpp>
#include <boost/range/iterator.hpp>
#include <iterator>

namespace impl
{

template<typename Iterator>
class cached_iterator : public boost::iterator_adaptor<cached_iterator<Iterator>,Iterator>
{
    typedef boost::iterator_adaptor<cached_iterator,Iterator> super;
    mutable bool invalidated;
    mutable typename std::iterator_traits<Iterator>::value_type cached;    
public:
    cached_iterator() : invalidated(true) {}
    cached_iterator(const Iterator &x) : super(x), invalidated(true) {}

    typename std::iterator_traits<Iterator>::value_type dereference() const
    {
        if(invalidated)
        {
            cached = *(this->base());
            invalidated=false;
            return cached;
        }
        else
        {
            return cached;
        }
    }
    void increment()
    {
        invalidated=true;
        ++(this->base_reference());
    }
    void decrement()
    {
        invalidated=true;
        --(this->base_reference());
    }
    void advance(typename super::difference_type n)
    {
        invalidated=true;
        (this->base_reference())+=n;
    }
};

template<typename Iterator> cached_iterator<Iterator> make_cached_iterator(Iterator it)
{
    return cached_iterator<Iterator>(it);
}

template< class R >
struct cached_range : public boost::iterator_range<cached_iterator<typename boost::range_iterator<R>::type> >
{
private:
    typedef boost::iterator_range<cached_iterator<typename boost::range_iterator<R>::type> > base;
public:
    typedef R source_range_type;
    cached_range( R& r )
        : base( make_cached_iterator(boost::begin(r)), make_cached_iterator(boost::end(r)) )
    { }
};

template<typename InputRange>
inline cached_range<const InputRange> cache(const InputRange& rng)
{
    return cached_range<const InputRange>(rng);
}

template<typename InputRange>
inline cached_range<InputRange> cache(InputRange& rng)
{
    return cached_range<InputRange>(rng);
}

struct cache_forwarder{};

cache_forwarder cached;

template< class InputRange >
inline cached_range<const InputRange>
operator|( const InputRange& r, cache_forwarder )
{
    return cache(r);
}

template< class InputRange >
inline cached_range<InputRange>
operator|( InputRange& r, cache_forwarder )
{
    return cache(r);
}

} // namespace impl

// ____________________________________________________________________________________________ //


struct OnlyEven
{
    typedef int argument_type;
    typedef std::pair<bool, int> result_type;
    result_type operator()(argument_type x) const
    {
        std::cout << "fun: " << x << std::endl;
        return std::make_pair(x % 2 == 0, x);
    }
} only_even;

int main()
{
    using namespace impl;
    using namespace boost::adaptors;
    using namespace std;

    int v[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    auto flt =  filtered([](std::pair<bool, int> x)->bool{ return x.first; });

    auto s = v | transformed(only_even) | cached | reversed | cached | flt | flt | flt | flt | flt | flt;

    boost::copy(s | map_values, ostream_iterator<int>(cout,"\n") );
    return 0;
}

輸出是:

fun: 10
10
fun: 9
fun: 8
8
fun: 7
fun: 6
6
fun: 5
fun: 4
4
fun: 3
fun: 2
2
fun: 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM