source: trunk/nv/stl/array.hh @ 375

Last change on this file since 375 was 375, checked in by epyon, 10 years ago
  • massive memory/array/string upgrades
File size: 11.2 KB
Line 
1// Copyright (C) 2014 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7/**
8 * @file array.hh
9 * @author Kornel Kisielewicz epyon@chaosforge.org
10 * @brief exception free array classes
11 */
12
13#ifndef NV_CORE_ARRAY_HH
14#define NV_CORE_ARRAY_HH
15
16#include <nv/core/common.hh>
17#include <nv/stl/memory.hh>
18#include <nv/stl/iterator.hh>
19#include <nv/stl/utility.hh>
20#include <vector>
21#include <algorithm>
22#include <array>
23
24namespace nv
25{
26        using std::vector;
27
28        template < typename T, typename Storage >
29        class array_base
30                : public detail::pointer_iterators < array_base< T, Storage >, Storage >
31        {
32        public:
33                typedef T         value_type;
34                typedef size_t    size_type;
35                typedef ptrdiff_t difference_type;
36                typedef T*        pointer;
37                typedef const T*  const_pointer;
38                typedef T*        iterator;
39                typedef const T*  const_iterator;
40                typedef T&        reference;
41                typedef const T&  const_reference;
42                typedef nv::reverse_iterator<iterator>       reverse_iterator;
43                typedef nv::reverse_iterator<const_iterator> const_reverse_iterator;
44
45                inline const_pointer data() const { return m_storage.data(); }
46                inline pointer data() { return m_storage.data(); }
47                inline size_t size() const { return m_storage.size(); }
48                inline bool empty() const { return m_storage.size() == 0; }
49                inline size_type   raw_size() const { return m_storage.size() * sizeof( value_type ); }
50                inline const char* raw_data() const { return (const char*)m_storage.data(); }
51                inline char*       raw_data() { return (char*)m_storage.data(); }
52
53                inline reference       front() { NV_ASSERT( !empty(), "front() called on empty data!" );  return m_storage.data()[0]; }
54                inline const_reference front() const { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_storage.data()[0]; }
55                inline reference       back() { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_storage.data()[size() - 1]; }
56                inline const_reference back() const { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_storage.data()[size() - 1]; }
57
58                reference operator[]( size_type i )
59                {
60                        NV_ASSERT( i < m_storage.size(), "Out of range" );
61                        return m_storage.data()[i];
62                }
63
64                const_reference operator[]( size_type i ) const
65                {
66                        NV_ASSERT( i < m_storage.size(), "Out of range" );
67                        return m_storage.data()[i];
68                }
69
70                inline void assign( const value_type& value ) { fill( value ); }
71
72                inline void fill( const value_type& value )
73                {
74                        fill_n( this->begin(), this->size(), value );
75                }
76
77                inline void clear()
78                {
79                        fill_default_n( this->begin(), this->size() );
80                }
81
82        protected:
83                Storage m_storage;
84        };
85
86        // TODO: using array =
87        template< typename T, size_t N >
88        class array : public array_base< T,
89                fixed_container_storage< static_storage< T, N > >
90        >
91        {
92
93        };
94
95#if 0
96
97        template< typename T, size_t N >
98        class array : public detail::pointer_iterators < array< T, N >, T, false >
99        {
100        public:
101                typedef T         value_type;
102                typedef size_t    size_type;
103                typedef ptrdiff_t difference_type;
104                typedef T*        pointer;
105                typedef const T*  const_pointer;
106                typedef T*        iterator;
107                typedef const T*  const_iterator;
108                typedef T&        reference;
109                typedef const T&  const_reference;
110                typedef nv::reverse_iterator<iterator>       reverse_iterator;
111                typedef nv::reverse_iterator<const_iterator> const_reverse_iterator;
112
113                static const size_type SIZE = N;
114                static const size_type ELEMENT_SIZE = sizeof( value_type );
115
116                inline const_pointer data() const { return m_data; }
117                inline pointer data() { return m_data; }
118                inline size_type size() const { return SIZE; }
119                inline bool empty() const { return false; }
120                inline size_type   raw_size() const { return SIZE * sizeof( T ); }
121                inline const char* raw_data() const { return (const char*)m_data; }
122                inline char*       raw_data() { return (char*)m_data; }
123
124                inline reference       front() { NV_ASSERT( !empty(), "front() called on empty data!" );  return m_data[0]; }
125                inline const_reference front() const { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_data[0]; }
126                inline reference       back() { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_data[SIZE - 1]; }
127                inline const_reference back() const { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_data[SIZE - 1]; }
128
129                reference operator[]( size_type i )
130                {
131                        NV_ASSERT( i < N, "Out of range" );
132                        return this->m_data[i];
133                }
134
135                const_reference operator[]( size_type i ) const
136                {     
137                        NV_ASSERT( i < N, "Out of range" );
138                        return this->m_data[i];
139                }
140
141                reference at( size_type i )
142                {
143                        NV_ASSERT( i < N, "Out of range" );
144                        return this->m_data[i];
145                }
146
147                const_reference at( size_type i ) const
148                {
149                        NV_ASSERT( i < N, "Out of range" );
150                        return this->m_data[i];
151                }
152
153                void assign( const value_type& value ) { fill( value ); }
154
155                void fill( const value_type& value )
156                {
157                        std::fill_n( this->begin(), this->size(), value );
158                }
159
160        private:
161                value_type m_data[SIZE];
162        };
163
164#endif
165//      template < typename T, typename ContainerAllocator >
166//      class vector_base
167//      {
168//      public:
169//              typedef T         value_type;
170//              typedef size_t    size_type;
171//              typedef ptrdiff_t difference_type;
172//              typedef T*        pointer;
173//              typedef const T*  const_pointer;
174//              typedef T*        iterator;
175//              typedef const T*  const_iterator;
176//              typedef T&        reference;
177//              typedef const T&  const_reference;
178//
179//      protected:
180//              ContainerAllocator m_storage;
181//      };
182
183//      template< typename T, size_t N >
184//      class static_vector : public detail::pointer_iterators < static_vector< T, N >, T, false >
185//      {
186//      public:
187//              typedef T         value_type;
188//              typedef size_t    size_type;
189//              typedef ptrdiff_t difference_type;
190//              typedef T*        pointer;
191//              typedef const T*  const_pointer;
192//              typedef T*        iterator;
193//              typedef const T*  const_iterator;
194//              typedef T&        reference;
195//              typedef const T&  const_reference;
196//              typedef nv::reverse_iterator<iterator>       reverse_iterator;
197//              typedef nv::reverse_iterator<const_iterator> const_reverse_iterator;
198//
199//              static_vector() : m_size(0) {}
200//
201//              inline const_pointer data() const { return m_data; }
202//              inline pointer data() { return m_data; }
203//              inline size_type size() const { return m_size; }
204//              inline bool empty() const { return !m_size; }
205//              inline size_type   raw_size() const { return N * sizeof( T ); }
206//              inline const char* raw_data() const { return (const char*)m_data; }
207//              inline char*       raw_data() { return (char*)m_data; }
208//
209//              inline reference       front() { NV_ASSERT( !empty(), "front() called on empty data!" );  return m_data[0]; }
210//              inline const_reference front() const { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_data[0]; }
211//              inline reference       back() { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_data[m_size - 1]; }
212//              inline const_reference back() const { NV_ASSERT( !empty(), "front() called on empty data!" ); return m_data[m_size - 1]; }
213//      protected:
214//              value_type m_data[N];
215//              size_type  m_size;
216//      };
217
218        template< typename T >
219        class dynamic_array : public detail::data_base< storage_view< T > >
220        {
221        public:
222                typedef T         value_type;
223                typedef T*        iterator;
224                typedef const T*  const_iterator;
225                typedef T&        reference;
226                typedef const T&  const_reference;
227                typedef size_t    size_type;
228                typedef ptrdiff_t difference_type;
229
230                typedef nv::reverse_iterator<iterator>       reverse_iterator;
231                typedef nv::reverse_iterator<const_iterator> const_reverse_iterator;
232
233                dynamic_array() : detail::data_base< storage_view< T > >() {}
234//                      : m_data( nullptr ), m_size(0) {}
235                explicit dynamic_array( size_type new_size ) : detail::data_base< storage_view< T > >( new value_type[new_size], new_size ) {}
236//                      : m_data( new value_type[ new_size ] ), m_size( new_size ) {}
237                dynamic_array( const value_type& value, size_type size ) : detail::data_base< storage_view< T > >()
238//                      : m_data( nullptr ), m_size(0)
239                { assign( value, size ); }
240                dynamic_array( const_iterator values, size_type size ) : detail::data_base< storage_view< T > >()
241//                      : m_data( nullptr ), m_size(0)
242                { assign( values, size ); }
243
244                void resize( size_type new_size )
245                {
246                        if ( new_size != this->size() )
247                        {
248                                value_type* old_data = this->data();
249                                value_type* new_data = new_size > 0 ? new value_type[new_size] : nullptr;
250                                if ( old_data && this->data() )
251                                {
252                                        std::copy_n( old_data, new_size > this->size() ? this->size() : new_size, new_data );
253                                }
254                                delete[] old_data;
255                                assign( new_data, new_size );
256                        }
257                }
258
259//              iterator        begin()        { return m_data; }
260//              const_iterator  begin()  const { return m_data; }
261//              const_iterator  cbegin() const { return m_data; }
262//
263//              iterator        end()        { return m_data+m_size; }
264//              const_iterator  end()  const { return m_data+m_size; }
265//              const_iterator  cend() const { return m_data+m_size; }
266//
267//              reverse_iterator rbegin()              { return reverse_iterator( end() ); }
268//              const_reverse_iterator rbegin() const  { return const_reverse_iterator( end() ); }
269//              const_reverse_iterator crbegin() const { return const_reverse_iterator( end() ); }
270//
271//              reverse_iterator rend()                { return reverse_iterator( begin() ); }
272//              const_reverse_iterator rend() const    { return const_reverse_iterator( begin() ); }
273//              const_reverse_iterator crend() const   { return const_reverse_iterator( begin() ); }
274
275                reference operator[]( size_type i )
276                {
277                        NV_ASSERT( i < this->size(), "Out of range" );
278                        return this->data()[i];
279                }
280
281                const_reference operator[]( size_type i ) const
282                {     
283                        NV_ASSERT( i < this->size(), "Out of range" );
284                        return this->data()[i];
285                }
286
287//              reference       front()       { return m_data[0]; }
288//              const_reference front() const { return m_data[0]; }
289//              reference       back()        { return m_data[m_size-1]; }
290//              const_reference back() const  { return m_data[m_size-1]; }
291//
292//              size_type        size() const     { return m_size; }
293//              bool             empty() const    { return m_size == 0; }
294//              static size_type max_size()       { return numeric_limits< size_type >::max(); }
295//              const value_type* data() const { return m_data; }
296//              value_type*       data()       { return m_data; }
297//
298//              size_type   raw_size() const { return m_size * ELEMENT_SIZE; }
299//              const char* raw_data() const { return (const char*)m_data; }
300//              char*       raw_data()       { return (char*)m_data; }
301
302                void assign( const value_type& value ) { std::fill_n( this->begin(), this->size(), value ); }
303                void assign( const value_type& value, size_type new_size )
304                {
305                        resize( new_size );
306                        std::fill_n( this->begin(), this->size(), value );
307                }
308                void assign( const_iterator values, size_type new_size )
309                {
310                        resize( new_size );
311                        std::copy_n( values, this->size(), this->data() );
312                }
313
314                ~dynamic_array() { delete[] this->data(); }
315
316                static const size_type ELEMENT_SIZE = sizeof(T);
317        };
318
319
320
321}
322
323#endif // NV_CORE_ARRAY_HH
Note: See TracBrowser for help on using the repository browser.