Returning map from map of maps without any copy

Discussion in 'C++' started by ittium, Dec 3, 2011.

  1. ittium

    ittium Guest

    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
     
    ittium, Dec 3, 2011
    #1
    1. Advertising

  2. ittium

    Ian Collins Guest

    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
     
    Ian Collins, Dec 3, 2011
    #2
    1. Advertising

  3. ittium

    Jorgen Grahn Guest

    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 .
     
    Jorgen Grahn, Dec 3, 2011
    #3
  4. ittium

    Goran Guest

    On Dec 3, 8:08 am, 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.


    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.
     
    Goran, Dec 3, 2011
    #4
  5. ittium

    ittium Guest

    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.
     
    ittium, Dec 8, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vlad
    Replies:
    0
    Views:
    389
  2. Simon Elliott
    Replies:
    4
    Views:
    1,195
    Simon Elliott
    Mar 10, 2005
  3. Alex
    Replies:
    2
    Views:
    1,294
  4. Marcus
    Replies:
    2
    Views:
    622
    Marcus
    Dec 9, 2005
  5. Replies:
    26
    Views:
    2,184
    Roland Pibinger
    Sep 1, 2006
Loading...

Share This Page