]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/ptr_container/ptr_sequence_adapter.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / ptr_container / ptr_sequence_adapter.hpp
1 //
2 // Boost.Pointer Container
3 //
4 // Copyright Thorsten Ottosen 2003-2005. 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_SEQUENCE_ADAPTER_HPP
13 #define BOOST_PTR_CONTAINER_PTR_SEQUENCE_ADAPTER_HPP
14
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif
18
19
20 #include <boost/ptr_container/detail/reversible_ptr_container.hpp>
21 #include <boost/ptr_container/indirect_fun.hpp>
22 #include <boost/ptr_container/detail/void_ptr_iterator.hpp>
23 #include <boost/type_traits/remove_pointer.hpp>
24 #include <boost/type_traits/is_same.hpp>
25
26
27 namespace boost
28 {
29 namespace ptr_container_detail
30 {
31 template
32 <
33 class T,
34 class VoidPtrSeq
35 >
36 struct sequence_config
37 {
38 typedef BOOST_DEDUCED_TYPENAME remove_nullable<T>::type
39 U;
40 typedef VoidPtrSeq
41 void_container_type;
42
43 typedef BOOST_DEDUCED_TYPENAME VoidPtrSeq::allocator_type
44 allocator_type;
45
46 typedef U value_type;
47
48 typedef void_ptr_iterator<
49 BOOST_DEDUCED_TYPENAME VoidPtrSeq::iterator, U >
50 iterator;
51
52 typedef void_ptr_iterator<
53 BOOST_DEDUCED_TYPENAME VoidPtrSeq::const_iterator, const U >
54 const_iterator;
55
56 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
57
58 template< class Iter >
59 static U* get_pointer( Iter i )
60 {
61 return static_cast<U*>( *i.base() );
62 }
63
64 #else
65 template< class Iter >
66 static U* get_pointer( void_ptr_iterator<Iter,U> i )
67 {
68 return static_cast<U*>( *i.base() );
69 }
70
71 template< class Iter >
72 static U* get_pointer( Iter i )
73 {
74 return &*i;
75 }
76 #endif
77
78 #if defined(BOOST_NO_SFINAE) && !BOOST_WORKAROUND(__MWERKS__, <= 0x3003)
79
80 template< class Iter >
81 static const U* get_const_pointer( Iter i )
82 {
83 return static_cast<const U*>( *i.base() );
84 }
85
86 #else // BOOST_NO_SFINAE
87
88 #if BOOST_WORKAROUND(__MWERKS__, <= 0x3003)
89 template< class Iter >
90 static const U* get_const_pointer( void_ptr_iterator<Iter,U> i )
91 {
92 return static_cast<const U*>( *i.base() );
93 }
94 #else // BOOST_WORKAROUND
95 template< class Iter >
96 static const U* get_const_pointer( void_ptr_iterator<Iter,const U> i )
97 {
98 return static_cast<const U*>( *i.base() );
99 }
100 #endif // BOOST_WORKAROUND
101
102 template< class Iter >
103 static const U* get_const_pointer( Iter i )
104 {
105 return &*i;
106 }
107 #endif // BOOST_NO_SFINAE
108
109 BOOST_STATIC_CONSTANT(bool, allow_null = boost::is_nullable<T>::value );
110 };
111
112 } // ptr_container_detail
113
114
115 template< class Iterator, class T >
116 inline bool is_null( void_ptr_iterator<Iterator,T> i )
117 {
118 return *i.base() == 0;
119 }
120
121
122
123 template
124 <
125 class T,
126 class VoidPtrSeq,
127 class CloneAllocator = heap_clone_allocator
128 >
129 class ptr_sequence_adapter : public
130 ptr_container_detail::reversible_ptr_container< ptr_container_detail::sequence_config<T,VoidPtrSeq>,
131 CloneAllocator >
132 {
133 typedef ptr_container_detail::reversible_ptr_container< ptr_container_detail::sequence_config<T,VoidPtrSeq>,
134 CloneAllocator >
135 base_type;
136
137 typedef ptr_sequence_adapter<T,VoidPtrSeq,CloneAllocator>
138 this_type;
139
140 protected:
141 typedef BOOST_DEDUCED_TYPENAME base_type::scoped_deleter scoped_deleter;
142
143 public:
144 typedef BOOST_DEDUCED_TYPENAME base_type::value_type value_type;
145 typedef BOOST_DEDUCED_TYPENAME base_type::reference reference;
146 typedef BOOST_DEDUCED_TYPENAME base_type::const_reference
147 const_reference;
148 typedef BOOST_DEDUCED_TYPENAME base_type::auto_type auto_type;
149 typedef BOOST_DEDUCED_TYPENAME base_type::clone_allocator_type
150 clone_allocator_type;
151 typedef BOOST_DEDUCED_TYPENAME base_type::iterator iterator;
152 typedef BOOST_DEDUCED_TYPENAME base_type::size_type size_type;
153 typedef BOOST_DEDUCED_TYPENAME base_type::allocator_type
154 allocator_type;
155
156 ptr_sequence_adapter()
157 { }
158
159 template< class Allocator >
160 explicit ptr_sequence_adapter( const Allocator& a )
161 : base_type( a )
162 { }
163
164 template< class SizeType >
165 ptr_sequence_adapter( SizeType n,
166 ptr_container_detail::fixed_length_sequence_tag tag )
167 : base_type( n, tag )
168 { }
169
170 template< class SizeType, class Allocator >
171 ptr_sequence_adapter( SizeType n, const Allocator& a,
172 ptr_container_detail::fixed_length_sequence_tag tag )
173 : base_type( n, a, tag )
174 { }
175
176 template< class InputIterator >
177 ptr_sequence_adapter( InputIterator first, InputIterator last )
178 : base_type( first, last )
179 { }
180
181 template< class InputIterator, class Allocator >
182 ptr_sequence_adapter( InputIterator first, InputIterator last,
183 const Allocator& a )
184 : base_type( first, last, a )
185 { }
186
187 template< class ForwardIterator >
188 ptr_sequence_adapter( ForwardIterator first,
189 ForwardIterator last,
190 ptr_container_detail::fixed_length_sequence_tag tag )
191 : base_type( first, last, tag )
192 { }
193
194 template< class SizeType, class ForwardIterator >
195 ptr_sequence_adapter( SizeType n,
196 ForwardIterator first,
197 ForwardIterator last,
198 ptr_container_detail::fixed_length_sequence_tag tag )
199 : base_type( n, first, last, tag )
200 { }
201
202 ptr_sequence_adapter( const ptr_sequence_adapter& r )
203 : base_type( r )
204 { }
205
206 template< class U >
207 ptr_sequence_adapter( const ptr_sequence_adapter<U,VoidPtrSeq,CloneAllocator>& r )
208 : base_type( r )
209 { }
210
211 ptr_sequence_adapter( const ptr_sequence_adapter& r,
212 ptr_container_detail::fixed_length_sequence_tag tag )
213 : base_type( r, tag )
214 { }
215
216 template< class U >
217 ptr_sequence_adapter( const ptr_sequence_adapter<U,VoidPtrSeq,CloneAllocator>& r,
218 ptr_container_detail::fixed_length_sequence_tag tag )
219 : base_type( r, tag )
220 { }
221
222 template< class PtrContainer >
223 explicit ptr_sequence_adapter( std::auto_ptr<PtrContainer> clone )
224 : base_type( clone )
225 { }
226
227 ptr_sequence_adapter& operator=( const ptr_sequence_adapter r )
228 {
229 this->swap( r );
230 return *this;
231 }
232
233 template< class PtrContainer >
234 ptr_sequence_adapter& operator=( std::auto_ptr<PtrContainer> clone )
235 {
236 base_type::operator=( clone );
237 return *this;
238 }
239
240 /////////////////////////////////////////////////////////////
241 // modifiers
242 /////////////////////////////////////////////////////////////
243
244 void push_back( value_type x ) // strong
245 {
246 this->enforce_null_policy( x, "Null pointer in 'push_back()'" );
247 auto_type ptr( x, *this ); // notrow
248 this->base().push_back( x ); // strong, commit
249 ptr.release(); // nothrow
250 }
251
252 template< class U >
253 void push_back( std::auto_ptr<U> x )
254 {
255 push_back( x.release() );
256 }
257
258 void push_front( value_type x )
259 {
260 this->enforce_null_policy( x, "Null pointer in 'push_front()'" );
261 auto_type ptr( x, *this ); // nothrow
262 this->base().push_front( x ); // strong, commit
263 ptr.release(); // nothrow
264 }
265
266 template< class U >
267 void push_front( std::auto_ptr<U> x )
268 {
269 push_front( x.release() );
270 }
271
272 auto_type pop_back()
273 {
274 BOOST_ASSERT( !this->empty() &&
275 "'pop_back()' on empty container" );
276 auto_type ptr( static_cast<value_type>(this->base().back()), *this );
277 // nothrow
278 this->base().pop_back(); // nothrow
279 return ptr_container_detail::move( ptr ); // nothrow
280 }
281
282 auto_type pop_front()
283 {
284 BOOST_ASSERT( !this->empty() &&
285 "'pop_front()' on empty container" );
286 auto_type ptr( static_cast<value_type>(this->base().front()), *this );
287 // nothrow
288 this->base().pop_front(); // nothrow
289 return ptr_container_detail::move( ptr );
290 }
291
292 reference front()
293 {
294 BOOST_ASSERT( !this->empty() &&
295 "accessing 'front()' on empty container" );
296
297 BOOST_ASSERT( !::boost::is_null( this->begin() ) );
298 return *this->begin();
299 }
300
301 const_reference front() const
302 {
303 return const_cast<ptr_sequence_adapter*>(this)->front();
304 }
305
306 reference back()
307 {
308 BOOST_ASSERT( !this->empty() &&
309 "accessing 'back()' on empty container" );
310 BOOST_ASSERT( !::boost::is_null( --this->end() ) );
311 return *--this->end();
312 }
313
314 const_reference back() const
315 {
316 return const_cast<ptr_sequence_adapter*>(this)->back();
317 }
318
319 public: // deque/vector inerface
320
321 reference operator[]( size_type n ) // nothrow
322 {
323 BOOST_ASSERT( n < this->size() );
324 BOOST_ASSERT( !this->is_null( n ) );
325 return *static_cast<value_type>( this->base()[n] );
326 }
327
328 const_reference operator[]( size_type n ) const // nothrow
329 {
330 BOOST_ASSERT( n < this->size() );
331 BOOST_ASSERT( !this->is_null( n ) );
332 return *static_cast<value_type>( this->base()[n] );
333 }
334
335 reference at( size_type n )
336 {
337 BOOST_PTR_CONTAINER_THROW_EXCEPTION( n >= this->size(), bad_index,
338 "'at()' out of bounds" );
339 BOOST_ASSERT( !this->is_null( n ) );
340 return (*this)[n];
341 }
342
343 const_reference at( size_type n ) const
344 {
345 BOOST_PTR_CONTAINER_THROW_EXCEPTION( n >= this->size(), bad_index,
346 "'at()' out of bounds" );
347 BOOST_ASSERT( !this->is_null( n ) );
348 return (*this)[n];
349 }
350
351 public: // vector interface
352
353 size_type capacity() const
354 {
355 return this->base().capacity();
356 }
357
358 void reserve( size_type n )
359 {
360 this->base().reserve( n );
361 }
362
363 void reverse()
364 {
365 this->base().reverse();
366 }
367
368 public: // assign, insert, transfer
369
370 // overhead: 1 heap allocation (very cheap compared to cloning)
371 template< class InputIterator >
372 void assign( InputIterator first, InputIterator last ) // strong
373 {
374 base_type temp( first, last );
375 this->swap( temp );
376 }
377
378 template< class Range >
379 void assign( const Range& r ) // strong
380 {
381 assign( boost::begin(r), boost::end(r ) );
382 }
383
384 private:
385 template< class I >
386 void insert_impl( iterator before, I first, I last, std::input_iterator_tag ) // strong
387 {
388 ptr_sequence_adapter temp(first,last); // strong
389 transfer( before, temp ); // strong, commit
390 }
391
392 template< class I >
393 void insert_impl( iterator before, I first, I last, std::forward_iterator_tag ) // strong
394 {
395 if( first == last )
396 return;
397 scoped_deleter sd( *this, first, last ); // strong
398 this->insert_clones_and_release( sd, before ); // strong, commit
399 }
400
401 public:
402
403 using base_type::insert;
404
405 template< class InputIterator >
406 void insert( iterator before, InputIterator first, InputIterator last ) // strong
407 {
408 insert_impl( before, first, last, BOOST_DEDUCED_TYPENAME
409 iterator_category<InputIterator>::type() );
410 }
411
412 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
413 #else
414 template< class Range >
415 BOOST_DEDUCED_TYPENAME
416 boost::disable_if< ptr_container_detail::is_pointer_or_integral<Range> >::type
417 insert( iterator before, const Range& r )
418 {
419 insert( before, boost::begin(r), boost::end(r) );
420 }
421
422 #endif
423
424 template< class PtrSeqAdapter >
425 void transfer( iterator before,
426 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator first,
427 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator last,
428 PtrSeqAdapter& from ) // strong
429 {
430 BOOST_ASSERT( (void*)&from != (void*)this );
431 if( from.empty() )
432 return;
433 this->base().
434 insert( before.base(), first.base(), last.base() ); // strong
435 from.base().erase( first.base(), last.base() ); // nothrow
436 }
437
438 template< class PtrSeqAdapter >
439 void transfer( iterator before,
440 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator object,
441 PtrSeqAdapter& from ) // strong
442 {
443 BOOST_ASSERT( (void*)&from != (void*)this );
444 if( from.empty() )
445 return;
446 this->base().insert( before.base(), *object.base() ); // strong
447 from.base().erase( object.base() ); // nothrow
448 }
449
450 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
451 #else
452
453 template< class PtrSeqAdapter, class Range >
454 BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
455 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator > >::type
456 transfer( iterator before, const Range& r, PtrSeqAdapter& from ) // strong
457 {
458 transfer( before, boost::begin(r), boost::end(r), from );
459 }
460
461 #endif
462 template< class PtrSeqAdapter >
463 void transfer( iterator before, PtrSeqAdapter& from ) // strong
464 {
465 BOOST_ASSERT( (void*)&from != (void*)this );
466 if( from.empty() )
467 return;
468 this->base().
469 insert( before.base(),
470 from.begin().base(), from.end().base() ); // strong
471 from.base().clear(); // nothrow
472 }
473
474 public: // C-array support
475
476 void transfer( iterator before, value_type* from,
477 size_type size, bool delete_from = true ) // strong
478 {
479 BOOST_ASSERT( from != 0 );
480 if( delete_from )
481 {
482 BOOST_DEDUCED_TYPENAME base_type::scoped_deleter
483 deleter( *this, from, size ); // nothrow
484 this->base().insert( before.base(), from, from + size ); // strong
485 deleter.release(); // nothrow
486 }
487 else
488 {
489 this->base().insert( before.base(), from, from + size ); // strong
490 }
491 }
492
493 value_type* c_array() // nothrow
494 {
495 if( this->empty() )
496 return 0;
497 T** res = reinterpret_cast<T**>( &this->begin().base()[0] );
498 return res;
499 }
500
501 public: // null functions
502
503 bool is_null( size_type idx ) const
504 {
505 BOOST_ASSERT( idx < this->size() );
506 return this->base()[idx] == 0;
507 }
508
509 public: // resize
510
511 void resize( size_type size ) // basic
512 {
513 size_type old_size = this->size();
514 if( old_size > size )
515 {
516 this->erase( boost::next( this->begin(), size ), this->end() );
517 }
518 else if( size > old_size )
519 {
520 for( ; old_size != size; ++old_size )
521 this->push_back( new BOOST_DEDUCED_TYPENAME
522 boost::remove_pointer<value_type>::type() );
523 }
524
525 BOOST_ASSERT( this->size() == size );
526 }
527
528 void resize( size_type size, value_type to_clone ) // basic
529 {
530 size_type old_size = this->size();
531 if( old_size > size )
532 {
533 this->erase( boost::next( this->begin(), size ), this->end() );
534 }
535 else if( size > old_size )
536 {
537 for( ; old_size != size; ++old_size )
538 this->push_back( this->null_policy_allocate_clone( to_clone ) );
539 }
540
541 BOOST_ASSERT( this->size() == size );
542 }
543
544 void rresize( size_type size ) // basic
545 {
546 size_type old_size = this->size();
547 if( old_size > size )
548 {
549 this->erase( this->begin(),
550 boost::next( this->begin(), old_size - size ) );
551 }
552 else if( size > old_size )
553 {
554 for( ; old_size != size; ++old_size )
555 this->push_front( new BOOST_DEDUCED_TYPENAME
556 boost::remove_pointer<value_type>::type() );
557 }
558
559 BOOST_ASSERT( this->size() == size );
560 }
561
562 void rresize( size_type size, value_type to_clone ) // basic
563 {
564 size_type old_size = this->size();
565 if( old_size > size )
566 {
567 this->erase( this->begin(),
568 boost::next( this->begin(), old_size - size ) );
569 }
570 else if( size > old_size )
571 {
572 for( ; old_size != size; ++old_size )
573 this->push_front( this->null_policy_allocate_clone( to_clone ) );
574 }
575
576 BOOST_ASSERT( this->size() == size );
577 }
578
579 public: // algorithms
580
581 void sort( iterator first, iterator last )
582 {
583 sort( first, last, std::less<T>() );
584 }
585
586 void sort()
587 {
588 sort( this->begin(), this->end() );
589 }
590
591 template< class Compare >
592 void sort( iterator first, iterator last, Compare comp )
593 {
594 BOOST_ASSERT( first <= last && "out of range sort()" );
595 BOOST_ASSERT( this->begin() <= first && "out of range sort()" );
596 BOOST_ASSERT( last <= this->end() && "out of range sort()" );
597 // some static assert on the arguments of the comparison
598 std::sort( first.base(), last.base(),
599 void_ptr_indirect_fun<Compare,T>(comp) );
600 }
601
602 template< class Compare >
603 void sort( Compare comp )
604 {
605 sort( this->begin(), this->end(), comp );
606 }
607
608 void unique( iterator first, iterator last )
609 {
610 unique( first, last, std::equal_to<T>() );
611 }
612
613 void unique()
614 {
615 unique( this->begin(), this->end() );
616 }
617
618 private:
619 struct is_not_zero_ptr
620 {
621 template< class U >
622 bool operator()( const U* r ) const
623 {
624 return r != 0;
625 }
626 };
627
628 protected:
629 template< class Fun, class Arg1 >
630 class void_ptr_delete_if
631 {
632 Fun fun;
633 public:
634
635 void_ptr_delete_if() : fun(Fun())
636 { }
637
638 void_ptr_delete_if( Fun f ) : fun(f)
639 { }
640
641 bool operator()( void* r ) const
642 {
643 BOOST_ASSERT( r != 0 );
644 Arg1 arg1 = static_cast<Arg1>(r);
645 if( fun( *arg1 ) )
646 {
647 clone_allocator_type::deallocate_clone( arg1 );
648 return true;
649 }
650 return false;
651 }
652 };
653
654 private:
655 void compact_and_erase_nulls( iterator first, iterator last ) // nothrow
656 {
657 typename base_type::ptr_iterator p = std::stable_partition(
658 first.base(),
659 last.base(),
660 is_not_zero_ptr() );
661 this->base().erase( p, this->end().base() );
662
663 }
664
665 void range_check_impl( iterator, iterator,
666 std::bidirectional_iterator_tag )
667 { /* do nothing */ }
668
669 void range_check_impl( iterator first, iterator last,
670 std::random_access_iterator_tag )
671 {
672 BOOST_ASSERT( first <= last && "out of range unique()/erase_if()" );
673 BOOST_ASSERT( this->begin() <= first && "out of range unique()/erase_if()" );
674 BOOST_ASSERT( last <= this->end() && "out of range unique()/erase_if)(" );
675 }
676
677 void range_check( iterator first, iterator last )
678 {
679 range_check_impl( first, last,
680 BOOST_DEDUCED_TYPENAME iterator_category<iterator>::type() );
681 }
682
683 public:
684
685 template< class Compare >
686 void unique( iterator first, iterator last, Compare comp )
687 {
688 range_check(first,last);
689
690 iterator prev = first;
691 iterator next = first;
692 ++next;
693 for( ; next != last; ++next )
694 {
695 BOOST_ASSERT( !::boost::is_null(prev) );
696 BOOST_ASSERT( !::boost::is_null(next) );
697 if( comp( *prev, *next ) )
698 {
699 this->remove( next ); // delete object
700 *next.base() = 0; // mark pointer as deleted
701 }
702 else
703 {
704 prev = next;
705 }
706 // ++next
707 }
708
709 compact_and_erase_nulls( first, last );
710 }
711
712 template< class Compare >
713 void unique( Compare comp )
714 {
715 unique( this->begin(), this->end(), comp );
716 }
717
718 template< class Pred >
719 void erase_if( iterator first, iterator last, Pred pred )
720 {
721 range_check(first,last);
722 this->base().erase( std::remove_if( first.base(), last.base(),
723 void_ptr_delete_if<Pred,value_type>(pred) ),
724 last.base() );
725 }
726
727 template< class Pred >
728 void erase_if( Pred pred )
729 {
730 erase_if( this->begin(), this->end(), pred );
731 }
732
733
734 void merge( iterator first, iterator last,
735 ptr_sequence_adapter& from )
736 {
737 merge( first, last, from, std::less<T>() );
738 }
739
740 template< class BinPred >
741 void merge( iterator first, iterator last,
742 ptr_sequence_adapter& from, BinPred pred )
743 {
744 void_ptr_indirect_fun<BinPred,T> bin_pred(pred);
745 size_type current_size = this->size();
746 this->transfer( this->end(), first, last, from );
747 typename base_type::ptr_iterator middle = this->begin().base();
748 std::advance(middle,current_size);
749 std::inplace_merge( this->begin().base(),
750 middle,
751 this->end().base(),
752 bin_pred );
753 }
754
755 void merge( ptr_sequence_adapter& r )
756 {
757 merge( r, std::less<T>() );
758 BOOST_ASSERT( r.empty() );
759 }
760
761 template< class BinPred >
762 void merge( ptr_sequence_adapter& r, BinPred pred )
763 {
764 merge( r.begin(), r.end(), r, pred );
765 BOOST_ASSERT( r.empty() );
766 }
767
768 };
769
770
771 } // namespace 'boost'
772
773 #endif