]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/ptr_container/ptr_circular_buffer.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / ptr_container / ptr_circular_buffer.hpp
1 //
2 // Boost.Pointer Container
3 //
4 // Copyright Thorsten Ottosen 2008. Use, modification and
5 // distribution is subject to the Boost Software License, Version
6 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // For more information, see http://www.boost.org/libs/ptr_container/
10 //
11
12 #ifndef BOOST_PTR_CONTAINER_PTR_CIRCULAR_BUFFER_HPP
13 #define BOOST_PTR_CONTAINER_PTR_CIRCULAR_BUFFER_HPP
14
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif
18
19 #include <boost/circular_buffer.hpp>
20 #include <boost/ptr_container/ptr_sequence_adapter.hpp>
21
22 namespace boost
23 {
24
25 template
26 <
27 class T,
28 class CloneAllocator = heap_clone_allocator,
29 class Allocator = std::allocator<void*>
30 >
31 class ptr_circular_buffer : public
32 ptr_sequence_adapter< T, boost::circular_buffer<
33 typename ptr_container_detail::void_ptr<T>::type,Allocator>,
34 CloneAllocator >
35 {
36 typedef ptr_sequence_adapter< T, boost::circular_buffer<
37 typename ptr_container_detail::void_ptr<T>::type,Allocator>,
38 CloneAllocator >
39 base_type;
40
41 typedef boost::circular_buffer<typename
42 ptr_container_detail::void_ptr<T>::type,Allocator> circular_buffer_type;
43 typedef ptr_circular_buffer<T,CloneAllocator,Allocator> this_type;
44
45 public: // typedefs
46 typedef typename base_type::value_type value_type;
47 typedef value_type* pointer;
48 typedef const value_type* const_pointer;
49 typedef typename base_type::size_type size_type;
50 typedef typename base_type::allocator_type allocator_type;
51 typedef typename base_type::iterator iterator;
52 typedef typename base_type::const_iterator const_iterator;
53 typedef typename base_type::auto_type auto_type;
54
55 typedef std::pair<pointer,size_type> array_range;
56 typedef std::pair<const_pointer,size_type> const_array_range;
57 typedef typename circular_buffer_type::capacity_type capacity_type;
58
59 public: // constructors
60 ptr_circular_buffer()
61 { }
62
63 explicit ptr_circular_buffer( capacity_type n )
64 : base_type( n, ptr_container_detail::fixed_length_sequence_tag() )
65 { }
66
67 ptr_circular_buffer( capacity_type n,
68 const allocator_type& alloc )
69 : base_type( n, alloc, ptr_container_detail::fixed_length_sequence_tag() )
70 { }
71
72 template< class ForwardIterator >
73 ptr_circular_buffer( ForwardIterator first, ForwardIterator last )
74 : base_type( first, last, ptr_container_detail::fixed_length_sequence_tag() )
75 { }
76
77 template< class InputIterator >
78 ptr_circular_buffer( capacity_type n, InputIterator first, InputIterator last )
79 : base_type( n, first, last, ptr_container_detail::fixed_length_sequence_tag() )
80 { }
81
82 ptr_circular_buffer( const ptr_circular_buffer& r )
83 : base_type( r.size(), r.begin(), r.end(),
84 ptr_container_detail::fixed_length_sequence_tag() )
85 { }
86
87 template< class U >
88 ptr_circular_buffer( const ptr_circular_buffer<U>& r )
89 : base_type( r.size(), r.begin(), r.end(),
90 ptr_container_detail::fixed_length_sequence_tag() )
91 { }
92
93 ptr_circular_buffer& operator=( ptr_circular_buffer r )
94 {
95 this->swap( r );
96 return *this;
97 }
98
99 BOOST_PTR_CONTAINER_DEFINE_RELEASE_AND_CLONE( ptr_circular_buffer,
100 base_type, this_type )
101
102 public: // allocators
103 allocator_type& get_allocator()
104 {
105 return this->base().get_allocator();
106 }
107
108 allocator_type get_allocator() const
109 {
110 return this->base().get_allocator();
111 }
112
113 public: // circular buffer functions
114 array_range array_one() // nothrow
115 {
116 typename circular_buffer_type::array_range r = this->base().array_one();
117 return array_range( reinterpret_cast<pointer>(r.first), r.second );
118 }
119
120 const_array_range array_one() const // nothrow
121 {
122 typename circular_buffer_type::const_array_range r = this->base().array_one();
123 return const_array_range( reinterpret_cast<const_pointer>(r.first), r.second );
124 }
125
126 array_range array_two() // nothrow
127 {
128 typename circular_buffer_type::array_range r = this->base().array_two();
129 return array_range( reinterpret_cast<pointer>(r.first), r.second );
130 }
131
132 const_array_range array_two() const // nothrow
133 {
134 typename circular_buffer_type::const_array_range r = this->base().array_two();
135 return const_array_range( reinterpret_cast<const_pointer>(r.first), r.second );
136 }
137
138 pointer linearize() // nothrow
139 {
140 return reinterpret_cast<pointer>(this->base().linearize());
141 }
142
143 bool full() const // nothrow
144 {
145 return this->base().full();
146 }
147
148 size_type reserve() const // nothrow
149 {
150 return this->base().reserve();
151 }
152
153 void reserve( size_type n ) // strong
154 {
155 if( capacity() < n )
156 set_capacity( n );
157 }
158
159 capacity_type capacity() const // nothrow
160 {
161 return this->base().capacity();
162 }
163
164 void set_capacity( capacity_type new_capacity ) // strong
165 {
166 if( this->size() > new_capacity )
167 {
168 this->erase( this->begin() + new_capacity, this->end() );
169 }
170 this->base().set_capacity( new_capacity );
171 }
172
173 void rset_capacity( capacity_type new_capacity ) // strong
174 {
175 if( this->size() > new_capacity )
176 {
177 this->erase( this->begin(),
178 this->begin() + (this->size()-new_capacity) );
179 }
180 this->base().rset_capacity( new_capacity );
181 }
182
183 void resize( size_type size ) // basic
184 {
185 size_type old_size = this->size();
186 if( old_size > size )
187 {
188 this->erase( boost::next( this->begin(), size ), this->end() );
189 }
190 else if( size > old_size )
191 {
192 for( ; old_size != size; ++old_size )
193 this->push_back( new BOOST_DEDUCED_TYPENAME
194 boost::remove_pointer<value_type>::type() );
195 }
196
197 BOOST_ASSERT( this->size() == size );
198 }
199
200 void resize( size_type size, value_type to_clone ) // basic
201 {
202 size_type old_size = this->size();
203 if( old_size > size )
204 {
205 this->erase( boost::next( this->begin(), size ), this->end() );
206 }
207 else if( size > old_size )
208 {
209 for( ; old_size != size; ++old_size )
210 this->push_back( this->null_policy_allocate_clone( to_clone ) );
211 }
212
213 BOOST_ASSERT( this->size() == size );
214 }
215
216 void rresize( size_type size ) // basic
217 {
218 size_type old_size = this->size();
219 if( old_size > size )
220 {
221 this->erase( this->begin(),
222 boost::next( this->begin(), old_size - size ) );
223 }
224 else if( size > old_size )
225 {
226 for( ; old_size != size; ++old_size )
227 this->push_front( new BOOST_DEDUCED_TYPENAME
228 boost::remove_pointer<value_type>::type() );
229 }
230
231 BOOST_ASSERT( this->size() == size );
232 }
233
234 void rresize( size_type size, value_type to_clone ) // basic
235 {
236 size_type old_size = this->size();
237 if( old_size > size )
238 {
239 this->erase( this->begin(),
240 boost::next( this->begin(), old_size - size ) );
241 }
242 else if( size > old_size )
243 {
244 for( ; old_size != size; ++old_size )
245 this->push_front( this->null_policy_allocate_clone( to_clone ) );
246 }
247
248 BOOST_ASSERT( this->size() == size );
249 }
250
251 template< class InputIterator >
252 void assign( InputIterator first, InputIterator last ) // strong
253 {
254 ptr_circular_buffer temp( first, last );
255 this->swap( temp );
256 }
257
258 template< class Range >
259 void assign( const Range& r ) // strong
260 {
261 assign( boost::begin(r), boost::end(r ) );
262 }
263
264 void assign( size_type n, value_type to_clone ) // strong
265 {
266 ptr_circular_buffer temp( n );
267 for( size_type i = 0u; i != n; ++i )
268 temp.push_back( temp.null_policy_allocate_clone( to_clone ) );
269 this->swap( temp );
270 }
271
272 void assign( capacity_type capacity, size_type n,
273 value_type to_clone ) // basic
274 {
275 this->assign( (std::min)(n,capacity), to_clone );
276 }
277
278 template< class InputIterator >
279 void assign( capacity_type capacity,
280 InputIterator first, InputIterator last ) // basic
281 {
282 this->assign( first, last );
283 this->set_capacity( capacity );
284 }
285
286 void push_back( value_type ptr ) // nothrow
287 {
288 BOOST_ASSERT( capacity() > 0 );
289 this->enforce_null_policy( ptr, "Null pointer in 'push_back()'" );
290
291 auto_type old_ptr( value_type(), *this );
292 if( full() )
293 old_ptr.reset( &*this->begin(), *this );
294 this->base().push_back( ptr );
295 }
296
297 template< class U >
298 void push_back( std::auto_ptr<U> ptr ) // nothrow
299 {
300 push_back( ptr.release() );
301 }
302
303 void push_front( value_type ptr ) // nothrow
304 {
305 BOOST_ASSERT( capacity() > 0 );
306 this->enforce_null_policy( ptr, "Null pointer in 'push_front()'" );
307
308 auto_type old_ptr( value_type(), *this );
309 if( full() )
310 old_ptr.reset( &*(--this->end()), *this );
311 this->base().push_front( ptr );
312 }
313
314 template< class U >
315 void push_front( std::auto_ptr<U> ptr ) // nothrow
316 {
317 push_front( ptr.release() );
318 }
319
320 iterator insert( iterator pos, value_type ptr ) // nothrow
321 {
322 BOOST_ASSERT( capacity() > 0 );
323 this->enforce_null_policy( ptr, "Null pointer in 'insert()'" );
324
325 auto_type new_ptr( ptr, *this );
326 iterator b = this->begin();
327 if( full() && pos == b )
328 return b;
329
330 new_ptr.release();
331 auto_type old_ptr( value_type(), *this );
332 if( full() )
333 old_ptr.reset( &*this->begin(), *this );
334
335 return this->base().insert( pos.base(), ptr );
336 }
337
338 template< class U >
339 iterator insert( iterator pos, std::auto_ptr<U> ptr ) // nothrow
340 {
341 return insert( pos, ptr.release() );
342 }
343
344 template< class InputIterator >
345 void insert( iterator pos, InputIterator first, InputIterator last ) // basic
346 {
347 for( ; first != last; ++first, ++pos )
348 pos = insert( pos, this->null_policy_allocate_clone( &*first ) );
349 }
350
351 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
352 #else
353 template< class Range >
354 BOOST_DEDUCED_TYPENAME
355 boost::disable_if< ptr_container_detail::is_pointer_or_integral<Range> >::type
356 insert( iterator before, const Range& r )
357 {
358 insert( before, boost::begin(r), boost::end(r) );
359 }
360
361 #endif
362
363 iterator rinsert( iterator pos, value_type ptr ) // nothrow
364 {
365 BOOST_ASSERT( capacity() > 0 );
366 this->enforce_null_policy( ptr, "Null pointer in 'rinsert()'" );
367
368 auto_type new_ptr( ptr, *this );
369 iterator b = this->end();
370 if (full() && pos == b)
371 return b;
372
373 new_ptr.release();
374 auto_type old_ptr( value_type(), *this );
375 if( full() )
376 old_ptr.reset( &this->back(), *this );
377
378 return this->base().rinsert( pos.base(), ptr );
379 }
380
381 template< class U >
382 iterator rinsert( iterator pos, std::auto_ptr<U> ptr ) // nothrow
383 {
384 return rinsert( pos, ptr.release() );
385 }
386
387
388 template< class InputIterator >
389 void rinsert( iterator pos, InputIterator first, InputIterator last ) // basic
390 {
391 for( ; first != last; ++first, ++pos )
392 pos = rinsert( pos, this->null_policy_allocate_clone( &*first ) );
393 }
394
395 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
396 #else
397 template< class Range >
398 BOOST_DEDUCED_TYPENAME
399 boost::disable_if< ptr_container_detail::is_pointer_or_integral<Range> >::type
400 rinsert( iterator before, const Range& r )
401 {
402 rinsert( before, boost::begin(r), boost::end(r) );
403 }
404
405 #endif
406
407 iterator rerase( iterator pos ) // nothrow
408 {
409 BOOST_ASSERT( !this->empty() );
410 BOOST_ASSERT( pos != this->end() );
411
412 this->remove( pos );
413 return iterator( this->base().rerase( pos.base() ) );
414 }
415
416 iterator rerase( iterator first, iterator last ) // nothrow
417 {
418 this->remove( first, last );
419 return iterator( this->base().rerase( first.base(),
420 last.base() ) );
421 }
422
423 template< class Range >
424 iterator rerase( const Range& r ) // nothrow
425 {
426 return rerase( boost::begin(r), boost::end(r) );
427 }
428
429 void rotate( const_iterator new_begin ) // nothrow
430 {
431 this->base().rotate( new_begin.base() );
432 }
433
434 public: // transfer
435 template< class PtrSeqAdapter >
436 void transfer( iterator before,
437 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator first,
438 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator last,
439 PtrSeqAdapter& from ) // nothrow
440 {
441 BOOST_ASSERT( (void*)&from != (void*)this );
442 if( from.empty() )
443 return;
444 for( BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator begin = first;
445 begin != last; ++begin, ++before )
446 before = insert( before, &*begin ); // nothrow
447 from.base().erase( first.base(), last.base() ); // nothrow
448 }
449
450 template< class PtrSeqAdapter >
451 void transfer( iterator before,
452 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator object,
453 PtrSeqAdapter& from ) // nothrow
454 {
455 BOOST_ASSERT( (void*)&from != (void*)this );
456 if( from.empty() )
457 return;
458 insert( before, &*object ); // nothrow
459 from.base().erase( object.base() ); // nothrow
460 }
461
462 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
463 #else
464
465 template< class PtrSeqAdapter, class Range >
466 BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
467 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator > >::type
468 transfer( iterator before, const Range& r, PtrSeqAdapter& from ) // nothrow
469 {
470 transfer( before, boost::begin(r), boost::end(r), from );
471 }
472
473 #endif
474 template< class PtrSeqAdapter >
475 void transfer( iterator before, PtrSeqAdapter& from ) // nothrow
476 {
477 transfer( before, from.begin(), from.end(), from );
478 }
479
480 public: // C-array support
481
482 void transfer( iterator before, value_type* from,
483 size_type size, bool delete_from = true ) // nothrow
484 {
485 BOOST_ASSERT( from != 0 );
486 if( delete_from )
487 {
488 BOOST_DEDUCED_TYPENAME base_type::scoped_deleter
489 deleter( *this, from, size ); // nothrow
490 for( size_type i = 0u; i != size; ++i, ++before )
491 before = insert( before, *(from+i) ); // nothrow
492 deleter.release(); // nothrow
493 }
494 else
495 {
496 for( size_type i = 0u; i != size; ++i, ++before )
497 before = insert( before, *(from+i) ); // nothrow
498 }
499 }
500
501 value_type* c_array() // nothrow
502 {
503 if( this->empty() )
504 return 0;
505 this->linearize();
506 T** res = reinterpret_cast<T**>( &this->begin().base()[0] );
507 return res;
508 }
509
510 };
511
512 //////////////////////////////////////////////////////////////////////////////
513 // clonability
514
515 template< typename T, typename CA, typename A >
516 inline ptr_circular_buffer<T,CA,A>* new_clone( const ptr_circular_buffer<T,CA,A>& r )
517 {
518 return r.clone().release();
519 }
520
521 /////////////////////////////////////////////////////////////////////////
522 // swap
523
524 template< typename T, typename CA, typename A >
525 inline void swap( ptr_circular_buffer<T,CA,A>& l, ptr_circular_buffer<T,CA,A>& r )
526 {
527 l.swap(r);
528 }
529
530 }
531
532 #endif