swapping keys in multiindex
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. --David
David Gruener
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
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
Joaquin M Lopez Munoz wrote:
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);
Well, i did something like this (in my case unused_value is INT_MIN). But i really dont find that a clean solution. I think the container must not assume that i always have something like an "unused" value in the particular domain, it seems easy for int, but that might not be true for other types.
Providing a general key_swap facility is not as simple as it might seem
Thats what I expected. Not simple, but don't you think thats possible, do you? I think we need a swap() for values too, because a value might be used as a uniq key in another index. IMHO such a swap method is valuable. --David
David Gruener ha escrito:
Joaquin M Lopez Munoz wrote:
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);
Well, i did something like this (in my case unused_value is INT_MIN). But i really dont find that a clean solution. I think the container must not assume that i always have something like an "unused" value in the particular domain, it seems easy for int, but that might not be true for other types.
I totally agree with you; it's an ugly workaround (but at least is more than you can do with an std::container, since you retain iterator and reference stability.)
Providing a general key_swap facility is not as simple as it might seem
Thats what I expected. Not simple, but don't you think thats possible, do you?
It's possible, at the expense of fattening the internal implementation of all indices :(
I think we need a swap() for values too, because a value might be used as a uniq key in another index. IMHO such a swap method is valuable.
From the point of view of the internal implementation, a value swap is entirely equivalent to a key swap (same relationship as between modify and modify_key, internally the implementation is the same.) The practical usefulness of a value swap AFAICS is limited, though, since it only would have any important effect on sequenced indices, and for those you can use relocate().
In any case, I'm writing this down into my list of things to consider. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (3)
-
David Gruener
-
Joaquin M Lopez Munoz
-
Joaquín Mª López Muñoz