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 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!--
(C) Jeremy Siek 2001.
(C) Gennaro Prota 2003-2004.
Distributed under 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)
-->
<!--
Copyright (c) 1996-1999
Silicon Graphics Computer Systems, Inc.
Permission to use, copy, modify, distribute and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appears in all copies and
that both that copyright notice and this permission notice appear
in supporting documentation. Silicon Graphics makes no
representations about the suitability of this software for any
purpose. It is provided "as is" without express or implied warranty.
Copyright (c) 1994
Hewlett-Packard Company
Permission to use, copy, modify, distribute and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appears in all copies and
that both that copyright notice and this permission notice appear
in supporting documentation. Hewlett-Packard Company makes no
representations about the suitability of this software for any
purpose. It is provided "as is" without express or implied warranty.
-->
<head>
<title>dynamic_bitset<Block, Allocator></title>
</head>
<body text="#000000" link="#006600" alink="#003300"
vlink="#7C7F87" bgcolor="#FFFFFF">
<img src="../../boost.png" alt="boost.png (6897 bytes)"
align="middle" width="277" height="86">
<p><!--end header-->
<br>
</p>
<h1>dynamic_bitset<Block, Allocator></h1>
<h2>Contents</h2>
<dl class="index">
<dt><a href="#description">Description</a></dt>
<dt><a href="#synopsis">Synopsis</a></dt>
<dt><a href="#definitions">Definitions</a></dt>
<dt><a href="#examples">Examples</a></dt>
<dt><a href="#rationale">Rationale</a></dt>
<dt><a href="#header-files">Header Files</a></dt>
<dt><a href="#template-parameters">Template Parameters</a></dt>
<dt><a href="#concepts-modeled">Concepts modeled</a></dt>
<dt><a href="#type-requirements">Type requirements</a></dt>
<dt><a href="#public-base-classes">Public base classes</a></dt>
<dt><a href="#nested-type-names">Nested type names</a></dt>
<dt><a href="#public-data-members">Public data members</a></dt>
<dt><a href="#constructors">Constructors</a></dt>
<dt><a href="#destructor">Destructor</a></dt>
<dt><a href="#member-functions">Member functions</a></dt>
<dt><a href="#non-member-functions">Non-member functions</a></dt>
<dt><a href="#exception-guarantees">Exception guarantees</a></dt>
<dt><a href="#changes-from-previous-ver"><b>Changes from previous version(s)</b></a></dt>
<dt><a href="#see-also">See also</a></dt>
<dt><a href="#acknowledgements">Acknowledgements</a></dt>
</dl>
<h3><a name="description">Description</a></h3>
<p>The <tt>dynamic_bitset</tt> class represents a set of bits. It
provides accesses to the value of individual bits via an
<tt>operator[]</tt> and provides all of the bitwise operators
that one can apply to builtin integers, such as
<tt>operator&</tt> and <tt>operator<<</tt>. The number
of bits in the set is specified at runtime via a parameter to the
constructor of the <tt>dynamic_bitset</tt>.</p>
<p>The <tt>dynamic_bitset</tt> class is nearly identical to the
<a href=
"http://www.sgi.com/tech/stl/bitset.html"><tt>std::bitset</tt></a>
class. The difference is that the size of the
<tt>dynamic_bitset</tt> (the number of bits) is specified at
run-time during the construction of a <tt>dynamic_bitset</tt>
object, whereas the size of a <tt>std::bitset</tt> is specified
at compile-time through an integer template parameter.</p>
<p>The main problem that <tt>dynamic_bitset</tt> is designed to
solve is that of representing a subset of a finite set. Each bit
represents whether an element of the finite set is in the subset
or not. As such the bitwise operations of
<tt>dynamic_bitset</tt>, such as <tt>operator&</tt> and
<tt>operator|</tt>, correspond to set operations, such as
intersection and union.</p>
<h3><a name="synopsis">Synopsis</a></h3>
<pre>
namespace boost {
template <typename Block, typename Allocator>
class dynamic_bitset
{
public:
typedef Block <a href="#block_type">block_type</a>;
typedef Allocator <a href="#allocator_type">allocator_type</a>;
typedef <i>implementation-defined</i> <a href="#size_type">size_type</a>;
static const int <a href=
"#bits_per_block">bits_per_block</a> = <i>implementation-defined</i>;
static const size_type <a href=
"#npos">npos</a> = <i>implementation-defined</i>;
class <a href="#reference">reference</a>
{
void operator&(); // not defined
public:
// An automatically generated copy constructor.
reference& operator=(bool value);
reference& operator=(const reference& rhs);
reference& operator|=(bool value);
reference& operator&=(bool value);
reference& operator^=(bool value);
reference& operator-=(bool value);
bool operator~() const;
operator bool() const;
reference& flip();
};
typedef bool <a href="#const_reference">const_reference</a>;
explicit <a href=
"#cons1">dynamic_bitset</a>(const Allocator& alloc = Allocator());
explicit <a href=
"#cons2">dynamic_bitset</a>(size_type num_bits, unsigned long value = 0,
const Allocator& alloc = Allocator());
template <typename CharT, typename Traits, typename Alloc>
explicit <a href=
"#cons3">dynamic_bitset</a>(const std::basic_string<CharT, Traits, Alloc>& s,
typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0,
typename std::basic_string<CharT, Traits, Alloc>::size_type n = std::basic_string<CharT, Traits, Alloc>::npos,
const Allocator& alloc = Allocator());
template <typename BlockInputIterator>
<a href=
"#cons4">dynamic_bitset</a>(BlockInputIterator first, BlockInputIterator last,
const Allocator& alloc = Allocator());
<a href=
"#cons5">dynamic_bitset</a>(const dynamic_bitset& b);
void <a href="#swap">swap</a>(dynamic_bitset& b);
dynamic_bitset& <a href=
"#assign">operator=</a>(const dynamic_bitset& b);
allocator_type <a href="#get_allocator">get_allocator()</a> const;
void <a href=
"#resize">resize</a>(size_type num_bits, bool value = false);
void <a href="#clear">clear</a>();
void <a href="#push_back">push_back</a>(bool bit);
void <a href="#append1">append</a>(Block block);
template <typename BlockInputIterator>
void <a href=
"#append2">append</a>(BlockInputIterator first, BlockInputIterator last);
dynamic_bitset& <a href=
"#op-and-assign">operator&=</a>(const dynamic_bitset& b);
dynamic_bitset& <a href=
"#op-or-assign">operator|=</a>(const dynamic_bitset& b);
dynamic_bitset& <a href=
"#op-xor-assign">operator^=</a>(const dynamic_bitset& b);
dynamic_bitset& <a href=
"#op-sub-assign">operator-=</a>(const dynamic_bitset& b);
dynamic_bitset& <a href=
"#op-sl-assign">operator<<=</a>(size_type n);
dynamic_bitset& <a href=
"#op-sr-assign">operator>>=</a>(size_type n);
dynamic_bitset <a href=
"#op-sl">operator<<</a>(size_type n) const;
dynamic_bitset <a href=
"#op-sr">operator>></a>(size_type n) const;
dynamic_bitset& <a href=
"#set2">set</a>(size_type n, bool val = true);
dynamic_bitset& <a href="#set1">set</a>();
dynamic_bitset& <a href="#reset2">reset</a>(size_type n);
dynamic_bitset& <a href="#reset1">reset</a>();
dynamic_bitset& <a href="#flip2">flip</a>(size_type n);
dynamic_bitset& <a href="#flip1">flip</a>();
bool <a href="#test">test</a>(size_type n) const;
bool <a href="#any">any</a>() const;
bool <a href="#none">none</a>() const;
dynamic_bitset <a href="#op-not">operator~</a>() const;
size_type <a href="#count">count</a>() const;
reference <a href=
"#bracket">operator[]</a>(size_type pos);
bool <a href=
"#const-bracket">operator[]</a>(size_type pos) const;
unsigned long <a href="#to_ulong">to_ulong</a>() const;
size_type <a href="#size">size</a>() const;
size_type <a href="#num_blocks">num_blocks</a>() const;
size_type <a href="#max_size">max_size</a>() const;
bool <a href="#empty">empty</a>() const;
bool <a href=
"#is_subset_of">is_subset_of</a>(const dynamic_bitset& a) const;
bool <a href=
"#is_proper_subset_of">is_proper_subset_of</a>(const dynamic_bitset& a) const;
size_type <a href="#find_first">find_first</a>() const;
size_type <a href="#find_next">find_next</a>(size_type pos) const;
};
template <typename B, typename A>
bool <a href=
"#op-equal">operator==</a>(const dynamic_bitset<B, A>& a, const dynamic_bitset<B, A>& b);
template <typename Block, typename Allocator>
bool <a href=
"#op-not-equal">operator!=</a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b);
template <typename B, typename A>
bool <a href=
"#op-less">operator<</a>(const dynamic_bitset<B, A>& a, const dynamic_bitset<B, A>& b);
template <typename Block, typename Allocator>
bool <a href=
"#op-less-equal">operator<=</a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b);
template <typename Block, typename Allocator>
bool <a href=
"#op-greater">operator></a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b);
template <typename Block, typename Allocator>
bool <a href=
"#op-greater-equal">operator>=</a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b);
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
<a href=
"#op-and">operator&</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2);
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
<a href=
"#op-or">operator|</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2);
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
<a href=
"#op-xor">operator^</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2);
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
<a href=
"#op-sub">operator-</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2);
template <typename Block, typename Allocator, typename CharT, typename Alloc>
void <a href=
"#to_string">to_string</a>(const dynamic_bitset<Block, Allocator>& b,
std::basic_string<CharT, Alloc>& s);
template <typename Block, typename Allocator, typename BlockOutputIterator>
void <a href=
"#to_block_range">to_block_range</a>(const dynamic_bitset<Block, Allocator>& b,
BlockOutputIterator result);
template <typename CharT, typename Traits, typename Block, typename Allocator>
std::basic_ostream<CharT, Traits>&
<a href=
"#op-out">operator<<</a>(std::basic_ostream<CharT, Traits>& os, const dynamic_bitset<Block, Allocator>& b);
template <typename CharT, typename Traits, typename Block, typename Allocator>
std::basic_istream<CharT, Traits>&
<a href=
"#op-in">operator>></a>(std::basic_istream<CharT, Traits>& is, dynamic_bitset<Block, Allocator>& b);
} // namespace boost
</pre>
<h3><a name="definitions">Definitions</a></h3>
<p>Each bit represents either the Boolean value true or false (1
or 0). To <i>set</i> a bit is to assign it 1. To <i>clear</i> or
<i>reset</i> a bit is to assign it 0. To <i>flip</i> a bit is to
change the value to 1 if it was 0 and to 0 if it was 1. Each bit
has a non-negative <i>position</i>. A bitset <tt>x</tt> contains
<tt>x.size()</tt> bits, with each bit assigned a unique position
in the range <tt>[0,x.size())</tt>. The bit at position 0 is
called the <i>least significant bit</i> and the bit at position
<tt>size() - 1</tt> is the <i>most significant bit</i>. When
converting an instance of <tt>dynamic_bitset</tt> to or from an
unsigned long <tt>n</tt>, the bit at position <tt>i</tt> of the
bitset has the same value as <tt>(n >> i) & 1</tt>.</p>
<h3><a name="examples">Examples</a></h3>
<p>An example of setting and reading some bits. Note that
<tt>operator[]</tt> goes from the least-significant bit at
<tt>0</tt> to the most significant bit at <tt>size()-1</tt>. The
<tt>operator<<</tt> for <tt>dynamic_bitset</tt> prints the
bitset from most-significant to least-significant, since that is
the format most people are use to reading.</p>
<blockquote>
<pre>
#include <iostream>
#include <boost/dynamic_bitset.hpp>
int main(int, char*[]) {
boost::dynamic_bitset<> x(5); // all 0's by default
x[0] = 1;
x[1] = 1;
x[4] = 1;
for (boost::dynamic_bitset<>::size_type i = 0; i < x.size(); ++i)
std::cout << x[i];
std::cout << "\n";
std::cout << x << "\n";
return EXIT_SUCCESS;
}
</pre>
</blockquote>
<p>The output is</p>
<blockquote>
<pre>
11001
10011
</pre>
</blockquote>
<p>An example of creating some bitsets from integers (unsigned
longs).</p>
<blockquote>
<pre>
#include <iostream>
#include <boost/dynamic_bitset.hpp>
int main(int, char*[])
{
const boost::dynamic_bitset<> b0(2, 0ul);
std::cout << "bits(0) = " << b0 << std::endl;
const boost::dynamic_bitset<> b1(2, 1ul);
std::cout << "bits(1) = " << b1 << std::endl;
const boost::dynamic_bitset<> b2(2, 2ul);
std::cout << "bits(2) = " << b2 << std::endl;
const boost::dynamic_bitset<> b3(2, 3ul);
std::cout << "bits(3) = " << b3 << std::endl;
return EXIT_SUCCESS;
}
</pre>
</blockquote>
<p>The output is</p>
<blockquote>
<pre>
bits(0) = 00
bits(1) = 01
bits(2) = 10
bits(3) = 11
</pre>
</blockquote>
<p>An example of performing some bitwise operations.</p>
<blockquote>
<pre>
#include <iostream>
#include <boost/dynamic_bitset.hpp>
int main(int, char*[]) {
const boost::dynamic_bitset<> mask(12, 2730ul);
std::cout << "mask = " << mask << std::endl;
boost::dynamic_bitset<> x(12);
std::cout << "Enter a 12-bit bitset in binary: " << std::flush;
if (std::cin >> x) {
std::cout << "input number: " << x << std::endl;
std::cout << "As unsigned long: " << x.to_ulong() << std::endl;
std::cout << "And with mask: " << (x & mask) << std::endl;
std::cout << "Or with mask: " << (x | mask) << std::endl;
std::cout << "Shifted left: " << (x << 1) << std::endl;
std::cout << "Shifted right: " << (x >> 1) << std::endl;
}
return EXIT_SUCCESS;
}
</pre>
</blockquote>
<p>The output is</p>
<blockquote>
<pre>
mask = 101010101010
Enter a 12-bit bitset in binary: 100110101101
input number = 100110101101
As unsigned long: 2477
And with mask: 100010101000
Or with mask: 101110101111
Shifted left: 001101011010
Shifted right: 010011010110
</pre>
</blockquote>
<h3><a name="rationale">Rationale</a></h3>
The <tt>dynamic_bitset</tt> does not provide iterators (and
therefore is not a <a href=
"http://www.sgi.com/tech/stl/Container.html">Container</a>) for
the following reasons:
<ol>
<li><tt>std::bitset</tt> does not have iterators, and
<tt>dynamic_bitset</tt> is meant to be a run-time sized version
of <tt>std::bitset</tt>.</li>
<li>The <tt>dynamic_bitset</tt> is not designed to be a <a href=
"http://www.sgi.com/tech/stl/Container.html">Container</a>.</li>
<li>A container with a proxy <tt>reference</tt> type can not
fulfill the container requirements as specified in the C++
standard (unless one resorts to strange iterator semantics).
<tt>std::vector<bool></tt> has a proxy <tt>reference</tt>
type and does not fulfill the container requirements and as a
result has caused many problems. One common problem is when
people try to use iterators from <tt>std::vector<bool></tt>
with a Standard algorithm such as <tt>std::search</tt>. The
<tt>std::search</tt> requirements say that the iterator must be a
<a href=
"http://www.sgi.com/tech/stl/ForwardIterator.html">Forward
Iterator</a>, but the <tt>std::vector<bool>::iterator</tt>
does not meet this requirement because of the proxy reference.
Depending on the implementation, they may or not be a compile
error or even a run-time error due to this misuse. For further
discussion of the problem see <i>Effective STL</i> by Scott
Meyers). So <tt>dynamic_bitset</tt> tries to avoid these problems
by not pretending to be a container.</li>
</ol>
<p>Some people prefer the name "toggle" to "flip". The name
"flip" was chosen because that is the name used in <a href=
"http://www.sgi.com/tech/stl/bitset.html"><tt>std::bitset</tt></a>.
In fact, most of the function names for <tt>dynamic_bitset</tt>
were chosen for this reason.</p>
<p><tt>dynamic_bitset</tt> does not throw exceptions when a
precondition is violated (as is done in <tt>std::bitset</tt>).
Instead <tt>assert</tt> is used. See the guidelines for <a href=
"http://www.boost.org/more/error_handling.html">Error and Exception Handling</a>
for the explanation.</p>
<h3><a name="header-files">Header Files</a></h3>
<p>The class <tt>dynamic_bitset</tt> is defined in the header <a
href=
"../../boost/dynamic_bitset.hpp">boost/dynamic_bitset.hpp</a>.
Also, there is a forward declaration for <tt>dynamic_bitset</tt>
in the header <a href=
"../../boost/dynamic_bitset_fwd.hpp">boost/dynamic_bitset_fwd.hpp</a>.</p>
<h3><a name="template-parameters">Template parameters</a></h3>
<table border summary=
"Describes the meaning of the template parameters and lists their corresponding default arguments">
<tr>
<th>Parameter</th>
<th>Description</th>
<th>Default</th>
</tr>
<tr>
<td valign="top"><tt>Block</tt> </td>
<td valign="top">The integer type in which the bits are
stored.</td>
<td valign="top"><tt>unsigned long</tt> </td>
</tr>
<tr>
<td valign="top"><tt>Allocator</tt> </td>
<td valign="top">The allocator type used for all internal memory
management.</td>
<td valign="top"><tt>std::allocator<Block></tt> </td>
</tr>
</table>
<h3><a name="concepts-modeled">Concepts Modeled</a></h3>
<a href=
"http://www.sgi.com/tech/stl/Assignable.html">Assignable</a>, <a
href=
"http://www.sgi.com/tech/stl/DefaultConstructible.html">Default
Constructible</a>, <a href=
"http://www.sgi.com/tech/stl/EqualityComparable.html">Equality
Comparable</a>, <a href=
"http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan
Comparable</a>.
<h3><a name="type-requirements">Type requirements</a></h3>
<tt>Block</tt> is an unsigned integer type. <tt>Allocator</tt>
satisfies the Standard requirements for an allocator.
<h3><a name="public-base-classes">Public base classes</a></h3>
None.
<h3><a name="nested-type-names">Nested type names</a></h3>
<hr>
<pre>
<a name="reference">dynamic_bitset::reference</a>
</pre>
<p>A proxy class that acts as a reference to a single bit. It
contains an assignment operator, a conversion to <tt>bool</tt>,
an <tt>operator~</tt>, and a member function <tt>flip</tt>. It
exists only as a helper class for <tt>dynamic_bitset</tt>'s
<tt>operator[]</tt>. The following table describes the valid
operations on the <tt>reference</tt> type. Assume that <tt>b</tt>
is an instance of <tt>dynamic_bitset</tt>, <tt>i, j</tt> are of
<tt>size_type</tt> and in the range <tt>[0,b.size())</tt>. Also,
note that when we write <tt>b[i]</tt> we mean an object of type
<tt>reference</tt> that was initialized from <tt>b[i]</tt>. The
variable <tt>x</tt> is a <tt>bool</tt>.</p>
<table border="1" summary=
"Semantics of several expressions involving usage of dynamic_bitset::reference">
<tr>
<th>Expression</th>
<th>Semantics</th>
</tr>
<tr>
<td><tt>x = b[i]</tt></td>
<td>Assign the ith bit of <tt>b</tt> to <tt>x</tt>.</td>
</tr>
<tr>
<td><tt>(bool)b[i]</tt></td>
<td>Return the ith bit of <tt>b</tt>.</td>
</tr>
<tr>
<td><tt>b[i] = x</tt></td>
<td>Set the ith bit of <tt>b</tt> to the value of <tt>x</tt> and
return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] |= x</tt></td>
<td>Or the ith bit of <tt>b</tt> with the value of <tt>x</tt> and
return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] &= x</tt></td>
<td>And the ith bit of <tt>b</tt> with the value of <tt>x</tt>
and return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] ^= x</tt></td>
<td>Exclusive-Or the ith bit of <tt>b</tt> with the value of
<tt>x</tt> and return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] -= x</tt></td>
<td>If <tt>x==true</tt>, clear the ith bit of <tt>b</tt>. Returns
<tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] = b[j]</tt></td>
<td>Set the ith bit of <tt>b</tt> to the value of the jth bit of
<tt>b</tt> and return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] |= b[j]</tt></td>
<td>Or the ith bit of <tt>b</tt> with the jth bit of <tt>b</tt>
and return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] &= b[j]</tt></td>
<td>And the ith bit of <tt>b</tt> with the jth bit of <tt>b</tt>
and return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] ^= b[j]</tt></td>
<td>Exclusive-Or the ith bit of <tt>b</tt> with the jth bit of
<tt>b</tt> and return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] -= b[j]</tt></td>
<td>If the jth bit of <tt>b</tt> is set, clear the ith bit of
<tt>b</tt>. Returns <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>x = ~b[i]</tt></td>
<td>Assign the opposite of the ith bit of <tt>b</tt> to
<tt>x</tt>.</td>
</tr>
<tr>
<td><tt>(bool)~b[i]</tt></td>
<td>Return the opposite of the ith bit of <tt>b</tt>.</td>
</tr>
<tr>
<td><tt>b[i].flip()</tt> </td>
<td>Flip the ith bit of <tt>b</tt> and return <tt>b[i]</tt>.</td>
</tr>
</table>
<hr>
<pre>
<a name="const_reference">dynamic_bitset::const_reference</a>
</pre>
The type <tt>bool</tt>.
<hr>
<pre>
<a name="size_type">dynamic_bitset::size_type</a>
</pre>
The unsigned integer type for representing the size of the bit set.
<hr>
<pre>
<a name="block_type">dynamic_bitset::block_type</a>
</pre>
The same type as <tt>Block</tt>.
<hr>
<pre>
<a name="allocator_type">dynamic_bitset::allocator_type;</a>
</pre>
The same type as <tt>Allocator</tt>.
<hr>
<h3><a name="public-data-members">Public data members</a></h3>
<hr>
<pre>
<a name="bits_per_block">dynamic_bitset::bits_per_block</a>
</pre>
The number of bits the type <tt>Block</tt> uses to represent values,
excluding any padding bits. Numerically equal
to <tt>std::numeric_limits<Block>::digits</tt>.
<hr>
<pre>
<a name="npos">dynamic_bitset::npos</a>
</pre>
The maximum value of <tt>size_type</tt>.
<hr>
<h3><a name="constructors">Constructors</a></h3>
<hr>
<pre>
<a name=
"cons1">dynamic_bitset</a>(const Allocator& alloc = Allocator())
</pre>
<b>Effects:</b> Constructs a bitset of size zero. A copy of the
<tt>alloc</tt> object will be used in subsequent bitset
operations such as <tt>resize</tt> to allocate memory.<br>
<b>Postconditions:</b> <tt>this->size() == 0</tt>.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br>
(Required by <a href=
"http://www.sgi.com/tech/stl/DefaultConstructible.html">Default
Constructible</a>.)
<hr>
<pre>
<a name="cons2">dynamic_bitset</a>(size_type num_bits,
unsigned long value = 0,
const Allocator& alloc = Allocator())
</pre>
<b>Effects:</b> Constructs a bitset from an integer. The first
<tt>M</tt> bits are initialized to the corresponding bits in
<tt>val</tt> and all other bits, if any, to zero (where <tt>M =
min(num_bits, std::numeric_limits<unsigned long>::digits)</tt>). A copy of
the <tt>alloc</tt> object will be used in subsequent bitset
operations such as <tt>resize</tt> to allocate memory.<br>
<b>Postconditions:</b>
<ul>
<li><tt>this->size() == num_bits</tt></li>
<li>For all <tt>i</tt> in the range <tt>[0,M)</tt>,
<tt>(*this)[i] == (value >> i) & 1</tt>.</li>
<li>For all <tt>i</tt> in the range <tt>[M,num_bits)</tt>,
<tt>(*this)[i] == false</tt>.</li>
</ul>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br>
<hr>
<pre>
<a name="cons5">dynamic_bitset</a>(const dynamic_bitset& x)
</pre>
<b>Effects:</b> Constructs a bitset that is a copy of the bitset
<tt>x</tt>. The allocator for this bitset is a copy of the
allocator in <tt>x</tt>. <br>
<b>Postconditions:</b> For all <tt>i</tt> in the range
<tt>[0,x.size())</tt>, <tt>(*this)[i] == x[i]</tt>.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br>
(Required by <a href=
"http://www.sgi.com/tech/stl/Assignable.html">Assignable</a>.)
<hr>
<pre>
template <typename BlockInputIterator>
explicit
<a name=
"cons4">dynamic_bitset</a>(BlockInputIterator first, BlockInputIterator last,
const Allocator& alloc = Allocator());
</pre>
<b>Effects:</b> Constructs a bitset based on a range of blocks.
Let <tt>*first</tt> be block number 0, <tt>*++first</tt> block
number 1, etc. Block number <tt>b</tt> is used to initialize the
bits of the dynamic_bitset in the position range
<tt>[b*bits_per_block, (b+1)*bits_per_block)</tt>. For each block
number <tt>b</tt> with value <tt>bval</tt>, the bit <tt>(bval
>> i) & 1</tt> corresponds to the bit at position
<tt>(b * bits_per_block + i)</tt> in the bitset (where <tt>i</tt>
goes through the range <tt>[0, bits_per_block)</tt>).<br>
<b>Requires:</b> The type <tt>BlockInputIterator</tt> must be a
model of <a href=
"http://www.sgi.com/tech/stl/InputIterator.html">Input
Iterator</a> and its <tt>value_type</tt> must be the same type as
<tt>Block</tt>.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br>
<hr>
<pre>
template<typename Char, typename Traits, typename Alloc>
explicit
<a name="cons3">dynamic_bitset</a>(const <a href=
"http://www.sgi.com/tech/stl/basic_string.html">std::basic_string</a><Char,Traits,Alloc>& s,
typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0,
typename std::basic_string<CharT, Traits, Alloc>::size_type n = <a
href=
"http://www.sgi.com/tech/stl/basic_string.html">std::basic_string</a><Char,Traits,Alloc>::npos,
const Allocator& alloc = Allocator())
</pre>
<b>Precondition:</b> <tt>pos <= s.size()</tt> and the
characters used to initialize the bits must be <tt>0</tt> or
<tt>1</tt>.<br>
<b>Effects:</b> Constructs a bitset from a string of 0's and
1's. The first <tt>M</tt> bits are initialized to the
corresponding characters in <tt>s</tt>, where <tt>M =
min(s.size() - pos, n)</tt>. Note that the <i>highest</i>
character position in <tt>s</tt>, not the lowest, corresponds to
the least significant bit. That is, character position <tt>pos +
M - 1 - i</tt> corresponds to bit <tt>i</tt>. So, for example,
<tt>dynamic_bitset(string("1101"))</tt> is the same as
<tt>dynamic_bitset(13ul)</tt>.<br>
<b>Throws:</b> an allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
<hr>
<h3><a name="destructor">Destructor</a></h3>
<hr>
<pre>
~dynamic_bitset()
</pre>
<b>Effects:</b> Releases the memory associated with this bitset
and destroys the bitset object itself.<br>
<b>Throws:</b> nothing.
<hr>
<h3><a name="member-functions">Member Functions</a></h3>
<hr>
<pre>
void <a name="swap">swap</a>(dynamic_bitset& b);
</pre>
<b>Effects:</b> The contents of this bitset and bitset <tt>b</tt>
are exchanged.<br>
<b>Postconditions:</b> This bitset is equal to the original
<tt>b</tt>, and <tt>b</tt> is equal to the previous version of
this bitset.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
dynamic_bitset& <a name=
"assign">operator=</a>(const dynamic_bitset& x)
</pre>
<b>Effects:</b> This bitset becomes a copy of the bitset
<tt>x</tt>.<br>
<b>Postconditions:</b> For all <tt>i</tt> in the range
<tt>[0,x.size())</tt>, <tt>(*this)[i] == x[i]</tt>.<br>
<b>Returns:</b> <tt>*this</tt>.<br>
<b>Throws:</b> nothing. <br>
(Required by <a href=
"http://www.sgi.com/tech/stl/Assignable.html">Assignable</a>.)
<hr>
<pre>
allocator_type <a name="get_allocator">get_allocator()</a> const;
</pre>
<b>Returns:</b> A copy of the allocator object used to construct <tt>*this</tt>.
<hr>
<pre>
void <a name=
"resize">resize</a>(size_type num_bits, bool value = false);
</pre>
<b>Effects:</b> Changes the number of bits of the bitset to
<tt>num_bits</tt>. If <tt>num_bits > size()</tt> then the bits
in the range <tt>[0,size())</tt> remain the same, and the bits in
<tt>[size(),num_bits)</tt> are all set to <tt>value</tt>. If
<tt>num_bits < size()</tt> then the bits in the range
<tt>[0,num_bits)</tt> stay the same (and the remaining bits are
discarded).<br>
<b>Postconditions:</b> <tt>this->size() == num_bits</tt>.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br>
<hr>
<pre>
void <a name="clear">clear</a>()
</pre>
<b>Effects:</b> The size of the bitset becomes zero.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
void <a name="push_back">push_back</a>(bool value);
</pre>
<b>Effects:</b> Increases the size of the bitset by one, and sets
the value of the new most-significant bit to <tt>value</tt>.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br>
<hr>
<pre>
void <a name="append1">append</a>(Block value);
</pre>
<b>Effects:</b> Appends the bits in <tt>value</tt> to the bitset
(appends to the most-significant end). This increases the size of
the bitset by <tt>bits_per_block</tt>. Let <tt>s</tt> be the old
size of the bitset, then for <tt>i</tt> in the range
<tt>[0,bits_per_block)</tt>, the bit at position <tt>(s + i)</tt>
is set to <tt>((value >> i) & 1)</tt>.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br>
<hr>
<pre>
template <typename BlockInputIterator>
void <a name=
"append2">append</a>(BlockInputIterator first, BlockInputIterator last);
</pre>
<b>Effects:</b> This function provides the same end result as the
following code, but is typically more efficient.
<pre>
for (; first != last; ++first)
append(*first);
</pre>
<b>Requires:</b> The <tt>BlockInputIterator</tt> type must be a
model of <a href=
"http://www.sgi.com/tech/stl/InputIterator.html">Input
Iterator</a> and the <tt>value_type</tt> must be the same type as
<tt>Block</tt>.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br>
<hr>
<pre>
dynamic_bitset& <a name=
"op-and-assign">operator&=</a>(const dynamic_bitset& rhs)
</pre>
<b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br>
<b>Effects:</b> Bitwise-AND all the bits in <tt>rhs</tt> with
the bits in this bitset. This is equivalent to:
<pre>
for (size_type i = 0; i != this->size(); ++i)
(*this)[i] = (*this)[i] & rhs[i];
</pre>
<b>Returns:</b> <tt>*this</tt>.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
dynamic_bitset& <a name=
"op-or-assign">operator|=</a>(const dynamic_bitset& rhs)
</pre>
<b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br>
<b>Effects:</b> Bitwise-OR's all the bits in <tt>rhs</tt> with
the bits in this bitset. This is equivalent to:
<pre>
for (size_type i = 0; i != this->size(); ++i)
(*this)[i] = (*this)[i] | rhs[i];
</pre>
<b>Returns:</b> <tt>*this</tt>.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
dynamic_bitset& <a name=
"op-xor-assign">operator^=</a>(const dynamic_bitset& rhs)
</pre>
<b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br>
<b>Effects:</b> Bitwise-XOR's all the bits in <tt>rhs</tt> with
the bits in this bitset. This is equivalent to:
<pre>
for (size_type i = 0; i != this->size(); ++i)
(*this)[i] = (*this)[i] ^ rhs[i];
</pre>
<b>Returns:</b> <tt>*this</tt>.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
dynamic_bitset& <a name=
"op-sub-assign">operator-=</a>(const dynamic_bitset& rhs)
</pre>
<b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br>
<b>Effects:</b> Computes the set difference of this bitset and
the <tt>rhs</tt> bitset. This is equivalent to:
<pre>
for (size_type i = 0; i != this->size(); ++i)
(*this)[i] = (*this)[i] && !rhs[i];
</pre>
<b>Returns:</b> <tt>*this</tt>.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
dynamic_bitset& <a name=
"op-sl-assign">operator<<=</a>(size_type n)
</pre>
<b>Effects:</b> Shifts the bits in this bitset to the left by
<tt>n</tt> bits. For each bit in the bitset, the bit at position
pos takes on the previous value of the bit at position <tt>pos -
n</tt>, or zero if no such bit exists.<br>
<b>Returns:</b> <tt>*this</tt>.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
dynamic_bitset& <a name=
"op-sr-assign">operator>>=</a>(size_type n)
</pre>
<b>Effects:</b> Shifts the bits in this bitset to the right by
<tt>n</tt> bits. For each bit in the bitset, the bit at position
<tt>pos</tt> takes on the previous value of bit <tt>pos + n</tt>,
or zero if no such bit exists.<br>
<b>Returns:</b> <tt>*this</tt>.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
dynamic_bitset <a name=
"op-sl">operator<<</a>(size_type n) const
</pre>
<b>Returns:</b> a copy of <tt>*this</tt> shifted to the left by
<tt>n</tt> bits. For each bit in the returned bitset, the bit at
position pos takes on the value of the bit at position <tt>pos -
n</tt> of this bitset, or zero if no such bit exists. Note that
the expression <tt>b << n</tt> is equivalent to
constructing a temporary copy of <tt>b</tt> and then using
<tt>operator<<=</tt>.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
<hr>
<pre>
dynamic_bitset <a name=
"op-sr">operator>></a>(size_type n) const
</pre>
<b>Returns:</b> a copy of <tt>*this</tt> shifted to the right by
<tt>n</tt> bits. For each bit in the returned bitset, the bit at
position pos takes on the value of the bit at position <tt>pos +
n</tt> of this bitset, or zero if no such bit exists. Note that
the expression <tt>b >> n</tt> is equivalent to
constructing a temporary copy of <tt>b</tt> and then using
<tt>operator>>=</tt>.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
<hr>
<pre>
dynamic_bitset& <a name="set1">set</a>()
</pre>
<b>Effects:</b> Sets every bit in this bitset to 1.<br>
<b>Returns:</b> <tt>*this</tt><br>
<b>Throws:</b> nothing.
<hr>
<pre>
dynamic_bitset& <a name="flip1">flip</a>()
</pre>
<b>Effects:</b> Flips the value of every bit in this bitset.<br>
<b>Returns:</b> <tt>*this</tt><br>
<b>Throws:</b> nothing.
<hr>
<pre>
dynamic_bitset <a name="op-not">operator~</a>() const
</pre>
<b>Returns:</b> a copy of <tt>*this</tt> with all of its bits
flipped.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
<hr>
<pre>
dynamic_bitset& <a name="reset1">reset</a>()
</pre>
<b>Effects:</b> Clears every bit in this bitset.<br>
<b>Returns:</b> <tt>*this</tt><br>
<b>Throws:</b> nothing.
<hr>
<pre>
dynamic_bitset& <a name=
"set2">set</a>(size_type n, bool val = true)
</pre>
<b>Precondition:</b> <tt>n < this->size()</tt>.<br>
<b>Effects:</b> Sets bit <tt>n</tt> if <tt>val</tt> is
<tt>true</tt>, and clears bit <tt>n</tt> if <tt>val</tt> is
<tt>false</tt>. <br>
<b>Returns:</b> <tt>*this</tt>
<hr>
<pre>
dynamic_bitset& <a name="reset2">reset</a>(size_type n)
</pre>
<b>Precondition:</b> <tt>n < this->size()</tt>.<br>
<b>Effects:</b> Clears bit <tt>n</tt>.<br>
<b>Returns:</b> <tt>*this</tt>
<hr>
<pre>
dynamic_bitset& <a name="flip2">flip</a>(size_type n)
</pre>
<b>Precondition:</b> <tt>n < this->size()</tt>.<br>
<b>Effects:</b> Flips bit <tt>n</tt>.<br>
<b>Returns:</b> <tt>*this</tt>
<hr>
<pre>
size_type <a name="size">size</a>() const
</pre>
<b>Returns:</b> the number of bits in this bitset.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
size_type <a name="num_blocks">num_blocks</a>() const
</pre>
<b>Returns:</b> the number of blocks in this bitset.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
size_type <a name="max_size">max_size</a>() const;
</pre>
<b>Returns:</b> the maximum size of a <tt>dynamic_bitset</tt>
object having the same type as <tt>*this</tt>. Note that if
any <tt>dynamic_bitset</tt> operation causes <tt>size()</tt> to
exceed <tt>max_size()</tt> then the <i>behavior is undefined</i>.
<br><br>[The semantics of this function could change slightly
when lib issue 197 will be closed]<br>
<hr>
<pre>
bool <a name="empty">empty</a>() const;
</pre>
<b>Returns:</b> <tt>true</tt> if <tt>this->size() == 0</tt>, <tt>false</tt>
otherwise. <i>Note</i>: not to be confused with <tt>none()</tt>, that has
different semantics.
<hr>
<pre>
size_type <a name="count">count</a>() const
</pre>
<b>Returns:</b> the number of bits in this bitset that are
set.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
bool <a name="any">any</a>() const
</pre>
<b>Returns:</b> <tt>true</tt> if any bits in this bitset are set,
and otherwise returns <tt>false</tt>.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
bool <a name="none">none</a>() const
</pre>
<b>Returns:</b> <tt>true</tt> if no bits are set, and otherwise
returns <tt>false</tt>.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
bool <a name="test">test</a>(size_type n) const
</pre>
<b>Precondition:</b> <tt>n < this->size()</tt>.<br>
<b>Returns:</b> <tt>true</tt> if bit <tt>n</tt> is set and
<tt>false</tt> is bit <tt>n</tt> is 0.
<hr>
<pre>
reference <a name="bracket">operator[]</a>(size_type n)
</pre>
<b>Precondition:</b> <tt>n < this->size()</tt>.<br>
<b>Returns:</b> a <tt>reference</tt> to bit <tt>n</tt>. Note
that <tt>reference</tt> is a proxy class with an assignment
operator and a conversion to <tt>bool</tt>, which allows you to
use <tt>operator[]</tt> for assignment. That is, you can write
both <tt>x = b[n]</tt> and <tt>b[n] = x</tt>. However, in many
other respects the proxy is not the same as the true reference
type <tt>bool&</tt>.
<hr>
<pre>
bool <a name="const-bracket">operator[]</a>(size_type n) const
</pre>
<b>Precondition:</b> <tt>n < this->size()</tt>.<br>
<b>Returns:</b> The same as <tt>test(n)</tt>.
<hr>
<pre>
unsigned long <a name="to_ulong">to_ulong</a>() const
</pre>
<b>Returns:</b> The numeric value corresponding to the bits in <tt>*this</tt>.
<br>
<b>Throws:</b> <tt>std::overflow_error</tt> if that value is too large to
be represented in an <tt>unsigned long</tt>, i.e. if <tt>*this</tt> has
any non-zero bit at a position <tt>>=
std::numeric_limits<unsigned long>::digits</tt>.
<hr>
<pre>
bool <a name=
"is_subset_of">is_subset_of</a>(const dynamic_bitset& a) const
</pre>
<b>Requires:</b> <tt>this->size() == a.size()</tt><br>
<b>Returns:</b> true if this bitset is a subset of bitset
<tt>a</tt>. That is, it returns true if, for every bit that is
set in this bitset, the corresponding bit in bitset <tt>a</tt> is
also set. Otherwise this function returns false.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
bool <a name=
"is_proper_subset_of">is_proper_subset_of</a>(const dynamic_bitset& a) const
</pre>
<b>Requires:</b> <tt>this->size() == a.size()</tt><br>
<b>Returns:</b> true if this bitset is a proper subset of bitset
<tt>a</tt>. That is, it returns true if, for every bit that is
set in this bitset, the corresponding bit in bitset <tt>a</tt> is
also set and if <tt>this->count() < a.count()</tt>.
Otherwise this function returns false.<br>
<b>Throws:</b> nothing.
<hr>
<pre>
size_type <a name = "find_first">find_first</a>() const;
</pre>
<b>Returns:</b> the lowest index <tt>i</tt> such as bit <tt>i</tt>
is set, or <tt>npos</tt> if <tt>*this</tt> has no on bits.
<hr>
<pre>
size_type <a name="find_next">find_next</a>(size_type pos) const;
</pre>
<b>Returns:</b> the lowest index <tt>i</tt> greater than
<tt>pos</tt> such as bit <tt>i</tt> is set, or <tt>npos</tt> if
no such index exists.
<hr>
<pre>
bool <a name=
"op-equal">operator==</a>(const dynamic_bitset& rhs) const
</pre>
<b>Returns:</b> <tt>true</tt> if <tt>this->size() ==
rhs.size()</tt> and if for all <tt>i</tt> in the range
<tt>[0,rhs.size())</tt>, <tt>(*this)[i] == rhs[i]</tt>. Otherwise
returns <tt>false</tt>.<br>
<b>Throws:</b> nothing.<br>
(Required by <a href=
"http://www.sgi.com/tech/stl/EqualityComparable.html">Equality
Comparable</a>.)
<hr>
<pre>
bool <a name=
"op-not-equal">operator!=</a>(const dynamic_bitset& rhs) const
</pre>
<b>Returns:</b> <tt>!((*this) == rhs)</tt><br>
<b>Throws:</b> nothing.<br>
(Required by <a href=
"http://www.sgi.com/tech/stl/EqualityComparable.html">Equality
Comparable</a>.)
<hr>
<pre>
bool <a name=
"op-less">operator<</a>(const dynamic_bitset& rhs) const
</pre>
<b>Returns:</b> <tt>true</tt> if this bitset is lexicographically
less than <tt>rhs</tt>, and returns <tt>false</tt> otherwise.
(See the description of <a href=
"http://www.sgi.com/tech/stl/lexicographical_compare.html">lexicographical_compare</a>
for a definition of lexicographic ordering). <br>
<b>Throws:</b> nothing.<br>
(Required by <a href=
"http://www.sgi.com/tech/stl/LessThanComparable.html">Less Than
Comparable</a>.)
<hr>
<pre>
bool <a name=
"op-greater">operator></a>(const dynamic_bitset& rhs) const
</pre>
<b>Returns:</b> <tt>!((*this) < rhs || (*this) ==
rhs)</tt><br>
<b>Throws:</b> nothing.<br>
(Required by <a href=
"http://www.sgi.com/tech/stl/LessThanComparable.html">Less Than
Comparable</a>.)
<hr>
<pre>
bool <a name=
"op-less-equal">operator<=</a>(const dynamic_bitset& rhs) const
</pre>
<b>Returns:</b> <tt>(*this) < rhs || (*this) == rhs</tt><br>
<b>Throws:</b> nothing.<br>
(Required by <a href=
"http://www.sgi.com/tech/stl/LessThanComparable.html">Less Than
Comparable</a>.)
<hr>
<pre>
bool <a name=
"op-greater-equal">operator>=</a>(const dynamic_bitset& rhs) const
</pre>
<b>Returns:</b> <tt>(*this) > rhs || (*this) == rhs</tt><br>
<b>Throws:</b> nothing.<br>
(Required by <a href=
"http://www.sgi.com/tech/stl/LessThanComparable.html">Less Than
Comparable</a>.)
<hr>
<h3><a name="non-member-functions">Non-Member Functions</a></h3>
<hr>
<pre>
dynamic_bitset <a name=
"op-and">operator&</a>(const dynamic_bitset& a, const dynamic_bitset& b)
</pre>
<b>Requires:</b> <tt>a.size() == b.size()</tt><br>
<b>Returns:</b> A new bitset that is the bitwise-AND of the
bitsets <tt>a</tt> and <tt>b</tt>. Note that the expression
<tt>b1 & b2</tt> is equivalent to creating a temporary copy
of <tt>b1</tt>, using <tt>operator&=</tt>, and returning the
temporary copy.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
<hr>
<pre>
dynamic_bitset <a name=
"op-or">operator|</a>(const dynamic_bitset& a, const dynamic_bitset& b)
</pre>
<b>Requires:</b> <tt>a.size() == b.size()</tt><br>
<b>Returns:</b> A new bitset that is the bitwise-OR of the
bitsets <tt>a</tt> and <tt>b</tt>. Note that the expression
<tt>b1 & b2</tt> is equivalent to creating a temporary copy
of <tt>b1</tt>, using <tt>operator&=</tt>, and returning the
temporary copy.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
<hr>
<pre>
dynamic_bitset <a name=
"op-xor">operator^</a>(const dynamic_bitset& a, const dynamic_bitset& b)
</pre>
<b>Requires:</b> <tt>a.size() == b.size()</tt><br>
<b>Returns:</b> A new bitset that is the bitwise-XOR of the
bitsets <tt>a</tt> and <tt>b</tt>. Note that the expression
<tt>b1 & b2</tt> is equivalent to creating a temporary copy
of <tt>b1</tt>, using <tt>operator&=</tt>, and returning the
temporary copy.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
<hr>
<pre>
dynamic_bitset <a name=
"op-sub">operator-</a>(const dynamic_bitset& a, const dynamic_bitset& b)
</pre>
<b>Requires:</b> <tt>a.size() == b.size()</tt><br>
<b>Returns:</b> A new bitset that is the set difference of the
bitsets <tt>a</tt> and <tt>b</tt>. Note that the expression
<tt>b1 - b2</tt> is equivalent to creating a temporary copy of
<tt>b1</tt>, using <tt>operator-=</tt>, and returning the
temporary copy.<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
<hr>
<pre>
template <typename CharT, typename Alloc>
void <a name=
"to_string">to_string</a>(const dynamic_bitset<Block, Allocator>& b,
<a href=
"http://www.sgi.com/tech/stl/basic_string.html">std::basic_string</a><Char,Traits,Alloc>& s)
</pre>
<b>Effects:</b> Copies a representation of <tt>b</tt> into the
string <tt>s</tt>. A character in the string is <tt>'1'</tt> if
the corresponding bit is set, and <tt>'0'</tt> if it is not.
Character position <tt>i</tt> in the string corresponds to bit
position <tt>b.size() - 1 - i</tt>. <br>
<b>Throws:</b> If memory is exhausted, the string will throw an
allocation error.<br>
<b>Rationale:</b> This function is not a member function taking
zero arguments and returning a string for a couple reasons.
First, this version can be slighly more efficient because the
string is not copied (due to being passed by value). Second, as a
member function, to allow for flexibility with regards to the
template parameters of <tt>basic_string</tt>, the member function
would require explicit template parameters. Few C++ programmers
are familiar with explicit template parameters, and some C++
compilers do not handle them properly.
<hr>
<pre>
template <typename Block, typename Alloc, typename BlockOutputIterator>
void <a name=
"to_block_range">to_block_range</a>(const dynamic_bitset<Block, Alloc>& b, BlockOutputIterator result)
</pre>
<b>Effects:</b> Writes the bits of the bitset into the iterator
<tt>result</tt> a block at a time. The first block written
represents the bits in the position range
<tt>[0,bits_per_block)</tt> in the bitset, the second block
written the bits in the range
<tt>[bits_pre_block,2*bits_per_block)</tt>, and so on. For each
block <tt>bval</tt> written, the bit <tt>(bval >> i) &
1</tt> corresponds to the bit at position <tt>(b * bits_per_block
+ i)</tt> in the bitset.<br>
<b>Requires:</b> The type <tt>BlockOutputIterator</tt> must be a
model of <a href=
"http://www.sgi.com/tech/stl/OutputIterator.html">Output
Iterator</a> and its <tt>value_type</tt> must be the same type as
<tt>Block</tt>. Further, the size of the output range must be
greater or equal <tt>b.num_blocks()</tt>.
<hr>
<pre>
template <typename BlockIterator, typename Block, typename Alloc>
void <a name=
"from_block_range">from_block_range</a>(BlockIterator first,
BlockIterator last, const dynamic_bitset<Block, Alloc>& b)
</pre>
<b>Effects:</b> Reads blocks from the iterator range into the
bitset. <br>
<b>Requires:</b> The type <tt>BlockIterator</tt> must be a model
of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input
Iterator</a> and its <tt>value_type</tt> must be the same type as
<tt>Block</tt>. The size of the iterator range must be less or
equal to <tt>b.num_blocks()</tt>.
<hr>
<pre>
template <typename Char, typename Traits, typename Block, typename Alloc>
basic_ostream<Char, Traits>&
<a name=
"op-out">operator<<</a>(basic_ostream<Char, Traits>& os, const dynamic_bitset<Block, Alloc>& b)
</pre>
<b>Effects:</b> Inserts a textual representation of b into the stream
<tt>os</tt> (highest bit first). Informally, the output is the same as doing
<pre>
std::basic_string<Char, Traits> s;
boost::to_string(x, s):
os << s;
</pre>
except that the stream inserter takes into accout the locale imbued into
<tt>os</tt>, which <tt>boost::to_string()</tt> can't do. Here is a more
precise specification, given in terms of "as if" rule: first, for each
valid position i into the bitset <tt>b</tt> let's put:
<tt>character_of(b[i)]) = b[i]? os.widen('1') : os.widen('0');</tt>
Let also <tt>s</tt> be a <tt>std::basic_string<Char, Traits></tt>
object, having length <tt>b.size()</tt> and such as, for each <tt>i</tt>
in <tt>[0, b.size())</tt>,
<tt>s[i] is character_of(b[i])</tt>
Then, the output, the effects on <tt>os</tt> and the exception behavior
is the same as outputting the object <tt>s</tt> to <tt>os</tt> (same
width, same exception mask, same padding, same setstate() logic)
<br>
<b>Returns:</b> os <br>
<b>Throws:</b> <tt>std::ios_base::failure</tt> if there is a
problem writing to the stream.
<hr>
<pre>
template <typename Char, typename Traits, typename Block, typename Alloc>
std::basic_istream<Char,Traits>&
<a name=
"op-in">operator>></a>(std::basic_istream<Char,Traits>& is, dynamic_bitset<Block, Alloc>& b)
</pre>
<b>Effects:</b> Extracts a <tt>dynamic_bitset</tt> from an input stream.
<br><br>
<i>Definitions:</i><br><br>
Let <i>Tr</i> be the traits_type of <i>is</i>. Then:
<ol>
<li>
A (non-eof) character <tt>c</tt> extracted from <tt>is</tt>
is a <i>bitset digit</i> if and only if either Tr::eq(c, is.widen('0')) or
Tr::eq(c, is.widen('1')) return true.
</li>
<li>If c is a bitset digit, it's <i>corresponding bit value</i> is 0 if
Tr::eq(c, is.widen('0')) is true, 1 otherwise.
</li>
</ol>
The function begins by constructing a <tt>sentry</tt> object <tt>k</tt> as if <tt>k</tt>
were constructed by
<tt>typename std::basic_istream<Char, Traits>::sentry k(is)</tt>.
If <tt>bool(k)</tt> is true, it calls <tt>b.clear()</tt>
then attempts to extract characters from <tt>is</tt>. For each character c
that is a <i>bitset digit</i> the <i>corresponding bit value</i> is
appended to the less significant end of <tt>b</tt> (appending may throw).
If <tt>is.width()</tt> is greater than zero and smaller than <tt>b.max_size()</tt>
then the maximum number <tt>n</tt> of bits appended is <tt>is.width()</tt>;
otherwise <tt>n</tt> = <tt>b.max_size()</tt>.
Unless the extractor is exited via an exception, characters are extracted (and
corresponding bits appended) until any of the following occurs:<br>
<ul>
<li> <tt>n</tt> bits are stored into the bitset;</li>
<li> end-of-file, or an error, occurs on the input sequence;</li>
<li> the next available input character isn't a bitset digit</li>
</ul>
<br> If no exception caused the function to exit then <tt>is.width(0)</tt> is
called, regardless of how many characters were actually extracted. The
sentry object k is destroyed.
<br>
<br>If the function extracts no characters[???], it calls is.setstate(std::ios::failbit),
which may throw <tt>std::ios_base::failure</tt>.
<br>------
<br>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
A <tt>std::ios_base::failure</tt> if there is a problem reading
from the stream.
<hr>
<h3><a name="exception-guarantees">Exception guarantees</a></h3>
All of <tt>dynamic_bitset</tt> functions offer at least the basic
guarantee. In addition some functions offer the strong or the nothrow
guarantee as summarized in the table below (the destructor, as usual,
doesn't throw):
<table summary="" border>
<tr><td>-</td><td><i>strong</i></td><td><i>nothrow</i></td></tr>
<tr><td>f</td><td> </td><td><b> x </b></td></tr>
</table>
<hr>
<h3><a name="changes-from-previous-ver">Changes from previous version(s)</a></h3>
<!-- Changes from Boost 1.31.0 -->
<h4><i>Changes from Boost 1.31.0</i></h4>
<ul>
<li>
The stream extractor has completely different semantics: as
natural for a dynamic structure, it now expands the bitset as needed
during extraction. The new behaviour mimics that of the basic_string
extractor but there are some differences the user should be aware
of; so, please, look at the <a href="#op-in">documentation</a>.
(One of the differences concerns the case where
stream.width() > bitset.max_size() > 0;
in that circumstance the extractor of dynamic_bitset never attempts to
extract more than max_size() characters, whereas the extractor of
basic_string goes on and, on conforming implementations, eventually
throws a length_error. Note: That's what the standard mandates -see
especially library issue 83- but not all implementations conform)
<br><br>
The stream extractor is now also 'exception-aware' in the sense that
it works correctly when setting exception masks on the stream.<br><br>
</li>
<li>
Several member functions (<tt>empty()</tt>, <tt>find_first()</tt>
, <tt>find_next()</tt>, <tt>get_allocator()</tt>, <tt>intersects()</tt>
, <tt>max_size()</tt> <!--, <tt>reserve()</tt>, <tt>capacity()</tt> -->)
have been added.
</li>
<li>
The constructor from basic_string has a new parameter that was totally
forgotten before.
</li>
</ul>
<i>Technicalities and minor changes</i>
<ul>
<li>
The class <tt>reference</tt> has been reimplemented so that
dynamic_bitset's references behave more like references to standard
container elements. In particular it is now guaranteed that they
cannot be invalidated from a standard library swap() function
applied to their corresponding <tt>dynamic_bitset</tt>s.
</li>
</ul>
<i>General improvements</i>
<br><br>
Several optimizations to member and non-member functions and to the
nested class <tt>reference</tt>.
<hr>
<h3><a name="see-also">See also</a></h3>
<tt><a href=
"http://www.sgi.com/tech/stl/bitset.html">std::bitset</a></tt>,
<tt><a href=
"http://www.sgi.com/tech/stl/Vector.html">std::vector</a></tt>,
<h3><a name="acknowledgements">Acknowledgements</a></h3>
<p>We would like to thank the Boost community for putting in the
time to review and accept this library. This library is much
better than it ever would have been due to all the suggestions
from Boost members. We especially thank Matt Marcus for taking on
the task of review manager. Also, a special thanks goes to
James Kanze for his invaluable help with the internationalization
issues.</p>
<hr>
<table summary="">
<tr>
<td></td>
</tr>
<tr valign="top">
<td nowrap>Copyright © 2001</td>
<td><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</a>,
Indiana University (<a href=
"mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</a>)<br>
<a href="http://freshsources.com">Chuck Allison</a>, Senior
Editor, C/C++ Users Journal (<a href=
"mailto:cda@freshsources.com">cda@freshsources.com</a>)<br>
</td>
</tr>
<tr valign="top">
<td nowrap>Copyright © 2003-2004</td>
<td>Gennaro Prota</td>
</tr>
</table>
<!-- LocalWords: dynamic bitset alt gif iostream hpp int bitsets const ul ulong
-->
<!-- LocalWords: STL LessThan alloc num typename BlockInputIterator html pos
-->
<!-- LocalWords: npos bool rhs OR's XOR's val CharT istream ostream os siek
-->
<!-- LocalWords: htm namespace enum sizeof BlockOutputIterator fwd ith jth
-->
</body>
</html>
|