Is it possible to override "find" and "erase" methods of boost::bimaps::bimap.left? How to do it?

806 Views Asked by At

I have following:

struct foo_and_number_helper {
  std::string foo;
  uint64_t number;
};
struct foo_and_number {};
struct bar {};

using my_bimap = boost::bimaps::bimap<
  boost::bimaps::unordered_set_of<boost::bimaps::tagged<foo_and_number_helper, foo_and_number>>, 
  boost::bimaps::multiset_of<boost::bimaps::tagged<std::string, bar>>
>;

my_bimap instance;

and I want to be able to call find and erase methods like this:
instance.left.find("foo") instead of instance.left.find({"foo",1}) and
instance.left.erase("foo") instead of instance.left.erase({"foo",1}).

I just want to use only the "foo" part of "foo_and_number_helper" instead of both parts for methods find and erase called from the left side. How to achieve that? I tried to read the bimap implementation, but it's still hard for me to do it.

I already asked more broad question: Is C++ bimap possible with one side of view having different key than other side of the view value? How to do that? and from the comments I have to override operator <, but I am not even sure about that and if it's enough.

2

There are 2 best solutions below

3
On

I'd go with boost::multi_index_container over boost::bimap here.

namespace bmi = boost::multi_index;

struct ElementType { 
  std::string foo; 
  std::string bar;
  uint64_t number; 
}

using my_bimap = boost::multi_index_container<
  ElementType,
  bmi::indexed_by<
    bmi::unordered_unique<
      bmi::tagged<struct Foo>, 
      bmi::member<ElementType, std::string, &ElementType::foo>
    >,
    bmi::ordered<
      bmi::tagged<struct Bar>, 
      bmi::member<ElementType, std::string, &ElementType::bar>
    >,
    // and others like
    bmi::sequenced<
      bmi::tagged<struct InsertionOrder>
    >
  >
>;

You would then use it like

my_bimap instance;

instance.get<Foo>().find("foo");
instance.get<Bar>().erase("bar");
std::cout << instance.get<InsertionOrder>()[10].foo;

I.e. rather than having a left and right view, you have any number of views

0
On

So I followed @Caleth's answer and tweaked it:

#include <boost/multi_index/hashed_index.hpp>
#include <boost/bimap/bimap.hpp>

using namespace std;

struct ElementType { 
  string foo; 
  string bar;
  uint64_t number; 
};

using namespace boost::multi_index;

using my_bimap = multi_index_container<
  ElementType,
  indexed_by<
    hashed_unique<member<ElementType, string, &ElementType::foo>>,
    ordered_non_unique<member<ElementType, string, &ElementType::bar>>
  >
>;

int main() {
  my_bimap instance;

  instance.insert({"foo", "bar", 0});
  instance.insert({"bar", "bar", 1});

  cout << instance.get<0>().find("bar")->foo << endl;
  cout << instance.get<0>().find("bar")->bar << endl;
  cout << instance.get<0>().find("bar")->number << endl;
  auto range = instance.get<1>().equal_range("bar");
  for (auto it = range.first; it != range.second; ++it) {
    cout << it->foo << endl;
    cout << it->number << endl;
  }

  cin.sync();
  cin.ignore();
}

Output:

bar
bar
1
foo
0
bar
1

So yes, it doesn't answer my question, but I think I achieved what I wanted.