ROSE 0.11.145.147
BitVector.h
1// WARNING: Changes to this file must be contributed back to Sawyer or else they will
2// be clobbered by the next update from Sawyer. The Sawyer repository is at
3// https://gitlab.com/charger7534/sawyer.git.
4
5
6
7
8#ifndef Sawyer_BitVector_H
9#define Sawyer_BitVector_H
10
11#include <Sawyer/Assert.h>
12#include <Sawyer/BitVectorSupport.h>
13#include <Sawyer/Optional.h>
14#include <Sawyer/Sawyer.h>
15
16#include <boost/algorithm/string/predicate.hpp>
17#include <boost/cstdint.hpp>
18
19#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
20#include <boost/serialization/vector.hpp>
21#endif
22
23#ifdef SAWYER_HAVE_CEREAL
24#include <cereal/types/vector.hpp>
25#endif
26
27namespace Sawyer {
28namespace Container {
29
30#ifdef BOOST_WINDOWS
34inline double log2(double n) {
35 return log(n) / log(2.0);
36}
37#endif
38
69class BitVector {
70public:
71 typedef unsigned Word;
74private:
75 std::vector<Word> words_;
76 size_t size_;
77
78#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
79private:
80 friend class boost::serialization::access;
81
82 template<class S>
83 void serialize(S &s, const unsigned /*version*/) {
84 s & BOOST_SERIALIZATION_NVP(size_);
85 s & BOOST_SERIALIZATION_NVP(words_);
86 }
87#endif
88
89#ifdef SAWYER_HAVE_CEREAL
90private:
91 friend class cereal::access;
92
93 template<class Archive>
94 void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &archive) {
95 archive(CEREAL_NVP(size_));
96 archive(CEREAL_NVP(words_));
97 }
98#endif
99
100public:
102 BitVector(): size_(0) {}
103
105 BitVector(const BitVector &other): words_(other.words_), size_(other.size_) {}
106
110 explicit BitVector(size_t nbits, bool newBits = false): size_(0) {
111 resize(nbits, newBits);
112 }
113
125 static BitVector parse(std::string str) {
126 // Radix information
127 size_t bitsPerDigit = 0;
128 const char *digits = NULL;
129 if (boost::starts_with(str, "0x")) {
130 bitsPerDigit = 4;
131 digits = "0123456789abcdefABCDEF";
132 str = str.substr(2);
133 } else if (boost::starts_with(str, "0b")) {
134 bitsPerDigit = 1;
135 digits = "01";
136 str = str.substr(2);
137 } else if (boost::ends_with(str, "h")) {
138 bitsPerDigit = 4;
139 digits = "0123456789abcdefABCDEF";
140 str = str.substr(0, str.size()-1);
141 } else if (boost::starts_with(str, "0")) {
142 bitsPerDigit = 2;
143 digits = "01234567";
144 str = str.substr(1);
145 } else {
146 bitsPerDigit = 0; // special case
147 digits = "0123456789";
148 }
149
150 // Count digits
151 size_t nDigits = 0;
152 for (const char *t=str.c_str(); *t; ++t) {
153 if (strchr(digits, *t))
154 ++nDigits;
155 }
156 if (0==nDigits)
157 throw std::runtime_error("BitVector::parse: no valid digits");
158
159 // Number of bits
160 size_t nBits = 0;
161 if (bitsPerDigit) {
162 nBits = bitsPerDigit * nDigits;
163 } else {
164 nBits = ceil(log2(pow(10.0, (double)nDigits)));
165 }
166
167 // Parse the string
168 BitVector result(nBits);
169 switch (bitsPerDigit) {
170 case 0:
171 result.fromDecimal(str);
172 break;
173 case 2:
174 result.fromBinary(str);
175 break;
176 case 3:
177 result.fromOctal(str);
178 break;
179 case 4:
180 result.fromHex(str);
181 break;
182 default:
183 assert(!"invalid radix");
184 break;
185 }
186 return result;
187 }
188
195 words_ = other.words_;
196 size_ = other.size_;
197 return *this;
198 }
199
203 bool isEmpty() const { return 0 == size_; }
204
208 size_t size() const { return size_; }
209
215 BitVector& resize(size_t newSize, bool newBits=false) {
216 if (0==newSize) {
217 words_.clear();
218 size_ = 0;
219 } else if (newSize > size_) {
220 size_t nwords = BitVectorSupport::numberOfWords<Word>(newSize);
221 words_.resize(nwords, Word(0));
222 BitVectorSupport::setValue(data(), BitRange::hull(size_, newSize-1), newBits);
223 size_ = newSize;
224 } else {
225 size_t nwords = BitVectorSupport::numberOfWords<Word>(newSize);
226 words_.resize(nwords);
227 size_ = newSize;
228 }
229 return *this;
230 }
231
236 size_t capacity() const {
237 return BitVectorSupport::bitsPerWord<Word>::value * words_.capacity();
238 }
239
243 BitRange hull() const {
244 return 0==size_ ? BitRange() : BitRange::hull(0, size_-1);
245 }
246
251 static BitRange baseSize(size_t base, size_t size) {
252 return BitRange::baseSize(base, size);
253 }
254
259 static BitRange hull(size_t minOffset, size_t maxOffset) {
260 return BitRange::hull(minOffset, maxOffset);
261 }
262
264 // Value access
266
271 bool get(size_t idx) const {
272 checkRange(idx);
273 return BitVectorSupport::get(data(), idx);
274 }
275
281 BitVector& clear(const BitRange &range) {
282 checkRange(range);
284 return *this;
285 }
286
294 return *this;
295 }
296
301 BitVector& set(const BitRange &range) {
302 checkRange(range);
303 BitVectorSupport::set(data(), range);
304 return *this;
305 }
306
313 return *this;
314 }
315
319 BitVector& setValue(const BitRange &range, bool value) {
320 checkRange(range);
321 BitVectorSupport::setValue(data(), range, value);
322 return *this;
323 }
324
328 BitVector& setValue(bool value) {
330 return *this;
331 }
332
340 BitVector& copy(const BitRange &to, const BitVector &other, const BitRange &from) {
341 checkRange(to);
342 other.checkRange(from);
343 BitVectorSupport::copy(other.data(), from, data(), to);
344 return *this;
345 }
346
352 BitVector& copy(const BitRange &to, const BitRange &from) {
353 checkRange(to);
354 checkRange(from);
355 BitVectorSupport::copy(data(), from, data(), to);
356 return *this;
357 }
358
364 BitVector& swap(const BitRange &range1, BitVector &other, const BitRange &range2) {
365 checkRange(range1);
366 other.checkRange(range2);
367 BitVectorSupport::swap(data(), range1, other.data(), range2);
368 return *this;
369 }
370
375 BitVector& swap(const BitRange &range1, const BitRange &range2) {
376 checkRange(range1);
377 checkRange(range2);
378 BitVectorSupport::swap(data(), range1, data(), range2);
379 return *this;
380 }
381
385 bool equalTo(const BitRange &range1, BitVector &other, const BitRange &range2) const {
386 checkRange(range1);
387 other.checkRange(range2);
388 return BitVectorSupport::equalTo(data(), range1, other.data(), range2);
389 }
390
395 bool equalTo(const BitRange &range1, const BitRange &range2) const {
396 checkRange(range1);
397 checkRange(range2);
398 return BitVectorSupport::equalTo(data(), range1, data(), range2);
399 }
400
405 bool equalTo(const BitVector &other) const {
406 if (size() != other.size())
407 return false;
408 return BitVectorSupport::equalTo(data(), hull(), other.data(), other.hull());
409 }
410
412 // Counting/searching
414
424
432
442
450
457 checkRange(range);
459 }
460
468
478
486
491 bool isAllSet(const BitRange &range) const {
492 checkRange(range);
493 return BitVectorSupport::isAllSet(data(), range);
494 }
495
499 bool isAllSet() const {
501 }
502
509 bool isAllClear(const BitRange &range) const {
510 checkRange(range);
511 return BitVectorSupport::isAllClear(data(), range);
512 }
513
519 bool isAllClear() const {
521 }
522
526 size_t nSet(const BitRange &range) const {
527 checkRange(range);
528 return BitVectorSupport::nSet(data(), range);
529 }
530
534 size_t nSet() const {
535 return BitVectorSupport::nSet(data(), hull());
536 }
537
541 size_t nClear(const BitRange &range) const {
542 checkRange(range);
543 return BitVectorSupport::nClear(data(), range);
544 }
545
549 size_t nClear() const {
551 }
552
564 const BitRange &range2) const {
565 checkRange(range1);
566 other.checkRange(range2);
567 return BitVectorSupport::mostSignificantDifference(data(), range1, other.data(), range2);
568 }
569
578 Optional<size_t> mostSignificantDifference(const BitRange &range1, const BitRange &range2) const {
579 checkRange(range1);
580 checkRange(range2);
581 return BitVectorSupport::mostSignificantDifference(data(), range1, data(), range2);
582 }
583
592
604 const BitRange &range2) const {
605 checkRange(range1);
606 other.checkRange(range2);
607 return BitVectorSupport::leastSignificantDifference(data(), range1, other.data(), range2);
608 }
609
618 Optional<size_t> leastSignificantDifference(const BitRange &range1, const BitRange &range2) const {
619 checkRange(range1);
620 checkRange(range2);
621 return BitVectorSupport::leastSignificantDifference(data(), range1, data(), range2);
622 }
623
632
634 // Shift/rotate
636
644 BitVector& shiftLeft(const BitRange &range, size_t nShift, bool newBits = 0) {
645 checkRange(range);
646 BitVectorSupport::shiftLeft(data(), range, nShift, newBits);
647 return *this;
648 }
649
657 BitVector& shiftLeft(size_t nShift, bool newBits = 0) {
658 BitVectorSupport::shiftLeft(data(), hull(), nShift, newBits);
659 return *this;
660 }
661
669 BitVector& shiftRight(const BitRange &range, size_t nShift, bool newBits = 0) {
670 checkRange(range);
671 BitVectorSupport::shiftRight(data(), range, nShift, newBits);
672 return *this;
673 }
674
682 BitVector& shiftRight(size_t nShift, bool newBits = 0) {
683 BitVectorSupport::shiftRight(data(), hull(), nShift, newBits);
684 return *this;
685 }
686
695 BitVector& shiftRightArithmetic(const BitRange &range, size_t nShift) {
696 checkRange(range);
698 return *this;
699 }
700
711 return *this;
712 }
713
719 BitVector& rotateRight(const BitRange &range, size_t nShift) {
720 checkRange(range);
721 BitVectorSupport::rotateRight(data(), range, nShift);
722 return *this;
723 }
724
730 BitVector& rotateRight(size_t nShift) {
732 return *this;
733 }
734
740 BitVector& rotateLeft(const BitRange &range, size_t nShift) {
741 checkRange(range);
742 BitVectorSupport::rotateLeft(data(), range, nShift);
743 return *this;
744 }
745
751 BitVector& rotateLeft(size_t nShift) {
753 return *this;
754 }
755
757 // Arithmetic
759
764 BitVector& negate(const BitRange &range1) {
765 checkRange(range1);
767 return *this;
768 }
769
776 return *this;
777 }
778
784 bool increment(const BitRange &range1) {
785 checkRange(range1);
786 return BitVectorSupport::increment(data(), range1);
787 }
788
793 bool increment() {
795 }
796
802 bool decrement(const BitRange &range1) {
803 checkRange(range1);
804 return BitVectorSupport::decrement(data(), range1);
805 }
806
811 bool decrement() {
813 }
814
821 bool add(const BitRange &range1, const BitVector &other, const BitRange &range2) {
822 checkRange(range1);
823 other.checkRange(range2);
824 return BitVectorSupport::add(other.data(), range2, data(), range1, false);
825 }
826
832 bool add(const BitRange &range1, const BitRange &range2) {
833 checkRange(range1);
834 checkRange(range2);
835 return BitVectorSupport::add(data(), range2, data(), range1, false);
836 }
837
844 bool add(const BitVector &other) {
845 return BitVectorSupport::add(other.data(), other.hull(), data(), hull(), false);
846 }
847
857 bool subtract(const BitRange &range1, const BitVector &other, const BitRange &range2) {
858 checkRange(range1);
859 other.checkRange(range2);
860 return BitVectorSupport::subtract(other.data(), range2, data(), range1);
861 }
862
871 bool subtract(const BitRange &range1, const BitRange &range2) {
872 checkRange(range1);
873 checkRange(range2);
874 return BitVectorSupport::subtract(data(), range2, data(), range1);
875 }
876
882 bool subtract(const BitVector &other) {
883 return BitVectorSupport::subtract(other.data(), other.hull(), data(), hull());
884 }
885
892 BitVector& signExtend(const BitRange &range1, const BitVector &other, const BitRange &range2) {
893 checkRange(range1);
894 other.checkRange(range2);
895 BitVectorSupport::signExtend(other.data(), range2, data(), range1);
896 return *this;
897 }
898
904 BitVector& signExtend(const BitRange &range1, const BitRange &range2) {
905 checkRange(range1);
906 checkRange(range2);
907 BitVectorSupport::signExtend(data(), range2, data(), range1);
908 return *this;
909 }
910
916 BitVectorSupport::signExtend(other.data(), other.hull(), data(), hull());
917 return *this;
918 }
919
926 return *this;
927 }
928
935 return *this;
936 }
937
942 BitVector multiply(const BitVector &other) const {
943 BitVector product(size() + other.size());
944 BitVector addend = other;
945 addend.resize(product.size());
946 for (size_t i = 0; i < size(); ++i) {
947 if (get(i))
948 product.add(addend);
949 addend.shiftLeft(1);
950 }
951 return product;
952 }
953
958 BitVector multiplySigned(const BitVector &other) const {
959 // Unoptimized version using simple elementary school long multiply. The Wikipedia "Binary multiplier" article has a good
960 // description of a more optimized approach.
961 BitVector product(size() + other.size());
962
963 // Absolute value of A
964 BitVector a = *this;
965 bool aIsNeg = false;
966 if (a.size() > 1 && a.get(a.size()-1)) {
967 aIsNeg = true;
968 a.negate();
969 }
970
971 // Absolute value of B, and extended to result width
972 BitVector b = other;
973 bool bIsNeg = false;
974 if (b.size() > 1 && b.get(b.size()-1)) {
975 bIsNeg = true;
976 b.negate();
977 }
978 b.resize(product.size());
979
980 // Long multiplication
981 for (size_t i = 0; i < a.size(); ++i) {
982 if (a.get(i))
983 product.add(b);
984 b.shiftLeft(1);
985 }
986
987 // Correct the result sign
988 if (aIsNeg != bIsNeg)
989 product.negate();
990
991 return product;
992 }
993
994 // FIXME[Robb Matzke 2014-05-01]: we should also have zeroExtend, which is like copy but allows the source and destination
995 // to be different sizes.
996
998 // Bit-wise Boolean logic
1000
1004 BitVector& invert(const BitRange &range) {
1005 checkRange(range);
1007 return *this;
1008 }
1009
1015 return *this;
1016 }
1017
1023 BitVector& bitwiseAnd(const BitRange &range1, const BitVector &other, const BitRange &range2) {
1024 checkRange(range1);
1025 other.checkRange(range2);
1026 BitVectorSupport::bitwiseAnd(other.data(), range2, data(), range1);
1027 return *this;
1028 }
1029
1034 BitVector& bitwiseAnd(const BitRange &range1, const BitRange &range2) {
1035 checkRange(range1);
1036 checkRange(range2);
1037 BitVectorSupport::bitwiseAnd(data(), range2, data(), range1);
1038 return *this;
1039 }
1040
1046 BitVectorSupport::bitwiseAnd(other.data(), other.hull(), data(), hull());
1047 return *this;
1048 }
1049
1055 BitVector& bitwiseOr(const BitRange &range1, const BitVector &other, const BitRange &range2) {
1056 checkRange(range1);
1057 other.checkRange(range2);
1058 BitVectorSupport::bitwiseOr(other.data(), range2, data(), range1);
1059 return *this;
1060 }
1061
1066 BitVector& bitwiseOr(const BitRange &range1, const BitRange &range2) {
1067 checkRange(range1);
1068 checkRange(range2);
1069 BitVectorSupport::bitwiseOr(data(), range2, data(), range1);
1070 return *this;
1071 }
1072
1078 BitVectorSupport::bitwiseOr(other.data(), other.hull(), data(), hull());
1079 return *this;
1080 }
1081
1087 BitVector& bitwiseXor(const BitRange &range1, const BitVector &other, const BitRange &range2) {
1088 checkRange(range1);
1089 other.checkRange(range2);
1090 BitVectorSupport::bitwiseXor(other.data(), range2, data(), range1);
1091 return *this;
1092 }
1093
1098 BitVector& bitwiseXor(const BitRange &range1, const BitRange &range2) {
1099 checkRange(range1);
1100 checkRange(range2);
1101 BitVectorSupport::bitwiseXor(data(), range2, data(), range1);
1102 return *this;
1103 }
1104
1110 BitVectorSupport::bitwiseXor(other.data(), other.hull(), data(), hull());
1111 return *this;
1112 }
1113
1115 // Numeric comparisons
1117
1124 bool isEqualToZero(const BitRange &range) const {
1125 checkRange(range);
1126 return BitVectorSupport::isEqualToZero(data(), range);
1127 }
1128
1134 bool isEqualToZero() const {
1136 }
1137
1146 int compare(const BitRange &range1, const BitVector &other, const BitRange &range2) const {
1147 checkRange(range1);
1148 other.checkRange(range2);
1149 return BitVectorSupport::compare(data(), range1, other.data(), range2);
1150 }
1151
1159 int compare(const BitRange &range1, const BitRange &range2) const {
1160 checkRange(range1);
1161 checkRange(range2);
1162 return BitVectorSupport::compare(data(), range1, data(), range2);
1163 }
1164
1172 int compare(const BitVector &other) const {
1173 return BitVectorSupport::compare(data(), hull(), other.data(), other.hull());
1174 }
1175
1185 int compareSigned(const BitRange &range1, const BitVector &other, const BitRange &range2) const {
1186 checkRange(range1);
1187 other.checkRange(range2);
1188 return BitVectorSupport::compareSigned(data(), range1, other.data(), range2);
1189 }
1190
1199 int compareSigned(const BitRange &range1, const BitRange &range2) const {
1200 checkRange(range1);
1201 checkRange(range2);
1202 return BitVectorSupport::compareSigned(data(), range1, data(), range2);
1203 }
1204
1212 int compareSigned(const BitVector &other) const {
1213 return BitVectorSupport::compareSigned(data(), hull(), other.data(), other.hull());
1214 }
1215
1217 // Conversion
1219
1224 boost::uint64_t toInteger(const BitRange &range) const {
1225 checkRange(range);
1226 return BitVectorSupport::toInteger(data(), range);
1227 }
1228
1233 boost::uint64_t toInteger() const {
1234 if (size() <= 64)
1237 }
1238
1245 boost::int64_t toSignedInteger(const BitRange &range) const {
1246 checkRange(range);
1247 return BitVectorSupport::toInteger(data(), range);
1248 }
1249
1256 boost::int64_t toSignedInteger() const {
1257 if (size() <= 64)
1260 }
1261
1268 std::string toHex(const BitRange &range) const {
1269 return BitVectorSupport::toHex(data(), range);
1270 }
1271
1278 std::string toHex() const {
1279 return BitVectorSupport::toHex(data(), hull());
1280 }
1281
1288 std::string toOctal(const BitRange &range) const {
1289 return BitVectorSupport::toOctal(data(), range);
1290 }
1291
1297 std::string toOctal() const {
1298 return BitVectorSupport::toOctal(data(), hull());
1299 }
1300
1307 std::string toBinary(const BitRange &range) const {
1308 return BitVectorSupport::toBinary(data(), range);
1309 }
1310
1316 std::string toBinary() const {
1318 }
1319
1327 std::vector<uint8_t> toBytes() const {
1328 return BitVectorSupport::toBytes(data(), hull());
1329 }
1330 std::vector<uint8_t> toBytes(const BitRange &range) const {
1331 return BitVectorSupport::toBytes(data(), range);
1332 }
1340 BitVector& fromInteger(const BitRange &range, boost::uint64_t value) {
1341 checkRange(range);
1342 BitVectorSupport::fromInteger(data(), range, value);
1343 return *this;
1344 }
1345
1353 BitVector& fromInteger(boost::uint64_t value) {
1355 return *this;
1356 }
1357
1367 BitVector& fromDecimal(const BitRange &range, const std::string &input) {
1368 checkRange(range);
1369 BitVectorSupport::fromDecimal(data(), range, input);
1370 return *this;
1371 }
1372
1381 BitVector& fromDecimal(const std::string &input) {
1383 return *this;
1384 }
1385
1394 BitVector& fromHex(const BitRange &range, const std::string &input) {
1395 checkRange(range);
1396 BitVectorSupport::fromHex(data(), range, input);
1397 return *this;
1398 }
1399
1408 BitVector& fromHex(const std::string &input) {
1409 BitVectorSupport::fromHex(data(), hull(), input);
1410 return *this;
1411 }
1412
1421 BitVector& fromOctal(const BitRange &range, const std::string &input) {
1422 checkRange(range);
1423 BitVectorSupport::fromOctal(data(), range, input);
1424 return *this;
1425 }
1426
1435 BitVector& fromOctal(const std::string &input) {
1437 return *this;
1438 }
1439
1448 BitVector& fromBinary(const BitRange &range, const std::string &input) {
1449 checkRange(range);
1450 BitVectorSupport::fromBinary(data(), range, input);
1451 return *this;
1452 }
1453
1462 BitVector& fromBinary(const std::string &input) {
1464 return *this;
1465 }
1466
1474 BitVector& fromBytes(const std::vector<uint8_t> &input) {
1475 BitVectorSupport::fromBytes(data(), hull(), input);
1476 return *this;
1477 }
1478 BitVector& fromBytes(const BitRange &range, const std::vector<uint8_t> &input) {
1479 BitVectorSupport::fromBytes(data(), range, input);
1480 return *this;
1481 }
1485 // Utility
1487
1492 void checkRange(const BitRange &range) const {
1493 ASSERT_always_require(hull().contains(range)); // so range is always used
1494 }
1495
1503 return words_.empty() ? NULL : &words_[0];
1504 }
1505
1506 const Word* data() const {
1507 return words_.empty() ? NULL : &words_[0];
1508 }
1514 size_t dataSize() const {
1515 return words_.size();
1516 }
1517};
1518
1519} // namespace
1520} // namespace
1521#endif
unsigned Word
Base storage type.
Definition BitVector.h:71
bool isAllSet() const
True if all bits are set.
Definition BitVector.h:499
BitVector & rotateLeft(size_t nShift)
Rotate bits left.
Definition BitVector.h:751
BitVector & clear(const BitRange &range)
Assign zero to some bits.
Definition BitVector.h:281
Optional< size_t > mostSignificantSetBit() const
Find the most significant set bit.
Definition BitVector.h:465
BitVector & shiftRight(size_t nShift, bool newBits=0)
Shift bits right.
Definition BitVector.h:682
BitVector()
Default construct an empty vector.
Definition BitVector.h:102
bool increment(const BitRange &range1)
Increment bits as integer.
Definition BitVector.h:784
BitVector & fromHex(const std::string &input)
Obtain bits from a hexadecimal representation.
Definition BitVector.h:1408
BitVector & multiply10(const BitRange &range)
Multiply by 10.
Definition BitVector.h:933
BitVector & fromOctal(const BitRange &range, const std::string &input)
Obtain bits from an octal representation.
Definition BitVector.h:1421
Optional< size_t > mostSignificantDifference(const BitVector &other) const
Find most significant difference.
Definition BitVector.h:589
std::string toOctal() const
Convert to an octal string.
Definition BitVector.h:1297
bool isAllClear() const
True if all bits are clear.
Definition BitVector.h:519
bool increment()
Increment bits as integer.
Definition BitVector.h:793
BitVector & shiftRight(const BitRange &range, size_t nShift, bool newBits=0)
Shift bits right.
Definition BitVector.h:669
bool isAllClear(const BitRange &range) const
True if all bits are clear.
Definition BitVector.h:509
bool decrement()
Decrement bits as integer.
Definition BitVector.h:811
Optional< size_t > leastSignificantClearBit(const BitRange &range) const
Find the least significant clear bit.
Definition BitVector.h:438
BitVector & fromOctal(const std::string &input)
Obtain bits from an octal representation.
Definition BitVector.h:1435
BitVector & bitwiseAnd(const BitRange &range1, const BitRange &range2)
Bit-wise AND.
Definition BitVector.h:1034
BitVector & negate(const BitRange &range1)
Negates bits as integer.
Definition BitVector.h:764
size_t capacity() const
Maximum size before reallocation.
Definition BitVector.h:236
Optional< size_t > mostSignificantSetBit(const BitRange &range) const
Find the most significant set bit.
Definition BitVector.h:456
BitVector multiplySigned(const BitVector &other) const
Multiply two signed integers.
Definition BitVector.h:958
boost::uint64_t toInteger(const BitRange &range) const
Interpret bits as an unsigned integer.
Definition BitVector.h:1224
bool equalTo(const BitRange &range1, const BitRange &range2) const
Checks whether the bits of two ranges are equal.
Definition BitVector.h:395
std::string toHex() const
Convert to a hexadecimal string.
Definition BitVector.h:1278
bool isEqualToZero(const BitRange &range) const
Compare to zero.
Definition BitVector.h:1124
BitVector & copy(const BitRange &to, const BitRange &from)
Copy some bits.
Definition BitVector.h:352
BitVector & negate()
Negates bits as integer.
Definition BitVector.h:774
BitVector & fromInteger(const BitRange &range, boost::uint64_t value)
Obtain bits from an integer.
Definition BitVector.h:1340
BitVector & signExtend(const BitVector &other)
Copy bits and sign extend.
Definition BitVector.h:915
int compareSigned(const BitRange &range1, const BitRange &range2) const
Compare bits as signed integers.
Definition BitVector.h:1199
BitVector & signExtend(const BitRange &range1, const BitVector &other, const BitRange &range2)
Copy bits and sign extend.
Definition BitVector.h:892
int compare(const BitVector &other) const
Compare bits as integers.
Definition BitVector.h:1172
BitVector & bitwiseOr(const BitVector &other)
Bit-wise OR.
Definition BitVector.h:1077
BitVector & rotateLeft(const BitRange &range, size_t nShift)
Rotate bits left.
Definition BitVector.h:740
size_t size() const
Size of vector in bits.
Definition BitVector.h:208
size_t nClear() const
Number of clear bits.
Definition BitVector.h:549
void checkRange(const BitRange &range) const
Assert valid range.
Definition BitVector.h:1492
std::vector< uint8_t > toBytes() const
Convert to a vector of bytes.
Definition BitVector.h:1327
std::string toOctal(const BitRange &range) const
Convert to an octal string.
Definition BitVector.h:1288
Optional< size_t > leastSignificantSetBit(const BitRange &range) const
Find the least significant set bit.
Definition BitVector.h:420
static BitRange hull(size_t minOffset, size_t maxOffset)
Create a bit range from min and max positions.
Definition BitVector.h:259
BitVector & swap(const BitRange &range1, BitVector &other, const BitRange &range2)
Swap some bits.
Definition BitVector.h:364
boost::int64_t toSignedInteger() const
Interpret bits as a signed integer.
Definition BitVector.h:1256
bool add(const BitRange &range1, const BitRange &range2)
Add bits as integers.
Definition BitVector.h:832
BitVector & multiply10()
Multiply by 10.
Definition BitVector.h:924
Optional< size_t > leastSignificantClearBit() const
Find the least significant clear bit.
Definition BitVector.h:447
BitVector & set()
Assign true to all bits.
Definition BitVector.h:311
bool subtract(const BitRange &range1, const BitVector &other, const BitRange &range2)
Subtract bits as integers.
Definition BitVector.h:857
std::string toBinary(const BitRange &range) const
Convert to a binary string.
Definition BitVector.h:1307
size_t dataSize() const
Raw data size.
Definition BitVector.h:1514
BitVector(size_t nbits, bool newBits=false)
Create a vector of specified size.
Definition BitVector.h:110
Optional< size_t > mostSignificantDifference(const BitRange &range1, const BitVector &other, const BitRange &range2) const
Find most significant difference.
Definition BitVector.h:563
BitVector & shiftRightArithmetic(size_t nShift)
Shift bits right.
Definition BitVector.h:709
int compareSigned(const BitRange &range1, const BitVector &other, const BitRange &range2) const
Compare bits as signed integers.
Definition BitVector.h:1185
BitRange hull() const
Interval representing the entire vector.
Definition BitVector.h:243
BitVector & bitwiseAnd(const BitVector &other)
Bit-wise AND.
Definition BitVector.h:1045
BitVector & resize(size_t newSize, bool newBits=false)
Change vector size.
Definition BitVector.h:215
BitVector & rotateRight(size_t nShift)
Rotate bits right.
Definition BitVector.h:730
static BitRange baseSize(size_t base, size_t size)
Create a bit range from a starting offset and size.
Definition BitVector.h:251
BitVector & bitwiseXor(const BitRange &range1, const BitVector &other, const BitRange &range2)
Bit-wise XOR.
Definition BitVector.h:1087
BitVector & clear()
Assign zero to all bits.
Definition BitVector.h:292
Word * data()
Raw data for vector.
Definition BitVector.h:1502
BitVector & invert(const BitRange &range)
Invert bits.
Definition BitVector.h:1004
BitVector & setValue(const BitRange &range, bool value)
Assign true/false to some bits.
Definition BitVector.h:319
bool decrement(const BitRange &range1)
Decrement bits as integer.
Definition BitVector.h:802
bool equalTo(const BitVector &other) const
Checks whether the bits of one vector are equal to the bits of the other.
Definition BitVector.h:405
std::vector< uint8_t > toBytes(const BitRange &range) const
Convert to a vector of bytes.
Definition BitVector.h:1330
BitVector & rotateRight(const BitRange &range, size_t nShift)
Rotate bits right.
Definition BitVector.h:719
bool subtract(const BitVector &other)
Subtract bits as integers.
Definition BitVector.h:882
BitVector & set(const BitRange &range)
Assign true to some bits.
Definition BitVector.h:301
bool get(size_t idx) const
Retrieve one bit.
Definition BitVector.h:271
BitVector & signExtend(const BitRange &range1, const BitRange &range2)
Copy bits and sign extend.
Definition BitVector.h:904
BitVector & bitwiseOr(const BitRange &range1, const BitVector &other, const BitRange &range2)
Bit-wise OR.
Definition BitVector.h:1055
BitVector & bitwiseOr(const BitRange &range1, const BitRange &range2)
Bit-wise OR.
Definition BitVector.h:1066
Optional< size_t > leastSignificantDifference(const BitRange &range1, const BitRange &range2) const
Find least significant difference.
Definition BitVector.h:618
BitVector & fromBinary(const BitRange &range, const std::string &input)
Obtain bits from a binary representation.
Definition BitVector.h:1448
BitVector & fromHex(const BitRange &range, const std::string &input)
Obtain bits from a hexadecimal representation.
Definition BitVector.h:1394
int compare(const BitRange &range1, const BitRange &range2) const
Compare bits as integers.
Definition BitVector.h:1159
int compare(const BitRange &range1, const BitVector &other, const BitRange &range2) const
Compare bits as integers.
Definition BitVector.h:1146
Optional< size_t > mostSignificantDifference(const BitRange &range1, const BitRange &range2) const
Find most significant difference.
Definition BitVector.h:578
static BitVector parse(std::string str)
Create a bit vector by reading a string.
Definition BitVector.h:125
BitVector(const BitVector &other)
Copy constructor.
Definition BitVector.h:105
BitVector & fromBytes(const std::vector< uint8_t > &input)
Obtain bits from a byte vector.
Definition BitVector.h:1474
boost::uint64_t toInteger() const
Interpret bits as an unsigned integer.
Definition BitVector.h:1233
BitVector & fromDecimal(const BitRange &range, const std::string &input)
Obtains bits from a decimal representation.
Definition BitVector.h:1367
bool isEqualToZero() const
Compare to zero.
Definition BitVector.h:1134
BitVector & setValue(bool value)
Assign true/false to all bits.
Definition BitVector.h:328
BitVector & fromInteger(boost::uint64_t value)
Obtain bits from an integer.
Definition BitVector.h:1353
BitVector & bitwiseXor(const BitVector &other)
Bit-wise XOR.
Definition BitVector.h:1109
BitVector multiply(const BitVector &other) const
Multiply two bit vectors.
Definition BitVector.h:942
Optional< size_t > mostSignificantClearBit() const
Find the most significant clear bit.
Definition BitVector.h:483
boost::int64_t toSignedInteger(const BitRange &range) const
Interpret bits as a signed integer.
Definition BitVector.h:1245
std::string toHex(const BitRange &range) const
Convert to a hexadecimal string.
Definition BitVector.h:1268
BitVector & operator=(const BitVector &other)
Assignment.
Definition BitVector.h:194
BitVector & fromBinary(const std::string &input)
Obtain bits from a binary representation.
Definition BitVector.h:1462
size_t nSet() const
Number of set bits.
Definition BitVector.h:534
Optional< size_t > leastSignificantSetBit() const
Find the least significant set bit.
Definition BitVector.h:429
Optional< size_t > leastSignificantDifference(const BitVector &other) const
Find least significant difference.
Definition BitVector.h:629
BitVector & bitwiseXor(const BitRange &range1, const BitRange &range2)
Bit-wise XOR.
Definition BitVector.h:1098
BitVector & shiftRightArithmetic(const BitRange &range, size_t nShift)
Shift bits right.
Definition BitVector.h:695
BitVectorSupport::BitRange BitRange
Describes an inclusive interval of bit indices.
Definition BitVector.h:72
std::string toBinary() const
Convert to an binary string.
Definition BitVector.h:1316
bool add(const BitRange &range1, const BitVector &other, const BitRange &range2)
Add bits as integers.
Definition BitVector.h:821
bool isAllSet(const BitRange &range) const
True if all bits are set.
Definition BitVector.h:491
BitVector & shiftLeft(size_t nShift, bool newBits=0)
Shift bits left.
Definition BitVector.h:657
size_t nSet(const BitRange &range) const
Number of set bits.
Definition BitVector.h:526
const Word * data() const
Raw data for vector.
Definition BitVector.h:1506
BitVector & copy(const BitRange &to, const BitVector &other, const BitRange &from)
Copy some bits.
Definition BitVector.h:340
bool subtract(const BitRange &range1, const BitRange &range2)
Subtract bits as integers.
Definition BitVector.h:871
Optional< size_t > leastSignificantDifference(const BitRange &range1, const BitVector &other, const BitRange &range2) const
Find least significant difference.
Definition BitVector.h:603
BitVector & swap(const BitRange &range1, const BitRange &range2)
Swap some bits.
Definition BitVector.h:375
BitVector & shiftLeft(const BitRange &range, size_t nShift, bool newBits=0)
Shift bits left.
Definition BitVector.h:644
bool add(const BitVector &other)
Add bits as integers.
Definition BitVector.h:844
size_t nClear(const BitRange &range) const
Number of clear bits.
Definition BitVector.h:541
bool equalTo(const BitRange &range1, BitVector &other, const BitRange &range2) const
Checks whether two ranges are equal.
Definition BitVector.h:385
Optional< size_t > mostSignificantClearBit(const BitRange &range) const
Find the most significant clear bit.
Definition BitVector.h:474
BitVector & fromBytes(const BitRange &range, const std::vector< uint8_t > &input)
Obtain bits from a byte vector.
Definition BitVector.h:1478
bool isEmpty() const
Determines if the vector is empty.
Definition BitVector.h:203
BitVector & invert()
Invert bits.
Definition BitVector.h:1013
BitVector & bitwiseAnd(const BitRange &range1, const BitVector &other, const BitRange &range2)
Bit-wise AND.
Definition BitVector.h:1023
int compareSigned(const BitVector &other) const
Compare bits as signed integers.
Definition BitVector.h:1212
BitVector & fromDecimal(const std::string &input)
Obtain bits from a decimal representation.
Definition BitVector.h:1381
static Interval baseSize(size_t lo, size_t size)
Construct an interval from one endpoint and a size.
Definition Interval.h:173
static Interval hull(size_t v1, size_t v2)
Construct an interval from two endpoints.
Definition Interval.h:162
Holds a value or nothing.
Definition Optional.h:56
bool isEqualToZero(const Word *vec1, const BitRange &range1)
Compares with zero.
bool isAllSet(const Word *words, const BitRange &range)
True if all bits are set.
void rotateLeft(Word *words, const BitRange &range, size_t nShift)
Rotate bits to the left.
Optional< size_t > mostSignificantDifference(const Word *vec1, const BitRange &range1, const Word *vec2, const BitRange &range2)
Find most significant different bits.
void negate(Word *vec1, const BitRange &range)
Negate bits as an integer.
std::string toBinary(const Word *vec, const BitRange &range)
Binary representation.
size_t nClear(const Word *words, const BitRange &range)
Number of clear bits.
bool signExtend(const Word *vec1, const BitRange &range1, Word *vec2, const BitRange &range2)
Sign extend.
bool decrement(Word *vec1, const BitRange &range1)
Decrement.
void bitwiseOr(const Word *vec1, const BitRange &range1, Word *vec2, const BitRange &range2)
Bit-wise OR.
void shiftLeft(Word *words, const BitRange &range, size_t nShift, bool newBits=0)
Shift bits left.
int compare(const Word *vec1, const BitRange &range1, const Word *vec2, const BitRange &range2)
Unsigned comparison.
boost::uint64_t toInteger(const Word *words, const BitRange &range)
Convert a bit vector to an integer.
void multiply10(Word *vec, const BitRange &range)
Multiply by 10.
bool subtract(const Word *vec1, const BitRange &range1, Word *vec2, const BitRange &range2)
Subtract bits.
void fromBinary(Word *vec, const BitRange &range, const std::string &input)
Obtain bits from a binary representation.
void fromOctal(Word *vec, const BitRange &range, const std::string &input)
Obtain bits from an octal representation.
void fromDecimal(Word *vec, const BitRange &range, const std::string &input)
Obtain bits from a decimal representation.
void shiftRightArithmetic(Word *words, const BitRange &range, size_t nShift)
Shift bits right arithmetically.
Optional< size_t > leastSignificantClearBit(const Word *words, const BitRange &range)
Index of least significant zero bit.
void swap(Word *vec1, const BitRange &range1, Word *vec2, const BitRange &range2)
Swap some bits.
size_t nSet(const Word *words, const BitRange &range)
Number of set bits.
void clear(Word *words, const BitRange &where)
Clear some bits.
Optional< size_t > mostSignificantClearBit(const Word *words, const BitRange &range)
Index of most significant clear bit.
void bitwiseAnd(const Word *vec1, const BitRange &range1, Word *vec2, const BitRange &range2)
Bit-wise AND.
void set(Word *words, const BitRange &where)
Set some bits.
void fromHex(Word *vec, const BitRange &range, const std::string &input)
Obtain bits from a hexadecimal representation.
bool get(const Word *words, size_t idx)
Return a single bit.
void rotateRight(Word *words, const BitRange &range, size_t nShift)
Rotate bits to the right.
void bitwiseXor(const Word *vec1, const BitRange &range1, Word *vec2, const BitRange &range2)
Bit-wise XOR.
bool equalTo(const Word *vec1, const BitRange &range1, const Word *vec2, const BitRange &range2)
Compare bits for equality.
boost::int64_t toSignedInteger(const Word *words, const BitRange &range)
Convert a bit vector to a signed integer.
bool increment(Word *vec1, const BitRange &range1)
Increment.
int compareSigned(const Word *vec1, const BitRange &range1, const Word *vec2, const BitRange &range2)
Signed comparison.
bool add(const Word *vec1, const BitRange &range1, Word *vec2, const BitRange &range2, bool carryIn=false)
Add bits.
Optional< size_t > leastSignificantSetBit(const Word *words, const BitRange &range)
Index of least significant set bit.
Optional< size_t > mostSignificantSetBit(const Word *words, const BitRange &range)
Index of most significant set bit.
std::string toOctal(const Word *vec, const BitRange &range)
Octal representation.
void fromInteger(Word *words, const BitRange &range, boost::uint64_t value)
Assign an unsigned value to a bit range.
void shiftRight(Word *words, const BitRange &range, size_t nShift, bool newBits=0)
Shift bits right.
void invert(Word *words, const BitRange &range)
Invert bits.
bool isAllClear(const Word *words, const BitRange &range)
True if all bits are clear.
Optional< size_t > leastSignificantDifference(const Word *vec1, const BitRange &range1, const Word *vec2, const BitRange &range2)
Find least significant different bits.
std::string toHex(const Word *vec, const BitRange &range)
Hexadecimal representation.
void setValue(Word *words, const BitRange &where, bool value)
Set or clear some bits.
void copy(const Word *src, const BitRange &srcRange, Word *dst, const BitRange &dstRange)
Copy some bits.
Sawyer support library.