Boost logo

Boost Users :

Subject: [Boost-users] [multi-index] retrieve unique keys of a non-unique index
From: Rory McCarthy (rorymccarthy77_at_[hidden])
Date: 2013-08-01 16:47:17


Hi everyone (my apologies if this shows as a report),

In the following example is there a more efficient way to retrieve the
unique keys of a non-unique index?

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/tuple/tuple.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>

using boost::multi_index_container;
using namespace boost::multi_index;

struct name {};
struct value {};
struct guid {};

struct TaggedNodeEntry
{
  int name;
  int value;
  std::string guid;

  TaggedNodeEntry(int name_, int value_, std::string guid_) : name(name_),
value(value_), guid(guid_) {}

  friend std::ostream& operator<<(std::ostream& os, const TaggedNodeEntry&
e)
  {
    os << e.name << " " << e.value << " " << e.guid;
    return os;
  }
};

typedef multi_index_container<
  TaggedNodeEntry,
  indexed_by<
    hashed_non_unique<
      tag<name>,
      BOOST_MULTI_INDEX_MEMBER(TaggedNodeEntry, int, name)
>,
    hashed_non_unique<
      tag<value>,
      BOOST_MULTI_INDEX_MEMBER(TaggedNodeEntry, int, value)
>,
    hashed_non_unique<
      tag<guid>,
      BOOST_MULTI_INDEX_MEMBER(TaggedNodeEntry, std::string, guid)
>
>
> TaggedNodes;

int main()
{
  TaggedNodes taggedNodes;
  taggedNodes.insert(TaggedNodeEntry(1, 1, "one-one"));
  taggedNodes.insert(TaggedNodeEntry(1, 2, "one-two"));
  taggedNodes.insert(TaggedNodeEntry(1, 3, "one-three"));
  taggedNodes.insert(TaggedNodeEntry(2, 1, "two-one"));
  taggedNodes.insert(TaggedNodeEntry(2, 2, "two-two"));
  taggedNodes.insert(TaggedNodeEntry(3, 6, "three-six"));

  std::cout << std::endl << "Unique names :-" << std::endl;
  boost::multi_index::index<TaggedNodes, name>::type::iterator ic0 =
get<name>(taggedNodes).begin();
  boost::multi_index::index<TaggedNodes, name>::type::iterator ic1 =
get<name>(taggedNodes).end();
  while (ic0 != ic1)
  {
    std::cout << ic0->name << std::endl;
    ic0 = get<name>(taggedNodes).equal_range(ic0->name).second;
  }

  return 0;
}



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