3 #ifndef __CDS_IMPL_BOUNDED_ARRAY_H
4 #define __CDS_IMPL_BOUNDED_ARRAY_H
7 Dynamic non-growing array
10 2008.03.08 Maxim.Khiszinsky Created
13 #include <cds/details/allocator.h>
21 /// Upper bounded dynamic array
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
26 \par Template parameters
27 - \p T type of elements
28 - \p Allocator dynamic memory allocator class (<tt>std::allocator</tt> semantics)
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.
33 template <typename T, class Allocator = CDS_DEFAULT_ALLOCATOR >
34 class BoundedArray: private std::vector< T, typename Allocator::template rebind<T>::other >
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
41 typedef typename vector_type::iterator iterator ; ///< item iterator
42 typedef typename vector_type::const_iterator const_iterator ; ///< item const iterator
46 explicit BoundedArray(
47 size_t nCapacity ///< capacity
50 vector_type::resize( nCapacity );
51 assert( size() == capacity() );
54 /// Ctor with item's initialization
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
61 assert( nInitCount <= nCapacity );
62 vector_type::resize( nCapacity );
63 assign( nInitCount ? nInitCount : vector_type::capacity(), init );
64 assert( size() == capacity() );
67 const value_type& operator []( size_t nItem ) const
69 return vector_type::operator[](nItem);
72 value_type& operator []( size_t nItem )
74 return vector_type::operator[](nItem);
79 return vector_type::size();
82 size_t capacity() const
84 return vector_type::capacity();
88 static size_t itemSize()
93 /// Returns pointer to the first item in the array
96 return & vector_type::front();
99 friend value_type * operator +( BoundedArray<value_type, allocator_type>& arr, size_t i )
104 /// Get begin iterator
105 const_iterator begin() const
107 return vector_type::begin();
111 return vector_type::begin();
115 const_iterator end() const
117 return vector_type::end();
121 return vector_type::end();
124 /// Get end iterator for \p nMax-th item
125 const_iterator end( size_t nMax ) const
127 assert( nMax <= vector_type::capacity());
128 return vector_type::begin() + nMax;
130 iterator end( size_t nMax )
132 assert( nMax <= vector_type::capacity());
133 return vector_type::begin() + nMax;
137 /// Bounded dynamic array
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.
143 \par Template parameters
144 - \p T type of elements
145 - \p Allocator dynamic memory allocator class (<tt>std::allocator</tt> semantics)
148 template <typename T, class Allocator = CDS_DEFAULT_ALLOCATOR >
152 typedef T value_type ; ///< value type stored in the array
153 typedef Allocator allocator_type ; ///< allocator type
155 typedef value_type * iterator ; ///< item iterator
156 typedef value_type const * const_iterator ; ///< item const iterator
159 typedef cds::details::Allocator< T, allocator_type> allocator_impl;
162 const size_t m_nCapacity;
166 explicit bounded_array(
167 size_t nCapacity ///< capacity
169 : m_arr( allocator_impl().NewArray( nCapacity ) )
170 , m_nCapacity( nCapacity )
175 allocator_impl().Delete( m_arr, capacity() );
178 const value_type& operator []( size_t nItem ) const
180 assert( nItem < capacity() );
184 value_type& operator []( size_t nItem )
186 assert( nItem < capacity() );
190 size_t size() const CDS_NOEXCEPT
195 size_t capacity() const CDS_NOEXCEPT
200 /// Returns pointer to the first item in the array
206 /// Get begin iterator
207 const_iterator begin() const CDS_NOEXCEPT
211 iterator begin() CDS_NOEXCEPT
217 const_iterator end() const CDS_NOEXCEPT
219 return begin() + capacity();
221 iterator end() CDS_NOEXCEPT
223 return begin() + capacity();
227 } // namespace details
231 #endif // #ifndef __CDS_IMPL_BOUNDED_ARRAY_H