[range] - Proposal to change the indexed range adaptor

Fellow maintainers and developers, Having gained experience using the Boost.Range library changes I submitted for Boost 1.43, I have become dissatisfied with my indexed adaptor. Currently the underling index_iterator has a return type identical to the wrapped iterator, and the index is extracted via a method call on the iterator. While this works for the case where one is writing algorithms to explicitly utilise the underlying iterator, it is clearly a poor design choice since it severly limits the interoperation with other algorithms. I feel that I have made a mistake. I propose that a breaking change is made to the index range adaptor so that it returns a pair<original_reference_type, index_type>. How do you feel about this change? Do you have alternative suggestions? Regards, Neil Groves

On Sat, Oct 30, 2010 at 12:38 PM, Neil Groves <neil@grovescomputing.com>wrote:
I propose that a breaking change is made to the index range adaptor so that it returns a pair<original_reference_type, index_type>.
Hi Neil Could you provide a use-case example, before and after. Thx - Rob.

On Sat, Oct 30, 2010 at 6:28 PM, Robert Jones <robertgbjones@gmail.com>wrote:
On Sat, Oct 30, 2010 at 12:38 PM, Neil Groves <neil@grovescomputing.com
wrote:
I propose that a breaking change is made to the index range adaptor so
that
it returns a pair<original_reference_type, index_type>.
Hi Neil
Could you provide a use-case example, before and after.
Certainly. I should have thought to do this in the original email. Thank you for taking the time to help me acquire information. Before: BOOST_FOREACH( auto item, rng | index ) { // access to the index is not possible - doh :-( // The return type of the dereferenced element from an index range adaptor loses the index const element_type& elem = item; } After: BOOST_FOREACH( auto item, rng | indexed ) { // After my proposed modification, the return type becomes a pair of the index // and the original reference type of the underlying iterator/range. std::size i = item.first; const element_type& elem = item.second; }
Thx - Rob.
I hope this makes the motivation for the change clearer. My original design does, of course, work if one writes the algorithm specifically to extract the index directly from the iterator. This is unnecessarily limiting. Regards, Neil Groves

On Sun, Oct 31, 2010 at 8:43 PM, Neil Groves <neil@grovescomputing.com>wrote:
On Sat, Oct 30, 2010 at 6:28 PM, Robert Jones <robertgbjones@gmail.com
wrote:
On Sat, Oct 30, 2010 at 12:38 PM, Neil Groves <neil@grovescomputing.com
wrote:
I propose that a breaking change is made to the index range adaptor so
that
it returns a pair<original_reference_type, index_type>.
Hi Neil
Could you provide a use-case example, before and after.
Certainly. I should have thought to do this in the original email. Thank you for taking the time to help me acquire information.
Before:
BOOST_FOREACH( auto item, rng | index ) { // access to the index is not possible - doh :-( // The return type of the dereferenced element from an index range adaptor loses the index const element_type& elem = item; }
Hi Neil ...so in the 'BEFORE' case, how can this be written now, with your library as it stands (presumably not with BOOST_FOREACH)? Thx - Rob. ps. I must, rather sheepishly, admit to not having been able to use your library in real code - sorry! Having actively encouraged you write it, I personally am constrained to 1.39 and before - doesn't that just suck!

Den 31-10-2010 21:43, Neil Groves skrev:
On Sat, Oct 30, 2010 at 6:28 PM, Robert Jones<robertgbjones@gmail.com>wrote:
On Sat, Oct 30, 2010 at 12:38 PM, Neil Groves<neil@grovescomputing.com
wrote:
I propose that a breaking change is made to the index range adaptor so
that
it returns a pair<original_reference_type, index_type>.
Hi Neil
Could you provide a use-case example, before and after.
Certainly. I should have thought to do this in the original email. Thank you for taking the time to help me acquire information.
Before:
BOOST_FOREACH( auto item, rng | index ) { // access to the index is not possible - doh :-( // The return type of the dereferenced element from an index range adaptor loses the index const element_type& elem = item; }
After:
BOOST_FOREACH( auto item, rng | indexed ) { // After my proposed modification, the return type becomes a pair of the index // and the original reference type of the underlying iterator/range. std::size i = item.first; const element_type& elem = item.second; }
Your new version works well with foreach, but perhaps less so with other iteration mathods. Therefore I suggest that you add a new range instead of messing with the old one. -Thorsten

Is it possible that something imitating python would work? In [1]: for e in (('a', 'b', 'c')): ...: print e a b c In [3]: for i,e in enumerate (('a', 'b', 'c')): ...: print i,e ...: 0 a 1 b 2 c

At Sun, 31 Oct 2010 20:43:42 +0000, Neil Groves wrote:
// After my proposed modification, the return type becomes a // pair of the index and the original reference type of the // underlying iterator/range.
Copying those elements into the pair could get expensive. Maybe you want to wrap a counting_iterator around that iterator; just a thought. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

At Sun, 31 Oct 2010 20:43:42 +0000, Neil Groves wrote:
// After my proposed modification, the return type becomes a // pair of the index and the original reference type of the // underlying iterator/range.
Copying those elements into the pair could get expensive. Maybe you want to wrap a counting_iterator around that iterator; just a thought. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
participants (5)
-
David Abrahams
-
Neal Becker
-
Neil Groves
-
Robert Jones
-
Thorsten Ottosen