OpenWalnut  1.4.0
WSharedSequenceContainer.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WSHAREDSEQUENCECONTAINER_H
26 #define WSHAREDSEQUENCECONTAINER_H
27 
28 #include <algorithm>
29 
30 #include <boost/thread.hpp>
31 
32 #include "WSharedObject.h"
33 
34 /**
35  * This class provides a common interface for thread-safe access to sequence containers (list, vector, dequeue ).
36  * \param S the sequence container to use. Everything is allowed here which provides push_back and pop_back as well as size functionality.
37  */
38 template < typename S >
40 {
41 public:
42  // Some helpful typedefs
43 
44  /**
45  * A typedef for the correct const iterator useful to traverse this sequence container.
46  */
47  typedef typename S::const_iterator ConstIterator;
48 
49  /**
50  * A typedef for the correct iterator to traverse this sequence container.
51  */
52  typedef typename S::iterator Iterator;
53 
54  /**
55  * The type of the elements
56  */
57  typedef typename S::value_type value_type;
58 
59  /**
60  * Default constructor.
61  */
63 
64  /**
65  * Destructor.
66  */
67  virtual ~WSharedSequenceContainer();
68 
69  //////////////////////////////////////////////////////////////////////////////////////////
70  // These methods implement common methods of all sequence containers. The list is not
71  // complete but should be enough for now.
72  // \NOTE: all methods using or returning iterators are NOT implemented here. Use the access
73  // Object (getAccessObject) to iterate.
74  //////////////////////////////////////////////////////////////////////////////////////////
75 
76  /**
77  * Adds a new element at the end of the container.
78  *
79  * \param x the new element.
80  */
81  void push_back( const typename S::value_type& x );
82 
83  /**
84  * Adds a new element at the beginning of the container.
85  *
86  * \param x the new element.
87  */
88  void push_front( const typename S::value_type& x );
89 
90  /**
91  * Removes an element from the end.
92  */
93  void pop_back();
94 
95  /**
96  * Clears the container.
97  */
98  void clear();
99 
100  /**
101  * The size of the container.
102  *
103  * \return the size.
104  *
105  * \note: be aware that the size can change at every moment after getting the size, since the read lock got freed. Better use
106  * access objects to lock the container and use size() on the container directly.
107  */
108  size_t size() const;
109 
110  /**
111  * Get item at position n. Uses the [] operator of the underlying container. Please do not use this for iteration as it locks every access.
112  * Use iterators and read/write tickets for fast iteration.
113  *
114  * \param n the item index
115  *
116  * \return reference to element at the specified position
117  */
118  typename S::value_type& operator[]( size_t n );
119 
120  /**
121  * Get item at position n. Uses the [] operator of the underlying container. Please do not use this for iteration as it locks every access.
122  * Use iterators and read/write tickets for fast iteration.
123  *
124  * \param n the item index
125  *
126  * \return reference to element at the specified position
127  */
128  const typename S::value_type& operator[]( size_t n ) const;
129 
130  /**
131  * Get item at position n. Uses the at-method of the underlying container. Please do not use this for iteration as it locks every access.
132  * Use iterators and read/write tickets for fast iteration.
133  *
134  * \param n the item index
135  *
136  * \return reference to element at the specified position
137  */
138  typename S::value_type& at( size_t n );
139 
140  /**
141  * Get item at position n. Uses the at-method of the underlying container. Please do not use this for iteration as it locks every access.
142  * Use iterators and read/write tickets for fast iteration.
143  *
144  * \param n the item index
145  *
146  * \return reference to element at the specified position
147  */
148  const typename S::value_type& at( size_t n ) const;
149 
150  /**
151  * Searches and removes the specified element. If it is not found, nothing happens. It mainly is a comfortable forwarder for std::remove and
152  * S::erase.
153  *
154  * \param element the element to remove
155  */
156  void remove( const typename S::value_type& element );
157 
158  /**
159  * Erase the element at the specified position. Read your STL reference for more details.
160  *
161  * \param position where to erase
162  *
163  * \return A random access iterator pointing to the new location of the element that followed the last element erased by the function call.
164  */
166 
167  /**
168  * Erase the specified range of elements. Read your STL reference for more details.
169  *
170  * \param first Iterators specifying a range within the vector to be removed: [first,last).
171  * \param last Iterators specifying a range within the vector to be removed: [first,last).
172  *
173  * \return A random access iterator pointing to the new location of the element that followed the last element erased by the function call.
174  */
177 
178  /**
179  * Replaces the specified old value by a new one. If the old one does not exist, nothing happens. This is a comfortable forwarder for
180  * std::replace.
181  *
182  * \param oldValue the old value to replace
183  * \param newValue the new value
184  */
185  void replace( const typename S::value_type& oldValue, const typename S::value_type& newValue );
186 
187  /**
188  * Counts the number of occurrences of the specified value inside the container. This is a comfortable forwarder for std::count.
189  *
190  * \param value the value to count
191  *
192  * \return the number of items found.
193  */
194  size_t count( const value_type& value );
195 
196  /**
197  * Resorts the container using the specified comparator from its begin to its end.
198  *
199  * \tparam Comparator the comparator type. Usually a boost::function or class providing the operator().
200  *
201  * \param comp the comparator
202  */
203  template < typename Comparator >
204  void sort( Comparator comp );
205 
206  /**
207  * Resorts the container using the specified comparator between [first,last) in ascending order.
208  *
209  * \param first the first element
210  * \param last the last element
211  * \param comp the comparator
212  */
213  template < typename Comparator >
214  void sort( typename WSharedSequenceContainer< S >::Iterator first, typename WSharedSequenceContainer< S >::Iterator last, Comparator comp );
215 
216  /**
217  * Resorts the container using the specified comparator from its begin to its end. Uses stable sort algorithm.
218  *
219  * \tparam Comparator the comparator type. Usually a boost::function or class providing the operator().
220  *
221  * \param comp the comparator
222  */
223  template < typename Comparator >
224  void stableSort( Comparator comp );
225 
226  /**
227  * Resorts the container using the specified comparator between [first,last) in ascending order. Uses stable sort algorithm.
228  *
229  * \param first the first element
230  * \param last the last element
231  * \param comp the comparator
232  */
233  template < typename Comparator >
234  void stableSort( typename WSharedSequenceContainer< S >::Iterator first, typename WSharedSequenceContainer< S >::Iterator last, Comparator comp );
235 
236  /**
237  * Searches the specified value in the range [first,last).
238  *
239  * \param first the first element
240  * \param last the last element
241  * \param value the value to search.
242  *
243  * \return the iterator pointing to the found element.
244  */
247  const typename S::value_type& value );
248 
249  /**
250  * Searches the specified value in the range [begin,end).
251  *
252  * \param value the value to search.
253  *
254  * \return the iterator pointing to the found element.
255  */
256  typename WSharedSequenceContainer< S >::ConstIterator find( const typename S::value_type& value );
257 
258 protected:
259 private:
260 };
261 
262 template < typename S >
264  WSharedObject< S >()
265 {
266  // init members
267 }
268 
269 template < typename S >
271 {
272  // clean up
273 }
274 
275 template < typename S >
276 void WSharedSequenceContainer< S >::push_back( const typename S::value_type& x )
277 {
278  // Lock, if "a" looses focus -> look is freed
280  a->get().push_back( x );
281 }
282 
283 template < typename S >
284 void WSharedSequenceContainer< S >::push_front( const typename S::value_type& x )
285 {
286  // Lock, if "a" looses focus -> look is freed
288  a->get().insert( a->get().begin(), x );
289 }
290 
291 template < typename S >
293 {
294  // Lock, if "a" looses focus -> look is freed
296  a->get().pop_back();
297 }
298 
299 template < typename S >
301 {
302  // Lock, if "a" looses focus -> look is freed
304  a->get().clear();
305 }
306 
307 template < typename S >
309 {
310  // Lock, if "a" looses focus -> look is freed
312  size_t size = a->get().size();
313  return size;
314 }
315 
316 template < typename S >
317 typename S::value_type& WSharedSequenceContainer< S >::operator[]( size_t n )
318 {
320  return const_cast< S& >( a->get() ).operator[]( n ); // read tickets return the handled object const. This is bad here although in most cases
321  // it is useful and needed.
322 }
323 
324 template < typename S >
325 const typename S::value_type& WSharedSequenceContainer< S >::operator[]( size_t n ) const
326 {
328  return a->get().operator[]( n );
329 }
330 
331 template < typename S >
332 typename S::value_type& WSharedSequenceContainer< S >::at( size_t n )
333 {
335  return const_cast< S& >( a->get() ).at( n ); // read tickets return the handled object const. This is bad here although in most cases it
336  // is useful and needed.
337 }
338 
339 template < typename S >
340 const typename S::value_type& WSharedSequenceContainer< S >::at( size_t n ) const
341 {
343  return a->get().at( n );
344 }
345 
346 template < typename S >
347 void WSharedSequenceContainer< S >::remove( const typename S::value_type& element )
348 {
349  // Lock, if "a" looses focus -> look is freed
351  a->get().erase( std::remove( a->get().begin(), a->get().end(), element ), a->get().end() );
352 }
353 
354 template < typename S >
356 {
357  // Lock, if "a" looses focus -> look is freed
359  return a->get().erase( position );
360 }
361 
362 template < typename S >
366 {
367  // Lock, if "a" looses focus -> look is freed
369  return a->get().erase( first, last );
370 }
371 
372 template < typename S >
373 void WSharedSequenceContainer< S >::replace( const typename S::value_type& oldValue, const typename S::value_type& newValue )
374 {
376  std::replace( a->get().begin(), a->get().end(), oldValue, newValue );
377 }
378 
379 template < typename S >
381 {
383  return std::count( a->get().begin(), a->get().end(), value );
384 }
385 
386 template < typename S >
387 template < typename Comparator >
388 void WSharedSequenceContainer< S >::sort( Comparator comp )
389 {
391  return std::sort( a->get().begin(), a->get().end(), comp );
392 }
393 
394 template < typename S >
395 template < typename Comparator >
398  Comparator comp )
399 {
400  return std::sort( first, last, comp );
401 }
402 
403 template < typename S >
404 template < typename Comparator >
406 {
408  return std::stable_sort( a->get().begin(), a->get().end(), comp );
409 }
410 
411 template < typename S >
412 template < typename Comparator >
415  Comparator comp )
416 {
417  return std::stable_sort( first, last, comp );
418 }
419 
420 template < typename S >
424  const typename S::value_type& value )
425 {
426  return std::find( first, last, value );
427 }
428 
429 template < typename S >
431 {
433  return std::find( a->get().begin(), a->get().end(), value );
434 }
435 
436 #endif // WSHAREDSEQUENCECONTAINER_H
437 
void pop_back()
Removes an element from the end.
void push_back(const typename S::value_type &x)
Adds a new element at the end of the container.
S::value_type & at(size_t n)
Get item at position n.
WSharedSequenceContainer< S >::Iterator erase(typename WSharedSequenceContainer< S >::Iterator position)
Erase the element at the specified position.
void sort(Comparator comp)
Resorts the container using the specified comparator from its begin to its end.
size_t size() const
The size of the container.
This class provides a common interface for thread-safe access to sequence containers (list...
WSharedSequenceContainer()
Default constructor.
ReadTicket getReadTicket() const
Returns a ticket to get read access to the contained data.
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.
S::const_iterator ConstIterator
A typedef for the correct const iterator useful to traverse this sequence container.
void clear()
Clears the container.
void replace(const typename S::value_type &oldValue, const typename S::value_type &newValue)
Replaces the specified old value by a new one.
virtual ~WSharedSequenceContainer()
Destructor.
void remove(const typename S::value_type &element)
Searches and removes the specified element.
Wrapper around an object/type for thread safe sharing of objects among multiple threads.
Definition: WSharedObject.h:41
WSharedSequenceContainer< S >::Iterator find(typename WSharedSequenceContainer< S >::Iterator first, typename WSharedSequenceContainer< S >::Iterator last, const typename S::value_type &value)
Searches the specified value in the range [first,last).
S::value_type value_type
The type of the elements.
S::value_type & operator[](size_t n)
Get item at position n.
void push_front(const typename S::value_type &x)
Adds a new element at the beginning of the container.
S::iterator Iterator
A typedef for the correct iterator to traverse this sequence container.
size_t count(const value_type &value)
Counts the number of occurrences of the specified value inside the container.
void stableSort(Comparator comp)
Resorts the container using the specified comparator from its begin to its end.