Search code examples
c++boostmultimap

Equal keys function in boost::unordered_multimap: Is the query key guaranteed to be the first arg?


I have a boost::unordered_multimap< std::vector<int>, float>. The keys that I use to query the multimap may contain additional 0 ints that I'd like to ignore (but the keys inserted in the map never contain 0).

Example:

int main() {
  typedef std::vector<int> Vec;
  typedef boost::unordered_multimap<Vec, float, MyHash, MyEqualKeys> Map;
  Map map;

  Vec vec1;
  vec1.push_back(2);
  vec1.push_back(6);
  map.insert(Map::value_type(vec1, 4.3));
  map.insert(Map::value_type(vec1, 6.8));

  Vec queryVec;
  queryVec.push_back(2);
  queryVec.push_back(0); // additional 0, to be ignored                                                                                                                         
  queryVec.push_back(6);
  for (std::pair<Map::iterator, Map::iterator> iter = map.equal_range(queryVec);
       iter.first != iter.second; ++iter.first) {
    std::cout << iter.first->second << std::endl; // 4.3 and 6.8
  }
}

I wrote a hash function MyHash that ignores the 0 in the key to be hashed.

My question is: When I write MyEqualKeys, is it guaranteed that a query key (which in my case may have additional 0) is always the first argument?

So, when I write this functor:

struct MyEqualKeys {
  bool operator()(Vec const& x, Vec const& y) const {...}
};

Can only the x argument contain the additional 0?

I'd like to know because above I slightly simplified, and in reality I may have to check for more than just a 0 and it may be slightly costly to have to check the y argument too (for millions of queries).


Solution

  • No, there is no such guarantee or requirement (on any of the unordered associative containers). I'd suggest adding a 'sanitised' flag to your key objects.