Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #12039 > unrolled thread

Returning map from map of maps without any copy

Started byittium <ittium@gmail.com>
First post2011-12-03 12:38 +0530
Last post2011-12-08 11:29 +0530
Articles 5 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  Returning map from map of maps without any copy ittium <ittium@gmail.com> - 2011-12-03 12:38 +0530
    Re: Returning map from map of maps without any copy Ian Collins <ian-news@hotmail.com> - 2011-12-03 20:36 +1300
    Re: Returning map from map of maps without any copy Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-03 09:27 +0000
    Re: Returning map from map of maps without any copy Goran <goran.pusic@gmail.com> - 2011-12-03 02:05 -0800
    Re: Returning map from map of maps without any copy ittium <ittium@gmail.com> - 2011-12-08 11:29 +0530

#12039 — Returning map from map of maps without any copy

Fromittium <ittium@gmail.com>
Date2011-12-03 12:38 +0530
SubjectReturning map from map of maps without any copy
Message-ID<jbchsm$kac$1@dont-email.me>
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

[toc] | [next] | [standalone]


#12040

FromIan Collins <ian-news@hotmail.com>
Date2011-12-03 20:36 +1300
Message-ID<9ju1sjF570U3@mid.individual.net>
In reply to#12039
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

[toc] | [prev] | [next] | [standalone]


#12041

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2011-12-03 09:27 +0000
Message-ID<slrnjdjqsr.1bk.grahn+nntp@frailea.sa.invalid>
In reply to#12039
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   .

[toc] | [prev] | [next] | [standalone]


#12042

FromGoran <goran.pusic@gmail.com>
Date2011-12-03 02:05 -0800
Message-ID<12410feb-1dc9-443f-aca0-4c06d18014fe@cu3g2000vbb.googlegroups.com>
In reply to#12039
On Dec 3, 8:08 am, ittium <itt...@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#12145

Fromittium <ittium@gmail.com>
Date2011-12-08 11:29 +0530
Message-ID<jbpjs5$jda$1@dont-email.me>
In reply to#12039
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web