Boost logo

Boost Users :

From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2005-04-14 03:14:56


David Gruener <gruenedd <at> idmt.fraunhofer.de> writes:

>
> Hello,
>
> how can one swap keys in an *ordered* index of a multi index container?
> Well, you cannot say something like:
>
> blah.modify_key(blah.find(foo1), _1 = foo2);
> // from now index contains one element fewer, since
> // keys must be uniq
> blah.modify_key(blah.find(foo2), _1 = foo1);
>
> I dind't found a way in the manual to swap directly.
>

There's no particular provision for swapping keys. You
could do something like:

blah.modify_key(blah.find(foo1), _1 = unused_value);
blah.modify_key(blah.find(foo2), _1 = foo1);
blah.modify_key(blah.find(unused_value), _1 = foo2);

Providing a general key_swap facility is not as simple as it
might seem: swapping keys in an index can, in general, have
side effects on other indices --for instance, on those
indices that also depend on that particular key. Example:

multi_index_container<
  int,
  indexed_by<
    ordered_unique<identity<int> >,
    ordered_unique<identity<int> >
>
>

Swapping keys in index #0 has also an impact on index #1.

Best regards,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net