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 #ifndef Q_MOC_RUN
31 #include <boost/thread.hpp>
32 #endif
33 
34 #include "WSharedObject.h"
35 
36 /**
37  * This class provides a common interface for thread-safe access to sequence containers (list, vector, dequeue ).
38  * \param S the sequence container to use. Everything is allowed here which provides push_back and pop_back as well as size functionality.
39  */
40 template < typename S >
42 {
43 public:
44  // Some helpful typedefs
45 
46  /**
47  * A typedef for the correct const iterator useful to traverse this sequence container.
48  */
49  typedef typename S::const_iterator ConstIterator;
50 
51  /**
52  * A typedef for the correct iterator to traverse this sequence container.
53  */
54  typedef typename S::iterator Iterator;
55 
56  /**
57  * The type of the elements
58  */
59  typedef typename S::value_type value_type;
60 
61  /**
62  * Default constructor.
63  */
65 
66  /**
67  * Destructor.
68  */
69  virtual ~WSharedSequenceContainer();
70 
71  //////////////////////////////////////////////////////////////////////////////////////////
72  // These methods implement common methods of all sequence containers. The list is not
73  // complete but should be enough for now.
74  // \NOTE: all methods using or returning iterators are NOT implemented here. Use the access
75  // Object (getAccessObject) to iterate.
76  //////////////////////////////////////////////////////////////////////////////////////////
77 
78  /**
79  * Adds a new element at the end of the container.
80  *
81  * \param x the new element.
82  */
83  void push_back( const typename S::value_type& x );
84 
85  /**
86  * Adds a new element at the beginning of the container.
87  *
88  * \param x the new element.
89  */
90  void push_front( const typename S::value_type& x );
91 
92  /**
93  * Removes an element from the end.
94  */
95  void pop_back();
96 
97  /**
98  * Clears the container.
99  */
100  void clear();
101 
102  /**
103  * The size of the container.
104  *
105  * \return the size.
106  *
107  * \note: be aware that the size can change at every moment after getting the size, since the read lock got freed. Better use
108  * access objects to lock the container and use size() on the container directly.
109  */
110  size_t size() const;
111 
112  /**
113  * Get item at position n. Uses the [] operator of the underlying container. Please do not use this for iteration as it locks every access.
114  * Use iterators and read/write tickets for fast iteration.
115  *
116  * \param n the item index
117  *
118  * \return reference to element at the specified position
119  */
120  typename S::value_type& operator[]( size_t n );
121 
122  /**
123  * Get item at position n. Uses the [] operator of the underlying container. Please do not use this for iteration as it locks every access.
124  * Use iterators and read/write tickets for fast iteration.
125  *
126  * \param n the item index
127  *
128  * \return reference to element at the specified position
129  */
130  const typename S::value_type& operator[]( size_t n ) const;
131 
132  /**
133  * 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.
134  * Use iterators and read/write tickets for fast iteration.
135  *
136  * \param n the item index
137  *
138  * \return reference to element at the specified position
139  */
140  typename S::value_type& at( size_t n );
141 
142  /**
143  * 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.
144  * Use iterators and read/write tickets for fast iteration.
145  *
146  * \param n the item index
147  *
148  * \return reference to element at the specified position
149  */
150  const typename S::value_type& at( size_t n ) const;
151 
152  /**
153  * Searches and removes the specified element. If it is not found, nothing happens. It mainly is a comfortable forwarder for std::remove and
154  * S::erase.
155  *
156  * \param element the element to remove
157  */
158  void remove( const typename S::value_type& element );
159 
160  /**
161  * Erase the element at the specified position. Read your STL reference for more details.
162  *
163  * \param position where to erase
164  *
165  * \return A random access iterator pointing to the new location of the element that followed the last element erased by the function call.
166  */
168 
169  /**
170  * Erase the specified range of elements. Read your STL reference for more details.
171  *
172  * \param first Iterators specifying a range within the vector to be removed: [first,last).
173  * \param last Iterators specifying a range within the vector to be removed: [first,last).
174  *
175  * \return A random access iterator pointing to the new location of the element that followed the last element erased by the function call.
176  */
179 
180  /**
181  * Replaces the specified old value by a new one. If the old one does not exist, nothing happens. This is a comfortable forwarder for
182  * std::replace.
183  *
184  * \param oldValue the old value to replace
185  * \param newValue the new value
186  */
187  void replace( const typename S::value_type& oldValue, const typename S::value_type& newValue );
188 
189  /**
190  * Counts the number of occurrences of the specified value inside the container. This is a comfortable forwarder for std::count.
191  *
192  * \param value the value to count
193  *
194  * \return the number of items found.
195  */
196  size_t count( const value_type& value );
197 
198  /**
199  * Resorts the container using the specified comparator from its begin to its end.
200  *
201  * \tparam Comparator the comparator type. Usually a boost::function or class providing the operator().
202  *
203  * \param comp the comparator
204  */
205  template < typename Comparator >
206  void sort( Comparator comp );
207 
208  /**
209  * Resorts the container using the specified comparator between [first,last) in ascending order.
210  *
211  * \param first the first element
212  * \param last the last element
213  * \param comp the comparator
214  */
215  template < typename Comparator >
216  void sort( typename WSharedSequenceContainer< S >::Iterator first, typename WSharedSequenceContainer< S >::Iterator last, Comparator comp );
217 
218  /**
219  * Resorts the container using the specified comparator from its begin to its end. Uses stable sort algorithm.
220  *
221  * \tparam Comparator the comparator type. Usually a boost::function or class providing the operator().
222  *
223  * \param comp the comparator
224  */
225  template < typename Comparator >
226  void stableSort( Comparator comp );
227 
228  /**
229  * Resorts the container using the specified comparator between [first,last) in ascending order. Uses stable sort algorithm.
230  *
231  * \param first the first element
232  * \param last the last element
233  * \param comp the comparator
234  */
235  template < typename Comparator >
236  void stableSort( typename WSharedSequenceContainer< S >::Iterator first, typename WSharedSequenceContainer< S >::Iterator last, Comparator comp );
237 
238  /**
239  * Searches the specified value in the range [first,last).
240  *
241  * \param first the first element
242  * \param last the last element
243  * \param value the value to search.
244  *
245  * \return the iterator pointing to the found element.
246  */
249  const typename S::value_type& value );
250 
251  /**
252  * Searches the specified value in the range [begin,end).
253  *
254  * \param value the value to search.
255  *
256  * \return the iterator pointing to the found element.
257  */
258  typename WSharedSequenceContainer< S >::ConstIterator find( const typename S::value_type& value );
259 
260 protected:
261 private:
262 };
263 
264 template < typename S >
266  WSharedObject< S >()
267 {
268  // init members
269 }
270 
271 template < typename S >
273 {
274  // clean up
275 }
276 
277 template < typename S >
278 void WSharedSequenceContainer< S >::push_back( const typename S::value_type& x )
279 {
280  // Lock, if "a" looses focus -> look is freed
282  a->get().push_back( x );
283 }
284 
285 template < typename S >
286 void WSharedSequenceContainer< S >::push_front( const typename S::value_type& x )
287 {
288  // Lock, if "a" looses focus -> look is freed
290  a->get().insert( a->get().begin(), x );
291 }
292 
293 template < typename S >
295 {
296  // Lock, if "a" looses focus -> look is freed
298  a->get().pop_back();
299 }
300 
301 template < typename S >
303 {
304  // Lock, if "a" looses focus -> look is freed
306  a->get().clear();
307 }
308 
309 template < typename S >
311 {
312  // Lock, if "a" looses focus -> look is freed
314  size_t size = a->get().size();
315  return size;
316 }
317 
318 template < typename S >
319 typename S::value_type& WSharedSequenceContainer< S >::operator[]( size_t n )
320 {
322  return const_cast< S& >( a->get() ).operator[]( n ); // read tickets return the handled object const. This is bad here although in most cases
323  // it is useful and needed.
324 }
325 
326 template < typename S >
327 const typename S::value_type& WSharedSequenceContainer< S >::operator[]( size_t n ) const
328 {
330  return a->get().operator[]( n );
331 }
332 
333 template < typename S >
334 typename S::value_type& WSharedSequenceContainer< S >::at( size_t n )
335 {
337  return const_cast< S& >( a->get() ).at( n ); // read tickets return the handled object const. This is bad here although in most cases it
338  // is useful and needed.
339 }
340 
341 template < typename S >
342 const typename S::value_type& WSharedSequenceContainer< S >::at( size_t n ) const
343 {
345  return a->get().at( n );
346 }
347 
348 template < typename S >
349 void WSharedSequenceContainer< S >::remove( const typename S::value_type& element )
350 {
351  // Lock, if "a" looses focus -> look is freed
353  a->get().erase( std::remove( a->get().begin(), a->get().end(), element ), a->get().end() );
354 }
355 
356 template < typename S >
358 {
359  // Lock, if "a" looses focus -> look is freed
361  return a->get().erase( position );
362 }
363 
364 template < typename S >
368 {
369  // Lock, if "a" looses focus -> look is freed
371  return a->get().erase( first, last );
372 }
373 
374 template < typename S >
375 void WSharedSequenceContainer< S >::replace( const typename S::value_type& oldValue, const typename S::value_type& newValue )
376 {
378  std::replace( a->get().begin(), a->get().end(), oldValue, newValue );
379 }
380 
381 template < typename S >
383 {
385  return std::count( a->get().begin(), a->get().end(), value );
386 }
387 
388 template < typename S >
389 template < typename Comparator >
390 void WSharedSequenceContainer< S >::sort( Comparator comp )
391 {
393  return std::sort( a->get().begin(), a->get().end(), comp );
394 }
395 
396 template < typename S >
397 template < typename Comparator >
400  Comparator comp )
401 {
402  return std::sort( first, last, comp );
403 }
404 
405 template < typename S >
406 template < typename Comparator >
408 {
410  return std::stable_sort( a->get().begin(), a->get().end(), comp );
411 }
412 
413 template < typename S >
414 template < typename Comparator >
417  Comparator comp )
418 {
419  return std::stable_sort( first, last, comp );
420 }
421 
422 template < typename S >
426  const typename S::value_type& value )
427 {
428  return std::find( first, last, value );
429 }
430 
431 template < typename S >
433 {
435  return std::find( a->get().begin(), a->get().end(), value );
436 }
437 
438 #endif // WSHAREDSEQUENCECONTAINER_H
439 
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:43
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.