Changing map keys (no reordering of keys)

Discussion in 'C++' started by alan, Nov 28, 2007.

  1. alan

    alan Guest

    Hello world,

    I currently have implemented a sparse array needed by a class as a
    map<int, boost::shared_ptr<my_type> >.
    However, one of the derived classes needs to periodically "tighten"
    the sparse array (i.e. make it non-sparse). For example:

    a[0] = 1
    a[4] = 2
    a[42] = 54

    =>

    a[0] = 1
    a[1] = 2
    a[2] = 54


    I'm currently somewhat puzzled at how I would rearrange the keys. One
    possible solution I thought would be:
    /*where inv is the member containing the map*/
    int lim = inv.size();
    map<int, boost::shared_ptr<my_type> >::iterator it = inv.begin();
    for( int i = 0; i < lim; ++i, ++it){
    inv = it->second;
    }
    /*delete everything after the last*/
    inv.erase(it, inv.end());

    However, this does create some new objects each time I need to
    tighten; granted that my current application uses only smart pointers,
    it's not a big deal, but suppose that a future map with similar
    requirements is needed, where the value is no longer a pointer but a
    reasonably costly object? Is there a better way of implementing this?
    alan, Nov 28, 2007
    #1
    1. Advertising

  2. alan wrote:
    > Hello world,
    >
    > I currently have implemented a sparse array needed by a class as a
    > map<int, boost::shared_ptr<my_type> >.
    > However, one of the derived classes needs to periodically "tighten"
    > the sparse array (i.e. make it non-sparse). For example:
    >
    > a[0] = 1
    > a[4] = 2
    > a[42] = 54
    >
    > =>
    >
    > a[0] = 1
    > a[1] = 2
    > a[2] = 54
    >
    >
    > I'm currently somewhat puzzled at how I would rearrange the keys. One
    > possible solution I thought would be:
    > /*where inv is the member containing the map*/
    > int lim = inv.size();
    > map<int, boost::shared_ptr<my_type> >::iterator it = inv.begin();
    > for( int i = 0; i < lim; ++i, ++it){
    > inv = it->second;
    > }
    > /*delete everything after the last*/
    > inv.erase(it, inv.end());


    I would probably copy everything into a new map and then simply swap
    the new one with the old one (and let the new one be disposed of).

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 28, 2007
    #2
    1. Advertising

  3. alan

    alan Guest

    On Nov 28, 10:57 pm, "Victor Bazarov" <> wrote:
    > alan wrote:
    > > Hello world,

    >
    > > I currently have implemented a sparse array needed by a class as a
    > > map<int, boost::shared_ptr<my_type> >.
    > > However, one of the derived classes needs to periodically "tighten"
    > > the sparse array (i.e. make it non-sparse). For example:

    >
    > > a[0] = 1
    > > a[4] = 2
    > > a[42] = 54

    >
    > > =>

    >
    > > a[0] = 1
    > > a[1] = 2
    > > a[2] = 54

    >
    > > I'm currently somewhat puzzled at how I would rearrange the keys. One
    > > possible solution I thought would be:
    > > /*where inv is the member containing the map*/
    > > int lim = inv.size();
    > > map<int, boost::shared_ptr<my_type> >::iterator it = inv.begin();
    > > for( int i = 0; i < lim; ++i, ++it){
    > > inv = it->second;
    > > }
    > > /*delete everything after the last*/
    > > inv.erase(it, inv.end());

    >
    > I would probably copy everything into a new map and then simply swap
    > the new one with the old one (and let the new one be disposed of).

    Hmm. I thought of that too. However, suppose for the sake of
    argument that the copying of the value objects are nontrivial (even if
    arguably, when object copying is nontrivial, you probably really want
    to use pointers of some kind). Is there some way to avoid the copy?

    It's an admittedly academic question, of course, since a pointer of
    some sort, or at least a wrapper object which copies the
    implementation object only on mutation would be better.

    >
    > > [..]

    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
    alan, Nov 28, 2007
    #3
  4. alan wrote:
    > On Nov 28, 10:57 pm, "Victor Bazarov" <> wrote:
    >> alan wrote:
    >> [..]
    >>> I'm currently somewhat puzzled at how I would rearrange the keys.
    >>> One possible solution I thought would be:
    >>> /*where inv is the member containing the map*/
    >>> int lim = inv.size();
    >>> map<int, boost::shared_ptr<my_type> >::iterator it = inv.begin();
    >>> for( int i = 0; i < lim; ++i, ++it){
    >>> inv = it->second;
    >>> }
    >>> /*delete everything after the last*/
    >>> inv.erase(it, inv.end());

    >>
    >> I would probably copy everything into a new map and then simply swap
    >> the new one with the old one (and let the new one be disposed of).

    > Hmm. I thought of that too. However, suppose for the sake of
    > argument that the copying of the value objects are nontrivial (even if
    > arguably, when object copying is nontrivial, you probably really want
    > to use pointers of some kind). Is there some way to avoid the copy?


    Store pointers to begin with. Or, you could actually introduce some
    kind of a wrapper map (instead of destroying the initial one), which
    would store pointers to the objects in the other map instead of copying
    the values. When done, destroy both maps.

    > It's an admittedly academic question, of course, since a pointer of
    > some sort, or at least a wrapper object which copies the
    > implementation object only on mutation would be better.


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 28, 2007
    #4
    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. John Wilson

    DataGrid reordering

    John Wilson, Sep 10, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    414
    Scott Allen
    Sep 10, 2005
  2. Jon Berg
    Replies:
    1
    Views:
    1,712
    Leif K-Brooks
    Feb 3, 2005
  3. DaKoadMunky
    Replies:
    4
    Views:
    867
    Thomas Matthews
    Aug 9, 2004
  4. Erik Arner
    Replies:
    0
    Views:
    1,311
    Erik Arner
    Nov 2, 2004
  5. Laszlo Zsolt Nagy

    Self reordering list in Python

    Laszlo Zsolt Nagy, Sep 15, 2005, in forum: Python
    Replies:
    11
    Views:
    2,035
    zooko
    Sep 30, 2005
Loading...

Share This Page