[Boost-users] [range] for_each and std::map of std::list

35 views
Skip to first unread message

Mark Snelling

unread,
Apr 6, 2011, 10:03:52 AM4/6/11
to boost-users
Hi,

Consider a map defined as...

std::map< int, std::list< std::string > > my_map;

I'm wondering if/how it's possible to use Boost.Range to call a function on each std::string in each of the lists contained in the map. I have an implementation below which works using a C++0x lambda function but am curious if this can be done purely using Boost.Range?

boost::for_each( 
boost::adaptors::values( my_map), 
[]( const std::list< std::string >& my_list ) { boost::for_each( my_list, some_func ); } );

ideas anyone?

Robert Jones

unread,
Apr 6, 2011, 12:07:03 PM4/6/11
to boost...@lists.boost.org
In the absence of C++0x lambdas I think you'd need something like Boost.Lambda, but I guess
that's probably not interoperable with the new range stuff for nested std algorithm invocations.

- Rob.
 

Thorsten Ottosen

unread,
Apr 6, 2011, 1:55:20 PM4/6/11
to boost...@lists.boost.org, bo...@lists.boost.org


What we need is a generic range adaptor that can make a container of
containers look like a "flat" sequence.

I suggest the syntax

rngOfContainers | boost::adaptors::flattened

and the underlying iterator should be up to bidirectional (if the
underlying two container types support it).

Anyone interested in implementing this?

-Thorsten

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Neil Groves

unread,
Apr 6, 2011, 4:55:27 PM4/6/11
to Thorsten Ottosen, bo...@lists.boost.org, boost...@lists.boost.org

What we need is a generic range adaptor that can make a container of containers look like a "flat" sequence.

I suggest the syntax

 rngOfContainers | boost::adaptors::flattened

and the underlying iterator should be up to bidirectional (if the underlying two container types support it).

Anyone interested in implementing this?


I've been interested in implementing something similar for a while. I was thinking slightly more generally that any n-dimensional structure can often be linearised or projected into a linear sequence. Hence this would enable not just the above example to work, but many standard algorithms could work with tree structures, hyper-cube trees, directed acyclic graphs etc. I've implemented a number of these already, but wanted to gain experience with them in my own projects before committing them to the trunk.
 
-Thorsten


Regards,
Neil Groves 

Thorsten Ottosen

unread,
Apr 6, 2011, 5:14:05 PM4/6/11
to boost...@lists.boost.org, bo...@lists.boost.org
Den 06-04-2011 22:55, Neil Groves skrev:

> I've been interested in implementing something similar for a while. I
> was thinking slightly more generally that any n-dimensional structure
> can often be linearised or projected into a linear sequence. Hence this
> would enable not just the above example to work, but many standard
> algorithms could work with tree structures, hyper-cube trees, directed
> acyclic graphs etc. I've implemented a number of these already, but
> wanted to gain experience with them in my own projects before committing
> them to the trunk.

Cool. I have a little problem seeing how it works with acyclic graphs.
For example, there are many ways to do this. Which one do we pick?

I think the container of containers case meets 90% or more of all use
cases. A container of containers of containers can be done manually by
applying it twice.

Even trees, surely they provide some kind of iteration already?
(Like std::map/std::set). So what is special here?

Mathias Gaunard

unread,
Apr 7, 2011, 5:37:16 AM4/7/11
to boost...@lists.boost.org, bo...@lists.boost.org
On 06/04/2011 19:55, Thorsten Ottosen wrote:

> What we need is a generic range adaptor that can make a container of
> containers look like a "flat" sequence.
>
> I suggest the syntax
>
> rngOfContainers | boost::adaptors::flattened
>
> and the underlying iterator should be up to bidirectional (if the
> underlying two container types support it).
>
> Anyone interested in implementing this?

I have an implementation of this somewhere.

One annoying thing is that you cannot detect that a type is a range
reliably.

Mathias Gaunard

unread,
Apr 7, 2011, 5:47:37 AM4/7/11
to boost...@lists.boost.org, bo...@lists.boost.org
On 06/04/2011 23:14, Thorsten Ottosen wrote:
> Den 06-04-2011 22:55, Neil Groves skrev:
>
>> I've been interested in implementing something similar for a while. I
>> was thinking slightly more generally that any n-dimensional structure
>> can often be linearised or projected into a linear sequence. Hence this
>> would enable not just the above example to work, but many standard
>> algorithms could work with tree structures, hyper-cube trees, directed
>> acyclic graphs etc. I've implemented a number of these already, but
>> wanted to gain experience with them in my own projects before committing
>> them to the trunk.
>
> Cool. I have a little problem seeing how it works with acyclic graphs.
> For example, there are many ways to do this. Which one do we pick?

True, there are many ways to linearize a n-dimensional structure.
Consider a matrix, do you start with rows or with columns?


> I think the container of containers case meets 90% or more of all use
> cases. A container of containers of containers can be done manually by
> applying it twice.

For ranges of ranges (of ranges...), unless they're random access, I
don't think anything but linearizing in the "natural" order of iteration
makes sense.

Thorsten Ottosen

unread,
Apr 7, 2011, 6:56:09 AM4/7/11
to boost...@lists.boost.org, bo...@lists.boost.org
Den 07-04-2011 11:37, Mathias Gaunard skrev:
> On 06/04/2011 19:55, Thorsten Ottosen wrote:

>> I suggest the syntax
>>
>> rngOfContainers | boost::adaptors::flattened
>>
>> and the underlying iterator should be up to bidirectional (if the
>> underlying two container types support it).
>>
>> Anyone interested in implementing this?
>
> I have an implementation of this somewhere.
>
> One annoying thing is that you cannot detect that a type is a range
> reliably.

Well, that is only a problem if you want to avoid manually applying the
adaptor twice (or more), right?

If so, I think that is a limitation we can live with. A simple solution
covers most cases IMO.

-Thorsten

Mathias Gaunard

unread,
Apr 7, 2011, 7:39:17 AM4/7/11
to boost...@lists.boost.org, bo...@lists.boost.org
On 07/04/2011 12:56, Thorsten Ottosen wrote:

> Well, that is only a problem if you want to avoid manually applying the
> adaptor twice (or more), right?
>
> If so, I think that is a limitation we can live with. A simple solution
> covers most cases IMO.

Oh, the adaptor I wrote flattens recursively.

I guess just flattening the top level could be fairly easy.

Reply all
Reply to author
Forward
0 new messages