NeuralEngine A Game Engine with embeded Machine Learning algorithms based on Gaussian Processes. |

NeThreadSafeMap.inl
1
11 template <typename Key, typename Value>
12 ThreadSafeMap<Key, Value>::~ThreadSafeMap()
13{
14}
15
16 template <typename Key, typename Value>
17 ThreadSafeMap<Key, Value>::ThreadSafeMap()
18{
19}
20
21 template <typename Key, typename Value>
22 bool ThreadSafeMap<Key, Value>::HasElements() const
23{
24 bool hasElements;
25 mMutex.lock();
26 {
27 hasElements = (mMap.size() > 0);
28 }
29 mMutex.unlock();
30 return hasElements;
31}
32
33 template <typename Key, typename Value>
34 bool ThreadSafeMap<Key, Value>::Exists(Key key) const
35{
36 bool exists;
37 mMutex.lock();
38 {
39 exists = (mMap.find(key) != mMap.end());
40 }
41 mMutex.unlock();
42 return exists;
43}
44
45 template <typename Key, typename Value>
46 void ThreadSafeMap<Key, Value>::Insert(Key key, Value value)
47{
48 mMutex.lock();
49 {
50 mMap[key] = value;
51 }
52 mMutex.unlock();
53}
54
55 template <typename Key, typename Value>
56 bool ThreadSafeMap<Key, Value>::Remove(Key key, Value& value)
57{
58 bool exists;
59 mMutex.lock();
60 {
61 auto iter = mMap.find(key);
62 if (iter != mMap.end())
63 {
64 value = iter->second;
65 mMap.erase(iter);
66 exists = true;
67 }
68 else
69 {
70 exists = false;
71 }
72 }
73 mMutex.unlock();
74 return exists;
75}
76
77 template <typename Key, typename Value>
78 void ThreadSafeMap<Key, Value>::RemoveAll()
79{
80 mMutex.lock();
81 {
82 mMap.clear();
83 }
84 mMutex.unlock();
85}
86
87 template <typename Key, typename Value>
88 bool ThreadSafeMap<Key, Value>::Get(Key key, Value& value) const
89{
90 bool exists;
91 mMutex.lock();
92 {
93 auto iter = mMap.find(key);
94 if (iter != mMap.end())
95 {
96 value = iter->second;
97 exists = true;
98 }
99 else
100 {
101 exists = false;
102 }
103 }
104 mMutex.unlock();
105 return exists;
106}
107
108 template <typename Key, typename Value>
109 void ThreadSafeMap<Key, Value>::GatherAll(std::vector<Value>& values) const
110{
111 mMutex.lock();
112 {
113 if (mMap.size() > 0)
114 {
115 values.resize(mMap.size());
116 auto viter = values.begin();
117 for (auto const& m : mMap)
118 {
119 *viter++ = m.second;
120 }
121 }
122 else
123 {
124 values.clear();
125 }
126 }
127 mMutex.unlock();
128}
NeuralEngine::ThreadSafeMap
Definition: NeThreadSafeMap.h:23