WSharedSequenceContainer.h 15.5 KB
Newer Older
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
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#ifndef WSHAREDSEQUENCECONTAINER_H
#define WSHAREDSEQUENCECONTAINER_H

28 29
#include <algorithm>

30 31 32 33 34 35
#include <boost/thread.hpp>

#include "WSharedObject.h"

/**
 * 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 >
39
class WSharedSequenceContainer: public WSharedObject< S >
40 41
{
public:
42 43 44 45 46 47 48 49 50 51 52 53
    // Some helpful typedefs

    /**
     * A typedef for the correct const iterator useful to traverse this sequence container.
     */
    typedef typename S::const_iterator   ConstIterator;

    /**
     * A typedef for the correct iterator to traverse this sequence container.
     */
    typedef typename S::iterator         Iterator;

54 55 56 57 58
    /**
     * The type of the elements
     */
    typedef typename S::value_type value_type;

59 60 61 62 63 64 65 66 67 68
    /**
     * Default constructor.
     */
    WSharedSequenceContainer();

    /**
     * Destructor.
     */
    virtual ~WSharedSequenceContainer();

69 70 71 72 73 74 75 76 77 78 79 80
    //////////////////////////////////////////////////////////////////////////////////////////
    // These methods implement common methods of all sequence containers. The list is not
    // complete but should be enough for now.
    // \NOTE: all methods using or returning iterators are NOT implemented here. Use the access
    // Object (getAccessObject) to iterate.
    //////////////////////////////////////////////////////////////////////////////////////////

    /**
     * Adds a new element at the end of the container.
     *
     * \param x the new element.
     */
81
    void push_back( const typename S::value_type& x );
82

83 84 85 86 87 88 89
    /**
     * Adds a new element at the beginning of the container.
     *
     * \param x the new element.
     */
    void push_front( const typename S::value_type& x );

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    /**
     * Removes an element from the end.
     */
    void pop_back();

    /**
     * Clears the container.
     */
    void clear();

    /**
     * The size of the container.
     *
     * \return the size.
     *
     * \note: be aware that the size can change at every moment after getting the size, since the read lock got freed. Better use
     * access objects to lock the container and use size() on the container directly.
     */
108
    size_t size() const;
109

110 111 112 113 114 115 116 117
    /**
     * Get item at position n. Uses the [] operator of the underlying container. Please do not use this for iteration as it locks every access.
     * Use iterators and read/write tickets for fast iteration.
     *
     * \param n the item index
     *
     * \return reference to element at the specified position
     */
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
118
    typename S::value_type& operator[]( size_t n );
119 120 121 122 123 124 125 126 127

    /**
     * Get item at position n. Uses the [] operator of the underlying container. Please do not use this for iteration as it locks every access.
     * Use iterators and read/write tickets for fast iteration.
     *
     * \param n the item index
     *
     * \return reference to element at the specified position
     */
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    const typename S::value_type& operator[]( size_t n ) const;

    /**
     * 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.
     * Use iterators and read/write tickets for fast iteration.
     *
     * \param n the item index
     *
     * \return reference to element at the specified position
     */
    typename S::value_type& at( size_t n );

    /**
     * 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.
     * Use iterators and read/write tickets for fast iteration.
     *
     * \param n the item index
     *
     * \return reference to element at the specified position
     */
    const typename S::value_type& at( size_t n ) const;
149 150

    /**
151 152
     * Searches and removes the specified element. If it is not found, nothing happens. It mainly is a comfortable forwarder for std::remove and
     * S::erase.
153 154 155
     *
     * \param element the element to remove
     */
156
    void remove( const typename S::value_type& element );
157 158 159 160 161 162 163 164

    /**
     * Erase the element at the specified position. Read your STL reference for more details.
     *
     * \param position where to erase
     *
     * \return A random access iterator pointing to the new location of the element that followed the last element erased by the function call.
     */
165
    typename WSharedSequenceContainer< S >::Iterator erase( typename WSharedSequenceContainer< S >::Iterator position );
166 167 168 169 170 171 172 173 174

    /**
     * Erase the specified range of elements. Read your STL reference for more details.
     *
     * \param first Iterators specifying a range within the vector to be removed: [first,last).
     * \param last Iterators specifying a range within the vector to be removed: [first,last).
     *
     * \return A random access iterator pointing to the new location of the element that followed the last element erased by the function call.
     */
175 176
    typename WSharedSequenceContainer< S >::Iterator erase( typename WSharedSequenceContainer< S >::Iterator first,
                                                            typename WSharedSequenceContainer< S >::Iterator last );
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    /**
     * Replaces the specified old value by a new one. If the old one does not exist, nothing happens. This is a comfortable forwarder for
     * std::replace.
     *
     * \param oldValue the old value to replace
     * \param newValue the new value
     */
    void replace( const typename S::value_type& oldValue, const typename S::value_type& newValue );

    /**
     * Counts the number of occurrences of the specified value inside the container. This is a comfortable forwarder for std::count.
     *
     * \param value the value to count
     *
     * \return the number of items found.
     */
    size_t count( const value_type& value );

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    /**
     * Resorts the container using the specified comparator from its begin to its end.
     *
     * \tparam Comparator the comparator type. Usually a boost::function or class providing the operator().
     *
     * \param comp the comparator
     */
    template < typename Comparator >
    void sort( Comparator comp );

    /**
     * Resorts the container using the specified comparator between [first,last) in ascending order.
     *
     * \param first the first element
     * \param last the last element
     * \param comp the comparator
     */
    template < typename Comparator >
    void sort( typename WSharedSequenceContainer< S >::Iterator first, typename WSharedSequenceContainer< S >::Iterator last, Comparator comp );

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    /**
     * Resorts the container using the specified comparator from its begin to its end. Uses stable sort algorithm.
     *
     * \tparam Comparator the comparator type. Usually a boost::function or class providing the operator().
     *
     * \param comp the comparator
     */
    template < typename Comparator >
    void stableSort( Comparator comp );

    /**
     * Resorts the container using the specified comparator between [first,last) in ascending order. Uses stable sort algorithm.
     *
     * \param first the first element
     * \param last the last element
     * \param comp the comparator
     */
    template < typename Comparator >
    void stableSort( typename WSharedSequenceContainer< S >::Iterator first, typename WSharedSequenceContainer< S >::Iterator last, Comparator comp );

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    /**
     * Searches the specified value in the range [first,last).
     *
     * \param first the first element
     * \param last the last element
     * \param value the value to search.
     *
     * \return the iterator pointing to the found element.
     */
    typename 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 [begin,end).
     *
     * \param value the value to search.
     *
     * \return the iterator pointing to the found element.
     */
256
    typename WSharedSequenceContainer< S >::ConstIterator find( const typename S::value_type& value );
257

258 259 260 261
protected:
private:
};

262 263
template < typename S >
WSharedSequenceContainer< S >::WSharedSequenceContainer():
264 265 266 267 268
    WSharedObject< S >()
{
    // init members
}

269 270
template < typename S >
WSharedSequenceContainer< S >::~WSharedSequenceContainer()
271 272 273 274
{
    // clean up
}

275 276
template < typename S >
void WSharedSequenceContainer< S >::push_back( const typename S::value_type& x )
277
{
278 279
    // Lock, if "a" looses focus -> look is freed
    typename WSharedObject< S >::WriteTicket a = WSharedObject< S >::getWriteTicket();
280 281 282
    a->get().push_back( x );
}

283 284 285 286 287 288 289 290
template < typename S >
void WSharedSequenceContainer< S >::push_front( const typename S::value_type& x )
{
    // Lock, if "a" looses focus -> look is freed
    typename WSharedObject< S >::WriteTicket a = WSharedObject< S >::getWriteTicket();
    a->get().insert( a->get().begin(), x );
}

291 292
template < typename S >
void WSharedSequenceContainer< S >::pop_back()
293
{
294 295
    // Lock, if "a" looses focus -> look is freed
    typename WSharedObject< S >::WriteTicket a = WSharedObject< S >::getWriteTicket();
296 297 298
    a->get().pop_back();
}

299 300
template < typename S >
void WSharedSequenceContainer< S >::clear()
301
{
302 303
    // Lock, if "a" looses focus -> look is freed
    typename WSharedObject< S >::WriteTicket a = WSharedObject< S >::getWriteTicket();
304 305 306
    a->get().clear();
}

307
template < typename S >
308
size_t WSharedSequenceContainer< S >::size() const
309
{
310 311
    // Lock, if "a" looses focus -> look is freed
    typename WSharedObject< S >::ReadTicket a = WSharedObject< S >::getReadTicket();
312
    size_t size = a->get().size();
313
    return size;
314 315
}

316 317 318 319
template < typename S >
typename S::value_type& WSharedSequenceContainer< S >::operator[]( size_t n )
{
    typename WSharedObject< S >::ReadTicket a = WSharedObject< S >::getReadTicket();
320 321
    return const_cast< S& >( a->get() ).operator[]( n );    // read tickets return the handled object const. This is bad here although in most cases
    // it is useful and needed.
322 323 324 325 326 327 328 329 330
}

template < typename S >
const typename S::value_type& WSharedSequenceContainer< S >::operator[]( size_t n ) const
{
    typename WSharedObject< S >::ReadTicket a = WSharedObject< S >::getReadTicket();
    return a->get().operator[]( n );
}

331 332 333 334
template < typename S >
typename S::value_type& WSharedSequenceContainer< S >::at( size_t n )
{
    typename WSharedObject< S >::ReadTicket a = WSharedObject< S >::getReadTicket();
335 336
    return const_cast< S& >( a->get() ).at( n );    // read tickets return the handled object const. This is bad here although in most cases it
    // is useful and needed.
337 338 339 340 341 342 343 344 345 346
}

template < typename S >
const typename S::value_type& WSharedSequenceContainer< S >::at( size_t n ) const
{
    typename WSharedObject< S >::ReadTicket a = WSharedObject< S >::getReadTicket();
    return a->get().at( n );
}

template < typename S >
347
void WSharedSequenceContainer< S >::remove( const typename S::value_type& element )
348 349 350
{
    // Lock, if "a" looses focus -> look is freed
    typename WSharedObject< S >::WriteTicket a = WSharedObject< S >::getWriteTicket();
351
    a->get().erase( std::remove( a->get().begin(), a->get().end(), element ), a->get().end() );
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
}

template < typename S >
typename WSharedSequenceContainer< S >::Iterator WSharedSequenceContainer< S >::erase( typename WSharedSequenceContainer< S >::Iterator position )
{
    // Lock, if "a" looses focus -> look is freed
    typename WSharedObject< S >::WriteTicket a = WSharedObject< S >::getWriteTicket();
    return a->get().erase( position );
}

template < typename S >
typename WSharedSequenceContainer< S >::Iterator WSharedSequenceContainer< S >::erase(
        typename WSharedSequenceContainer< S >::Iterator first,
        typename WSharedSequenceContainer< S >::Iterator last )
{
    // Lock, if "a" looses focus -> look is freed
    typename WSharedObject< S >::WriteTicket a = WSharedObject< S >::getWriteTicket();
    return a->get().erase( first, last );
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385
template < typename S >
void WSharedSequenceContainer< S >::replace( const typename S::value_type& oldValue, const typename S::value_type& newValue )
{
    typename WSharedObject< S >::WriteTicket a = WSharedObject< S >::getWriteTicket();
    std::replace( a->get().begin(), a->get().end(), oldValue, newValue );
}

template < typename S >
size_t WSharedSequenceContainer< S >::count( const value_type& value )
{
    typename WSharedObject< S >::ReadTicket a = WSharedObject< S >::getReadTicket();
    return std::count( a->get().begin(), a->get().end(), value );
}

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
template < typename S >
template < typename Comparator >
void WSharedSequenceContainer< S >::sort( Comparator comp )
{
    typename WSharedObject< S >::WriteTicket a = WSharedObject< S >::getWriteTicket();
    return std::sort( a->get().begin(), a->get().end(), comp );
}

template < typename S >
template < typename Comparator >
void WSharedSequenceContainer< S >::sort( typename WSharedSequenceContainer< S >::Iterator first,
                                          typename WSharedSequenceContainer< S >::Iterator last,
                                          Comparator comp )
{
    return std::sort( first, last, comp );
}

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
template < typename S >
template < typename Comparator >
void WSharedSequenceContainer< S >::stableSort( Comparator comp )
{
    typename WSharedObject< S >::WriteTicket a = WSharedObject< S >::getWriteTicket();
    return std::stable_sort( a->get().begin(), a->get().end(), comp );
}

template < typename S >
template < typename Comparator >
void WSharedSequenceContainer< S >::stableSort( typename WSharedSequenceContainer< S >::Iterator first,
                                                typename WSharedSequenceContainer< S >::Iterator last,
                                                Comparator comp )
{
    return std::stable_sort( first, last, comp );
}

420 421 422 423 424 425 426 427 428 429
template < typename S >
typename WSharedSequenceContainer< S >::Iterator WSharedSequenceContainer< S >::find(
        typename WSharedSequenceContainer< S >::Iterator first,
        typename WSharedSequenceContainer< S >::Iterator last,
        const typename S::value_type& value )
{
    return std::find( first, last, value );
}

template < typename S >
430
typename WSharedSequenceContainer< S >::ConstIterator WSharedSequenceContainer< S >::find( const typename S::value_type& value )
431 432 433 434 435
{
    typename WSharedObject< S >::ReadTicket a = WSharedObject< S >::getReadTicket();
    return std::find( a->get().begin(), a->get().end(), value );
}

436 437
#endif  // WSHAREDSEQUENCECONTAINER_H