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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
|
// Implementation of the base circular buffer.
// Copyright (c) 2003
// Jan Gaspar, Whitestein Technologies
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
#if !defined(BOOST_CIRCULAR_BUFFER_BASE_HPP)
#define BOOST_CIRCULAR_BUFFER_BASE_HPP
#include <boost/concept_check.hpp>
#include <boost/iterator.hpp>
#include <boost/iterator_adaptors.hpp>
#include <boost/call_traits.hpp>
#include <boost/type_traits.hpp>
#include <boost/throw_exception.hpp>
#include <boost/assert.hpp>
#include <boost/version.hpp>
#if BOOST_VERSION >= 103100
#include <boost/iterator/reverse_iterator.hpp>
#endif
#include <memory>
#include <algorithm>
#if !defined(BOOST_NO_EXCEPTIONS)
#include <stdexcept>
#endif
namespace boost {
// Exception handling macros.
#if !defined(BOOST_NO_EXCEPTIONS)
#define BOOST_CB_TRY try {
#define BOOST_CB_UNWIND(action) } catch(...) { action; throw; }
#else
#define BOOST_CB_TRY
#define BOOST_CB_UNWIND(action)
#endif
namespace cb_details {
/*
\struct cb_int_iterator_tag
\brief Identifying tag for integer types (not for iterators).
*/
struct cb_int_iterator_tag {};
/*
\struct cb_iterator_category
\brief Defines iterator category.
*/
template <bool>
struct cb_iterator_category {
//! Represents iterators.
typedef std::input_iterator_tag iterator_category;
};
template <>
struct cb_iterator_category<true> {
//! Represents integral types (not iterators).
typedef cb_int_iterator_tag iterator_category;
};
/*
\struct cb_iterator_category_traits
\brief Defines the iterator category tag for the given iterator.
*/
template <class Iterator>
struct cb_iterator_category_traits {
//! Iterator category tag type.
/*!
Depending on the template parameter the <tt>tag</tt> distinguishes
between iterators and non-iterators. If the template parameter
is an iterator the <tt>tag</tt> is typedef for <tt>std::input_iterator_tag</tt>.
If the parameter is not an iterator the <tt>tag</tt> is typedef for
<tt>cb_int_iterator_tag</tt>.
*/
typedef typename cb_details::cb_iterator_category<
is_integral<Iterator>::value>::iterator_category tag;
};
template <class Traits> struct cb_nonconst_traits;
/*
\struct cb_const_traits
\brief Defines the data types for a const iterator.
\param Traits Defines the basic types.
*/
template <class Traits>
struct cb_const_traits {
// Basic types
typedef typename Traits::value_type value_type;
typedef typename Traits::const_pointer pointer;
typedef typename Traits::const_reference reference;
typedef typename Traits::size_type size_type;
typedef typename Traits::difference_type difference_type;
// Non-const traits
typedef cb_nonconst_traits<Traits> nonconst_traits;
};
/*
\struct cb_nonconst_traits
\brief Defines the data types for a non-const iterator.
\param Traits Defines the basic types.
*/
template <class Traits>
struct cb_nonconst_traits {
// Basic types
typedef typename Traits::value_type value_type;
typedef typename Traits::pointer pointer;
typedef typename Traits::reference reference;
typedef typename Traits::size_type size_type;
typedef typename Traits::difference_type difference_type;
// Non-const traits
typedef cb_nonconst_traits<Traits> nonconst_traits;
};
/*
\struct cb_internal_pointer
\brief Helper pointer used in the cb_iterator.
*/
template <class Traits0>
struct cb_helper_pointer {
bool m_end;
typename Traits0::pointer m_it;
};
/*
\class cb_iterator
\brief Random access iterator for the circular buffer.
\param Buff The type of the underlying circular buffer.
\param Traits Defines basic iterator types.
\note This iterator is not circular. It was designed
for iterating from begin() to end() of the circular buffer.
*/
template <class Buff, class Traits>
class cb_iterator :
public boost::iterator<
std::random_access_iterator_tag,
typename Traits::value_type,
typename Traits::difference_type,
typename Traits::pointer,
typename Traits::reference>
{
private:
// Helper types
//! Base iterator.
typedef boost::iterator<
std::random_access_iterator_tag,
typename Traits::value_type,
typename Traits::difference_type,
typename Traits::pointer,
typename Traits::reference> base_type;
//! Non-const iterator.
typedef cb_iterator<Buff, typename Traits::nonconst_traits> nonconst_self;
public:
// Basic types
//! The type of the elements stored in the circular buffer.
typedef typename base_type::value_type value_type;
//! Pointer to the element.
typedef typename base_type::pointer pointer;
//! Reference to the element.
typedef typename base_type::reference reference;
//! Size type.
typedef typename Traits::size_type size_type;
//! Difference type.
typedef typename base_type::difference_type difference_type;
public:
// Member variables
//! The circular buffer where the iterator points to.
const Buff* m_buff;
//! An internal iterator.
pointer m_it;
public:
// Construction & assignment
// Default copy constructor.
//! Default constructor.
cb_iterator() : m_buff(0), m_it(0) {}
//! Copy constructor (used for converting from a non-const to a const iterator).
cb_iterator(const nonconst_self& it)
: m_buff(it.m_buff), m_it(it.m_it) {}
//! Internal constructor.
/*!
\note This constructor is not intended to be used directly by the user.
*/
cb_iterator(const Buff* cb, const pointer it)
: m_buff(cb), m_it(it) {}
// Default assign operator.
public:
// Random access iterator methods
//! Dereferencing operator.
reference operator * () const {
BOOST_ASSERT(m_buff != 0); // uninitialized iterator
BOOST_ASSERT(m_it != 0); // iterator pointing to the end
return *m_it;
}
//! Dereferencing operator.
pointer operator -> () const { return &(operator*()); }
//! Difference operator.
difference_type operator - (const cb_iterator& it) const {
BOOST_ASSERT(m_buff != 0); // uninitialized iterator
BOOST_ASSERT(it.m_buff != 0); // uninitialized iterator
BOOST_ASSERT(m_buff == it.m_buff); // iterators of different containers or invalidated iterator
cb_helper_pointer<Traits> lhs = create_helper_pointer(*this);
cb_helper_pointer<Traits> rhs = create_helper_pointer(it);
if (less(rhs, lhs) && lhs.m_it <= rhs.m_it)
return lhs.m_it + m_buff->capacity() - rhs.m_it;
if (less(lhs, rhs) && lhs.m_it >= rhs.m_it)
return lhs.m_it - m_buff->capacity() - rhs.m_it;
return lhs.m_it - rhs.m_it;
}
//! Increment operator (prefix).
cb_iterator& operator ++ () {
BOOST_ASSERT(m_buff != 0); // uninitialized iterator
BOOST_ASSERT(m_it != 0); // iterator pointing to the end
m_buff->increment(m_it);
if (m_it == m_buff->m_last)
m_it = 0;
return *this;
}
//! Increment operator (postfix).
cb_iterator operator ++ (int) {
cb_iterator tmp = *this;
++*this;
return tmp;
}
//! Decrement operator (prefix).
cb_iterator& operator -- () {
BOOST_ASSERT(m_buff != 0); // uninitialized iterator
if (m_it == 0)
m_it = m_buff->m_last;
m_buff->decrement(m_it);
return *this;
}
//! Decrement operator (postfix).
cb_iterator operator -- (int) {
cb_iterator tmp = *this;
--*this;
return tmp;
}
//! Iterator addition.
cb_iterator& operator += (difference_type n) {
if (n > 0) {
BOOST_ASSERT(m_buff != 0); // uninitialized iterator
BOOST_ASSERT(m_it != 0); // iterator pointing to the end
m_it = m_buff->add(m_it, n);
if (m_it == m_buff->m_last)
m_it = 0;
} else if (n < 0) {
*this -= -n;
}
return *this;
}
//! Iterator addition.
cb_iterator operator + (difference_type n) const { return cb_iterator(*this) += n; }
//! Iterator subtraction.
cb_iterator& operator -= (difference_type n) {
if (n > 0) {
BOOST_ASSERT(m_buff != 0);
m_it = m_buff->sub(m_it == 0 ? m_buff->m_last : m_it, n);
} else if (n < 0) {
*this += -n;
}
return *this;
}
//! Iterator subtraction.
cb_iterator operator - (difference_type n) const { return cb_iterator(*this) -= n; }
//! Element access operator.
reference operator [] (difference_type n) const { return *(*this + n); }
public:
// Equality & comparison
//! Equality.
template <class Traits0>
bool operator == (const cb_iterator<Buff, Traits0>& it) const {
BOOST_ASSERT(m_buff != 0); // uninitialized iterator
BOOST_ASSERT(it.m_buff != 0); // uninitialized iterator
BOOST_ASSERT(m_buff == it.m_buff); // iterators of different containers or invalidated iterator
return m_it == it.m_it;
}
//! Inequality.
template <class Traits0>
bool operator != (const cb_iterator<Buff, Traits0>& it) const {
BOOST_ASSERT(m_buff != 0); // uninitialized iterator
BOOST_ASSERT(it.m_buff != 0); // uninitialized iterator
BOOST_ASSERT(m_buff == it.m_buff); // iterators of different containers or invalidated iterator
return m_it != it.m_it;
}
//! Less.
template <class Traits0>
bool operator < (const cb_iterator<Buff, Traits0>& it) const {
BOOST_ASSERT(m_buff != 0); // uninitialized iterator
BOOST_ASSERT(it.m_buff != 0); // uninitialized iterator
BOOST_ASSERT(m_buff == it.m_buff); // iterators of different containers or invalidated iterator
return less(create_helper_pointer(*this), create_helper_pointer(it));
}
//! Greater.
template <class Traits0>
bool operator > (const cb_iterator<Buff, Traits0>& it) const { return it < *this; }
//! Less or equal.
template <class Traits0>
bool operator <= (const cb_iterator<Buff, Traits0>& it) const { return !(it < *this); }
//! Greater or equal.
template <class Traits0>
bool operator >= (const cb_iterator<Buff, Traits0>& it) const { return !(*this < it); }
private:
// Helpers
//! Create helper pointer.
template <class Traits0>
cb_helper_pointer<Traits0> create_helper_pointer(const cb_iterator<Buff, Traits0>& it) const {
cb_helper_pointer<Traits0> helper;
helper.m_end = (it.m_it == 0);
helper.m_it = helper.m_end ? m_buff->m_last : it.m_it;
return helper;
}
//! Compare two pointers.
/*!
\return 1 if p1 is greater than p2.
\return 0 if p1 is equal to p2.
\return -1 if p1 is lower than p2.
*/
template <class Pointer0, class Pointer1>
static difference_type compare(Pointer0 p1, Pointer1 p2) {
return p1 < p2 ? -1 : (p1 > p2 ? 1 : 0);
}
//! Less.
template <class InternalIterator0, class InternalIterator1>
bool less(const InternalIterator0& lhs, const InternalIterator1& rhs) const {
switch (compare(lhs.m_it, m_buff->m_first)) {
case -1:
switch (compare(rhs.m_it, m_buff->m_first)) {
case -1: return lhs.m_it < rhs.m_it;
case 0: return rhs.m_end;
case 1: return false;
}
case 0:
switch (compare(rhs.m_it, m_buff->m_first)) {
case -1: return !lhs.m_end;
case 0: return !lhs.m_end && rhs.m_end;
case 1: return !lhs.m_end;
}
case 1:
switch (compare(rhs.m_it, m_buff->m_first)) {
case -1: return true;
case 0: return rhs.m_end;
case 1: return lhs.m_it < rhs.m_it;
}
}
return false;
}
};
//! Iterator addition.
template <class Buff, class Traits>
inline cb_iterator<Buff, Traits>
operator + (typename Traits::difference_type n, const cb_iterator<Buff, Traits>& it) {
return it + n;
}
#if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR)
//! Iterator category.
template <class Buff, class Traits>
inline std::random_access_iterator_tag
iterator_category(const cb_iterator<Buff, Traits>&) {
return std::random_access_iterator_tag();
}
//! The type of the elements stored in the circular buffer.
template <class Buff, class Traits>
inline typename Traits::value_type*
value_type(const cb_iterator<Buff, Traits>&) { return 0; }
//! Distance type.
template <class Buff, class Traits>
inline typename Traits::difference_type*
distance_type(const cb_iterator<Buff, Traits>&) { return 0; }
#endif // #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR)
}; // namespace cb_details
/*!
\class circular_buffer
\brief Circular buffer - a STL compliant container.
\param T The type of the elements stored in the circular buffer.
\param Alloc The allocator type used for all internal memory management.
\author <a href="mailto:jano_gaspar@yahoo.com">Jan Gaspar</a>
\version 3.3
\date 2003
For more information how to use the circular buffer see the
<a href="../circular_buffer.html">documentation</a>.
*/
template <class T, class Alloc>
class circular_buffer {
// Requirements
BOOST_CLASS_REQUIRE(T, boost, AssignableConcept);
public:
// Basic types
//! The type of the elements stored in the circular buffer.
typedef typename Alloc::value_type value_type;
//! Pointer to the element.
typedef typename Alloc::pointer pointer;
//! Const pointer to the element.
typedef typename Alloc::const_pointer const_pointer;
//! Reference to the element.
typedef typename Alloc::reference reference;
//! Const reference to the element.
typedef typename Alloc::const_reference const_reference;
//! Size type.
typedef typename Alloc::size_type size_type;
//! Difference type.
typedef typename Alloc::difference_type difference_type;
//! The type of the allocator used in the circular buffer.
typedef Alloc allocator_type;
//! Return the allocator.
/*!
\return Allocator
*/
allocator_type get_allocator() const { return m_alloc; }
// Helper types
// Define a type that represents the "best" way to pass the value_type to a method.
typedef typename call_traits<value_type>::param_type param_value_type;
// Iterators
//! Const (random access) iterator used to iterate through a circular buffer.
typedef cb_details::cb_iterator< circular_buffer<T, Alloc>, cb_details::cb_const_traits<Alloc> > const_iterator;
//! Iterator (random access) used to iterate through a circular buffer.
typedef cb_details::cb_iterator< circular_buffer<T, Alloc>, cb_details::cb_nonconst_traits<Alloc> > iterator;
#if BOOST_VERSION >= 103100
//! Const iterator used to iterate backwards through a circular buffer.
typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
//! Iterator used to iterate backwards through a circular buffer.
typedef boost::reverse_iterator<iterator> reverse_iterator;
#else
//! Const iterator used to iterate backwards through a circular buffer.
typedef typename reverse_iterator_generator<const_iterator>::type const_reverse_iterator;
//! Iterator used to iterate backwards through a circular buffer.
typedef typename reverse_iterator_generator<iterator>::type reverse_iterator;
#endif
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 (the leftmost element).
pointer m_first;
//! The virtual end of the circular buffer (the rightmost element).
pointer m_last;
//! The number of items currently stored in the circular buffer.
size_type m_size;
//! The allocator.
allocator_type m_alloc;
// Friends
#if defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
friend iterator;
friend const_iterator;
#else
friend struct cb_details::cb_iterator< circular_buffer<T, Alloc>, cb_details::cb_const_traits<Alloc> >;
friend struct cb_details::cb_iterator< circular_buffer<T, Alloc>, cb_details::cb_nonconst_traits<Alloc> >;
#endif
public:
// Element access
//! Return an iterator pointing to the beginning of the circular buffer.
iterator begin() { return iterator(this, empty() ? 0 : m_first); }
//! Return an iterator pointing to the end of the circular buffer.
iterator end() { return iterator(this, 0); }
//! Return a const iterator pointing to the beginning of the circular buffer.
const_iterator begin() const { return const_iterator(this, empty() ? 0 : m_first); }
//! Return a const iterator pointing to the end of the circular buffer.
const_iterator end() const { return const_iterator(this, 0); }
//! Return a reverse iterator pointing to the beginning of the reversed circular buffer.
reverse_iterator rbegin() { return reverse_iterator(end()); }
//! Return a reverse iterator pointing to the end of the reversed circular buffer.
reverse_iterator rend() { return reverse_iterator(begin()); }
//! Return a const reverse iterator pointing to the beginning of the reversed circular buffer.
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
//! Return a const reverse iterator pointing to the end of the reversed circular buffer.
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
//! Return the element at the <tt>index</tt> position.
reference operator [] (size_type index) { return *add(m_first, index); }
//! Return the element at the <tt>index</tt> position.
const_reference operator [] (size_type index) const { return *add(m_first, index); }
//! Return the element at the <tt>index</tt> position.
/*!
\throws std::out_of_range thrown when the <tt>index</tt> is invalid.
*/
reference at(size_type index) {
check_position(index);
return (*this)[index];
}
//! Return the element at the <tt>index</tt> position.
/*!
\throws std::out_of_range thrown when the <tt>index</tt> is invalid.
*/
const_reference at(size_type index) const {
check_position(index);
return (*this)[index];
}
//! Return the first (leftmost) element.
reference front() { return *m_first; }
//! Return the last (rightmost) element.
reference back() { return *((m_last == m_buff ? m_end : m_last) - 1); }
//! Return the first (leftmost) element.
const_reference front() const { return *m_first; }
//! Return the last (rightmost) element.
const_reference back() const { return *((m_last == m_buff ? m_end : m_last) - 1); }
//! Return pointer to data stored in the circular buffer as a continuous array of values.
/*!
This method can be usefull e.g. when passing the stored data into the legacy C API.
\post <tt>\&(*this)[0] \< \&(*this)[1] \< ... \< \&(*this).back()</tt>
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
pointer data() {
if (m_first < m_last || m_first == m_buff)
return m_first;
size_type constructed = 0;
pointer src = m_first;
pointer dest = m_buff;
BOOST_CB_TRY
for (pointer first = m_first; dest < src; src = first) {
for (size_type ii = 0; src < m_end; ++src, ++dest, ++ii) {
if (dest == first) {
first += ii;
break;
}
if (is_uninitialized(dest)) {
m_alloc.construct(dest, *src);
++constructed;
} else {
std::swap(*dest, *src);
}
}
}
BOOST_CB_UNWIND(
for (dest = m_last; constructed > 0; ++dest, --constructed)
m_alloc.destroy(dest);
)
for (dest = m_buff + size(); dest < m_end; ++dest)
m_alloc.destroy(dest);
m_first = m_buff;
m_last = add(m_buff, size());
return m_buff;
}
// Size and capacity
//! Return the number of elements currently stored in the circular buffer.
size_type size() const { return m_size; }
//! Return the largest possible size (or capacity) of the circular buffer.
size_type max_size() const { return m_alloc.max_size(); }
//! Is the circular buffer empty?
/*!
\return true if there are no elements stored in the circular buffer.
\return false otherwise.
*/
bool empty() const { return size() == 0; }
//! Is the circular buffer full?
/*!
\return true if the number of elements stored in the circular buffer
equals the capacity of the circular buffer.
\return false otherwise.
*/
bool full() const { return size() == capacity(); }
//! Return the capacity of the circular buffer.
size_type capacity() const { return m_end - m_buff; }
//! Change the capacity of the circular buffer.
/*!
\post <tt>(*this).capacity() == new_capacity</tt><br>
If the current number of elements stored in the circular
buffer is greater than the desired new capacity then the
first (leftmost) <tt>((*this).size() - new_capacity)</tt> elements
will be removed.
\throws "An allocation error" if memory is exhausted (<tt>std::bad_alloc</tt> if standard allocator is used).
\throws Whatever T::T(const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void set_capacity(size_type new_capacity) {
if (new_capacity == capacity())
return;
pointer buff = allocate(new_capacity);
size_type new_size = new_capacity < size() ? new_capacity : size();
BOOST_CB_TRY
std::uninitialized_copy(end() - new_size, end(), buff);
BOOST_CB_UNWIND(deallocate(buff, new_capacity))
destroy();
m_size = new_size;
m_buff = m_first = buff;
m_end = m_buff + new_capacity;
m_last = add(m_buff, size());
}
//! Change the size of the circular buffer.
/*!
\post <tt>(*this).size() == new_size</tt><br>
If the new size is greater than the current size, the rest
of the circular buffer is filled with copies of <tt>item</tt>.
In case the resulting size exceeds the current capacity
the capacity is set to <tt>new_size</tt>.
If the new size is lower than the current size, the first
(leftmost) <tt>((*this).size() - new_size)</tt> elements will be removed.
\throws "An allocation error" if memory is exhausted (<tt>std::bad_alloc</tt> if standard allocator is used).
\throws Whatever T::T(const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void resize(size_type new_size, param_value_type item = T()) {
if (new_size > size()) {
if (new_size > capacity())
set_capacity(new_size);
insert(end(), new_size - size(), item);
} else {
erase(begin(), end() - new_size);
}
}
// Construction/Destruction
//! Create an empty circular buffer with a given capacity.
/*!
\post <tt>(*this).capacity() == capacity \&\& (*this).size == 0</tt>
\throws "An allocation error" if memory is exhausted (<tt>std::bad_alloc</tt> if standard allocator is used).
*/
explicit circular_buffer(
size_type capacity,
const allocator_type& a = allocator_type())
: m_size(0), m_alloc(a) {
m_first = m_last = m_buff = allocate(capacity);
m_end = m_buff + capacity;
}
//! Create a full circular buffer with a given capacity and filled with copies of <tt>item</tt>.
/*!
\post <tt>(*this).size() == capacity \&\& (*this)[0] == (*this)[1] == ... == (*this).back() == item</tt>
\throws "An allocation error" if memory is exhausted (<tt>std::bad_alloc</tt> if standard allocator is used).
\throws Whatever T::T(const T&) throws.
*/
circular_buffer(
size_type capacity,
param_value_type item,
const allocator_type& a = allocator_type())
: m_size(capacity), m_alloc(a) {
m_first = m_last = m_buff = allocate(capacity);
m_end = m_buff + capacity;
BOOST_CB_TRY
std::uninitialized_fill_n(m_buff, size(), item);
BOOST_CB_UNWIND(deallocate(m_buff, capacity))
}
//! Copy constructor.
/*!
\post <tt>*this == cb</tt>
\throws "An allocation error" if memory is exhausted (<tt>std::bad_alloc</tt> if standard allocator is used).
\throws Whatever T::T(const T&) throws.
*/
circular_buffer(const circular_buffer<T, Alloc>& cb)
: m_size(cb.size()), m_alloc(cb.get_allocator()) {
m_first = m_last = m_buff = allocate(cb.capacity());
BOOST_CB_TRY
m_end = std::uninitialized_copy(cb.begin(), cb.end(), m_buff);
BOOST_CB_UNWIND(deallocate(m_buff, cb.capacity()))
}
//! Create a circular buffer with a copy of a range.
/*!
\post <tt>(*this).capacity() == capacity</tt><br>
If the number of items to copy from the range
<tt>[first, last)</tt> is greater than the specified
<tt>capacity</tt> then only elements from the range
<tt>[last - capacity, last)</tt> will be copied.
\throws "An allocation error" if memory is exhausted (<tt>std::bad_alloc</tt> if standard allocator is used).
\throws Whatever T::T(const T&) throws.
*/
template <class InputIterator>
circular_buffer(
size_type capacity,
InputIterator first,
InputIterator last,
const allocator_type& a = allocator_type())
: m_alloc(a) {
m_first = m_buff = allocate(capacity);
m_end = m_buff + capacity;
size_type diff = std::distance(first, last);
if (diff > capacity) {
std::advance(first, diff - capacity);
m_size = capacity;
m_last = m_buff;
} else {
m_size = diff;
if (diff == capacity)
m_last = m_buff;
else
m_last = m_buff + size();
}
BOOST_CB_TRY
std::uninitialized_copy(first, last, m_buff);
BOOST_CB_UNWIND(deallocate(m_buff, capacity))
}
//! Destructor.
~circular_buffer() { destroy(); }
private:
// Helper functors
// Functor for assigning n items.
struct assign_n {
size_type m_n;
param_value_type m_item;
assign_n(size_type n, param_value_type item) : m_n(n), m_item(item) {}
void operator () (pointer p) const {
std::uninitialized_fill_n(p, m_n, m_item);
}
private:
assign_n& operator = (const assign_n&); // do not generate
};
// Functor for assigning range of items.
template <class InputIterator>
struct assign_range {
InputIterator m_first;
InputIterator m_last;
assign_range(InputIterator first, InputIterator last) : m_first(first), m_last(last) {}
void operator() (pointer p) const {
std::uninitialized_copy(m_first, m_last, p);
}
};
public:
// Assign methods
//! Assignment operator.
/*!
\post <tt>*this == cb</tt>
\throws "An allocation error" if memory is exhausted (<tt>std::bad_alloc</tt> if standard allocator is used).
\throws Whatever T::T(const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
circular_buffer<T, Alloc>& operator = (const circular_buffer<T, Alloc>& cb) {
if (this == &cb)
return *this;
pointer buff = allocate(cb.capacity());
BOOST_CB_TRY
pointer last = std::uninitialized_copy(cb.begin(), cb.end(), buff);
destroy();
m_size = cb.size();
m_first = m_buff = buff;
m_end = m_buff + cb.capacity();
m_last = full() ? m_buff : last;
BOOST_CB_UNWIND(deallocate(buff, cb.capacity()))
return *this;
}
//! Assign <tt>n</tt> items into the circular buffer.
/*!
\post <tt>(*this).size() == n \&\&
(*this)[0] == (*this)[1] == ... == (*this).back() == item</tt><br>
If the number of items to be assigned exceeds
the capacity of the circular buffer the capacity
is increased to <tt>n</tt> otherwise it stays unchanged.
\throws "An allocation error" if memory is exhausted (<tt>std::bad_alloc</tt> if standard allocator is used).
\throws Whatever T::T(const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void assign(size_type n, param_value_type item) { do_assign(n, assign_n(n, item)); }
//! Assign a copy of range.
/*!
\post <tt>(*this).size() == std::distance(first, last)</tt><br>
If the number of items to be assigned exceeds
the capacity of the circular buffer the capacity
is set to that number otherwise is stays unchanged.
\throws "An allocation error" if memory is exhausted (<tt>std::bad_alloc</tt> if standard allocator is used).
\throws Whatever T::T(const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
template <class InputIterator>
void assign(InputIterator first, InputIterator last) {
assign(first, last, cb_details::cb_iterator_category_traits<InputIterator>::tag());
}
//! Swap the contents of two circular buffers.
/*!
\post <tt>this</tt> contains elements of <tt>cb</tt> and vice versa.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void swap(circular_buffer& cb) {
std::swap(m_alloc, cb.m_alloc); // in general this is not necessary,
// because allocators should not have state
std::swap(m_buff, cb.m_buff);
std::swap(m_end, cb.m_end);
std::swap(m_first, cb.m_first);
std::swap(m_last, cb.m_last);
std::swap(m_size, cb.m_size);
}
// push and pop
//! Insert a new element at the end.
/*!
\post <tt>(*this).back() == item</tt><br>
If the circular buffer is full, the first (leftmost) element will be removed.
\throws Whatever T::T(const T&) throws.
\throws Whatever T::operator = (const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void push_back(param_value_type item) {
if (full()) {
if (empty())
return;
*m_last = item;
increment(m_first);
m_last = m_first;
} else {
m_alloc.construct(m_last, item);
increment(m_last);
++m_size;
}
}
//! Insert a new element with the default value at the end.
/*!
\post <tt>(*this).back() == value_type()</tt><br>
If the circular buffer is full, the first (leftmost) element will be removed.
\throws Whatever T::T(const T&) throws.
\throws Whatever T::operator = (const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void push_back() { push_back(value_type()); }
//! Insert a new element at the start.
/*!
\post <tt>(*this).front() == item</tt><br>
If the circular buffer is full, the last (rightmost) element will be removed.
\throws Whatever T::T(const T&) throws.
\throws Whatever T::operator = (const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void push_front(param_value_type item) {
BOOST_CB_TRY
if (full()) {
if (empty())
return;
decrement(m_first);
*m_first = item;
m_last = m_first;
} else {
decrement(m_first);
m_alloc.construct(m_first, item);
++m_size;
}
BOOST_CB_UNWIND(increment(m_first))
}
//! Insert a new element with the default value at the start.
/*!
\post <tt>(*this).front() == value_type()</tt><br>
If the circular buffer is full, the last (rightmost) element will be removed.
\throws Whatever T::T(const T&) throws.
\throws Whatever T::operator = (const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void push_front() { push_front(value_type()); }
//! Remove the last (rightmost) element.
/*!
\pre <tt>iterator it = (*this).end()</tt>
\post <tt>(*this).end() != it</tt>
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void pop_back() {
decrement(m_last);
m_alloc.destroy(m_last);
--m_size;
}
//! Remove the first (leftmost) element.
/*!
\pre <tt>iterator it = (*this).begin()</tt>
\post <tt>(*this).begin() != it</tt>
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void pop_front() {
m_alloc.destroy(m_first);
increment(m_first);
--m_size;
}
private:
// Helper wrappers
// Iterator dereference wrapper.
template <class InputIterator>
struct item_wrapper {
mutable InputIterator m_it;
item_wrapper(InputIterator it) : m_it(it) {}
operator const_reference () const { return *m_it++; }
};
public:
// Insert
//! Insert the <tt>item</tt> before the given position.
/*!
\post The <tt>item</tt> will be inserted at the position <tt>pos</tt>.<br>
If the circular buffer is full, the first (leftmost) element will be removed.
\return iterator to the inserted element.
\throws Whatever T::T(const T&) throws.
\throws Whatever T::operator = (const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
iterator insert(iterator pos, param_value_type item) {
if (full() && pos == begin())
return begin();
if (pos.m_it == 0) {
if (full())
*m_last = item;
else
m_alloc.construct(m_last, item);
pos.m_it = m_last;
} else {
pointer src = m_last;
pointer dest = m_last;
BOOST_CB_TRY
while (src != pos.m_it) {
decrement(src);
create_copy(dest, *src);
decrement(dest);
}
*pos = item;
BOOST_CB_UNWIND(
for (pointer it = m_last; it != dest; decrement(it))
destroy_copy(it);
)
}
increment(m_last);
if (full())
increment(m_first);
else
++m_size;
return iterator(this, pos.m_it);
}
//! Insert a new element with the default value before the given position.
/*!
\post <tt>value_type()</tt> will be inserted at the position <tt>pos</tt>.<br>
If the circular buffer is full, the first (leftmost) element will be removed.
\return iterator to the inserted element.
\throws Whatever T::T(const T&) throws.
\throws Whatever T::operator = (const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
iterator insert(iterator pos) { return insert(pos, value_type()); }
//! Insert <tt>n</tt> copies of the item before the given position.
/*!
\post This operation preserves the capacity of the circular buffer.
If the insertion would result in exceeding the capacity
of the circular buffer then the necessary number of elements
from the beginning (left) of the circular buffer will be removed
or not all <tt>n</tt> elements will be inserted or both.<tt><br>
Example:<br>
original circular buffer |1|2|3|4| | | - capacity: 6, size: 4<br>
position ---------------------^<br>
insert(position, (size_t)5, 6);<br>
(If the operation won't preserve capacity, the buffer
would look like this |1|2|6|6|6|6|6|3|4|)<br>
RESULTING circular buffer |6|6|6|6|3|4| - capacity: 6, size: 6</tt>
\throws Whatever T::T(const T&) throws.
\throws Whatever T::operator = (const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void insert(iterator pos, size_type n, param_value_type item) {
if (n == 0)
return;
size_type copy = capacity() - (end() - pos);
if (copy == 0)
return;
if (n > copy)
n = copy;
insert_n_item(pos, n, item);
}
//! Insert the range <tt>[first, last)</tt> before the given position.
/*!
\post This operation preserves the capacity of the circular buffer.
If the insertion would result in exceeding the capacity
of the circular buffer then the necessary number of elements
from the beginning (left) of the circular buffer will be removed
or not the whole range will be inserted or both.
In case the whole range cannot be inserted it will be inserted just
some elements from the end (right) of the range (see the example).<tt><br>
Example:<br>
array to insert: int array[] = { 5, 6, 7, 8, 9 };<br>
original circular buffer |1|2|3|4| | | - capacity: 6, size: 4<br>
position ---------------------^<br>
insert(position, array, array + 5);<br>
(If the operation won't preserve capacity, the buffer
would look like this |1|2|5|6|7|8|9|3|4|)<br>
RESULTING circular buffer |6|7|8|9|3|4| - capacity: 6, size: 6</tt>
\throws Whatever T::T(const T&) throws.
\throws Whatever T::operator = (const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
template <class InputIterator>
void insert(iterator pos, InputIterator first, InputIterator last) {
insert(pos, first, last, cb_details::cb_iterator_category_traits<InputIterator>::tag());
}
//! Insert an <tt>item</tt> before the given position.
/*!
\post The <tt>item</tt> will be inserted at the position <tt>pos</tt>.<br>
If the circular buffer is full, the last element (rightmost) will be removed.
\return iterator to the inserted element.
\throws Whatever T::T(const T&) throws.
\throws Whatever T::operator = (const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
iterator rinsert(iterator pos, param_value_type item) {
if (full() && pos == end())
return end();
if (pos == begin()) {
BOOST_CB_TRY
decrement(m_first);
if (full())
*m_first = item;
else
m_alloc.construct(m_first, item);
BOOST_CB_UNWIND(increment(m_first))
} else {
pointer src = m_first;
pointer dest = m_first;
pointer it = get_valid_pointer(pos.m_it);
decrement(dest);
BOOST_CB_TRY
while (src != it) {
create_copy(dest, *src);
increment(src);
increment(dest);
}
decrement(m_first);
*--pos = item;
BOOST_CB_UNWIND(
it = m_first;
for (increment(m_first); it != dest; increment(it))
destroy_copy(it);
)
}
if (full())
decrement(m_last);
else
++m_size;
return iterator(this, pos.m_it);
}
//! Insert a new element with the default value before the given position.
/*!
\post <tt>value_type()</tt> will be inserted at the position <tt>pos</tt>.<br>
If the circular buffer is full, the last (rightmost) element will be removed.
\return iterator to the inserted element.
\throws Whatever T::T(const T&) throws.
\throws Whatever T::operator = (const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
iterator rinsert(iterator pos) { return rinsert(pos, value_type()); }
//! Insert <tt>n</tt> copies of the item before the given position.
/*!
\post This operation preserves the capacity of the circular buffer.
If the insertion would result in exceeding the capacity
of the circular buffer then the necessary number of elements
from the end (right) of the circular buffer will be removed
or not all <tt>n</tt> elements will be inserted or both.<tt><br>
Example:<br>
original circular buffer |1|2|3|4| | | - capacity: 6, size: 4<br>
position ---------------------^<br>
insert(position, (size_t)5, 6);<br>
(If the operation won't preserve capacity, the buffer
would look like this |1|2|6|6|6|6|6|3|4|)<br>
RESULTING circular buffer |1|2|6|6|6|6| - capacity: 6, size: 6</tt>
\throws Whatever T::T(const T&) throws.
\throws Whatever T::operator = (const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void rinsert(iterator pos, size_type n, param_value_type item) { rinsert_n_item(pos, n, item); }
//! Insert the range <tt>[first, last)</tt> before the given position.
/*!
\post This operation preserves the capacity of the circular buffer.
If the insertion would result in exceeding the capacity
of the circular buffer then the necessary number of elements
from the end (right) of the circular buffer will be removed
or not the whole range will be inserted or both.
In case the whole range cannot be inserted it will be inserted just
some elements from the beginning (left) of the range (see the example).<tt><br>
Example:<br>
array to insert: int array[] = { 5, 6, 7, 8, 9 };<br>
original circular buffer |1|2|3|4| | | - capacity: 6, size: 4<br>
position ---------------------^<br>
insert(position, array, array + 5);<br>
(If the operation won't preserve capacity, the buffer
would look like this |1|2|5|6|7|8|9|3|4|)<br>
RESULTING circular buffer |1|2|5|6|7|8| - capacity: 6, size: 6</tt>
\throws Whatever T::T(const T&) throws.
\throws Whatever T::operator = (const T&) throws.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
template <class InputIterator>
void rinsert(iterator pos, InputIterator first, InputIterator last) {
rinsert(pos, first, last, cb_details::cb_iterator_category_traits<InputIterator>::tag());
}
// Erase
//! Erase the element at the given position.
/*!
\pre <tt>size_type old_size = (*this).size()</tt>
\post <tt>(*this).size() == old_size - 1</tt><br>
Removes an element at the position <tt>pos</tt>.
\return iterator to the first element remaining beyond the removed
element or <tt>(*this).end()</tt> if no such element exists.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
iterator erase(iterator pos) {
std::copy(pos + 1, end(), pos);
decrement(m_last);
m_alloc.destroy(m_last);
--m_size;
return iterator(this, pos.m_it == m_last ? 0 : pos.m_it);
}
//! Erase the range <tt>[first, last)</tt>.
/*!
\pre <tt>size_type old_size = (*this).size()</tt>
\post <tt>(*this).size() == old_size - std::distance(first, last)</tt><br>
Removes the elements from the range <tt>[first, last)</tt>.
\return iterator to the first element remaining beyond the removed
element or <tt>(*this).end()</tt> if no such element exists.
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
iterator erase(iterator first, iterator last) {
if (first != last)
std::copy(last, end(), first);
difference_type diff = last - first;
m_size -= diff;
for (; diff > 0; --diff) {
decrement(m_last);
m_alloc.destroy(m_last);
}
return iterator(this, first.m_it == m_last ? 0 : first.m_it);
}
//! Erase all the stored elements.
/*!
\post (*this).size() == 0
\note For iterator invalidation see the <a href="../circular_buffer.html#invalidation">documentation</a>.
*/
void clear() {
destroy_content();
m_first = m_last = m_buff;
m_size = 0;
}
private:
// Helper methods
//! Check if the <tt>index</tt> is valid.
void check_position(size_type index) const {
if (index >= size())
throw_exception(std::out_of_range("circular_buffer"));
}
//! Increment the pointer.
template <class Pointer0>
void increment(Pointer0& p) const {
if (++p == m_end)
p = m_buff;
}
//! Decrement the pointer.
template <class Pointer0>
void decrement(Pointer0& p) const {
if (p == m_buff)
p = m_end;
--p;
}
//! Add <tt>n</tt> to the pointer.
template <class Pointer0>
Pointer0 add(Pointer0 p, difference_type n) const {
return p + (n < (m_end - p) ? n : n - capacity());
}
//! Subtract <tt>n</tt> from the pointer.
template <class Pointer0>
Pointer0 sub(Pointer0 p, difference_type n) const {
return p - (n > (p - m_buff) ? n - capacity() : n);
}
//! Return valid pointer.
pointer get_valid_pointer(pointer p) const { return p == 0 ? m_last : p; }
//! Does the pointer point to the uninitialized memory?
bool is_uninitialized(pointer p) const { return p >= m_last && (m_first < m_last || p < m_first); }
//! Create a copy of the <tt>item</tt> at the given position.
/*!
The copy is created either at uninitialized memory
or replaces the old item.
*/
void create_copy(pointer pos, param_value_type item) {
if (is_uninitialized(pos))
m_alloc.construct(pos, item);
else
*pos = item;
}
//! Try to recover when the create_copy fails.
void destroy_copy(pointer pos) {
if (is_uninitialized(pos))
m_alloc.destroy(pos);
// the assignment cannot be rolled back
}
//! Allocate memory.
pointer allocate(size_type n) {
if (n > max_size())
throw_exception(std::length_error("circular_buffer"));
return (n == 0) ? 0 : m_alloc.allocate(n, 0);
}
//! Deallocate memory.
void deallocate(pointer p, size_type n) {
if (p != 0)
m_alloc.deallocate(p, n);
}
//! Destroy the content of the circular buffer.
void destroy_content() {
iterator last = end();
for (iterator it = begin(); it != last; ++it)
m_alloc.destroy(it.m_it);
}
//! Destroy content and frees allocated memory.
void destroy() {
destroy_content();
deallocate(m_buff, capacity());
}
//! Helper assign method.
template <class InputIterator>
void assign(InputIterator n, InputIterator item, cb_details::cb_int_iterator_tag) {
assign((size_type)n, item);
}
//! Helper assign method.
template <class InputIterator>
void assign(InputIterator first, InputIterator last, std::input_iterator_tag) {
do_assign(std::distance(first, last), assign_range<InputIterator>(first, last));
}
//! Helper assign method.
template <class Functor>
void do_assign(size_type n, const Functor& fnc) {
if (n > capacity()) {
pointer buff = allocate(n);
BOOST_CB_TRY
fnc(buff);
BOOST_CB_UNWIND(deallocate(buff, n))
destroy();
m_buff = buff;
m_end = m_buff + n;
} else {
destroy_content();
BOOST_CB_TRY
fnc(m_buff);
BOOST_CB_UNWIND(m_size = 0;)
}
m_size = n;
m_first = m_buff;
m_last = add(m_buff, size());
}
//! Helper insert method.
template <class InputIterator>
void insert(iterator pos, InputIterator n, InputIterator item, cb_details::cb_int_iterator_tag) {
insert(pos, (size_type)n, item);
}
//! Helper insert method.
template <class InputIterator>
void insert(iterator pos, InputIterator first, InputIterator last, std::input_iterator_tag) {
difference_type n = std::distance(first, last);
if (n == 0)
return;
difference_type copy = capacity() - (end() - pos);
if (copy == 0)
return;
if (n > copy) {
std::advance(first, n - copy);
n = copy;
}
insert_n_item(pos, n, item_wrapper<InputIterator>(first));
}
//! Helper insert method.
template <class Item>
void insert_n_item(iterator pos, size_type n, const Item& item) {
size_type construct = capacity() - size();
if (construct > n)
construct = n;
if (pos.m_it == 0) {
size_type ii = 0;
pointer p = m_last;
BOOST_CB_TRY
for (; ii < construct; ++ii, increment(p))
m_alloc.construct(p, item);
for (;ii < n; ++ii, increment(p))
*p = item;
BOOST_CB_UNWIND(
size_type unwind = ii < construct ? ii : construct;
for (ii = 0, p = m_last; ii < unwind; ++ii, increment(p))
m_alloc.destroy(p);
)
} else {
pointer src = m_last;
pointer dest = add(m_last, n - 1);
pointer p = pos.m_it;
size_type ii = 0;
BOOST_CB_TRY
while (src != p) {
decrement(src);
create_copy(dest, *src);
decrement(dest);
}
for (; ii < n; ++ii, increment(p))
create_copy(p, item);
BOOST_CB_UNWIND(
for (p = add(m_last, n - 1); p != dest; decrement(p))
destroy_copy(p);
for (n = 0, p = pos.m_it; n < ii; ++n, increment(p))
destroy_copy(p);
)
}
m_last = add(m_last, n);
m_first = add(m_first, n - construct);
m_size += construct;
}
//! Helper rinsert method.
template <class InputIterator>
void rinsert(iterator pos, InputIterator n, InputIterator item, cb_details::cb_int_iterator_tag) {
rinsert(pos, (size_type)n, item);
}
//! Helper rinsert method.
template <class InputIterator>
void rinsert(iterator pos, InputIterator first, InputIterator last, std::input_iterator_tag) {
rinsert_n_item(pos, std::distance(first, last), item_wrapper<InputIterator>(first));
}
//! Helper rinsert method.
template <class Item>
void rinsert_n_item(iterator pos, size_type n, const Item& item) {
if (n == 0)
return;
size_type copy = capacity() - (pos - begin());
if (copy == 0)
return;
if (n > copy)
n = copy;
size_type construct = capacity() - size();
if (construct > n)
construct = n;
if (pos == begin()) {
pointer p = sub(get_valid_pointer(pos.m_it), n);
size_type ii = n;
BOOST_CB_TRY
for (;ii > construct; --ii, increment(p))
*p = item;
for (; ii > 0; --ii, increment(p))
m_alloc.construct(p, item);
BOOST_CB_UNWIND(
size_type unwind = ii < construct ? construct - ii : 0;
p = sub(get_valid_pointer(pos.m_it), construct);
for (ii = 0; ii < unwind; ++ii, increment(p))
m_alloc.destroy(p);
)
} else {
pointer src = m_first;
pointer dest = sub(m_first, n);
pointer p = get_valid_pointer(pos.m_it);
size_type ii = 0;
BOOST_CB_TRY
while (src != p) {
create_copy(dest, *src);
increment(src);
increment(dest);
}
p = sub(p, n);
for (; ii < n; ++ii, increment(p))
create_copy(p, item);
BOOST_CB_UNWIND(
for (p = sub(m_first, n); p != dest; increment(p))
destroy_copy(p);
p = sub(get_valid_pointer(pos.m_it), n);
for (n = 0; n < ii; ++n, increment(p))
destroy_copy(p);
)
}
m_first = sub(m_first, n);
m_last = sub(m_last, n - construct);
m_size += construct;
}
};
// Non-member functions
//! Test two circular buffers for equality.
template <class T, class Alloc>
inline bool operator == (const circular_buffer<T, Alloc>& lhs,
const circular_buffer<T, Alloc>& rhs) {
return lhs.size() == rhs.size() &&
std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
//! Lexicographical comparison.
template <class T, class Alloc>
inline bool operator < (const circular_buffer<T, Alloc>& lhs,
const circular_buffer<T, Alloc>& rhs) {
return std::lexicographical_compare(
lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
#if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_MSVC)
//! Test two circular buffers for non-equality.
template <class T, class Alloc>
inline bool operator != (const circular_buffer<T, Alloc>& lhs,
const circular_buffer<T, Alloc>& rhs) {
return !(lhs == rhs);
}
//! Lexicographical comparison.
template <class T, class Alloc>
inline bool operator > (const circular_buffer<T, Alloc>& lhs,
const circular_buffer<T, Alloc>& rhs) {
return rhs < lhs;
}
//! Lexicographical comparison.
template <class T, class Alloc>
inline bool operator <= (const circular_buffer<T, Alloc>& lhs,
const circular_buffer<T, Alloc>& rhs) {
return !(rhs < lhs);
}
//! Lexicographical comparison.
template <class T, class Alloc>
inline bool operator >= (const circular_buffer<T, Alloc>& lhs,
const circular_buffer<T, Alloc>& rhs) {
return !(lhs < rhs);
}
//! Swap the contents of two circular buffers.
template <class T, class Alloc>
inline void swap(circular_buffer<T, Alloc>& lhs, circular_buffer<T, Alloc>& rhs) {
lhs.swap(rhs);
}
#endif // #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_MSVC)
#undef BOOST_CB_UNWIND
#undef BOOST_CB_TRY
} // namespace boost
#endif // #if !defined(BOOST_CIRCULAR_BUFFER_BASE_HPP)
|