Repository navigation
Expand file tree
/
Copy pathbase.hpp
More file actions
3132 lines (2926 loc) · 151 KB
/
Copy pathbase.hpp
File metadata and controls
3132 lines (2926 loc) · 151 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Implementation of the base circular buffer.
// Copyright (c) 2003-2008 Jan Gaspar
// Copyright (c) 2013 Paul A. Bristow // Doxygen comments changed.
// Copyright (c) 2013 Antony Polukhin // Move semantics implementation.
// Copyright 2014,2018 Glen Joseph Fernandes
// (glenjofe@gmail.com)
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if !defined(BOOST_CIRCULAR_BUFFER_BASE_HPP)
#define BOOST_CIRCULAR_BUFFER_BASE_HPP
#if defined(_MSC_VER)
#pragma once
#endif
#include <boost/config.hpp>
#include <boost/concept_check.hpp>
#include <boost/limits.hpp>
#include <boost/circular_buffer/allocators.hpp>
#include <boost/core/empty_value.hpp>
#include <boost/type_traits/is_stateless.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_scalar.hpp>
#include <boost/type_traits/is_nothrow_move_constructible.hpp>
#include <boost/type_traits/is_nothrow_move_assignable.hpp>
#include <boost/type_traits/is_copy_constructible.hpp>
#include <boost/type_traits/conditional.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <boost/move/move.hpp>
#include <algorithm>
#include <iterator>
#include <utility>
#include <deque>
#include <stdexcept>
#if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
#include <stddef.h>
#endif
namespace boost {
/*!
\class circular_buffer
\brief Circular buffer - a STL compliant container.
\tparam T The type of the elements stored in the <code>circular_buffer</code>.
\par Type Requirements T
The <code>T</code> has to be <a href="http://www.sgi.com/tech/stl/Assignable.html">
SGIAssignable</a> (SGI STL defined combination of <a href="../../../utility/Assignable.html">
Assignable</a> and <a href="../../../utility/CopyConstructible.html">CopyConstructible</a>).
Moreover <code>T</code> has to be <a href="http://www.sgi.com/tech/stl/DefaultConstructible.html">
DefaultConstructible</a> if supplied as a default parameter when invoking some of the
<code>circular_buffer</code>'s methods e.g.
<code>insert(iterator pos, const value_type& item = %value_type())</code>. And
<a href="http://www.sgi.com/tech/stl/EqualityComparable.html">EqualityComparable</a> and/or
<a href="../../../utility/LessThanComparable.html">LessThanComparable</a> if the <code>circular_buffer</code>
will be compared with another container.
\tparam Alloc The allocator type used for all internal memory management.
\par Type Requirements Alloc
The <code>Alloc</code> has to meet the allocator requirements imposed by STL.
\par Default Alloc
std::allocator<T>
For detailed documentation of the circular_buffer visit:
http://www.boost.org/libs/circular_buffer/doc/circular_buffer.html
*/
template <class T, class Alloc>
class circular_buffer
:
/*! \cond */
#if BOOST_CB_ENABLE_DEBUG
public cb_details::debug_iterator_registry,
#endif
/*! \endcond */
private empty_value<Alloc>
{
typedef empty_value<Alloc> base;
// Requirements
//BOOST_CLASS_REQUIRE(T, boost, SGIAssignableConcept);
//BOOST_CONCEPT_ASSERT((Assignable<T>));
//BOOST_CONCEPT_ASSERT((CopyConstructible<T>));
//BOOST_CONCEPT_ASSERT((DefaultConstructible<T>));
// Required if the circular_buffer will be compared with anther container.
//BOOST_CONCEPT_ASSERT((EqualityComparable<T>));
//BOOST_CONCEPT_ASSERT((LessThanComparable<T>));
public:
// Basic types
//! The type of this <code>circular_buffer</code>.
typedef circular_buffer<T, Alloc> this_type;
//! The type of elements stored in the <code>circular_buffer</code>.
typedef typename cb_details::allocator_traits<Alloc>::value_type value_type;
//! A pointer to an element.
typedef typename cb_details::allocator_traits<Alloc>::pointer pointer;
//! A const pointer to the element.
typedef typename cb_details::allocator_traits<Alloc>::const_pointer const_pointer;
//! A reference to an element.
typedef value_type& reference;
//! A const reference to an element.
typedef const value_type& const_reference;
//! The distance type.
/*!
(A signed integral type used to represent the distance between two iterators.)
*/
typedef typename cb_details::allocator_traits<Alloc>::difference_type difference_type;
//! The size type.
/*!
(An unsigned integral type that can represent any non-negative value of the container's distance type.)
*/
typedef typename cb_details::allocator_traits<Alloc>::size_type size_type;
//! The type of an allocator used in the <code>circular_buffer</code>.
typedef Alloc allocator_type;
// Iterators
//! A const (random access) iterator used to iterate through the <code>circular_buffer</code>.
typedef cb_details::iterator< circular_buffer<T, Alloc>, cb_details::const_traits<cb_details::allocator_traits<Alloc> > > const_iterator;
//! A (random access) iterator used to iterate through the <code>circular_buffer</code>.
typedef cb_details::iterator< circular_buffer<T, Alloc>, cb_details::nonconst_traits<cb_details::allocator_traits<Alloc> > > iterator;
//! A const iterator used to iterate backwards through a <code>circular_buffer</code>.
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
//! An iterator used to iterate backwards through a <code>circular_buffer</code>.
typedef std::reverse_iterator<iterator> reverse_iterator;
// Container specific types
//! An array range.
/*!
(A typedef for the <a href="http://www.sgi.com/tech/stl/pair.html"><code>std::pair</code></a> where
its first element is a pointer to a beginning of an array and its second element represents
a size of the array.)
*/
typedef std::pair<pointer, size_type> array_range;
//! A range of a const array.
/*!
(A typedef for the <a href="http://www.sgi.com/tech/stl/pair.html"><code>std::pair</code></a> where
its first element is a pointer to a beginning of a const array and its second element represents
a size of the const array.)
*/
typedef std::pair<const_pointer, size_type> const_array_range;
//! The capacity type.
/*!
(Same as <code>size_type</code> - defined for consistency with the __cbso class.
*/
// <a href="space_optimized.html"><code>circular_buffer_space_optimized</code></a>.)
typedef size_type capacity_type;
// Helper types
//! A type representing the "best" way to pass the value_type to a method.
typedef const value_type& param_value_type;
//! A type representing rvalue from param type.
//! On compilers without rvalue references support this type is the Boost.Moves type used for emulation.
typedef BOOST_RV_REF(value_type) rvalue_type;
private:
// Member variables
//! The internal buffer used for storing elements in the circular buffer.
pointer m_buff;
//! The internal buffer's end (end of the storage space).
pointer m_end;
//! The virtual beginning of the circular buffer.
pointer m_first;
//! The virtual end of the circular buffer (one behind the last element).
pointer m_last;
//! The number of items currently stored in the circular buffer.
size_type m_size;
// Friends
#if defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
friend iterator;
friend const_iterator;
#else
template <class Buff, class Traits> friend struct cb_details::iterator;
#endif
public:
// Allocator
//! Get the allocator.
/*!
\return The allocator.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>get_allocator()</code> for obtaining an allocator %reference.
*/
allocator_type get_allocator() const BOOST_NOEXCEPT { return alloc(); }
//! Get the allocator reference.
/*!
\return A reference to the allocator.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\note This method was added in order to optimize obtaining of the allocator with a state,
although use of stateful allocators in STL is discouraged.
\sa <code>get_allocator() const</code>
*/
allocator_type& get_allocator() BOOST_NOEXCEPT { return alloc(); }
// Element access
//! Get the iterator pointing to the beginning of the <code>circular_buffer</code>.
/*!
\return A random access iterator pointing to the first element of the <code>circular_buffer</code>. If the
<code>circular_buffer</code> is empty it returns an iterator equal to the one returned by
<code>end()</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>end()</code>, <code>rbegin()</code>, <code>rend()</code>
*/
iterator begin() BOOST_NOEXCEPT { return iterator(this, empty() ? 0 : m_first); }
//! Get the iterator pointing to the end of the <code>circular_buffer</code>.
/*!
\return A random access iterator pointing to the element "one behind" the last element of the <code>
circular_buffer</code>. If the <code>circular_buffer</code> is empty it returns an iterator equal to
the one returned by <code>begin()</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>begin()</code>, <code>rbegin()</code>, <code>rend()</code>
*/
iterator end() BOOST_NOEXCEPT { return iterator(this, 0); }
//! Get the const iterator pointing to the beginning of the <code>circular_buffer</code>.
/*!
\return A const random access iterator pointing to the first element of the <code>circular_buffer</code>. If
the <code>circular_buffer</code> is empty it returns an iterator equal to the one returned by
<code>end() const</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>end() const</code>, <code>rbegin() const</code>, <code>rend() const</code>
*/
const_iterator begin() const BOOST_NOEXCEPT { return const_iterator(this, empty() ? 0 : m_first); }
//! Get the const iterator pointing to the end of the <code>circular_buffer</code>.
/*!
\return A const random access iterator pointing to the element "one behind" the last element of the <code>
circular_buffer</code>. If the <code>circular_buffer</code> is empty it returns an iterator equal to
the one returned by <code>begin() const</code> const.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>begin() const</code>, <code>rbegin() const</code>, <code>rend() const</code>
*/
const_iterator end() const BOOST_NOEXCEPT { return const_iterator(this, 0); }
//! Get the iterator pointing to the beginning of the "reversed" <code>circular_buffer</code>.
/*!
\return A reverse random access iterator pointing to the last element of the <code>circular_buffer</code>.
If the <code>circular_buffer</code> is empty it returns an iterator equal to the one returned by
<code>rend()</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>rend()</code>, <code>begin()</code>, <code>end()</code>
*/
reverse_iterator rbegin() BOOST_NOEXCEPT { return reverse_iterator(end()); }
//! Get the iterator pointing to the end of the "reversed" <code>circular_buffer</code>.
/*!
\return A reverse random access iterator pointing to the element "one before" the first element of the <code>
circular_buffer</code>. If the <code>circular_buffer</code> is empty it returns an iterator equal to
the one returned by <code>rbegin()</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>rbegin()</code>, <code>begin()</code>, <code>end()</code>
*/
reverse_iterator rend() BOOST_NOEXCEPT { return reverse_iterator(begin()); }
//! Get the const iterator pointing to the beginning of the "reversed" <code>circular_buffer</code>.
/*!
\return A const reverse random access iterator pointing to the last element of the
<code>circular_buffer</code>. If the <code>circular_buffer</code> is empty it returns an iterator equal
to the one returned by <code>rend() const</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>rend() const</code>, <code>begin() const</code>, <code>end() const</code>
*/
const_reverse_iterator rbegin() const BOOST_NOEXCEPT { return const_reverse_iterator(end()); }
//! Get the const iterator pointing to the end of the "reversed" <code>circular_buffer</code>.
/*!
\return A const reverse random access iterator pointing to the element "one before" the first element of the
<code>circular_buffer</code>. If the <code>circular_buffer</code> is empty it returns an iterator equal
to the one returned by <code>rbegin() const</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>rbegin() const</code>, <code>begin() const</code>, <code>end() const</code>
*/
const_reverse_iterator rend() const BOOST_NOEXCEPT { return const_reverse_iterator(begin()); }
//! Get the element at the <code>index</code> position.
/*!
\pre <code>0 \<= index \&\& index \< size()</code>
\param index The position of the element.
\return A reference to the element at the <code>index</code> position.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>at()</code>
*/
reference operator [] (size_type index) {
BOOST_CB_ASSERT(index < size()); // check for invalid index
return *add(m_first, index);
}
//! Get the element at the <code>index</code> position.
/*!
\pre <code>0 \<= index \&\& index \< size()</code>
\param index The position of the element.
\return A const reference to the element at the <code>index</code> position.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>\link at(size_type)const at() const \endlink</code>
*/
const_reference operator [] (size_type index) const {
BOOST_CB_ASSERT(index < size()); // check for invalid index
return *add(m_first, index);
}
//! Get the element at the <code>index</code> position.
/*!
\param index The position of the element.
\return A reference to the element at the <code>index</code> position.
\throws <code>std::out_of_range</code> when the <code>index</code> is invalid (when
<code>index >= size()</code>).
\par Exception Safety
Strong.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>\link operator[](size_type) operator[] \endlink</code>
*/
reference at(size_type index) {
check_position(index);
return (*this)[index];
}
//! Get the element at the <code>index</code> position.
/*!
\param index The position of the element.
\return A const reference to the element at the <code>index</code> position.
\throws <code>std::out_of_range</code> when the <code>index</code> is invalid (when
<code>index >= size()</code>).
\par Exception Safety
Strong.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>\link operator[](size_type)const operator[] const \endlink</code>
*/
const_reference at(size_type index) const {
check_position(index);
return (*this)[index];
}
//! Get the first element.
/*!
\pre <code>!empty()</code>
\return A reference to the first element of the <code>circular_buffer</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>back()</code>
*/
reference front() {
BOOST_CB_ASSERT(!empty()); // check for empty buffer (front element not available)
return *m_first;
}
//! Get the last element.
/*!
\pre <code>!empty()</code>
\return A reference to the last element of the <code>circular_buffer</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>front()</code>
*/
reference back() {
BOOST_CB_ASSERT(!empty()); // check for empty buffer (back element not available)
return *((m_last == m_buff ? m_end : m_last) - 1);
}
//! Get the first element.
/*!
\pre <code>!empty()</code>
\return A const reference to the first element of the <code>circular_buffer</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>back() const</code>
*/
const_reference front() const {
BOOST_CB_ASSERT(!empty()); // check for empty buffer (front element not available)
return *m_first;
}
//! Get the last element.
/*!
\pre <code>!empty()</code>
\return A const reference to the last element of the <code>circular_buffer</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>front() const</code>
*/
const_reference back() const {
BOOST_CB_ASSERT(!empty()); // check for empty buffer (back element not available)
return *((m_last == m_buff ? m_end : m_last) - 1);
}
//! Get the first continuous array of the internal buffer.
/*!
This method in combination with <code>array_two()</code> can be useful when passing the stored data into
a legacy C API as an array. Suppose there is a <code>circular_buffer</code> of capacity 10, containing 7
characters <code>'a', 'b', ..., 'g'</code> where <code>buff[0] == 'a'</code>, <code>buff[1] == 'b'</code>,
... and <code>buff[6] == 'g'</code>:<br><br>
<code>circular_buffer<char> buff(10);</code><br><br>
The internal representation is often not linear and the state of the internal buffer may look like this:<br>
<br><code>
|e|f|g| | | |a|b|c|d|<br>
end ___^<br>
begin _______^</code><br><br>
where <code>|a|b|c|d|</code> represents the "array one", <code>|e|f|g|</code> represents the "array two" and
<code>| | | |</code> is a free space.<br>
Now consider a typical C style function for writing data into a file:<br><br>
<code>int write(int file_desc, char* buff, int num_bytes);</code><br><br>
There are two ways how to write the content of the <code>circular_buffer</code> into a file. Either relying
on <code>array_one()</code> and <code>array_two()</code> methods and calling the write function twice:<br><br>
<code>array_range ar = buff.array_one();<br>
write(file_desc, ar.first, ar.second);<br>
ar = buff.array_two();<br>
write(file_desc, ar.first, ar.second);</code><br><br>
Or relying on the <code>linearize()</code> method:<br><br><code>
write(file_desc, buff.linearize(), buff.size());</code><br><br>
Since the complexity of <code>array_one()</code> and <code>array_two()</code> methods is constant the first
option is suitable when calling the write method is "cheap". On the other hand the second option is more
suitable when calling the write method is more "expensive" than calling the <code>linearize()</code> method
whose complexity is linear.
\return The array range of the first continuous array of the internal buffer. In the case the
<code>circular_buffer</code> is empty the size of the returned array is <code>0</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\warning In general invoking any method which modifies the internal state of the circular_buffer may
delinearize the internal buffer and invalidate the array ranges returned by <code>array_one()</code>
and <code>array_two()</code> (and their const versions).
\note In the case the internal buffer is linear e.g. <code>|a|b|c|d|e|f|g| | | |</code> the "array one" is
represented by <code>|a|b|c|d|e|f|g|</code> and the "array two" does not exist (the
<code>array_two()</code> method returns an array with the size <code>0</code>).
\sa <code>array_two()</code>, <code>linearize()</code>
*/
array_range array_one() {
return array_range(m_first, (m_last <= m_first && !empty() ? m_end : m_last) - m_first);
}
//! Get the second continuous array of the internal buffer.
/*!
This method in combination with <code>array_one()</code> can be useful when passing the stored data into
a legacy C API as an array.
\return The array range of the second continuous array of the internal buffer. In the case the internal buffer
is linear or the <code>circular_buffer</code> is empty the size of the returned array is
<code>0</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>array_one()</code>
*/
array_range array_two() {
return array_range(m_buff, m_last <= m_first && !empty() ? m_last - m_buff : 0);
}
//! Get the first continuous array of the internal buffer.
/*!
This method in combination with <code>array_two() const</code> can be useful when passing the stored data into
a legacy C API as an array.
\return The array range of the first continuous array of the internal buffer. In the case the
<code>circular_buffer</code> is empty the size of the returned array is <code>0</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>array_two() const</code>; <code>array_one()</code> for more details how to pass data into a legacy C
API.
*/
const_array_range array_one() const {
return const_array_range(m_first, (m_last <= m_first && !empty() ? m_end : m_last) - m_first);
}
//! Get the second continuous array of the internal buffer.
/*!
This method in combination with <code>array_one() const</code> can be useful when passing the stored data into
a legacy C API as an array.
\return The array range of the second continuous array of the internal buffer. In the case the internal buffer
is linear or the <code>circular_buffer</code> is empty the size of the returned array is
<code>0</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>array_one() const</code>
*/
const_array_range array_two() const {
return const_array_range(m_buff, m_last <= m_first && !empty() ? m_last - m_buff : 0);
}
//! Linearize the internal buffer into a continuous array.
/*!
This method can be useful when passing the stored data into a legacy C API as an array.
\post <code>\&(*this)[0] \< \&(*this)[1] \< ... \< \&(*this)[size() - 1]</code>
\return A pointer to the beginning of the array or <code>0</code> if empty.
\throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
\par Exception Safety
Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
\par Iterator Invalidation
Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
<code>end()</code>); does not invalidate any iterators if the postcondition (the <i>Effect</i>) is already
met prior calling this method.
\par Complexity
Linear (in the size of the <code>circular_buffer</code>); constant if the postcondition (the
<i>Effect</i>) is already met.
\warning In general invoking any method which modifies the internal state of the <code>circular_buffer</code>
may delinearize the internal buffer and invalidate the returned pointer.
\sa <code>array_one()</code> and <code>array_two()</code> for the other option how to pass data into a legacy
C API; <code>is_linearized()</code>, <code>rotate(const_iterator)</code>
*/
pointer linearize() {
if (empty())
return 0;
if (m_first < m_last || m_last == m_buff)
return m_first;
pointer src = m_first;
pointer dest = m_buff;
size_type moved = 0;
size_type constructed = 0;
BOOST_TRY {
for (pointer first = m_first; dest < src; src = first) {
for (size_type ii = 0; src < m_end; ++src, ++dest, ++moved, ++ii) {
if (moved == size()) {
first = dest;
break;
}
if (dest == first) {
first += ii;
break;
}
if (is_uninitialized(dest)) {
cb_details::allocator_traits<Alloc>::construct(alloc(), boost::to_address(dest), boost::move_if_noexcept(*src));
++constructed;
} else {
value_type tmp = boost::move_if_noexcept(*src);
replace(src, boost::move_if_noexcept(*dest));
replace(dest, boost::move(tmp));
}
}
}
} BOOST_CATCH(...) {
m_last += constructed;
m_size += constructed;
BOOST_RETHROW
}
BOOST_CATCH_END
for (src = m_end - constructed; src < m_end; ++src)
destroy_item(src);
m_first = m_buff;
m_last = add(m_buff, size());
#if BOOST_CB_ENABLE_DEBUG
invalidate_iterators_except(end());
#endif
return m_buff;
}
//! Is the <code>circular_buffer</code> linearized?
/*!
\return <code>true</code> if the internal buffer is linearized into a continuous array (i.e. the
<code>circular_buffer</code> meets a condition
<code>\&(*this)[0] \< \&(*this)[1] \< ... \< \&(*this)[size() - 1]</code>);
<code>false</code> otherwise.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>linearize()</code>, <code>array_one()</code>, <code>array_two()</code>
*/
bool is_linearized() const BOOST_NOEXCEPT { return m_first < m_last || m_last == m_buff; }
//! Rotate elements in the <code>circular_buffer</code>.
/*!
A more effective implementation of
<code><a href="http://www.sgi.com/tech/stl/rotate.html">std::rotate</a></code>.
\pre <code>new_begin</code> is a valid iterator pointing to the <code>circular_buffer</code> <b>except</b> its
end.
\post Before calling the method suppose:<br><br>
<code>m == std::distance(new_begin, end())</code><br><code>n == std::distance(begin(), new_begin)</code>
<br><code>val_0 == *new_begin, val_1 == *(new_begin + 1), ... val_m == *(new_begin + m)</code><br>
<code>val_r1 == *(new_begin - 1), val_r2 == *(new_begin - 2), ... val_rn == *(new_begin - n)</code><br>
<br>then after call to the method:<br><br>
<code>val_0 == (*this)[0] \&\& val_1 == (*this)[1] \&\& ... \&\& val_m == (*this)[m - 1] \&\& val_r1 ==
(*this)[m + n - 1] \&\& val_r2 == (*this)[m + n - 2] \&\& ... \&\& val_rn == (*this)[m]</code>
\param new_begin The new beginning.
\throws See <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
\par Exception Safety
Basic; no-throw if the <code>circular_buffer</code> is full or <code>new_begin</code> points to
<code>begin()</code> or if the operations in the <i>Throws</i> section do not throw anything.
\par Iterator Invalidation
If <code>m \< n</code> invalidates iterators pointing to the last <code>m</code> elements
(<b>including</b> <code>new_begin</code>, but not iterators equal to <code>end()</code>) else invalidates
iterators pointing to the first <code>n</code> elements; does not invalidate any iterators if the
<code>circular_buffer</code> is full.
\par Complexity
Linear (in <code>(std::min)(m, n)</code>); constant if the <code>circular_buffer</code> is full.
\sa <code><a href="http://www.sgi.com/tech/stl/rotate.html">std::rotate</a></code>
*/
void rotate(const_iterator new_begin) {
BOOST_CB_ASSERT(new_begin.is_valid(this)); // check for uninitialized or invalidated iterator
BOOST_CB_ASSERT(new_begin.m_it != 0); // check for iterator pointing to end()
if (full()) {
m_first = m_last = const_cast<pointer>(new_begin.m_it);
} else {
difference_type m = end() - new_begin;
difference_type n = new_begin - begin();
if (m < n) {
for (; m > 0; --m) {
push_front(boost::move_if_noexcept(back()));
pop_back();
}
} else {
for (; n > 0; --n) {
push_back(boost::move_if_noexcept(front()));
pop_front();
}
}
}
}
// Size and capacity
//! Get the number of elements currently stored in the <code>circular_buffer</code>.
/*!
\return The number of elements stored in the <code>circular_buffer</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>capacity()</code>, <code>max_size()</code>, <code>reserve()</code>,
<code>\link resize() resize(size_type, const_reference)\endlink</code>
*/
size_type size() const BOOST_NOEXCEPT { return m_size; }
/*! \brief Get the largest possible size or capacity of the <code>circular_buffer</code>. (It depends on
allocator's %max_size()).
\return The maximum size/capacity the <code>circular_buffer</code> can be set to.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>size()</code>, <code>capacity()</code>, <code>reserve()</code>
*/
size_type max_size() const BOOST_NOEXCEPT {
return (std::min<size_type>)(cb_details::allocator_traits<Alloc>::max_size(alloc()), (std::numeric_limits<difference_type>::max)());
}
//! Is the <code>circular_buffer</code> empty?
/*!
\return <code>true</code> if there are no elements stored in the <code>circular_buffer</code>;
<code>false</code> otherwise.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>full()</code>
*/
bool empty() const BOOST_NOEXCEPT { return size() == 0; }
//! Is the <code>circular_buffer</code> full?
/*!
\return <code>true</code> if the number of elements stored in the <code>circular_buffer</code>
equals the capacity of the <code>circular_buffer</code>; <code>false</code> otherwise.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>empty()</code>
*/
bool full() const BOOST_NOEXCEPT { return capacity() == size(); }
/*! \brief Get the maximum number of elements which can be inserted into the <code>circular_buffer</code> without
overwriting any of already stored elements.
\return <code>capacity() - size()</code>
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>capacity()</code>, <code>size()</code>, <code>max_size()</code>
*/
size_type reserve() const BOOST_NOEXCEPT { return capacity() - size(); }
//! Get the capacity of the <code>circular_buffer</code>.
/*!
\return The maximum number of elements which can be stored in the <code>circular_buffer</code>.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>reserve()</code>, <code>size()</code>, <code>max_size()</code>,
<code>set_capacity(capacity_type)</code>
*/
capacity_type capacity() const BOOST_NOEXCEPT { return m_end - m_buff; }
//! Change the capacity of the <code>circular_buffer</code>.
/*!
\pre If <code>T</code> is a move only type, then compiler shall support <code>noexcept</code> modifiers
and move constructor of <code>T</code> must be marked with it (must not throw exceptions).
\post <code>capacity() == new_capacity \&\& size() \<= new_capacity</code><br><br>
If the current number of elements stored in the <code>circular_buffer</code> is greater than the desired
new capacity then number of <code>[size() - new_capacity]</code> <b>last</b> elements will be removed and
the new size will be equal to <code>new_capacity</code>.
\param new_capacity The new capacity.
\throws "An allocation error" if memory is exhausted, (<code>std::bad_alloc</code> if the standard allocator is
used).
Whatever <code>T::T(const T&)</code> throws or nothing if <code>T::T(T&&)</code> is noexcept.
\par Exception Safety
Strong.
\par Iterator Invalidation
Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
<code>end()</code>) if the new capacity is different from the original.
\par Complexity
Linear (in <code>min[size(), new_capacity]</code>).
\sa <code>rset_capacity(capacity_type)</code>,
<code>\link resize() resize(size_type, const_reference)\endlink</code>
*/
void set_capacity(capacity_type new_capacity) {
if (new_capacity == capacity())
return;
pointer buff = allocate(new_capacity);
iterator b = begin();
BOOST_TRY {
reset(buff,
cb_details::uninitialized_move_if_noexcept(b, b + (std::min)(new_capacity, size()), buff, alloc()),
new_capacity);
} BOOST_CATCH(...) {
deallocate(buff, new_capacity);
BOOST_RETHROW
}
BOOST_CATCH_END
}
//! Change the size of the <code>circular_buffer</code>.
/*!
\post <code>size() == new_size \&\& capacity() >= new_size</code><br><br>
If the new size is greater than the current size, copies of <code>item</code> will be inserted at the
<b>back</b> of the of the <code>circular_buffer</code> in order to achieve the desired size. In the case
the resulting size exceeds the current capacity the capacity will be set to <code>new_size</code>.<br>
If the current number of elements stored in the <code>circular_buffer</code> is greater than the desired
new size then number of <code>[size() - new_size]</code> <b>last</b> elements will be removed. (The
capacity will remain unchanged.)
\param new_size The new size.
\param item The element the <code>circular_buffer</code> will be filled with in order to gain the requested
size. (See the <i>Effect</i>.)
\throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
used).
Whatever <code>T::T(const T&)</code> throws or nothing if <code>T::T(T&&)</code> is noexcept.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
<code>end()</code>) if the new size is greater than the current capacity. Invalidates iterators pointing
to the removed elements if the new size is lower that the original size. Otherwise it does not invalidate
any iterator.
\par Complexity
Linear (in the new size of the <code>circular_buffer</code>).
\sa <code>\link rresize() rresize(size_type, const_reference)\endlink</code>,
<code>set_capacity(capacity_type)</code>
*/
void resize(size_type new_size, param_value_type item = value_type()) {
if (new_size > size()) {
if (new_size > capacity())
set_capacity(new_size);
insert(end(), new_size - size(), item);
} else {
iterator e = end();
erase(e - (size() - new_size), e);
}
}
//! Change the capacity of the <code>circular_buffer</code>.
/*!
\pre If <code>T</code> is a move only type, then compiler shall support <code>noexcept</code> modifiers
and move constructor of <code>T</code> must be marked with it (must not throw exceptions).
\post <code>capacity() == new_capacity \&\& size() \<= new_capacity</code><br><br>
If the current number of elements stored in the <code>circular_buffer</code> is greater than the desired
new capacity then number of <code>[size() - new_capacity]</code> <b>first</b> elements will be removed
and the new size will be equal to <code>new_capacity</code>.
\param new_capacity The new capacity.
\throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
used).
Whatever <code>T::T(const T&)</code> throws or nothing if <code>T::T(T&&)</code> is noexcept.
\par Exception Safety
Strong.
\par Iterator Invalidation
Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
<code>end()</code>) if the new capacity is different from the original.
\par Complexity
Linear (in <code>min[size(), new_capacity]</code>).
\sa <code>set_capacity(capacity_type)</code>,
<code>\link rresize() rresize(size_type, const_reference)\endlink</code>
*/
void rset_capacity(capacity_type new_capacity) {
if (new_capacity == capacity())
return;
pointer buff = allocate(new_capacity);
iterator e = end();
BOOST_TRY {
reset(buff, cb_details::uninitialized_move_if_noexcept(e - (std::min)(new_capacity, size()),
e, buff, alloc()), new_capacity);
} BOOST_CATCH(...) {
deallocate(buff, new_capacity);
BOOST_RETHROW
}
BOOST_CATCH_END
}
//! Change the size of the <code>circular_buffer</code>.
/*!
\post <code>size() == new_size \&\& capacity() >= new_size</code><br><br>
If the new size is greater than the current size, copies of <code>item</code> will be inserted at the
<b>front</b> of the of the <code>circular_buffer</code> in order to achieve the desired size. In the case
the resulting size exceeds the current capacity the capacity will be set to <code>new_size</code>.<br>
If the current number of elements stored in the <code>circular_buffer</code> is greater than the desired
new size then number of <code>[size() - new_size]</code> <b>first</b> elements will be removed. (The
capacity will remain unchanged.)
\param new_size The new size.
\param item The element the <code>circular_buffer</code> will be filled with in order to gain the requested
size. (See the <i>Effect</i>.)
\throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
used).
Whatever <code>T::T(const T&)</code> throws or nothing if <code>T::T(T&&)</code> is noexcept.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
<code>end()</code>) if the new size is greater than the current capacity. Invalidates iterators pointing
to the removed elements if the new size is lower that the original size. Otherwise it does not invalidate
any iterator.
\par Complexity
Linear (in the new size of the <code>circular_buffer</code>).
\sa <code>\link resize() resize(size_type, const_reference)\endlink</code>,
<code>rset_capacity(capacity_type)</code>
*/
void rresize(size_type new_size, param_value_type item = value_type()) {
if (new_size > size()) {
if (new_size > capacity())
set_capacity(new_size);
rinsert(begin(), new_size - size(), item);
} else {
rerase(begin(), end() - new_size);
}
}