opensurgsim
Groups-inl.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_INL_H
17 #define SURGSIM_DATASTRUCTURES_GROUPS_INL_H
18 
19 namespace SurgSim
20 {
21 namespace DataStructures
22 {
23 
24 template <typename Key, typename T>
25 bool Groups<Key, T>::add(const Key& group, const T& element)
26 {
27  UniqueLock lock(m_mutex);
28  auto result = m_groups[group].insert(element);
29  if (result.second == true)
30  {
31  m_membership[element].insert(group);
32  }
33  return result.second;
34 }
35 
36 
37 template <typename Key, typename T>
38 bool Groups<Key, T>::add(const std::vector<Key>& groups, const T& element)
39 {
40  bool result = false;
41  for (auto& group : groups)
42  {
43  result = add(group, element) || result;
44  }
45  return result;
46 }
47 
48 
49 template <typename Key, typename T>
51 {
52  bool result = false;
53  for (auto& members : other.m_membership)
54  {
55  result = add(std::vector<Key>(members.second.begin(), members.second.end()), members.first) || result;
56  }
57  return result;
58 }
59 
60 
61 template <typename Key, typename T>
62 bool Groups<Key, T>::remove(const Key& group, const T& element)
63 {
64  bool result = false;
65  UniqueLock lock(m_mutex);
66  auto found = m_groups.find(group);
67  if (found != m_groups.end())
68  {
69  auto count = found->second.erase(element);
70  if (count > 0)
71  {
72  if (found->second.empty())
73  {
74  m_groups.erase(group);
75  }
76 
77  m_membership[element].erase(group);
78 
79  if (m_membership[element].empty())
80  {
81  m_membership.erase(element);
82  }
83 
84  result = true;
85  }
86  }
87  return result;
88 }
89 
90 template <typename Key, typename T>
91 std::vector<T> Groups<Key, T>::getMembers(const Key& group) const
92 {
93  std::vector<T> result;
94  SharedLock lock(m_mutex);
95  auto found = m_groups.find(group);
96  if (found != m_groups.end())
97  {
98  result.assign(found->second.cbegin(), found->second.cend());
99  }
100  return result;
101 }
102 
103 template <typename Key, typename T>
104 std::vector<Key> Groups<Key, T>::getGroups(const T& element) const
105 {
106  std::vector<Key> result;
107  SharedLock lock(m_mutex);
108  auto found = m_membership.find(element);
109  if (found != m_membership.end())
110  {
111  result.assign(found->second.cbegin(), found->second.cend());
112  }
113  return result;
114 }
115 
116 template <typename Key, typename T>
117 std::vector<Key> Groups<Key, T>::getGroups() const
118 {
119  std::vector<Key> result;
120  {
121  SharedLock lock(m_mutex);
122  std::for_each(m_groups.cbegin(), m_groups.cend(),
123  [&result](const std::pair<Key, std::unordered_set<T>>& value)
124  {
125  result.emplace(result.end(), value.first);
126  });
127  }
128  return result;
129 }
130 
131 template <typename Key, typename T>
132 bool Groups<Key, T>::remove(const T& element)
133 {
134  bool result = false;
135  UniqueLock lock(m_mutex);
136  if (m_membership.find(element) != m_membership.end())
137  {
138  for (auto& group : m_membership[element])
139  {
140  m_groups[group].erase(element);
141  }
142  m_membership.erase(element);
143  result = true;
144  }
145  return result;
146 }
147 
148 template <typename Key, typename T>
149 std::vector<T> Groups<Key, T>::operator[](const Key& group) const
150 {
151  // Get member does the locking, not needed here
152  return getMembers(group);
153 }
154 
155 
156 template <typename Key, typename T>
158 {
159  UniqueLock lock(m_mutex);
160  m_groups.clear();
161  m_membership.clear();
162 }
163 
164 
165 }
166 }
167 
168 #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
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 t...
Definition: Groups-inl.h:62