Changeset 434 for trunk/nv/stl/memory.hh


Ignore:
Timestamp:
07/22/15 13:19:38 (10 years ago)
Author:
epyon
Message:
  • simplification of container hierarchy
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/stl/memory.hh

    r433 r434  
    1919#include <nv/stl/type_traits/properties.hh>
    2020#include <nv/stl/type_traits/alignment.hh>
     21#include <nv/stl/container/random_access.hh>
    2122#include <nv/stl/algorithm/fill.hh>
    2223#include <nv/stl/algorithm/copy.hh>
     
    416417        }
    417418
    418         namespace detail
    419         {
    420 
    421                 template < typename Super, bool Const = Super::is_const >
    422                 class add_iterators {};
    423 
    424                 template < typename Super >
    425                 class add_iterators < Super, true > : public Super
    426                 {
    427                 public:
    428                         typedef typename Super::value_type           value_type;
    429                         typedef const value_type*                    iterator;
    430                         typedef nv::reverse_iterator<iterator>       reverse_iterator;
    431                         typedef const value_type*                    const_iterator;
    432                         typedef nv::reverse_iterator<const_iterator> const_reverse_iterator;
    433 
    434                         using Super::Super;
    435 
    436                         inline const_iterator begin() const { return const_iterator( Super::data() ); }
    437                         inline const_iterator end() const { return const_iterator( Super::data() + Super::size() ); }
    438                         inline const_iterator cbegin() const { return const_iterator( Super::data() ); }
    439                         inline const_iterator cend() const { return const_iterator( Super::data() + Super::size() ); }
    440                         inline const_reverse_iterator rbegin() const { return const_reverse_iterator( end() ); }
    441                         inline const_reverse_iterator crbegin() const { return const_reverse_iterator( end() ); }
    442                         inline const_reverse_iterator rend() const { return const_reverse_iterator( begin() ); }
    443                         inline const_reverse_iterator crend() const { return const_reverse_iterator( begin() ); }
    444                         inline const_iterator iat( size_t i ) const { NV_ASSERT( i <= Super::size(), "Index out of range" ); return begin() + i; }
    445                 };
    446 
    447                 template < typename Super >
    448                 class add_iterators < Super, false > : public Super
    449                 {
    450                 public:
    451                         typedef typename Super::value_type           value_type;
    452                         typedef value_type*                          iterator;
    453                         typedef const value_type*                    const_iterator;
    454                         typedef nv::reverse_iterator<iterator>       reverse_iterator;
    455                         typedef nv::reverse_iterator<const_iterator> const_reverse_iterator;
    456 
    457                         using Super::Super;
    458 
    459                         inline const_iterator begin() const { return const_iterator( Super::data() ); }
    460                         inline const_iterator end() const { return const_iterator( Super::data() + Super::size() ); }
    461                         inline iterator begin() { return iterator( Super::data() ); }
    462                         inline iterator end() { return iterator( Super::data() + Super::size() ); }
    463                         inline const_iterator cbegin() const { return const_iterator( Super::data() ); }
    464                         inline const_iterator cend() const { return const_iterator( Super::data() + Super::size() ); }
    465                         inline reverse_iterator rbegin() { return reverse_iterator( end() ); }
    466                         inline const_reverse_iterator rbegin() const { return const_reverse_iterator( end() ); }
    467                         inline const_reverse_iterator crbegin() const { return const_reverse_iterator( end() ); }
    468                         inline reverse_iterator rend() { return reverse_iterator( begin() ); }
    469                         inline const_reverse_iterator rend() const { return const_reverse_iterator( begin() ); }
    470                         inline const_reverse_iterator crend() const { return const_reverse_iterator( begin() ); }
    471                         inline const_iterator iat( size_t i ) const { NV_ASSERT( i <= Super::size(), "Index out of range" ); return begin() + i; }
    472                         inline iterator       iat( size_t i ) { NV_ASSERT( i <= Super::size(), "Index out of range" ); return begin() + i; }
    473                 };
    474 
    475                 template < typename Super, bool Const = Super::is_const >
    476                 class add_random_access {};
    477 
    478                 template < typename Super >
    479                 class add_random_access < Super, true > : public Super
    480                 {
    481                 public:
    482                         typedef typename Super::value_type           value_type;
    483                         typedef typename Super::size_type            size_type;
    484                         typedef const value_type*                    const_iterator;
    485                         typedef value_type&                          reference;
    486                         typedef const value_type&                    const_reference;
    487 
    488                         using Super::Super;
    489 
    490                         inline const_reference front() const { NV_ASSERT( !Super::empty(), "front() called on empty data!" ); return Super::data()[0]; }
    491                         inline const_reference back() const { NV_ASSERT( !Super::empty(), "front() called on empty data!" ); return Super::data()[Super::size() - 1]; }
    492 
    493                         const_reference operator[]( size_type i ) const
    494                         {
    495                                 NV_ASSERT( i < Super::size(), "Out of range" );
    496                                 return Super::data()[i];
    497                         }
    498                 };
    499 
    500                 template < typename Super >
    501                 class add_random_access < Super, false > : public Super
    502                 {
    503                 public:
    504                         typedef typename Super::value_type           value_type;
    505                         typedef typename Super::size_type            size_type;
    506                         typedef value_type*                          iterator;
    507                         typedef const value_type*                    const_iterator;
    508                         typedef value_type&                          reference;
    509                         typedef const value_type&                    const_reference;
    510 
    511                         using Super::Super;
    512 
    513                         inline reference       front() { NV_ASSERT( !Super::empty(), "front() called on empty data!" );  return Super::data()[0]; }
    514                         inline const_reference front() const { NV_ASSERT( !Super::empty(), "front() called on empty data!" ); return Super::data()[0]; }
    515                         inline reference       back() { NV_ASSERT( !Super::empty(), "front() called on empty data!" ); return Super::data()[Super::size() - 1]; }
    516                         inline const_reference back() const { NV_ASSERT( !Super::empty(), "front() called on empty data!" ); return Super::data()[Super::size() - 1]; }
    517 
    518                         reference operator[]( size_type i )
    519                         {
    520                                 NV_ASSERT( i < Super::size(), "Out of range" );
    521                                 return Super::data()[i];
    522                         }
    523 
    524                         const_reference operator[]( size_type i ) const
    525                         {
    526                                 NV_ASSERT( i < Super::size(), "Out of range" );
    527                                 return Super::data()[i];
    528                         }
    529 
    530                         inline void fill( const value_type& value )
    531                         {
    532                                 fill_n( iterator( Super::data() ), iterator( Super::data() + this->size() ), value );
    533                         }
    534 
    535                 };
    536 
    537         }
    538 
    539419}
    540420
Note: See TracChangeset for help on using the changeset viewer.