Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Returning map from map of maps without any copy

Reply
Thread Tools

Returning map from map of maps without any copy

 
 
ittium
Guest
Posts: n/a
 
      12-03-2011
Group,
I have a class that contains (STL) map of maps. This Class has a getMap
function that return one map out of the map of maps. I need to avoid
copies while extracting the map. There are two approaches
1. I pass a "map" reference in getMap function, the getMap method
extract appropriate map and put it in the passed map reference. It seems
copy will be made while doing
map=Extracted Map; //map is reference parameter in getMap function

2. Second option is to return a reference from the getMap function
(please ignore dangling pointer issue since this class is likely to live
forever). My doubt is
- Can I return a iterator->second as reference
- If I can return it, how can caller save it (without making a copy).

Please note that maps to be returned are big and copying them is going
to be a big overhead.'

Is there a better solution to handle this.
thanks
Ittium

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-03-2011
On 12/ 3/11 08:08 PM, ittium wrote:
> Group,
> I have a class that contains (STL) map of maps. This Class has a getMap
> function that return one map out of the map of maps. I need to avoid
> copies while extracting the map. There are two approaches
> 1. I pass a "map" reference in getMap function, the getMap method
> extract appropriate map and put it in the passed map reference. It seems
> copy will be made while doing
> map=Extracted Map; //map is reference parameter in getMap function
>
> 2. Second option is to return a reference from the getMap function
> (please ignore dangling pointer issue since this class is likely to live
> forever). My doubt is
> - Can I return a iterator->second as reference
> - If I can return it, how can caller save it (without making a copy).
>
> Please note that maps to be returned are big and copying them is going
> to be a big overhead.'
>
> Is there a better solution to handle this.


One way is to return a proxy object to a map. Some like this:

#include <iostream>
#include <map>

typedef std::map<int,int> Inner;
typedef std::map<int,Inner> Outer;

struct Container
{
Outer map;

struct OuterProxy
{
const int index;
Outer& mine;

OuterProxy( Outer& outer, int n ) : mine(outer), index(n) {}

int operator[]( int n ) { return mine[index][n]; }
};

OuterProxy operator[]( int n ) { return OuterProxy( map, n ); }
};

int main( int argc, char** argv )
{
Container container;

Inner inner;

inner[42] = 21;

container.map[2] = inner;

std::cout << container[2][42] << std::endl;

return 0;
}



--
Ian Collins
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-03-2011
On Sat, 2011-12-03, ittium wrote:
> Group,
> I have a class that contains (STL) map of maps. This Class has a getMap
> function that return one map out of the map of maps. I need to avoid
> copies while extracting the map. There are two approaches
> 1. I pass a "map" reference in getMap function, the getMap method
> extract appropriate map and put it in the passed map reference. It seems
> copy will be made while doing
> map=Extracted Map; //map is reference parameter in getMap function
>
> 2. Second option is to return a reference from the getMap function
> (please ignore dangling pointer issue since this class is likely to live
> forever). My doubt is
> - Can I return a iterator->second as reference
> - If I can return it, how can caller save it (without making a copy).
>
> Please note that maps to be returned are big and copying them is going
> to be a big overhead.'
>
> Is there a better solution to handle this.


I don't really understand. It seems irrelevant that the inner type is
also a map, and that you've wrapped the outer map in a class. So
you're basically saying:

I have a std::map<Key, Foo>
where Foo is really large and I don't want to copy it. I want to
"extract" a Foo without copying it.

You can easily get a Foo& from the map (using e.g. operator[] if you
know the key exists) -- std::map clearly supports this. So where is
the problem?

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      12-03-2011
On Dec 3, 8:08*am, ittium <(E-Mail Removed)> wrote:
> Group,
> I have a class that contains (STL) map of maps. This Class has a getMap
> function that return one map out of the map of maps. I need to avoid
> copies while extracting the map.


Your question is full of irrelevant details. Your class in "I have a
class" doesn't matter. STL doesn't matter. Map of maps doesn't matter.

There's one thing that's actually relevant, and that is: how to return
an object from a function, if the price to copy the object is
prohibitive.

When you ask like that, the answer is easy: you can't. The only way to
avoid a copy is to return a reference. That's it.

If your function needs to find something, and you deem absence of that
something a normal occurrence, you might want to return a pointer.

> There are two approaches
> 1. I pass a "map" reference in getMap function, the getMap method
> extract appropriate map and put it in the passed map reference. It seems
> copy will be made while doing
> * * * * map=Extracted Map; //map is reference parameter in getMap function
>
> 2. Second option is to return a reference from the getMap function
> (please ignore dangling pointer issue since this class is likely to live
> forever). My doubt is
> * * * * - Can I return a iterator->second as reference


Yes, provided that you don't erase the actual map entry later.

> * * * * - If I can return it, how can caller save it (without making a copy).


Only by keeping the reference. That's it. If you need to copy that
around, you'll need a pointer (p = &thing_returned_from_function).

Goran.
 
Reply With Quote
 
ittium
Guest
Posts: n/a
 
      12-08-2011
On 12/3/2011 12:38 PM, ittium wrote:
> Group,
> I have a class that contains (STL) map of maps. This Class has a getMap
> function that return one map out of the map of maps. I need to avoid
> copies while extracting the map. There are two approaches
> 1. I pass a "map" reference in getMap function, the getMap method
> extract appropriate map and put it in the passed map reference. It seems
> copy will be made while doing
> map=Extracted Map; //map is reference parameter in getMap function
>
> 2. Second option is to return a reference from the getMap function
> (please ignore dangling pointer issue since this class is likely to live
> forever). My doubt is
> - Can I return a iterator->second as reference
> - If I can return it, how can caller save it (without making a copy).
>
> Please note that maps to be returned are big and copying them is going
> to be a big overhead.'
>
> Is there a better solution to handle this.
> thanks
> Ittium
>


Thanks everyone for the explanation. I will return the appropriate map
by reference and save it in reference. This will avoid all the copies
and solve the issue.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MSN maps vs. Google maps Ralph Fox NZ Computing 0 07-30-2007 10:30 AM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
STL: Map of maps possible, but no multi-map of maps? Workarounds? Marcus C++ 2 12-09-2005 06:34 AM
std::maps within std::maps -- optimisation Simon Elliott C++ 4 03-10-2005 10:11 AM
map that maps to iterators in the same map ? Vlad C++ 0 12-15-2003 08:29 PM



Advertisments