ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SmallVector.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File SmallVector.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
51/*--------------------------------------------------------------------------*/
52/*----------------------------- DEFINITIONS --------------------------------*/
53/*--------------------------------------------------------------------------*/
54
55#ifndef __ADS_SmallVector
56 #define __ADS_SmallVector
57/* self-identification: #endif constant */
58
59/*--------------------------------------------------------------------------*/
60/*------------------------------ INCLUDES ----------------------------------*/
61/*--------------------------------------------------------------------------*/
62
63#include "ADS/Contract.h"
65
66#include <algorithm>
67#include <cstddef>
68#include <initializer_list>
69#include <iterator>
70#include <memory>
71#include <new>
72#include <type_traits>
73#include <utility>
74
75// the items of boost::container::small_vector, with ADS_WITH_BOOST
76#if defined( ADS_WITH_BOOST ) && ! defined( ADS_SMALLVECTOR_BOOST )
77 #define ADS_SMALLVECTOR_BOOST 1
78#endif
79#ifndef ADS_SMALLVECTOR_BOOST
80 #define ADS_SMALLVECTOR_BOOST 0
81#endif
82#if ADS_SMALLVECTOR_BOOST
83 #include <boost/container/options.hpp>
84 #include <boost/container/small_vector.hpp>
85#endif
86
87/*--------------------------------------------------------------------------*/
88/*--------------------------- NAMESPACE ------------------------------------*/
89/*--------------------------------------------------------------------------*/
90
91namespace ADS
92{
93
94/*--------------------------------------------------------------------------*/
95/*-------------------------- CLASS SmallVector -----------------------------*/
96/*--------------------------------------------------------------------------*/
97
99template< class T , std::size_t N = 8 , class A = std::allocator< T > >
102 : private detail::AllocHolder< detail::RebindAlloc< A , T > >
103#endif
104{
105#if ! ADS_SMALLVECTOR_BOOST
107 using Holder = detail::AllocHolder< detail::RebindAlloc< A , T > >;
109 using Traits = std::allocator_traits< detail::RebindAlloc< A , T > >;
110#endif
111
114 static constexpr bool PLAIN =
115 detail::alwaysEqualAlloc< A > && std::is_default_constructible_v< A >;
116
117 static_assert( N >= 1 , "room for one item at least" );
118
119/*--------------------------------------------------------------------------*/
120 public:
122 using value_type = T;
124 using size_type = std::size_t;
126 using difference_type = std::ptrdiff_t;
128 using reference = T &;
130 using const_reference = const T &;
132 using pointer = T *;
134 using const_pointer = const T *;
136 using iterator = T *;
138 using const_iterator = const T *;
140 using reverse_iterator = std::reverse_iterator< iterator >;
142 using const_reverse_iterator = std::reverse_iterator< const_iterator >;
143
145#if ! ADS_SMALLVECTOR_BOOST
146 SmallVector( void ) noexcept : f_A( inner() ) { }
147#else
148 SmallVector( void ) noexcept { }
149#endif
150
152#if ! ADS_SMALLVECTOR_BOOST
153 explicit SmallVector( const A & alloc ) noexcept
154 : Holder( alloc ) , f_A( inner() ) { }
155#else
156 explicit SmallVector( const A & alloc ) noexcept
157 : f_v( typename Vector::allocator_type( alloc ) ) { }
158#endif
159
161 explicit SmallVector( std::size_t n , const T & x = T() );
162
164 SmallVector( std::initializer_list< T > items );
165
167 template< std::input_iterator I , std::sentinel_for< I > S >
168 SmallVector( I first , S last );
169
171 SmallVector( const SmallVector & other )
172 requires( PLAIN );
173
176 SmallVector( const SmallVector & other )
177 requires( ! PLAIN )
178 : SmallVector( other , detail::copyAlloc( other.get_allocator() ) ) { }
179
181 SmallVector( const SmallVector & other , const A & alloc );
182
184 SmallVector( SmallVector && other ) noexcept(
185 std::is_nothrow_move_constructible_v< T > );
186
189 SmallVector( SmallVector && other , const A & alloc );
190
193 requires( PLAIN );
194
197 requires( ! PLAIN )
198 {
199 return( detail::copyAssign( *this , other ) );
200 }
201
203 SmallVector & operator=( SmallVector && other ) noexcept(
204 std::is_nothrow_move_constructible_v< T > )
205 requires( PLAIN );
206
210 requires( ! PLAIN )
211 {
212 return( detail::moveAssign( *this , std::move( other ) ) );
213 }
214
216 using allocator_type = A;
217
219 [[nodiscard]] allocator_type get_allocator( void ) const {
220#if ! ADS_SMALLVECTOR_BOOST
221 return( allocator_type( this->alloc() ) );
222#else
223 return( allocator_type( f_v.get_allocator() ) );
224#endif
225 }
226
229
231 void swap( SmallVector & other );
232
234 [[nodiscard]] bool empty( void ) const { return( size() == 0 ); }
235
236#if ! ADS_SMALLVECTOR_BOOST
238 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
239
241 [[nodiscard]] std::size_t capacity( void ) const { return( f_cap ); }
242
244 [[nodiscard]] bool small( void ) const { return( f_A == inner() ); }
245
247 [[nodiscard]] T * data( void ) { return( f_A ); }
249 [[nodiscard]] const T * data( void ) const { return( f_A ); }
250#else
252 [[nodiscard]] std::size_t size( void ) const { return( f_v.size() ); }
253
255 [[nodiscard]] std::size_t capacity( void ) const {
256 return( f_v.capacity() );
257 }
258
261 [[nodiscard]] bool small( void ) const {
262 return( f_v.capacity() == Vector::internal_capacity() );
263 }
264
266 [[nodiscard]] T * data( void ) { return( f_v.data() ); }
268 [[nodiscard]] const T * data( void ) const { return( f_v.data() ); }
269#endif
270
272 [[nodiscard]] iterator begin( void ) { return( data() ); }
274 [[nodiscard]] iterator end( void ) { return( data() + size() ); }
276 [[nodiscard]] const_iterator begin( void ) const { return( data() ); }
278 [[nodiscard]] const_iterator end( void ) const { return( data() + size() ); }
280 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
282 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
284 [[nodiscard]] reverse_iterator rbegin( void ) {
285 return( reverse_iterator( end() ) );
286 }
287
288 [[nodiscard]] reverse_iterator rend( void ) {
289 return( reverse_iterator( begin() ) );
290 }
291
292 [[nodiscard]] const_reverse_iterator rbegin( void ) const {
293 return( const_reverse_iterator( end() ) );
294 }
295
296 [[nodiscard]] const_reverse_iterator rend( void ) const {
297 return( const_reverse_iterator( begin() ) );
298 }
299
302 [[nodiscard]] T & operator[]( std::size_t i ) {
303 ADS_PRE( i < size() );
304 return( data()[ i ] );
305 }
306
308 [[nodiscard]] const T & operator[]( std::size_t i ) const {
309 ADS_PRE( i < size() );
310 return( data()[ i ] );
311 }
312
315 [[nodiscard]] T & front( void ) {
316 ADS_PRE( ! empty() );
317 return( data()[ 0 ] );
318 }
319
321 [[nodiscard]] const T & front( void ) const {
322 ADS_PRE( ! empty() );
323 return( data()[ 0 ] );
324 }
325
327 [[nodiscard]] T & back( void ) {
328 ADS_PRE( ! empty() );
329 return( data()[ size() - 1 ] );
330 }
331
333 [[nodiscard]] const T & back( void ) const {
334 ADS_PRE( ! empty() );
335 return( data()[ size() - 1 ] );
336 }
337
340 void push_back( const T & x ) { emplace_back( x ); }
341
343 void push_back( T && x ) { emplace_back( std::move( x ) ); }
344
365 template< class... Args >
366 T & emplace_back( Args &&... args );
367
370 void pop_back( void );
371
373 void reserve( std::size_t n );
374
376 void resize( std::size_t n , const T & x = T() );
377
379 void clear( void );
380
382 [[nodiscard]] bool operator==( const SmallVector & o ) const {
383 return( std::equal( begin() , end() , o.begin() , o.end() ) );
384 }
385
386/*--------------------------------------------------------------------------*/
387 private:
388#if ! ADS_SMALLVECTOR_BOOST
389 T * inner( void ) { return( reinterpret_cast< T * >( f_room ) ); }
390 const T * inner( void ) const {
391 return( reinterpret_cast< const T * >( f_room ) );
392 }
393
396 void grow( std::size_t c );
397
399 void take( SmallVector & o );
400
401 T * f_A;
402 std::size_t f_n = 0;
403 std::size_t f_cap = N;
405 alignas( T ) unsigned char f_room[ N * sizeof( T ) ];
406#else
408 using Doubling =
409 boost::container::growth_factor< boost::container::growth_factor_100 >;
410
413 using Vector = boost::container::small_vector<
414 T , N , detail::RebindAlloc< A , T > ,
415 typename boost::container::small_vector_options< Doubling >::type >;
416
418 void renew( void ) noexcept;
419
420 Vector f_v;
421#endif
422
423 }; // end( class SmallVector )
424
425/*--------------------------------------------------------------------------*/
426
427} // end( namespace ADS )
428
429/*--------------------------------------------------------------------------*/
430/*-------------------------- IMPLEMENTATION --------------------------------*/
431/*--------------------------------------------------------------------------*/
432
433#include "ADS/sequence/impl/SmallVector.tpp"
434
435/*--------------------------------------------------------------------------*/
436
437#endif /* SmallVector.h included */
438
439/*--------------------------------------------------------------------------*/
440/*------------------------- End File SmallVector.h -------------------------*/
441/*--------------------------------------------------------------------------*/
The rules of the allocator-aware containers of the standard ([container.reqmts], [allocator....
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
#define ADS_SMALLVECTOR_BOOST
Definition SmallVector.h:80
const T & front(void) const
the first item, constant
Definition SmallVector.h:321
void swap(SmallVector &other)
exchanges the items with other
bool empty(void) const
true if there is no item
Definition SmallVector.h:234
std::ptrdiff_t difference_type
the type of the distances
Definition SmallVector.h:126
iterator end(void)
the iterator past the last item
Definition SmallVector.h:274
const T * const_iterator
the iterator on the constant items
Definition SmallVector.h:138
const_iterator begin(void) const
the constant iterator on the first item
Definition SmallVector.h:276
const_iterator cend(void) const
the constant iterator past the last item
Definition SmallVector.h:282
iterator begin(void)
the iterator on the first item
Definition SmallVector.h:272
T * pointer
a pointer to an item
Definition SmallVector.h:132
SmallVector & operator=(SmallVector &&other)
the move assignment: the memory of other if its allocator propagates or is equal, else its items move...
Definition SmallVector.h:209
void resize(std::size_t n, const T &x=T())
items, those added made from
SmallVector(std::size_t n, const T &x=T())
items made from
void push_back(const T &x)
at the end, amortized; may be an item of the vector
Definition SmallVector.h:340
const_reverse_iterator rbegin(void) const
the constant iterator on the last item, backwards
Definition SmallVector.h:292
std::size_t size(void) const
the number of items,
Definition SmallVector.h:238
bool operator==(const SmallVector &o) const
the same items in the same order
Definition SmallVector.h:382
SmallVector & operator=(const SmallVector &other)
a copy of other
~SmallVector()
destroys the items, and frees the memory of the allocator if any
SmallVector(SmallVector &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
the items of other, which is left empty
SmallVector(std::initializer_list< T > items)
the vector of the given items, in their order
reverse_iterator rbegin(void)
the iterator on the last item, backwards
Definition SmallVector.h:284
allocator_type get_allocator(void) const
the allocator of the items
Definition SmallVector.h:219
T & operator[](std::size_t i)
the item at the index
Definition SmallVector.h:302
SmallVector(const SmallVector &other, const A &alloc)
a copy of other, on alloc
std::size_t capacity(void) const
the room, items, at least
Definition SmallVector.h:241
void push_back(T &&x)
at the end, moved, as push_back() above
Definition SmallVector.h:343
T value_type
the type of the items
Definition SmallVector.h:122
const T & operator[](std::size_t i) const
the item at the index , constant
Definition SmallVector.h:308
SmallVector(I first, S last)
the items of [ first , last ), in their order
std::reverse_iterator< iterator > reverse_iterator
the iterator on the items, backwards
Definition SmallVector.h:140
SmallVector(SmallVector &&other, const A &alloc)
the items of other, which is left empty, on alloc: its memory if the allocators are equal,...
void clear(void)
removes the items, keeping the memory
A allocator_type
the type of the allocator
Definition SmallVector.h:216
SmallVector & operator=(SmallVector &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
the items of other, which is left empty
T * data(void)
the vector of the items
Definition SmallVector.h:247
SmallVector(void) noexcept
the empty vector, its items inside it
Definition SmallVector.h:146
SmallVector(const A &alloc) noexcept
the empty vector, its items inside it, the allocator alloc
Definition SmallVector.h:153
const T & const_reference
a constant reference to an item
Definition SmallVector.h:130
const_iterator cbegin(void) const
the constant iterator on the first item
Definition SmallVector.h:280
void reserve(std::size_t n)
room for items
T & reference
a reference to an item
Definition SmallVector.h:128
const T * data(void) const
the vector of the items, constant
Definition SmallVector.h:249
std::size_t size_type
the type of the sizes
Definition SmallVector.h:124
const T & back(void) const
the last item, constant
Definition SmallVector.h:333
T & front(void)
the first item
Definition SmallVector.h:315
SmallVector & operator=(const SmallVector &other)
the copy assignment, with the allocator of other if it propagates
Definition SmallVector.h:196
bool small(void) const
the items are inside the vector
Definition SmallVector.h:244
const_reverse_iterator rend(void) const
the constant iterator before the first item, backwards
Definition SmallVector.h:296
void pop_back(void)
removes the last item
std::reverse_iterator< const_iterator > const_reverse_iterator
the iterator on the constant items, backwards
Definition SmallVector.h:142
SmallVector(const SmallVector &other)
a copy of other
SmallVector(const SmallVector &other)
a copy of other, on the allocator that select_on_container_copy_construction() gives
Definition SmallVector.h:176
T & back(void)
the last item
Definition SmallVector.h:327
const T * const_pointer
a constant pointer to an item
Definition SmallVector.h:134
T & emplace_back(Args &&... args)
an item made from the arguments at the end, which may refer to items of the vector,...
reverse_iterator rend(void)
the iterator before the first item, backwards
Definition SmallVector.h:288
const_iterator end(void) const
the constant iterator past the last item
Definition SmallVector.h:278
T * iterator
the iterator on the items
Definition SmallVector.h:136
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96