|
Boost Users : |
Subject: Re: [Boost-users] [range] filtered dereferences underlying range elements twice
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2011-05-31 21:58:59
Thanks Akira, it is good to know that there is a workaround.
But I was wondering, is it necessary in the first place for filtered
to dereference the underlying range elements twice?
Thanks,
Nate.
----------------------------------------
> Date: Tue, 31 May 2011 15:07:19 +0900
> From: faithandbrave_at_[hidden]
> To: boost-users_at_[hidden]
> Subject: Re: [Boost-users] [range] filtered dereferences underlying range elements twice
>
> Hi, Nathan.
>
> Oven Library has solution for this problem.
> oven::memoized adaptor:
>
> http://p-stade.sourceforge.net/oven/doc/html/oven/range_adaptors.html#oven.range_adaptors.memoized
>
> #include
> #include
> #include
> #include
> #include
> #include
> int transform(int i)
> {
> std::cout << "calling transform() on " << i << "\n";
> return i + 1;
> }
> bool filter(int i)
> {
> return i % 2 == 0;
> }
> int main()
> {
> int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
> int b[8];
>
> boost::copy(a | boost::adaptors::transformed(transform)
> | pstade::oven::memoized
> | boost::adaptors::filtered(filter),
> b);
>
> return 0;
> }
>
> results:
> calling transform() on 1
> calling transform() on 2
> calling transform() on 3
> calling transform() on 4
> calling transform() on 5
> calling transform() on 6
> calling transform() on 7
> calling transform() on 8
>
>
> 2011/5/31 Nathan Ridge :
> >
>
> > As you can see, the transform function is called twice for elements
> > that pass the filter.
> >
> > Can this be avoided? In my real use case, the transformation
> > performs a database lookup, and it should not be done twice.
> >
> > Thanks,
> > Nate.
>
> >>========================
> Akira Takahashi
> mailto:faithandbrave_at_[hidden]
> blog:http://d.hatena.ne.jp/faith_and_brave/
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net