Changeset 374 for trunk/nv/stl/array.hh


Ignore:
Timestamp:
05/26/15 17:35:06 (10 years ago)
Author:
epyon
Message:
  • MASSIVE commit
  • common.hh - size_t, ptrdiff_t, nv:: namespace, NV_ALIGN_OF and basic template tools
  • STL - algorithm.hh, iterator.hh, limits.hh, numeric.hh and type_info.hh
  • STL - updates to memory, array and string
File:
1 edited

Legend:

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

    r368 r374  
    1616#include <nv/core/common.hh>
    1717#include <nv/stl/memory.hh>
     18#include <nv/stl/iterator.hh>
     19#include <nv/stl/utility.hh>
    1820#include <vector>
    1921#include <algorithm>
     
    2325{
    2426        using std::vector;
    25         using std::array;
    26 
    27         template< class T, std::size_t N >
    28         class static_array : public detail::data_base< T, false, N >
     27
     28        template< typename T, size_t N >
     29        class array : public detail::pointer_iterators < array< T, N >, T, false >
    2930        {
    3031        public:
    31                 typedef T              value_type;
    32                 typedef T*             iterator;
    33                 typedef const T*       const_iterator;
    34                 typedef T&             reference;
    35                 typedef const T&       const_reference;
    36                 typedef std::size_t    size_type;
    37                 typedef std::ptrdiff_t difference_type;
    38 
    39                 typedef std::reverse_iterator<iterator>       reverse_iterator;
    40                 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
    41 
    42 //              iterator        begin()        { return m_data; }
    43 //              const_iterator  begin()  const { return m_data; }
    44 //              const_iterator  cbegin() const { return m_data; }
    45 //
    46 //              iterator        end()        { return m_data+N; }
    47 //              const_iterator  end()  const { return m_data+N; }
    48 //              const_iterator  cend() const { return m_data+N; }
    49 //
    50 //              reverse_iterator rbegin()              { return reverse_iterator( end() ); }
    51 //              const_reverse_iterator rbegin() const  { return const_reverse_iterator( end() ); }
    52 //              const_reverse_iterator crbegin() const { return const_reverse_iterator( end() ); }
    53 //
    54 //              reverse_iterator rend()                { return reverse_iterator( begin() ); }
    55 //              const_reverse_iterator rend() const    { return const_reverse_iterator( begin() ); }
    56 //              const_reverse_iterator crend() const   { return const_reverse_iterator( begin() ); }
     32                typedef T         value_type;
     33                typedef size_t    size_type;
     34                typedef ptrdiff_t difference_type;
     35                typedef T*        pointer;
     36                typedef const T*  const_pointer;
     37                typedef T*        iterator;
     38                typedef const T*  const_iterator;
     39                typedef T&        reference;
     40                typedef const T&  const_reference;
     41                typedef nv::reverse_iterator<iterator>       reverse_iterator;
     42                typedef nv::reverse_iterator<const_iterator> const_reverse_iterator;
     43
     44                static const size_type SIZE = N;
     45                static const size_type ELEMENT_SIZE = sizeof( value_type );
     46
     47                inline const_pointer data() const { return m_data; }
     48                inline pointer data() { return m_data; }
     49                inline size_type size() const { return SIZE; }
     50                inline bool empty() const { return false; }
     51                inline size_type   raw_size() const { return SIZE * sizeof( T ); }
     52                inline const char* raw_data() const { return (const char*)m_data; }
     53                inline char*       raw_data() { return (char*)m_data; }
     54
     55                inline reference       front() { NV_ASSERT( !empty(), "front() called on empty data!" );  return m_data[0]; }
     56                inline const_reference front() const { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_data[0]; }
     57                inline reference       back() { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_data[SIZE - 1]; }
     58                inline const_reference back() const { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_data[SIZE - 1]; }
    5759
    5860                reference operator[]( size_type i )
     
    6870                }
    6971
    70 //              reference       front()       { return m_data[0]; }
    71 //              const_reference front() const { return m_data[0]; }
    72 //              reference       back()        { return m_data[N-1]; }
    73 //              const_reference back() const  { return m_data[N-1]; }
    74 //
    75 //              static size_type size()     { return N; }
    76 //              static bool      empty()    { return false; }
    77 //              static size_type max_size() { return N; }
    78 //
    79 //              const value_type* data() const { return m_data; }
    80 //              value_type*       data()       { return m_data; }
    81 //
    82 //              size_type   raw_size() const { return N * ELEMENT_SIZE; }
    83 //              const char* raw_data() const { return (const char*)m_data; }
    84 //              char*       raw_data()       { return (char*)m_data; }
    85 
    86                 void assign( const value_type& value ) { std::fill_n( this->begin(), this->size(), value ); }
    87 
    88                 static const size_type SIZE = N;
    89                 static const size_type ELEMENT_SIZE = sizeof(T);
     72                reference at( size_type i )
     73                {
     74                        NV_ASSERT( i < N, "Out of range" );
     75                        return this->m_data[i];
     76                }
     77
     78                const_reference at( size_type i ) const
     79                {
     80                        NV_ASSERT( i < N, "Out of range" );
     81                        return this->m_data[i];
     82                }
     83
     84                void swap( array<value_type, N>& y )
     85                {
     86                        for ( size_type i = 0; i < N; ++i )
     87                                nv::swap( m_data[i], y.m_data[i] );
     88                }
     89
     90                void assign( const value_type& value ) { fill( value ); }
     91
     92                void fill( const value_type& value )
     93                {
     94                        std::fill_n( this->begin(), this->size(), value );
     95                }
     96
     97        private:
     98                value_type m_data[SIZE];
     99        };
     100
     101        template< typename T, size_t N >
     102        inline void swap( array<T, N>& lhs, array<T, N>& rhs )
     103        {
     104                lhs.swap( rhs );
     105        }
     106
     107//      template < typename T, typename ContainerAllocator >
     108//      class vector_base
     109//      {
     110//      public:
     111//              typedef T         value_type;
     112//              typedef size_t    size_type;
     113//              typedef ptrdiff_t difference_type;
     114//              typedef T*        pointer;
     115//              typedef const T*  const_pointer;
     116//              typedef T*        iterator;
     117//              typedef const T*  const_iterator;
     118//              typedef T&        reference;
     119//              typedef const T&  const_reference;
     120//
     121//      protected:
     122//              ContainerAllocator m_storage;
     123//      };
     124
     125//      template< typename T, size_t N >
     126//      class static_vector : public detail::pointer_iterators < static_vector< T, N >, T, false >
     127//      {
     128//      public:
     129//              typedef T         value_type;
     130//              typedef size_t    size_type;
     131//              typedef ptrdiff_t difference_type;
     132//              typedef T*        pointer;
     133//              typedef const T*  const_pointer;
     134//              typedef T*        iterator;
     135//              typedef const T*  const_iterator;
     136//              typedef T&        reference;
     137//              typedef const T&  const_reference;
     138//              typedef nv::reverse_iterator<iterator>       reverse_iterator;
     139//              typedef nv::reverse_iterator<const_iterator> const_reverse_iterator;
     140//
     141//              static_vector() : m_size(0) {}
     142//
     143//              inline const_pointer data() const { return m_data; }
     144//              inline pointer data() { return m_data; }
     145//              inline size_type size() const { return m_size; }
     146//              inline bool empty() const { return !m_size; }
     147//              inline size_type   raw_size() const { return N * sizeof( T ); }
     148//              inline const char* raw_data() const { return (const char*)m_data; }
     149//              inline char*       raw_data() { return (char*)m_data; }
     150//
     151//              inline reference       front() { NV_ASSERT( !empty(), "front() called on empty data!" );  return m_data[0]; }
     152//              inline const_reference front() const { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_data[0]; }
     153//              inline reference       back() { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_data[m_size - 1]; }
     154//              inline const_reference back() const { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_data[m_size - 1]; }
     155//      protected:
     156//              value_type m_data[N];
     157//              size_type  m_size;
     158//      };
     159
     160        template < typename T, typename ContainerAllocator >
     161        class array_base : public detail:: pointer_iterators < array_base< T, ContainerAllocator >, T, false >
     162        {
     163        public:
     164                typedef T         value_type;
     165                typedef size_t    size_type;
     166                typedef ptrdiff_t difference_type;
     167                typedef T*        pointer;
     168                typedef const T*  const_pointer;
     169                typedef T*        iterator;
     170                typedef const T*  const_iterator;
     171                typedef T&        reference;
     172                typedef const T&  const_reference;
     173                typedef nv::reverse_iterator<iterator>       reverse_iterator;
     174                typedef nv::reverse_iterator<const_iterator> const_reverse_iterator;
     175
     176                inline array_base() : m_storage() {}
     177                inline array_base( pointer a_data, size_t a_size )
     178                {
     179
     180                }
     181                inline const_pointer data() const { return m_storage.data(); }
     182                inline pointer data() { return m_storage.data(); }
     183                inline size_t size() const { return m_storage.size(); }
     184                inline bool empty() const { return m_storage.size() != 0; }
     185                inline size_type   raw_size() const { return sizeof( T ) * m_storage.size(); }
     186                inline const char* raw_data() const { return (const char*)m_storage.data(); }
     187                inline char*       raw_data() { return (char*)m_storage.data(); }
     188
     189                inline reference       front() { NV_ASSERT( !empty(), "front() called on empty data!" );  return m_storage.data()[0]; }
     190                inline const_reference front() const { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_storage.data()[0]; }
     191                inline reference       back() { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_storage.data()[size() - 1]; }
     192                inline const_reference back() const { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_storage.data()[size() - 1]; }
     193        protected:
     194                void push_values( size_type n, const value_type& value )
     195                {
     196
     197                }
     198                ContainerAllocator m_storage;
    90199        };
    91200
     
    94203        {
    95204        public:
    96                 typedef T              value_type;
    97                 typedef T*             iterator;
    98                 typedef const T*       const_iterator;
    99                 typedef T&             reference;
    100                 typedef const T&       const_reference;
    101                 typedef std::size_t    size_type;
    102                 typedef std::ptrdiff_t difference_type;
    103 
    104                 typedef std::reverse_iterator<iterator>       reverse_iterator;
    105                 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
     205                typedef T         value_type;
     206                typedef T*        iterator;
     207                typedef const T*  const_iterator;
     208                typedef T&        reference;
     209                typedef const T&  const_reference;
     210                typedef size_t    size_type;
     211                typedef ptrdiff_t difference_type;
     212
     213                typedef nv::reverse_iterator<iterator>       reverse_iterator;
     214                typedef nv::reverse_iterator<const_iterator> const_reverse_iterator;
    106215
    107216                dynamic_array() : detail::data_base< T, false, 0 >() {}
     
    166275//              size_type        size() const     { return m_size; }
    167276//              bool             empty() const    { return m_size == 0; }
    168 //              static size_type max_size()       { return std::numeric_limits< size_type >::max(); }
     277//              static size_type max_size()       { return numeric_limits< size_type >::max(); }
    169278//              const value_type* data() const { return m_data; }
    170279//              value_type*       data()       { return m_data; }
     
    191300        };
    192301
     302
     303
    193304}
    194305
Note: See TracChangeset for help on using the changeset viewer.