Groups.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_DATASTRUCTURES_GROUPS_H
#define SURGSIM_DATASTRUCTURES_GROUPS_H
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <boost/thread.hpp>
namespace SurgSim
{
namespace DataStructures
{
template <typename Key, typename T>
class Groups
{
public:
typedef Key IdentifierType;
typedef T MemberType;
bool add(const Key& group, const T& element);
bool add(const std::vector<Key>& groups, const T& element);
bool add(const Groups<Key, T>& other);
bool remove(const Key& group, const T& element);
bool remove(const T& element);
std::vector<T> getMembers(const Key& group) const;
std::vector<Key> getGroups(const T& element) const;
std::vector<Key> getGroups() const;
std::vector<T> operator[](const Key& group) const;
void clear();
private:
typedef boost::shared_lock<boost::shared_mutex> SharedLock;
typedef boost::unique_lock<boost::shared_mutex> UniqueLock;
mutable boost::shared_mutex m_mutex;
std::unordered_map<Key, std::unordered_set<T>> m_groups;
std::unordered_map<T, std::unordered_set<Key>> m_membership;
};
}
}
#include "SurgSim/DataStructures/Groups-inl.h"
#endif
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::DataStructures::Groups
Class to wrap grouping operations, gives access to the members of a group and the groups of members...
Definition: Groups.h:37
SurgSim::DataStructures::Groups::operator[]
std::vector< T > operator[](const Key &group) const
Return all the members of the given group.
Definition: Groups-inl.h:149
SurgSim::DataStructures::Groups::clear
void clear()
Erases all entries.
Definition: Groups-inl.h:157
Groups-inl.h
SurgSim::DataStructures::Groups::add
bool add(const Key &group, const T &element)
Add an element to the given group, if the group doesn&#39;t exist it will be created, if the element is a...
Definition: Groups-inl.h:25
SurgSim::DataStructures::Groups::getGroups
std::vector< Key > getGroups() const
Definition: Groups-inl.h:117
SurgSim::DataStructures::Groups::getMembers
std::vector< T > getMembers(const Key &group) const
Return all the members of the given group.
Definition: Groups-inl.h:91
SurgSim::DataStructures::Groups::SharedLock
boost::shared_lock< boost::shared_mutex > SharedLock
Definition: Groups.h:99
SurgSim::DataStructures::Groups::m_membership
std::unordered_map< T, std::unordered_set< Key > > m_membership
Map members to groups.
Definition: Groups.h:109
SurgSim::DataStructures::Groups::m_groups
std::unordered_map< Key, std::unordered_set< T > > m_groups
Map groups to members.
Definition: Groups.h:106
SurgSim::DataStructures::Groups::m_mutex
boost::shared_mutex m_mutex
The mutex used to lock for reading and writing.
Definition: Groups.h:103
SurgSim::DataStructures::Groups::MemberType
T MemberType
Definition: Groups.h:42
SurgSim::DataStructures::Groups::IdentifierType
Key IdentifierType
Definition: Groups.h:41
SurgSim::DataStructures::Groups::UniqueLock
boost::unique_lock< boost::shared_mutex > UniqueLock
Definition: Groups.h:100