I have an unordered bimap like this:
using SymPressMap =
boost::bimap<boost::bimaps::unordered_set_of<sym>,
boost::bimaps::unordered_set_of<Press>>;
Which is basically a bijection between "sym" and "Press". I want to cycle the subset of "Presses", like shown on the picture:bimap state before and after
Here is the algorithm which compiles with std::unordered_map but fails with bimap:
void Layout::cycle(SymVector syms) {
assert(syms.size() >= 2);
for (auto it = syms.rbegin(); it != syms.rend() - 1; it++) {
std::swap(sympressmap.left.at(*it), sympressmap.left.at(*(it + 1)));
}
}
The basic idea is to consecutively swap adjacent (in terms of "syms") elements. But I'm getting this error:
Error C2678 binary '=': no operator found which takes a left-hand operand of type '_Ty' (or there is no acceptable conversion)
KeyboardOptimizer c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.16.27023\include\utility 68
So, the question is How to swap two elements in bimap?.
UPD: erase-insert version thanks to John Zwinck, which compiles
void Layout::cycle(SymVector syms) {
assert(syms.size() >= 2);
Press plast = pressmap.left.at(*syms.rbegin());
pressmap.left.erase(*syms.rbegin());
for (auto it = syms.rbegin() + 1; it != syms.rend(); it++) {
auto p = pressmap.left.at(*it);
pressmap.left.erase(*it);
pressmap.left.insert(SymPressMap::left_value_type(*(it - 1), p));
}
pressmap.left.insert(SymPressMap::left_value_type(*syms.begin(), plast));
}
With a regular unordered_map, swapping the mapped_type
values is no problem because the container structure does not depend on them. But modifying the key_type
keys is a common area of difficulty and confusion which arises because the keys define the structure of the container (which values go in which buckets).
You have the same problem here, which is that you are trying to modify keys stored in the container (you're doing it in terms of swapping the values, but in a bimap of course keys and values are duals). You can't do that. What you can do is to copy the key-value pairs, swap their values, remove the originals from the container, and insert the modified pairs.