97a783adf758ef5f17c681f3ff676b51d1795cdd
[libcds.git] / cds / details / bounded_array.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_IMPL_BOUNDED_ARRAY_H
4 #define __CDS_IMPL_BOUNDED_ARRAY_H
5
6 /*
7     Dynamic non-growing array
8
9     Editions:
10         2008.03.08    Maxim.Khiszinsky    Created
11 */
12
13 #include <cds/details/allocator.h>
14 #include <vector>
15 #include <assert.h>
16
17 //@cond
18 namespace cds {
19     namespace details {
20
21         /// Upper bounded dynamic array
22         /**
23             BoundedArray is dynamic allocated C-array of item of type T with the interface like STL.
24             The max size (capacity) of array is defined at ctor time and cannot be changed during object's lifetime
25
26             \par Template parameters
27                 - \p T type of elements
28                 - \p Allocator dynamic memory allocator class (<tt>std::allocator</tt> semantics)
29
30             This class is deprecated: it is based on std::vector and does not support non-copyable type \p T.
31             See \ref bounded_array.
32         */
33         template <typename T, class Allocator = CDS_DEFAULT_ALLOCATOR >
34         class BoundedArray: private std::vector< T, typename Allocator::template rebind<T>::other >
35         {
36         public:
37             typedef T value_type ;    ///< value type stored in the array
38             typedef Allocator allocator_type ;  ///< allocator type
39             typedef std::vector<T, typename Allocator::template rebind<T>::other>   vector_type ; ///< underlying vector type
40
41             typedef typename vector_type::iterator          iterator        ;    ///< item iterator
42             typedef typename vector_type::const_iterator    const_iterator    ;    ///< item const iterator
43
44         public:
45             /// Default ctor
46             explicit BoundedArray(
47                 size_t nCapacity            ///< capacity
48             )
49             {
50                 vector_type::resize( nCapacity );
51                 assert( size() == capacity() );
52             }
53
54             /// Ctor with item's initialization
55             BoundedArray(
56                 size_t nCapacity,            ///< capacity of array
57                 const value_type& init,                ///< initial value of any item
58                 size_t nInitCount = 0        ///< how many items will be initialized; 0 - all items
59             )
60             {
61                 assert( nInitCount <= nCapacity );
62                 vector_type::resize( nCapacity );
63                 assign( nInitCount ? nInitCount : vector_type::capacity(), init );
64                 assert( size() == capacity() );
65             }
66
67             const value_type& operator []( size_t nItem ) const
68             {
69                 return vector_type::operator[](nItem);
70             }
71
72             value_type& operator []( size_t nItem )
73             {
74                 return vector_type::operator[](nItem);
75             }
76
77             size_t   size() const
78             {
79                 return vector_type::size();
80             }
81
82             size_t   capacity() const
83             {
84                 return vector_type::capacity();
85             }
86
87             /// Returns sizeof(T)
88             static size_t itemSize()
89             {
90                 return sizeof(T);
91             }
92
93             /// Returns pointer to the first item in the array
94             value_type * top()
95             {
96                 return & vector_type::front();
97             }
98
99             friend value_type * operator +( BoundedArray<value_type, allocator_type>& arr, size_t i )
100             {
101                 return &( arr[i] );
102             }
103
104             /// Get begin iterator
105             const_iterator  begin() const
106             {
107                 return vector_type::begin();
108             }
109             iterator        begin()
110             {
111                 return vector_type::begin();
112             }
113
114             /// Get end iterator
115             const_iterator  end() const
116             {
117                 return vector_type::end();
118             }
119             iterator        end()
120             {
121                 return vector_type::end();
122             }
123
124             /// Get end iterator for \p nMax-th item
125             const_iterator    end( size_t nMax )    const
126             {
127                 assert( nMax <= vector_type::capacity());
128                 return vector_type::begin() + nMax;
129             }
130             iterator        end( size_t nMax )
131             {
132                 assert( nMax <= vector_type::capacity());
133                 return vector_type::begin() + nMax;
134             }
135         };
136
137         /// Bounded dynamic array
138         /**
139             The class template is intended for storing fixed-size sequences of objects.
140             Array capacity is constant and cannot be changed after creation of object of the class.
141             It is suitable for managing objects of non-copyable type \p T.
142
143             \par Template parameters
144                 - \p T type of elements
145                 - \p Allocator dynamic memory allocator class (<tt>std::allocator</tt> semantics)
146
147         */
148         template <typename T, class Allocator = CDS_DEFAULT_ALLOCATOR >
149         class bounded_array
150         {
151         public:
152             typedef T value_type ;    ///< value type stored in the array
153             typedef Allocator allocator_type ;  ///< allocator type
154
155             typedef value_type *          iterator          ; ///< item iterator
156             typedef value_type const *    const_iterator    ; ///< item const iterator
157
158         private:
159             typedef cds::details::Allocator< T, allocator_type> allocator_impl;
160
161             value_type *    m_arr;
162             const size_t    m_nCapacity;
163
164         public:
165             /// Default ctor
166             explicit bounded_array(
167                 size_t nCapacity            ///< capacity
168             )
169             : m_arr( allocator_impl().NewArray( nCapacity ) )
170             , m_nCapacity( nCapacity )
171             {}
172
173             ~bounded_array()
174             {
175                 allocator_impl().Delete( m_arr, capacity() );
176             }
177
178             const value_type& operator []( size_t nItem ) const
179             {
180                 assert( nItem < capacity() );
181                 return m_arr[nItem];
182             }
183
184             value_type& operator []( size_t nItem )
185             {
186                 assert( nItem < capacity() );
187                 return m_arr[nItem];
188             }
189
190             size_t   size() const CDS_NOEXCEPT
191             {
192                 return capacity();
193             }
194
195             size_t   capacity() const CDS_NOEXCEPT
196             {
197                 return m_nCapacity;
198             }
199
200             /// Returns pointer to the first item in the array
201             value_type * top()
202             {
203                 return m_arr;
204             }
205
206             /// Get begin iterator
207             const_iterator  begin() const CDS_NOEXCEPT
208             {
209                 return m_arr;
210             }
211             iterator        begin() CDS_NOEXCEPT
212             {
213                 return m_arr;
214             }
215
216             /// Get end iterator
217             const_iterator  end() const CDS_NOEXCEPT
218             {
219                 return begin() + capacity();
220             }
221             iterator        end() CDS_NOEXCEPT
222             {
223                 return begin() + capacity();
224             }
225         };
226
227     }    // namespace details
228 }    // namespace cds
229 //@endcond
230
231 #endif    // #ifndef __CDS_IMPL_BOUNDED_ARRAY_H