SurgSim::DataStructures::Groups< Key, T > Class Template Reference

Class to wrap grouping operations, gives access to the members of a group and the groups of members. More...

`#include <SurgSim/DataStructures/Groups.h>`

## Public Types

typedef Key | IdentifierType
typedef T | MemberType

## Public Member Functions

bool | add (const Key &group, const T &element)
| Add an element to the given group, if the group doesn't exist it will be created, if the element is already a member of the group, nothing happens. More...
bool | add (const std::vector< Key > &groups, const T &element)
| Add a member to the given groups, if any of the groups don't exist they will be created, if the element is already a member of a group, it won't be added to that specific group. More...
bool | add (const Groups< Key, T > &other)
| Add all the members from the other group to this group, essentially forming a union of the two. More...
bool | remove (const Key &group, const T &element)
| Remove an element from a given group, if the group does not exist or the element is not a member of that group, nothing will happen. More...
bool | remove (const T &element)
| Remove an element from all known groups, if the element is not a member of any group, nothing happens. More...
std::vector< T > | getMembers (const Key &group) const
| Return all the members of the given group. More...
std::vector< Key > | getGroups (const T &element) const
| Return all the groups that the given member is a member of. More...
std::vector< Key > | getGroups () const
std::vector< T > | operator[] (const Key &group) const
| Return all the members of the given group. More...
void | clear ()
| Erases all entries. More...

## Private Types

typedef boost::shared_lock< boost::shared_mutex > | SharedLock
typedef boost::unique_lock< boost::shared_mutex > | UniqueLock

## Private Attributes

boost::shared_mutex | m_mutex
| The mutex used to lock for reading and writing. More...
std::unordered_map< Key, std::unordered_set< T > > | m_groups
| Map groups to members. More...
std::unordered_map< T, std::unordered_set< Key > > | m_membership
| Map members to groups. More...

## Detailed Description

### template<typename Key, typename T>
class SurgSim::DataStructures::Groups< Key, T >

Class to wrap grouping operations, gives access to the members of a group and the groups of members.

Groups is threadsafe with regard to add and remove operations, and observations

Template Parameters

  Key | the label to be used for the groups
  T | the type of the group members

## Member Typedef Documentation

## IdentifierType

template<typename Key, typename T>

typedef Key SurgSim::DataStructures::Groups< Key, T >::IdentifierType

## MemberType

template<typename Key, typename T>

typedef T SurgSim::DataStructures::Groups< Key, T >::MemberType

## SharedLock

template<typename Key, typename T>

typedef boost::shared_lock<boost::shared_mutex> SurgSim::DataStructures::Groups< Key, T >::SharedLock

private

## UniqueLock

template<typename Key, typename T>

typedef boost::unique_lock<boost::shared_mutex> SurgSim::DataStructures::Groups< Key, T >::UniqueLock

private

## Member Function Documentation

## add() [1/3]

template<typename Key, typename T>

bool SurgSim::DataStructures::Groups< Key, T >::add | ( | const Key & | group,
|  | const T & | element
| ) |  |

Add an element to the given group, if the group doesn't exist it will be created, if the element is already a member of the group, nothing happens.

Parameters

  group | the group to use
  element | the element to add

Returns
  true if the element was actually added to the group

## add() [2/3]

template<typename Key, typename T>

bool SurgSim::DataStructures::Groups< Key, T >::add | ( | const std::vector< Key > & | groups,
|  | const T & | element
| ) |  |

Add a member to the given groups, if any of the groups don't exist they will be created, if the element is already a member of a group, it won't be added to that specific group.

Parameters

  groups | the groups to use
  element | the element to add

Returns
  true if the element was added to at least one group

## add() [3/3]

template<typename Key, typename T>

bool SurgSim::DataStructures::Groups< Key, T >::add | ( | const Groups< Key, T > & | other | ) |

Add all the members from the other group to this group, essentially forming a union of the two.

Parameters

  other | object to add groups from

Returns
  true if at least one new element was added

## clear()

template<typename Key , typename T >

void SurgSim::DataStructures::Groups< Key, T >::clear | ( |  | ) |

Erases all entries.

## getGroups() [1/2]

template<typename Key , typename T>

std::vector< Key > SurgSim::DataStructures::Groups< Key, T >::getGroups | ( | const T & | element | ) | const

Return all the groups that the given member is a member of.

Parameters

  element | the element to query

Returns
  groups which contain the given element, empty if the element is not member of any group

## getGroups() [2/2]

template<typename Key , typename T>

std::vector< Key > SurgSim::DataStructures::Groups< Key, T >::getGroups | ( |  | ) | const

Returns
  all the known groups that have members

## getMembers()

template<typename Key, typename T >

std::vector< T > SurgSim::DataStructures::Groups< Key, T >::getMembers | ( | const Key & | group | ) | const

Return all the members of the given group.

Parameters

  group | the group to query

Returns
  members of the given group, empty if the group has no members, or doesn't exist

## operator[]()

template<typename Key, typename T >

std::vector< T > SurgSim::DataStructures::Groups< Key, T >::operator[] | ( | const Key & | group | ) | const

Return all the members of the given group.

Parameters

  group | group to query

Returns
  members of the given group, empty if the group has no members

## remove() [1/2]

template<typename Key, typename T>

bool SurgSim::DataStructures::Groups< Key, T >::remove | ( | const Key & | group,
|  | const T & | element
| ) |  |

Remove an element from a given group, if the group does not exist or the element is not a member of that group, nothing will happen.

Parameters

  group | the group to use
  element | the element to remove

Returns
  true if the element was member of that group

## remove() [2/2]

template<typename Key, typename T>

bool SurgSim::DataStructures::Groups< Key, T >::remove | ( | const T & | element | ) |

Remove an element from all known groups, if the element is not a member of any group, nothing happens.

Parameters

  element | the element to remove

Returns
  true if there was an actual removal that was executed

## Member Data Documentation

## m_groups

template<typename Key, typename T>

std::unordered_map<Key, std::unordered_set<T> > SurgSim::DataStructures::Groups< Key, T >::m_groups

private

Map groups to members.

## m_membership

template<typename Key, typename T>

std::unordered_map<T, std::unordered_set<Key> > SurgSim::DataStructures::Groups< Key, T >::m_membership

private

Map members to groups.

## m_mutex

template<typename Key, typename T>

boost::shared_mutex SurgSim::DataStructures::Groups< Key, T >::m_mutex

mutable private

The mutex used to lock for reading and writing.

The documentation for this class was generated from the following files:

- SurgSim/DataStructures/Groups.h
- SurgSim/DataStructures/Groups-inl.h