-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS_ThreadSafeList.h
More file actions
278 lines (235 loc) · 7.98 KB
/
DS_ThreadSafeList.h
File metadata and controls
278 lines (235 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#ifndef __THREAD_SAFE_LIST_H
#define __THREAD_SAFE_LIST_H
#include "DS_List.h"
#include "SimpleMutex.h"
namespace DataStructures
{
/// \brief Array based implementation of a list.
/// \note ONLY USE THIS FOR SHALLOW COPIES. I don't bother with operator= to improve performance.
template <class list_type>
class ThreadSafeList
{
private:
List<list_type> m_list;
SimpleMutex mutex;
public:
/// Copy constructor
ThreadSafeList() {}
/// \param[in] original_copy The list to duplicate
ThreadSafeList( const List<list_type>& original_copy );
ThreadSafeList( const ThreadSafeList<list_type>& original_copy );
List<list_type> *LockList();
void ReleaseList();
/// Assign one list to another
ThreadSafeList& operator = ( const List<list_type>& original_copy );
ThreadSafeList& operator = ( const ThreadSafeList<list_type>& original_copy );
/// Access an element by its index in the array
/// \param[in] position The index into the array.
/// \return The element at position \a position.
list_type& operator[] ( const unsigned int position ) const;
/// Push an element at the end of the stack
/// \param[in] input The new element.
void Push(const list_type &input);
/// Pop an element from the end of the stack
/// \pre Size()>0
/// \return The element at the end.
list_type& Pop(void);
/// Insert an element at position \a position in the list
/// \param[in] input The new element.
/// \param[in] position The position of the new element.
void Insert( const list_type &input, const unsigned int position );
/// Insert at the end of the list.
/// \param[in] input The new element.
void Insert( const list_type &input );
/// Replace the value at \a position by \a input. If the size of
/// the list is less than @em position, it increase the capacity of
/// the list and fill slot with @em filler.
/// \param[in] input The element to replace at position @em position.
/// \param[in] filler The element use to fill new allocated capacity.
/// \param[in] position The position of input in the list.
void Replace( const list_type &input, const list_type &filler, const unsigned int position );
/// Replace the last element of the list by \a input .
/// \param[in] input The element used to replace the last element.
void Replace( const list_type &input );
/// Delete the element at position \a position.
/// \param[in] position The index of the element to delete
void RemoveAtIndex( const unsigned int position );
/// Delete the element at position \a position.
/// \note - swaps middle with end of list, only use if list order does not matter
/// \param[in] position The index of the element to delete
void RemoveAtIndexFast( const unsigned int position );
/// Delete the element at the end of the list
void RemoveFromEnd(const unsigned num=1);
/// Returns the index of the specified item or MAX_UNSIGNED_LONG if not found
/// \param[in] input The element to check for
/// \return The index or position of @em input in the list.
/// \retval MAX_UNSIGNED_LONG The object is not in the list
/// \retval [Integer] The index of the element in the list
unsigned int GetIndexOf( const list_type &input );
/// \return The number of elements in the list
unsigned int Size( void ) const;
/// Clear the list
void Clear( bool doNotDeallocateSmallBlocks=false );
// Preallocate the list, so it needs fewer reallocations at runtime
void Preallocate( unsigned countNeeded );
/// Frees overallocated members, to use the minimum memory necessary
/// \attention
/// This is a slow operation
void Compress( void );
};
template <class list_type>
List<list_type> *ThreadSafeList<list_type>::LockList()
{
mutex.Lock();
return &m_list;
}
template <class list_type>
void ThreadSafeList<list_type>::ReleaseList()
{
mutex.Unlock();
}
template <class list_type>
ThreadSafeList<list_type>::ThreadSafeList( const List<list_type>& original_copy )
{
List<list_type> *list = LockList();
*list = original_copy;
ReleaseList();
}
template <class list_type>
ThreadSafeList<list_type>::ThreadSafeList( const ThreadSafeList<list_type>& original_copy )
{
List<list_type> *dest = LockList();
List<list_type> *source = original_copy.LockList();
*dest = *source;
original_copy.ReleaseList();
ReleaseList();
}
template <class list_type>
ThreadSafeList<list_type>& ThreadSafeList<list_type>::operator= ( const List<list_type>& original_copy )
{
List<list_type> *list = LockList();
*list = original_copy;
ReleaseList();
return *this;
}
template <class list_type>
ThreadSafeList<list_type>& ThreadSafeList<list_type>::operator= ( const ThreadSafeList<list_type>& original_copy )
{
List<list_type> *dest = LockList();
List<list_type> *source = original_copy.LockList();
*dest = *source;
original_copy.ReleaseList();
ReleaseList();
return *this;
}
template <class list_type>
inline list_type& ThreadSafeList<list_type>::operator[] ( const unsigned int position ) const
{
List<list_type> *list = ((ThreadSafeList<list_type> *)(this))->LockList();
list_type &result = (*list)[position];
((ThreadSafeList<list_type> *)(this))->ReleaseList();
return result;
}
template <class list_type>
void ThreadSafeList<list_type>::Push(const list_type &input)
{
List<list_type> *list = LockList();
list->Push(input);
ReleaseList();
}
template <class list_type>
inline list_type& ThreadSafeList<list_type>::Pop(void)
{
List<list_type> *list = LockList();
list_type &result = list->Pop();
ReleaseList();
return result;
}
template <class list_type>
void ThreadSafeList<list_type>::Insert( const list_type &input, const unsigned int position )
{
List<list_type> *list = LockList();
list->Insert(input, position);
ReleaseList();
}
template <class list_type>
void ThreadSafeList<list_type>::Insert( const list_type &input )
{
List<list_type> *list = LockList();
list->Insert(input);
ReleaseList();
}
template <class list_type>
inline void ThreadSafeList<list_type>::Replace( const list_type &input, const list_type &filler, const unsigned int position )
{
List<list_type> *list = LockList();
list->Replace(input, filler, position);
ReleaseList();
}
template <class list_type>
inline void ThreadSafeList<list_type>::Replace( const list_type &input )
{
List<list_type> *list = LockList();
list->Replace(input);
ReleaseList();
}
template <class list_type>
void ThreadSafeList<list_type>::RemoveAtIndex( const unsigned int position )
{
List<list_type> *list = LockList();
list->RemoveAtIndex(position);
ReleaseList();
}
template <class list_type>
void ThreadSafeList<list_type>::RemoveAtIndexFast( const unsigned int position )
{
List<list_type> *list = LockList();
list->RemoveAtIndexFast(position);
ReleaseList();
}
template <class list_type>
inline void ThreadSafeList<list_type>::RemoveFromEnd( const unsigned num )
{
List<list_type> *list = LockList();
list->RemoveFromEnd(num);
ReleaseList();
}
template <class list_type>
inline unsigned int ThreadSafeList<list_type>::Size( void ) const
{
List<list_type> *list = ((ThreadSafeList<list_type> *)(this))->LockList();
unsigned int size = list->Size();
((ThreadSafeList<list_type> *)(this))->ReleaseList();
return size;
}
template <class list_type>
void ThreadSafeList<list_type>::Clear( bool doNotDeallocateSmallBlocks )
{
List<list_type> *list = LockList();
list->Clear();
ReleaseList();
}
template <class list_type>
void ThreadSafeList<list_type>::Compress( void )
{
List<list_type> *list = LockList();
list->Compress();
ReleaseList();
}
template <class list_type>
void ThreadSafeList<list_type>::Preallocate( unsigned countNeeded )
{
List<list_type> *list = LockList();
list->Preallocate(countNeeded);
ReleaseList();
}
template <class list_type>
unsigned int ThreadSafeList<list_type>::GetIndexOf( const list_type &input )
{
List<list_type> *list = LockList();
unsigned int result = list->GetIndexOf(input);
ReleaseList();
return result;
}
} // End namespace
#endif