opensurgsim
Groups.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_DATASTRUCTURES_GROUPS_H
17 #define SURGSIM_DATASTRUCTURES_GROUPS_H
18 
19 #include <unordered_map>
20 #include <unordered_set>
21 #include <vector>
22 
23 #include <boost/thread.hpp>
24 
25 namespace SurgSim
26 {
27 
28 
29 namespace DataStructures
30 {
31 
36 template <typename Key, typename T>
37 class Groups
38 {
39 public:
40 
41  typedef Key IdentifierType;
42  typedef T MemberType;
43 
49  bool add(const Key& group, const T& element);
50 
56  bool add(const std::vector<Key>& groups, const T& element);
57 
61  bool add(const Groups<Key, T>& other);
62 
68  bool remove(const Key& group, const T& element);
69 
73  bool remove(const T& element);
74 
78  std::vector<T> getMembers(const Key& group) const;
79 
83  std::vector<Key> getGroups(const T& element) const;
84 
86  std::vector<Key> getGroups() const;
87 
91  std::vector<T> operator[](const Key& group) const;
92 
94  void clear();
95 
96 
97 private:
98 
99  typedef boost::shared_lock<boost::shared_mutex> SharedLock;
100  typedef boost::unique_lock<boost::shared_mutex> UniqueLock;
101 
103  mutable boost::shared_mutex m_mutex;
104 
106  std::unordered_map<Key, std::unordered_set<T>> m_groups;
107 
109  std::unordered_map<T, std::unordered_set<Key>> m_membership;
110 };
111 
112 }
113 }
114 
115 #include "SurgSim/DataStructures/Groups-inl.h"
116 
117 #endif
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
Class to wrap grouping operations, gives access to the members of a group and the groups of members...
Definition: Groups.h:37
std::vector< T > operator[](const Key &group) const
Return all the members of the given group.
Definition: Groups-inl.h:149
void clear()
Erases all entries.
Definition: Groups-inl.h:157
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
std::vector< Key > getGroups() const
Definition: Groups-inl.h:117
std::vector< T > getMembers(const Key &group) const
Return all the members of the given group.
Definition: Groups-inl.h:91