Hi, I would like to raise a couple of points about the iterator adaptors. Firstly, is it really necessary to have the predicate as part of the template specification for the filter_iterator? Surely it can be assumed that it will always be convertible to boost::function<bool (const value_type&)> ? Secondly, by allowing the return type of the function used by the transform_iterator to be a reference can we not use it to implement a more generic form of indirect_iterator? The code below gives a flavour of the idea. struct dereference { typedef int result_type; int* data_; dereference(int* data):data_(data){} int operator()(int i) const { return data_[i]; } }; int main() { int data[] = { 1,2,3,4,5,6,7,8 }; int keys[] = { 0,3,1,6,5,4,2,7 }; typedef boost::transform_iterator_generator<dereference, int*>::type indirect_iterator; indirect_iterator i(keys, dereference(data)), i_end(keys+8,dereference(data)); while(i != i_end) std::cout << *i++ << ' '; return 0; } The output is: 1 4 2 7 6 5 3 8 Witz
Witz <witz@nildram.co.uk> writes:
Firstly, is it really necessary to have the predicate as part of the template specification for the filter_iterator? Surely it can be assumed that it will always be convertible to boost::function<bool (const value_type&)> ?
There is significant run-time overhead when using boost::function, including a virtual function call. You can, of course, manually specify boost::function<bool (const value_type &)> as a the function type. Even if the template parameters for filter_iterator were reversed, so that it would be possible to specify a default value for Predicate, setting it to a boost::function type would require that boost.function be a dependency of filter iterator, even if the default value is not used, and would encourage (inefficient) use of boost::function even when the actual function type is known.
Secondly, by allowing the return type of the function used by the transform_iterator to be a reference can we not use it to implement a more generic form of indirect_iterator?
If the life-time of the value to which the returned reference refers is longer than that of the reference itself, then you can use projection_iterator, which is also a random access iterator. transform_iterator is designed specifically for the case that the returned value is not stored somewhere else, and consequently is only an input iterator. (This distinction is of little importance, however, in the new iterator adaptors library, which David Abrahams has proposed for standardization.) -- Jeremy Maitin-Shepard
Witz <witz@nildram.co.uk> writes:
Hi,
I would like to raise a couple of points about the iterator adaptors.
Firstly, is it really necessary to have the predicate as part of the template specification for the filter_iterator? Surely it can be assumed that it will always be convertible to boost::function<bool (const value_type&)> ?
If you don't care about efficiency, yes it can. We do care, so we won't make that assumption. It might make sense to use that as a default template argument, though that would establish a dependency between filter_iterator and the function lib.
Secondly, by allowing the return type of the function used by the transform_iterator to be a reference can we not use it to implement a more generic form of indirect_iterator? The code below gives a flavour of the idea.
struct dereference { typedef int result_type;
int* data_;
dereference(int* data):data_(data){} int operator()(int i) const { return data_[i]; } };
int main() { int data[] = { 1,2,3,4,5,6,7,8 }; int keys[] = { 0,3,1,6,5,4,2,7 };
typedef boost::transform_iterator_generator<dereference, int*>::type indirect_iterator;
indirect_iterator i(keys, dereference(data)), i_end(keys+8,dereference(data));
while(i != i_end) std::cout << *i++ << ' ';
return 0; }
The output is:
1 4 2 7 6 5 3 8
The new iterator adaptors library in CVS *does* allow transform_iterator's return to be a reference. With the old library, you just want to use project_iterator_adaptor instead. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (3)
-
David Abrahams
-
Jeremy Maitin-Shepard
-
Witz