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
|
#ifndef EWAH_INL_H
#define EWAH_INL_H
#include "ewah.h"
namespace ewah {
/**
* computes the logical or (union) between "n" bitmaps (referenced by a
* pointer).
* The answer gets written out in container. This might be faster than calling
* logicalor n-1 times.
*/
template <class uword>
void fast_logicalor_tocontainer(size_t n, const EWAHBoolArray<uword> **inputs,
EWAHBoolArray<uword> &container);
/**
* computes the logical or (union) between "n" bitmaps (referenced by a
* pointer).
* Returns the answer. This might be faster than calling
* logicalor n-1 times.
*/
template <class uword>
EWAHBoolArray<uword> fast_logicalor(size_t n,
const EWAHBoolArray<uword> **inputs) {
EWAHBoolArray<uword> answer;
fast_logicalor_tocontainer(n, inputs, answer);
return answer;
}
/**
* Iterate over words of bits from a compressed bitmap.
*/
template <class uword> class EWAHBoolArrayIterator {
public:
/**
* is there a new word?
*/
bool hasNext() const { return pointer < myparent.size(); }
/**
* return next word.
*/
uword next() {
uword returnvalue;
if (compressedwords < rl) {
++compressedwords;
if (b)
returnvalue = notzero;
else
returnvalue = zero;
} else {
++literalwords;
++pointer;
returnvalue = myparent[pointer];
}
if ((compressedwords == rl) && (literalwords == lw)) {
++pointer;
if (pointer < myparent.size())
readNewRunningLengthWord();
}
return returnvalue;
}
EWAHBoolArrayIterator(const EWAHBoolArrayIterator<uword> &other)
: pointer(other.pointer), myparent(other.myparent),
compressedwords(other.compressedwords),
literalwords(other.literalwords), rl(other.rl), lw(other.lw),
b(other.b) {}
static const uword zero = 0;
static const uword notzero = static_cast<uword>(~zero);
private:
EWAHBoolArrayIterator(const std::vector<uword> &parent);
void readNewRunningLengthWord();
friend class EWAHBoolArray<uword>;
size_t pointer;
const std::vector<uword> &myparent;
uword compressedwords;
uword literalwords;
uword rl, lw;
bool b;
};
/**
* Used to go through the set bits. Not optimally fast, but convenient.
*/
template <class uword> class EWAHBoolArraySetBitForwardIterator {
public:
typedef std::forward_iterator_tag iterator_category;
typedef size_t *pointer;
typedef size_t &reference_type;
typedef size_t value_type;
typedef ptrdiff_t difference_type;
typedef EWAHBoolArraySetBitForwardIterator<uword> type_of_iterator;
/**
* Provides the location of the set bit.
*/
inline size_t operator*() const { return answer; }
bool operator<(const type_of_iterator &o) const {
if (!o.hasValue)
return true;
if (!hasValue)
return false;
return answer < o.answer;
}
bool operator<=(const type_of_iterator &o) const {
if (!o.hasValue)
return true;
if (!hasValue)
return false;
return answer <= o.answer;
}
bool operator>(const type_of_iterator &o) const { return !((*this) <= o); }
bool operator>=(const type_of_iterator &o) const { return !((*this) < o); }
EWAHBoolArraySetBitForwardIterator &operator++() { //++i
if (hasNext)
next();
else
hasValue = false;
return *this;
}
EWAHBoolArraySetBitForwardIterator operator++(int) { // i++
EWAHBoolArraySetBitForwardIterator old(*this);
if (hasNext)
next();
else
hasValue = false;
return old;
}
bool operator==(const EWAHBoolArraySetBitForwardIterator<uword> &o) const {
if ((!hasValue) && (!o.hasValue))
return true;
return (hasValue == o.hasValue) && (answer == o.answer);
}
bool operator!=(const EWAHBoolArraySetBitForwardIterator<uword> &o) const {
return !(*this == o);
}
static EWAHBoolArraySetBitForwardIterator<uword> &end() {
static EWAHBoolArraySetBitForwardIterator<uword> e;
return e;
}
EWAHBoolArraySetBitForwardIterator(const std::vector<uword> *parent,
size_t startpointer = 0)
: word(0), position(0), runningLength(0), literalPosition(0),
wordPosition(startpointer), wordLength(0), buffer(parent),
hasNext(false), hasValue(false), answer(0) {
if (wordPosition < buffer->size()) {
setRunningLengthWord();
hasNext = moveToNext();
if (hasNext) {
next();
hasValue = true;
}
}
}
EWAHBoolArraySetBitForwardIterator()
: word(0), position(0), runningLength(0), literalPosition(0),
wordPosition(0), wordLength(0), buffer(NULL), hasNext(false),
hasValue(false), answer(0) {}
inline bool runningHasNext() const { return position < runningLength; }
inline bool literalHasNext() {
while (word == 0 && wordPosition < wordLength) {
word = (*buffer)[wordPosition++];
literalPosition = position;
position += WORD_IN_BITS;
}
return word != 0;
}
inline void setRunningLengthWord() {
uword rlw = (*buffer)[wordPosition];
runningLength =
(size_t)WORD_IN_BITS * RunningLengthWord<uword>::getRunningLength(rlw) +
position;
if (!RunningLengthWord<uword>::getRunningBit(rlw)) {
position = runningLength;
}
wordPosition++; // point to first literal word
wordLength =
static_cast<uword>(wordPosition + RunningLengthWord<uword>::getNumberOfLiteralWords(rlw));
}
inline bool moveToNext() {
while (!runningHasNext() && !literalHasNext()) {
if (wordPosition >= buffer->size()) {
return false;
}
setRunningLengthWord();
}
return true;
}
void next() { // update answer
if (runningHasNext()) {
answer = position++;
if (runningHasNext())
return;
} else {
uword t = static_cast<uword>(word & (~word + 1));
answer = literalPosition + countOnes((UWORD)(t - 1));
word ^= t;
}
hasNext = moveToNext();
}
enum { WORD_IN_BITS = sizeof(uword) * 8 };
uword word; // lit word
size_t position;
size_t runningLength;
size_t literalPosition;
size_t wordPosition; // points to word in buffer
uword wordLength;
const std::vector<uword> *buffer;
bool hasNext;
bool hasValue;
size_t answer;
};
/**
* This object is returned by the compressed bitmap as a
* statistical descriptor.
*/
class BitmapStatistics {
public:
BitmapStatistics()
: totalliteral(0), totalcompressed(0), runningwordmarker(0),
maximumofrunningcounterreached(0) {}
size_t getCompressedSize() const { return totalliteral + runningwordmarker; }
size_t getUncompressedSize() const { return totalliteral + totalcompressed; }
size_t getNumberOfDirtyWords() const { return totalliteral; }
size_t getNumberOfCleanWords() const { return totalcompressed; }
size_t getNumberOfMarkers() const { return runningwordmarker; }
size_t getOverRuns() const { return maximumofrunningcounterreached; }
size_t totalliteral;
size_t totalcompressed;
size_t runningwordmarker;
size_t maximumofrunningcounterreached;
};
template <class uword> bool EWAHBoolArray<uword>::set(size_t i) {
if (i < sizeinbits)
return false;
const size_t dist = (i + wordinbits) / wordinbits -
(sizeinbits + wordinbits - 1) / wordinbits;
sizeinbits = i + 1;
if (dist > 0) { // easy
if (dist > 1) {
fastaddStreamOfEmptyWords(false, dist - 1);
}
addLiteralWord(
static_cast<uword>(static_cast<uword>(1) << (i % wordinbits)));
return true;
}
RunningLengthWord<uword> lastRunningLengthWord(buffer[lastRLW]);
if (lastRunningLengthWord.getNumberOfLiteralWords() == 0) {
lastRunningLengthWord.setRunningLength(
static_cast<uword>(lastRunningLengthWord.getRunningLength() - 1));
addLiteralWord(
static_cast<uword>(static_cast<uword>(1) << (i % wordinbits)));
return true;
}
buffer[buffer.size() - 1] |=
static_cast<uword>(static_cast<uword>(1) << (i % wordinbits));
// check if we just completed a stream of 1s
if (buffer[buffer.size() - 1] == static_cast<uword>(~0)) {
// we remove the last dirty word
buffer[buffer.size() - 1] = 0;
buffer.resize(buffer.size() - 1);
lastRunningLengthWord.setNumberOfLiteralWords(static_cast<uword>(
lastRunningLengthWord.getNumberOfLiteralWords() - 1));
// next we add one clean word
addEmptyWord(true);
}
return true;
}
template <class uword> void EWAHBoolArray<uword>::inplace_logicalnot() {
size_t pointer(0), lastrlw(0);
while (pointer < buffer.size()) {
RunningLengthWord<uword> rlw(buffer[pointer]);
lastrlw = pointer; // we save this up
if (rlw.getRunningBit())
rlw.setRunningBit(false);
else
rlw.setRunningBit(true);
++pointer;
for (size_t k = 0; k < rlw.getNumberOfLiteralWords(); ++k) {
buffer[pointer] = static_cast<uword>(~buffer[pointer]);
++pointer;
}
}
if (sizeinbits % wordinbits != 0) {
RunningLengthWord<uword> rlw(buffer[lastrlw]);
const uword maskbogus =
static_cast<uword>((static_cast<uword>(1) << (sizeinbits % wordinbits)) - 1);
if (rlw.getNumberOfLiteralWords() > 0) { // easy case
buffer[lastrlw + 1 + rlw.getNumberOfLiteralWords() - 1] &= maskbogus;
} else {
rlw.setRunningLength(rlw.getRunningLength() - 1);
addLiteralWord(maskbogus);
}
}
}
template <class uword> size_t EWAHBoolArray<uword>::numberOfWords() const {
size_t tot(0);
size_t pointer(0);
while (pointer < buffer.size()) {
ConstRunningLengthWord<uword> rlw(buffer[pointer]);
tot += rlw.size();
pointer += 1 + rlw.getNumberOfLiteralWords();
}
return tot;
}
template <class uword>
void EWAHBoolArray<uword>::assertWordCount(std::string message) const {
#ifdef EWAHASSERT
size_t tot = numberOfWords();
size_t expected = (sizeinbits + wordinbits - 1) / wordinbits;
if (expected != tot) {
std::cerr << "[assertWordCount] wordinbits " << wordinbits << std::endl;
std::cerr << "[assertWordCount] sizeinbits " << sizeinbits << std::endl;
std::cerr << "[assertWordCount] " << message << std::endl;
std::cerr << "[assertWordCount] number of words " << tot << std::endl;
std::cerr << "[assertWordCount] expected number of words " << expected
<< std::endl;
debugprintout();
throw std::runtime_error("bug");
}
#endif
}
template <class uword> void EWAHBoolArray<uword>::correctWordCount() {
size_t tot = numberOfWords();
size_t expected = (sizeinbits + wordinbits - 1) / wordinbits;
if (expected != tot) {
if (tot < expected) {
fastaddStreamOfEmptyWords(false, expected - tot);
} else {
RunningLengthWord<uword> lastRunningLengthWord(buffer[lastRLW]);
lastRunningLengthWord.setRunningLength(static_cast<uword>(
lastRunningLengthWord.getRunningLength() + expected - tot));
}
}
}
template <class uword> size_t EWAHBoolArray<uword>::numberOfOnes() const {
size_t tot(0);
size_t pointer(0);
while (pointer < buffer.size()) {
ConstRunningLengthWord<uword> rlw(buffer[pointer]);
if (rlw.getRunningBit()) {
tot += static_cast<size_t>(rlw.getRunningLength() * wordinbits);
}
++pointer;
for (size_t k = 0; k < rlw.getNumberOfLiteralWords(); ++k) {
tot += countOnes((UWORD)buffer[pointer]);
++pointer;
}
}
return tot;
}
template <class uword>
std::vector<size_t> EWAHBoolArray<uword>::toArray() const {
std::vector<size_t> ans;
size_t pos(0);
size_t pointer(0);
const size_t buffersize = buffer.size();
while (pointer < buffersize) {
ConstRunningLengthWord<uword> rlw(buffer[pointer]);
const size_t productofrl =
static_cast<size_t>(rlw.getRunningLength() * wordinbits);
if (rlw.getRunningBit()) {
size_t upper_limit = pos + productofrl;
for (; pos < upper_limit; ++pos) {
ans.push_back(pos);
}
} else {
pos += productofrl;
}
++pointer;
const size_t rlwlw = rlw.getNumberOfLiteralWords();
for (size_t k = 0; k < rlwlw; ++k) {
uword myword = buffer[pointer];
while (myword != 0) {
uint64_t t = myword & (~myword + 1);
uint32_t r = numberOfTrailingZeros(t);
ans.push_back(pos + r);
myword ^= t;
}
pos += wordinbits;
++pointer;
}
}
return ans;
}
template <class uword>
void EWAHBoolArray<uword>::logicalnot(EWAHBoolArray &x) const {
x.reset();
x.buffer.reserve(buffer.size());
EWAHBoolArrayRawIterator<uword> i = this->raw_iterator();
if (!i.hasNext())
return; // nothing to do
while (true) {
BufferedRunningLengthWord<uword> &rlw = i.next();
if (i.hasNext()) {
if (rlw.getRunningLength() > 0)
x.fastaddStreamOfEmptyWords(!rlw.getRunningBit(),
rlw.getRunningLength());
if (rlw.getNumberOfLiteralWords() > 0) {
const uword *dw = i.dirtyWords();
for (size_t k = 0; k < rlw.getNumberOfLiteralWords(); ++k) {
x.addLiteralWord(~dw[k]);
}
}
} else {
if (rlw.getNumberOfLiteralWords() == 0) {
if ((this->sizeinbits % wordinbits != 0) && !rlw.getRunningBit()) {
if (rlw.getRunningLength() > 1)
x.fastaddStreamOfEmptyWords(!rlw.getRunningBit(),
rlw.getRunningLength() - 1);
const uword maskbogus =
static_cast<uword>((static_cast<uword>(1) << (this->sizeinbits % wordinbits)) - 1);
x.addLiteralWord(maskbogus);
break;
} else {
if (rlw.getRunningLength() > 0)
x.fastaddStreamOfEmptyWords(!rlw.getRunningBit(),
rlw.getRunningLength());
break;
}
}
if (rlw.getRunningLength() > 0)
x.fastaddStreamOfEmptyWords(!rlw.getRunningBit(),
rlw.getRunningLength());
const uword *dw = i.dirtyWords();
for (size_t k = 0; k + 1 < rlw.getNumberOfLiteralWords(); ++k) {
x.addLiteralWord(~dw[k]);
}
const uword maskbogus =
(this->sizeinbits % wordinbits != 0)
? static_cast<uword>((static_cast<uword>(1) << (this->sizeinbits % wordinbits)) - 1)
: ~static_cast<uword>(0);
x.addLiteralWord(static_cast<uword>((~dw[rlw.getNumberOfLiteralWords() - 1]) & maskbogus));
break;
}
}
x.sizeinbits = this->sizeinbits;
}
template <class uword>
size_t EWAHBoolArray<uword>::addWord(const uword newdata,
const uint32_t bitsthatmatter) {
sizeinbits += bitsthatmatter;
if (newdata == 0) {
return addEmptyWord(0);
} else if (newdata == static_cast<uword>(~0)) {
return addEmptyWord(1);
} else {
return addLiteralWord(newdata);
}
}
template <class uword>
inline void EWAHBoolArray<uword>::writeBuffer(std::ostream &out) const {
if (!buffer.empty())
out.write(reinterpret_cast<const char *>(&buffer[0]),
sizeof(uword) * buffer.size());
}
template <class uword>
inline void EWAHBoolArray<uword>::readBuffer(std::istream &in,
const size_t buffersize) {
buffer.resize(buffersize);
if (buffersize > 0)
in.read(reinterpret_cast<char *>(&buffer[0]), sizeof(uword) * buffersize);
}
template <class uword>
size_t EWAHBoolArray<uword>::write(std::ostream &out,
const bool savesizeinbits) const {
size_t written = 0;
if (savesizeinbits) {
uint64_t sb = static_cast<uint64_t>(sizeinbits);
out.write(reinterpret_cast<const char *>(&sb), sizeof(sb));
written += sizeof(uint64_t);
}
const size_t buffersize = buffer.size();
uint64_t bs = static_cast<uint64_t>(buffersize);
out.write(reinterpret_cast<const char *>(&bs), sizeof(bs));
written += sizeof(uint64_t);
if (buffersize > 0) {
out.write(reinterpret_cast<const char *>(&buffer[0]),
static_cast<std::streamsize>(sizeof(uword) * buffersize));
written += sizeof(uword) * buffersize;
}
return written;
}
template <class uword>
size_t EWAHBoolArray<uword>::write(char *out, size_t capacity,
const bool savesizeinbits) const {
size_t written = 0;
if (savesizeinbits) {
uint64_t sb = static_cast<uint64_t>(sizeinbits);
if (capacity < sizeof(sb))
return 0;
capacity -= sizeof(sb);
memcpy(out, &sb, sizeof(sb));
out += sizeof(sb);
written += sizeof(uint64_t);
}
const size_t buffersize = buffer.size();
uint64_t bs = static_cast<uint64_t>(buffersize);
if (capacity < sizeof(bs))
return 0;
capacity -= sizeof(bs);
memcpy(out, &buffersize, sizeof(bs));
out += sizeof(bs);
written += sizeof(uint64_t);
if (buffersize > 0) {
if (capacity < sizeof(uword) * buffersize)
return 0;
memcpy(out, &buffer[0], sizeof(uword) * buffersize);
written += sizeof(uword) * buffersize;
}
return written;
}
template <class uword>
size_t EWAHBoolArray<uword>::read(std::istream &in, const bool savesizeinbits) {
size_t read = 0;
if (savesizeinbits) {
uint64_t tmp;
in.read(reinterpret_cast<char *>(&tmp), sizeof(tmp));
read += sizeof(tmp);
sizeinbits = static_cast<size_t>(tmp);
} else {
sizeinbits = 0;
}
size_t buffersize(0);
uint64_t tmp;
in.read(reinterpret_cast<char *>(&tmp), sizeof(tmp));
read += sizeof(tmp);
buffersize = static_cast<size_t>(tmp);
buffer.resize(buffersize);
if (buffersize > 0) {
in.read(reinterpret_cast<char *>(&buffer[0]),
static_cast<std::streamsize>(sizeof(uword) * buffersize));
read += sizeof(uword) * buffersize;
}
return read;
}
template <class uword>
size_t EWAHBoolArray<uword>::read(const char *in, size_t capacity,
const bool savesizeinbits) {
size_t read = 0;
if (savesizeinbits) {
uint64_t tmp;
if (capacity < sizeof(tmp))
return 0;
capacity -= sizeof(tmp);
memcpy(reinterpret_cast<char *>(&tmp), in, sizeof(tmp));
read += sizeof(tmp);
in += sizeof(tmp);
sizeinbits = static_cast<size_t>(tmp);
} else {
sizeinbits = 0;
}
size_t buffersize(0);
uint64_t tmp;
if (capacity < sizeof(uint64_t))
return 0;
capacity -= sizeof(uint64_t);
memcpy(reinterpret_cast<char *>(&tmp), in, sizeof(uint64_t));
in += sizeof(uint64_t);
read += sizeof(uint64_t);
buffersize = static_cast<size_t>(tmp);
buffer.resize(buffersize);
if (buffersize > 0) {
if (capacity < sizeof(uword) * buffersize)
return 0;
memcpy(&buffer[0], in, sizeof(uword) * buffersize);
read += sizeof(uword) * buffersize;
}
return read;
}
template <class uword>
size_t EWAHBoolArray<uword>::addLiteralWord(const uword newdata) {
RunningLengthWord<uword> lastRunningLengthWord(buffer[lastRLW]);
uword numbersofar = lastRunningLengthWord.getNumberOfLiteralWords();
if (numbersofar >=
RunningLengthWord<uword>::largestliteralcount) { // 0x7FFF) {
buffer.push_back(0);
lastRLW = buffer.size() - 1;
RunningLengthWord<uword> lastRunningLengthWord2(buffer[lastRLW]);
lastRunningLengthWord2.setNumberOfLiteralWords(1);
buffer.push_back(newdata);
return 2;
}
lastRunningLengthWord.setNumberOfLiteralWords(
static_cast<uword>(numbersofar + 1));
buffer.push_back(newdata);
return 1;
}
template <class uword>
size_t EWAHBoolArray<uword>::padWithZeroes(const size_t totalbits) {
size_t wordsadded = 0;
if (totalbits <= sizeinbits)
return wordsadded;
size_t missingbits = totalbits - sizeinbits;
RunningLengthWord<uword> rlw(buffer[lastRLW]);
if (rlw.getNumberOfLiteralWords() > 0) {
// Consume trailing zeroes of trailing literal word (past sizeinbits)
size_t remain = sizeinbits % wordinbits;
if (remain > 0) // Is last word partial?
{
size_t avail = wordinbits - remain;
if (avail > 0) {
if (missingbits > avail) {
missingbits -= avail;
} else {
missingbits = 0;
}
sizeinbits += avail;
}
}
}
if (missingbits > 0) {
size_t wordstoadd = missingbits / wordinbits;
if ((missingbits % wordinbits) != 0)
++wordstoadd;
wordsadded = addStreamOfEmptyWords(false, wordstoadd);
}
sizeinbits = totalbits;
return wordsadded;
}
/**
* This is a low-level iterator.
*/
template <class uword = uint32_t> class EWAHBoolArrayRawIterator {
public:
EWAHBoolArrayRawIterator(const EWAHBoolArray<uword> &p)
: pointer(0), myparent(&p.getBuffer()), rlw((*myparent)[pointer], this) {}
EWAHBoolArrayRawIterator(const EWAHBoolArrayRawIterator &o)
: pointer(o.pointer), myparent(o.myparent), rlw(o.rlw) {}
bool hasNext() const { return pointer < myparent->size(); }
BufferedRunningLengthWord<uword> &next() {
rlw.read((*myparent)[pointer]);
pointer = static_cast<size_t>(pointer + rlw.getNumberOfLiteralWords() + 1);
return rlw;
}
const uword *dirtyWords() const {
return myparent->data() +
static_cast<size_t>(pointer - rlw.getNumberOfLiteralWords());
}
EWAHBoolArrayRawIterator &operator=(const EWAHBoolArrayRawIterator &other) {
pointer = other.pointer;
myparent = other.myparent;
rlw = other.rlw;
return *this;
}
size_t pointer;
const std::vector<uword> *myparent;
BufferedRunningLengthWord<uword> rlw;
EWAHBoolArrayRawIterator();
};
template <class uword>
EWAHBoolArrayIterator<uword> EWAHBoolArray<uword>::uncompress() const {
return EWAHBoolArrayIterator<uword>(buffer);
}
template <class uword>
EWAHBoolArrayRawIterator<uword> EWAHBoolArray<uword>::raw_iterator() const {
return EWAHBoolArrayRawIterator<uword>(*this);
}
template <class uword>
bool EWAHBoolArray<uword>::operator==(const EWAHBoolArray &x) const {
EWAHBoolArrayRawIterator<uword> i = x.raw_iterator();
EWAHBoolArrayRawIterator<uword> j = raw_iterator();
if (!(i.hasNext() and j.hasNext())) { // hopefully this never happens...
return (i.hasNext() == false) && (j.hasNext() == false);
}
// at this point, this should be safe:
BufferedRunningLengthWord<uword> &rlwi = i.next();
BufferedRunningLengthWord<uword> &rlwj = j.next();
while ((rlwi.size() > 0) && (rlwj.size() > 0)) {
while ((rlwi.getRunningLength() > 0) || (rlwj.getRunningLength() > 0)) {
const bool i_is_prey = rlwi.getRunningLength() < rlwj.getRunningLength();
BufferedRunningLengthWord<uword> &prey = i_is_prey ? rlwi : rlwj;
BufferedRunningLengthWord<uword> &predator = i_is_prey ? rlwj : rlwi;
size_t index = 0;
const bool nonzero =
((!predator.getRunningBit())
? prey.nonzero_discharge(predator.getRunningLength(), index)
: prey.nonzero_dischargeNegated(predator.getRunningLength(),
index));
if (nonzero) {
return false;
}
if (predator.getRunningLength() - index > 0) {
if (predator.getRunningBit()) {
return false;
}
}
predator.discardRunningWordsWithReload();
}
const uword nbre_literal = std::min(rlwi.getNumberOfLiteralWords(),
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (size_t k = 0; k < nbre_literal; ++k)
if ((rlwi.getLiteralWordAt(k) ^ rlwj.getLiteralWordAt(k)) != 0)
return false;
rlwi.discardLiteralWordsWithReload(nbre_literal);
rlwj.discardLiteralWordsWithReload(nbre_literal);
}
}
const bool i_remains = rlwi.size() > 0;
BufferedRunningLengthWord<uword> &remaining = i_remains ? rlwi : rlwj;
return !remaining.nonzero_discharge();
}
template <class uword> void EWAHBoolArray<uword>::swap(EWAHBoolArray &x) {
buffer.swap(x.buffer);
size_t tmp = x.sizeinbits;
x.sizeinbits = sizeinbits;
sizeinbits = tmp;
tmp = x.lastRLW;
x.lastRLW = lastRLW;
lastRLW = tmp;
}
template <class uword>
void EWAHBoolArray<uword>::append(const EWAHBoolArray &x) {
if (sizeinbits % wordinbits == 0) {
// hoping for the best?
sizeinbits += x.sizeinbits;
ConstRunningLengthWord<uword> lRLW(buffer[lastRLW]);
if ((lRLW.getRunningLength() == 0) &&
(lRLW.getNumberOfLiteralWords() == 0)) {
// it could be that the running length word is empty, in such a case,
// we want to get rid of it!
lastRLW = x.lastRLW + buffer.size() - 1;
buffer.resize(buffer.size() - 1);
buffer.insert(buffer.end(), x.buffer.begin(), x.buffer.end());
} else {
lastRLW = x.lastRLW + buffer.size();
buffer.insert(buffer.end(), x.buffer.begin(), x.buffer.end());
}
} else {
std::stringstream ss;
ss << "This should really not happen! You are trying to append to a bitmap "
"having a fractional number of words, that is, "
<< static_cast<int>(sizeinbits) << " bits with a word size in bits of "
<< static_cast<int>(wordinbits) << ". ";
ss << "Size of the bitmap being appended: " << x.sizeinbits << " bits."
<< std::endl;
throw std::invalid_argument(ss.str());
}
}
template <class uword>
EWAHBoolArrayIterator<uword>::EWAHBoolArrayIterator(
const std::vector<uword> &parent)
: pointer(0), myparent(parent), compressedwords(0), literalwords(0), rl(0),
lw(0), b(0) {
if (pointer < myparent.size())
readNewRunningLengthWord();
}
template <class uword>
void EWAHBoolArrayIterator<uword>::readNewRunningLengthWord() {
literalwords = 0;
compressedwords = 0;
ConstRunningLengthWord<uword> rlw(myparent[pointer]);
rl = rlw.getRunningLength();
lw = rlw.getNumberOfLiteralWords();
b = rlw.getRunningBit();
if ((rl == 0) && (lw == 0)) {
if (pointer < myparent.size() - 1) {
++pointer;
readNewRunningLengthWord();
} else {
pointer = myparent.size();
}
}
}
template <class uword>
BoolArray<uword> EWAHBoolArray<uword>::toBoolArray() const {
BoolArray<uword> ans(sizeinbits);
EWAHBoolArrayIterator<uword> i = uncompress();
size_t counter = 0;
while (i.hasNext()) {
ans.setWord(counter++, i.next());
}
return ans;
}
template <class uword>
template <class container>
void EWAHBoolArray<uword>::appendSetBits(container &out,
const size_t offset) const {
size_t pointer(0);
size_t currentoffset(offset);
if (RESERVEMEMORY)
out.reserve(buffer.size() + 64); // trading memory for speed.
const size_t buffersize = buffer.size();
while (pointer < buffersize) {
ConstRunningLengthWord<uword> rlw(buffer[pointer]);
const size_t productofrl =
static_cast<size_t>(rlw.getRunningLength() * wordinbits);
if (rlw.getRunningBit()) {
const size_t upper_limit = currentoffset + productofrl;
for (; currentoffset < upper_limit; ++currentoffset) {
out.push_back(currentoffset);
}
} else {
currentoffset += productofrl;
}
++pointer;
const size_t rlwlw = rlw.getNumberOfLiteralWords();
for (uword k = 0; k < rlwlw; ++k) {
uword currentword = buffer[pointer];
while (currentword != 0) {
uword t = static_cast<uword>(currentword & (~currentword+1));
uint32_t r = numberOfTrailingZeros(t);
out.push_back(currentoffset + r);
currentword ^= t;
}
currentoffset += wordinbits;
++pointer;
}
}
}
template <class uword>
bool EWAHBoolArray<uword>::operator!=(const EWAHBoolArray<uword> &x) const {
return !(*this == x);
}
template <class uword>
bool EWAHBoolArray<uword>::operator==(const BoolArray<uword> &x) const {
// could be more efficient
return (this->toBoolArray() == x);
}
template <class uword>
bool EWAHBoolArray<uword>::operator!=(const BoolArray<uword> &x) const {
// could be more efficient
return (this->toBoolArray() != x);
}
template <class uword>
size_t EWAHBoolArray<uword>::addStreamOfEmptyWords(const bool v,
size_t number) {
if (number == 0)
return 0;
sizeinbits += number * wordinbits;
size_t wordsadded = 0;
if ((RunningLengthWord<uword>::getRunningBit(buffer[lastRLW]) != v) &&
(RunningLengthWord<uword>::size(buffer[lastRLW]) == 0)) {
RunningLengthWord<uword>::setRunningBit(buffer[lastRLW], v);
} else if ((RunningLengthWord<uword>::getNumberOfLiteralWords(
buffer[lastRLW]) != 0) ||
(RunningLengthWord<uword>::getRunningBit(buffer[lastRLW]) != v)) {
buffer.push_back(0);
++wordsadded;
lastRLW = buffer.size() - 1;
if (v)
RunningLengthWord<uword>::setRunningBit(buffer[lastRLW], v);
}
const uword runlen =
RunningLengthWord<uword>::getRunningLength(buffer[lastRLW]);
const uword whatwecanadd =
number < static_cast<size_t>(
RunningLengthWord<uword>::largestrunninglengthcount - runlen)
? static_cast<uword>(number)
: static_cast<uword>(
RunningLengthWord<uword>::largestrunninglengthcount - runlen);
RunningLengthWord<uword>::setRunningLength(
buffer[lastRLW], static_cast<uword>(runlen + whatwecanadd));
number -= static_cast<size_t>(whatwecanadd);
while (number >= RunningLengthWord<uword>::largestrunninglengthcount) {
buffer.push_back(0);
++wordsadded;
lastRLW = buffer.size() - 1;
if (v)
RunningLengthWord<uword>::setRunningBit(buffer[lastRLW], v);
RunningLengthWord<uword>::setRunningLength(
buffer[lastRLW], RunningLengthWord<uword>::largestrunninglengthcount);
number -= static_cast<size_t>(
RunningLengthWord<uword>::largestrunninglengthcount);
}
if (number > 0) {
buffer.push_back(0);
++wordsadded;
lastRLW = buffer.size() - 1;
if (v)
RunningLengthWord<uword>::setRunningBit(buffer[lastRLW], v);
RunningLengthWord<uword>::setRunningLength(buffer[lastRLW],
static_cast<uword>(number));
}
return wordsadded;
}
template <class uword>
void EWAHBoolArray<uword>::fastaddStreamOfEmptyWords(const bool v,
size_t number) {
if (number == 0)
return;
if ((RunningLengthWord<uword>::getRunningBit(buffer[lastRLW]) != v) &&
(RunningLengthWord<uword>::size(buffer[lastRLW]) == 0)) {
RunningLengthWord<uword>::setRunningBit(buffer[lastRLW], v);
} else if ((RunningLengthWord<uword>::getNumberOfLiteralWords(
buffer[lastRLW]) != 0) ||
(RunningLengthWord<uword>::getRunningBit(buffer[lastRLW]) != v)) {
buffer.push_back(0);
lastRLW = buffer.size() - 1;
if (v)
RunningLengthWord<uword>::setRunningBit(buffer[lastRLW], v);
}
const uword runlen =
RunningLengthWord<uword>::getRunningLength(buffer[lastRLW]);
const uword whatwecanadd =
number < static_cast<size_t>(
RunningLengthWord<uword>::largestrunninglengthcount - runlen)
? static_cast<uword>(number)
: static_cast<uword>(
RunningLengthWord<uword>::largestrunninglengthcount - runlen);
RunningLengthWord<uword>::setRunningLength(
buffer[lastRLW], static_cast<uword>(runlen + whatwecanadd));
number -= static_cast<size_t>(whatwecanadd);
while (number >= RunningLengthWord<uword>::largestrunninglengthcount) {
buffer.push_back(0);
lastRLW = buffer.size() - 1;
if (v)
RunningLengthWord<uword>::setRunningBit(buffer[lastRLW], v);
RunningLengthWord<uword>::setRunningLength(
buffer[lastRLW], RunningLengthWord<uword>::largestrunninglengthcount);
number -= static_cast<size_t>(
RunningLengthWord<uword>::largestrunninglengthcount);
}
if (number > 0) {
buffer.push_back(0);
lastRLW = buffer.size() - 1;
if (v)
RunningLengthWord<uword>::setRunningBit(buffer[lastRLW], v);
RunningLengthWord<uword>::setRunningLength(buffer[lastRLW],
static_cast<uword>(number));
}
}
template <class uword>
size_t EWAHBoolArray<uword>::addStreamOfDirtyWords(const uword *v,
const size_t number) {
if (number == 0)
return 0;
uword rlw = buffer[lastRLW];
size_t NumberOfLiteralWords =
RunningLengthWord<uword>::getNumberOfLiteralWords(rlw);
if (NumberOfLiteralWords + number <=
RunningLengthWord<uword>::largestliteralcount) {
RunningLengthWord<uword>::setNumberOfLiteralWords(
rlw, static_cast<uword>(NumberOfLiteralWords + number));
buffer[lastRLW] = rlw;
sizeinbits += number * wordinbits;
buffer.insert(buffer.end(), v, v + number);
return number;
}
// we proceed the long way
size_t howmanywecanadd =
RunningLengthWord<uword>::largestliteralcount - NumberOfLiteralWords;
RunningLengthWord<uword>::setNumberOfLiteralWords(
rlw, RunningLengthWord<uword>::largestliteralcount);
buffer[lastRLW] = rlw;
buffer.insert(buffer.end(), v, v + howmanywecanadd);
size_t wordadded = howmanywecanadd;
sizeinbits += howmanywecanadd * wordinbits;
buffer.push_back(0);
lastRLW = buffer.size() - 1;
++wordadded;
wordadded +=
addStreamOfDirtyWords(v + howmanywecanadd, number - howmanywecanadd);
return wordadded;
}
template <class uword>
void EWAHBoolArray<uword>::fastaddStreamOfDirtyWords(const uword *v,
const size_t number) {
if (number == 0)
return;
uword rlw = buffer[lastRLW];
size_t NumberOfLiteralWords =
RunningLengthWord<uword>::getNumberOfLiteralWords(rlw);
if (NumberOfLiteralWords + number <=
RunningLengthWord<uword>::largestliteralcount) {
RunningLengthWord<uword>::setNumberOfLiteralWords(
rlw, static_cast<uword>(NumberOfLiteralWords + number));
buffer[lastRLW] = rlw;
for (size_t i = 0; i < number; ++i)
buffer.push_back(v[i]);
// buffer.insert(buffer.end(), v, v+number); // seems slower than push_back?
return;
}
// we proceed the long way
size_t howmanywecanadd =
RunningLengthWord<uword>::largestliteralcount - NumberOfLiteralWords;
RunningLengthWord<uword>::setNumberOfLiteralWords(
rlw, RunningLengthWord<uword>::largestliteralcount);
buffer[lastRLW] = rlw;
for (size_t i = 0; i < howmanywecanadd; ++i)
buffer.push_back(v[i]);
// buffer.insert(buffer.end(), v, v+howmanywecanadd);// seems slower than
// push_back?
buffer.push_back(0);
lastRLW = buffer.size() - 1;
fastaddStreamOfDirtyWords(v + howmanywecanadd, number - howmanywecanadd);
}
template <class uword>
size_t EWAHBoolArray<uword>::addStreamOfNegatedDirtyWords(const uword *v,
const size_t number) {
if (number == 0)
return 0;
uword rlw = buffer[lastRLW];
size_t NumberOfLiteralWords =
RunningLengthWord<uword>::getNumberOfLiteralWords(rlw);
if (NumberOfLiteralWords + number <=
RunningLengthWord<uword>::largestliteralcount) {
RunningLengthWord<uword>::setNumberOfLiteralWords(
rlw, static_cast<uword>(NumberOfLiteralWords + number));
buffer[lastRLW] = rlw;
sizeinbits += number * wordinbits;
for (size_t k = 0; k < number; ++k)
buffer.push_back(~v[k]);
return number;
}
// we proceed the long way
size_t howmanywecanadd =
RunningLengthWord<uword>::largestliteralcount - NumberOfLiteralWords;
RunningLengthWord<uword>::setNumberOfLiteralWords(
rlw, RunningLengthWord<uword>::largestliteralcount);
buffer[lastRLW] = rlw;
for (size_t k = 0; k < howmanywecanadd; ++k)
buffer.push_back(~v[k]);
size_t wordadded = howmanywecanadd;
sizeinbits += howmanywecanadd * wordinbits;
buffer.push_back(0);
lastRLW = buffer.size() - 1;
++wordadded;
wordadded +=
addStreamOfDirtyWords(v + howmanywecanadd, number - howmanywecanadd);
return wordadded;
}
template <class uword> size_t EWAHBoolArray<uword>::addEmptyWord(const bool v) {
RunningLengthWord<uword> lastRunningLengthWord(buffer[lastRLW]);
const bool noliteralword =
(lastRunningLengthWord.getNumberOfLiteralWords() == 0);
// first, if the last running length word is empty, we align it
// this
uword runlen = lastRunningLengthWord.getRunningLength();
if ((noliteralword) && (runlen == 0)) {
lastRunningLengthWord.setRunningBit(v);
}
if ((noliteralword) && (lastRunningLengthWord.getRunningBit() == v) &&
(runlen < RunningLengthWord<uword>::largestrunninglengthcount)) {
lastRunningLengthWord.setRunningLength(static_cast<uword>(runlen + 1));
return 0;
} else {
// we have to start anew
buffer.push_back(0);
lastRLW = buffer.size() - 1;
RunningLengthWord<uword> lastRunningLengthWord2(buffer[lastRLW]);
lastRunningLengthWord2.setRunningBit(v);
lastRunningLengthWord2.setRunningLength(1);
return 1;
}
}
template <class uword>
void fast_logicalor_tocontainer(size_t n, const EWAHBoolArray<uword> **inputs,
EWAHBoolArray<uword> &container) {
class EWAHBoolArrayPtr {
public:
EWAHBoolArrayPtr(const EWAHBoolArray<uword> *p, bool o) : ptr(p), own(o) {}
const EWAHBoolArray<uword> *ptr;
bool own; // whether to clean
bool operator<(const EWAHBoolArrayPtr &o) const {
return o.ptr->sizeInBytes() < ptr->sizeInBytes(); // backward on purpose
}
};
if (n == 0) {
container.reset();
return;
}
if (n == 1) {
container = *inputs[0];
return;
}
std::priority_queue<EWAHBoolArrayPtr> pq;
for (size_t i = 0; i < n; i++) {
// could use emplace
pq.push(EWAHBoolArrayPtr(inputs[i], false));
}
while (pq.size() > 2) {
EWAHBoolArrayPtr x1 = pq.top();
pq.pop();
EWAHBoolArrayPtr x2 = pq.top();
pq.pop();
EWAHBoolArray<uword> *buffer = new EWAHBoolArray<uword>();
x1.ptr->logicalor(*x2.ptr, *buffer);
if (x1.own) {
delete x1.ptr;
}
if (x2.own) {
delete x2.ptr;
}
pq.push(EWAHBoolArrayPtr(buffer, true));
}
EWAHBoolArrayPtr x1 = pq.top();
pq.pop();
EWAHBoolArrayPtr x2 = pq.top();
pq.pop();
x1.ptr->logicalor(*x2.ptr, container);
if (x1.own) {
delete x1.ptr;
}
if (x2.own) {
delete x2.ptr;
}
}
template <class uword>
void EWAHBoolArray<uword>::logicalor(const EWAHBoolArray &a,
EWAHBoolArray &container) const {
container.reset();
if (RESERVEMEMORY)
container.buffer.reserve(buffer.size() + a.buffer.size());
EWAHBoolArrayRawIterator<uword> i = a.raw_iterator();
EWAHBoolArrayRawIterator<uword> j = raw_iterator();
if (!(i.hasNext() and j.hasNext())) { // hopefully this never happens...
container.setSizeInBits(sizeInBits());
return;
}
// at this point, this should be safe:
BufferedRunningLengthWord<uword> &rlwi = i.next();
BufferedRunningLengthWord<uword> &rlwj = j.next();
while ((rlwi.size() > 0) && (rlwj.size() > 0)) {
while ((rlwi.getRunningLength() > 0) || (rlwj.getRunningLength() > 0)) {
const bool i_is_prey = rlwi.getRunningLength() < rlwj.getRunningLength();
BufferedRunningLengthWord<uword> &prey = i_is_prey ? rlwi : rlwj;
BufferedRunningLengthWord<uword> &predator = i_is_prey ? rlwj : rlwi;
if (predator.getRunningBit()) {
container.fastaddStreamOfEmptyWords(true, predator.getRunningLength());
prey.discardFirstWordsWithReload(predator.getRunningLength());
} else {
const size_t index =
prey.discharge(container, predator.getRunningLength());
container.fastaddStreamOfEmptyWords(false, predator.getRunningLength() -
index);
}
predator.discardRunningWordsWithReload();
}
const uword nbre_literal = std::min(rlwi.getNumberOfLiteralWords(),
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (size_t k = 0; k < nbre_literal; ++k) {
container.addWord(rlwi.getLiteralWordAt(k) | rlwj.getLiteralWordAt(k));
}
rlwi.discardLiteralWordsWithReload(nbre_literal);
rlwj.discardLiteralWordsWithReload(nbre_literal);
}
}
const bool i_remains = rlwi.size() > 0;
BufferedRunningLengthWord<uword> &remaining = i_remains ? rlwi : rlwj;
remaining.discharge(container);
container.setSizeInBits(sizeInBits() > a.sizeInBits() ? sizeInBits()
: a.sizeInBits());
}
template <class uword>
size_t EWAHBoolArray<uword>::logicalorcount(const EWAHBoolArray &a) const {
size_t answer = 0;
EWAHBoolArrayRawIterator<uword> i = a.raw_iterator();
EWAHBoolArrayRawIterator<uword> j = raw_iterator();
if (!(i.hasNext() and j.hasNext())) { // hopefully this never happens...
return 0;
}
// at this point, this should be safe:
BufferedRunningLengthWord<uword> &rlwi = i.next();
BufferedRunningLengthWord<uword> &rlwj = j.next();
while ((rlwi.size() > 0) && (rlwj.size() > 0)) {
while ((rlwi.getRunningLength() > 0) || (rlwj.getRunningLength() > 0)) {
const bool i_is_prey = rlwi.getRunningLength() < rlwj.getRunningLength();
BufferedRunningLengthWord<uword> &prey = i_is_prey ? rlwi : rlwj;
BufferedRunningLengthWord<uword> &predator = i_is_prey ? rlwj : rlwi;
if (predator.getRunningBit()) {
answer += predator.getRunningLength() * wordinbits;
prey.discardFirstWordsWithReload(predator.getRunningLength());
} else {
// const size_t index =
prey.dischargeCount(predator.getRunningLength(), &answer);
}
predator.discardRunningWordsWithReload();
}
const uword nbre_literal = std::min(rlwi.getNumberOfLiteralWords(),
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (size_t k = 0; k < nbre_literal; ++k) {
answer += countOnes(
(uword)(rlwi.getLiteralWordAt(k) | rlwj.getLiteralWordAt(k)));
}
rlwi.discardLiteralWordsWithReload(nbre_literal);
rlwj.discardLiteralWordsWithReload(nbre_literal);
}
}
const bool i_remains = rlwi.size() > 0;
BufferedRunningLengthWord<uword> &remaining = i_remains ? rlwi : rlwj;
answer += remaining.dischargeCount();
return answer;
}
template <class uword>
void EWAHBoolArray<uword>::logicalxor(const EWAHBoolArray &a,
EWAHBoolArray &container) const {
container.reset();
if (RESERVEMEMORY)
container.buffer.reserve(buffer.size() + a.buffer.size());
EWAHBoolArrayRawIterator<uword> i = a.raw_iterator();
EWAHBoolArrayRawIterator<uword> j = raw_iterator();
if (!(i.hasNext() and j.hasNext())) { // hopefully this never happens...
container.setSizeInBits(sizeInBits());
return;
}
// at this point, this should be safe:
BufferedRunningLengthWord<uword> &rlwi = i.next();
BufferedRunningLengthWord<uword> &rlwj = j.next();
while ((rlwi.size() > 0) && (rlwj.size() > 0)) {
while ((rlwi.getRunningLength() > 0) || (rlwj.getRunningLength() > 0)) {
const bool i_is_prey = rlwi.getRunningLength() < rlwj.getRunningLength();
BufferedRunningLengthWord<uword> &prey = i_is_prey ? rlwi : rlwj;
BufferedRunningLengthWord<uword> &predator = i_is_prey ? rlwj : rlwi;
const size_t index =
(!predator.getRunningBit())
? prey.discharge(container, predator.getRunningLength())
: prey.dischargeNegated(container, predator.getRunningLength());
container.fastaddStreamOfEmptyWords(predator.getRunningBit(),
predator.getRunningLength() - index);
predator.discardRunningWordsWithReload();
}
const uword nbre_literal = std::min(rlwi.getNumberOfLiteralWords(),
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (size_t k = 0; k < nbre_literal; ++k)
container.addWord(rlwi.getLiteralWordAt(k) ^ rlwj.getLiteralWordAt(k));
rlwi.discardLiteralWordsWithReload(nbre_literal);
rlwj.discardLiteralWordsWithReload(nbre_literal);
}
}
const bool i_remains = rlwi.size() > 0;
BufferedRunningLengthWord<uword> &remaining = i_remains ? rlwi : rlwj;
remaining.discharge(container);
container.setSizeInBits(sizeInBits() > a.sizeInBits() ? sizeInBits()
: a.sizeInBits());
}
template <class uword>
size_t EWAHBoolArray<uword>::logicalxorcount(const EWAHBoolArray &a) const {
EWAHBoolArrayRawIterator<uword> i = a.raw_iterator();
EWAHBoolArrayRawIterator<uword> j = raw_iterator();
if (!i.hasNext())
return a.numberOfOnes();
if (!j.hasNext())
return this->numberOfOnes();
size_t answer = 0;
// at this point, this should be safe:
BufferedRunningLengthWord<uword> &rlwi = i.next();
BufferedRunningLengthWord<uword> &rlwj = j.next();
while ((rlwi.size() > 0) && (rlwj.size() > 0)) {
while ((rlwi.getRunningLength() > 0) || (rlwj.getRunningLength() > 0)) {
const bool i_is_prey = rlwi.getRunningLength() < rlwj.getRunningLength();
BufferedRunningLengthWord<uword> &prey = i_is_prey ? rlwi : rlwj;
BufferedRunningLengthWord<uword> &predator = i_is_prey ? rlwj : rlwi;
size_t index;
if (predator.getRunningBit()) {
index =
prey.dischargeCountNegated(predator.getRunningLength(), &answer);
} else {
index = prey.dischargeCount(predator.getRunningLength(), &answer);
}
if (predator.getRunningBit())
answer += (predator.getRunningLength() - index) * wordinbits;
predator.discardRunningWordsWithReload();
}
const uword nbre_literal = std::min(rlwi.getNumberOfLiteralWords(),
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (size_t k = 0; k < nbre_literal; ++k) {
answer += countOnes(
(uword)(rlwi.getLiteralWordAt(k) ^ rlwj.getLiteralWordAt(k)));
}
rlwi.discardLiteralWordsWithReload(nbre_literal);
rlwj.discardLiteralWordsWithReload(nbre_literal);
}
}
const bool i_remains = rlwi.size() > 0;
BufferedRunningLengthWord<uword> &remaining = i_remains ? rlwi : rlwj;
answer += remaining.dischargeCount();
return answer;
}
template <class uword>
void EWAHBoolArray<uword>::logicaland(const EWAHBoolArray &a,
EWAHBoolArray &container) const {
container.reset();
if (RESERVEMEMORY)
container.buffer.reserve(buffer.size() > a.buffer.size() ? buffer.size()
: a.buffer.size());
EWAHBoolArrayRawIterator<uword> i = a.raw_iterator();
EWAHBoolArrayRawIterator<uword> j = raw_iterator();
if (!(i.hasNext() and j.hasNext())) { // hopefully this never happens...
container.setSizeInBits(sizeInBits());
return;
}
// at this point, this should be safe:
BufferedRunningLengthWord<uword> &rlwi = i.next();
BufferedRunningLengthWord<uword> &rlwj = j.next();
while ((rlwi.size() > 0) && (rlwj.size() > 0)) {
while ((rlwi.getRunningLength() > 0) || (rlwj.getRunningLength() > 0)) {
const bool i_is_prey = rlwi.getRunningLength() < rlwj.getRunningLength();
BufferedRunningLengthWord<uword> &prey(i_is_prey ? rlwi : rlwj);
BufferedRunningLengthWord<uword> &predator(i_is_prey ? rlwj : rlwi);
if (!predator.getRunningBit()) {
container.fastaddStreamOfEmptyWords(false, predator.getRunningLength());
prey.discardFirstWordsWithReload(predator.getRunningLength());
} else {
const size_t index =
prey.discharge(container, predator.getRunningLength());
container.fastaddStreamOfEmptyWords(false, predator.getRunningLength() -
index);
}
predator.discardRunningWordsWithReload();
}
const uword nbre_literal = std::min(rlwi.getNumberOfLiteralWords(),
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (size_t k = 0; k < nbre_literal; ++k) {
container.addWord(rlwi.getLiteralWordAt(k) & rlwj.getLiteralWordAt(k));
}
rlwi.discardLiteralWordsWithReload(nbre_literal);
rlwj.discardLiteralWordsWithReload(nbre_literal);
}
}
BufferedRunningLengthWord<uword> &remain = rlwj.size() > 0 ? rlwj : rlwi;
while(remain.size() > 0) {
container.addStreamOfEmptyWords(false, remain.size());
if (!remain.next()) { break; }
}
container.setSizeInBits(sizeInBits() > a.sizeInBits() ? sizeInBits()
: a.sizeInBits());
container.assertWordCount("logicaland");
}
template <class uword>
void EWAHBoolArray<uword>::logicalandnot(const EWAHBoolArray &a,
EWAHBoolArray &container) const {
container.reset();
if (RESERVEMEMORY)
container.buffer.reserve(buffer.size() > a.buffer.size() ? buffer.size()
: a.buffer.size());
EWAHBoolArrayRawIterator<uword> i = raw_iterator();
EWAHBoolArrayRawIterator<uword> j = a.raw_iterator();
if (!j.hasNext()) { // the other fellow is empty
container = *this; // just copy, stupidly, the data
return;
}
if (!(i.hasNext())) { // hopefully this never happens...
container.setSizeInBits(sizeInBits());
return;
}
// at this point, this should be safe:
BufferedRunningLengthWord<uword> &rlwi = i.next();
BufferedRunningLengthWord<uword> &rlwj = j.next();
while ((rlwi.size() > 0) && (rlwj.size() > 0)) {
while ((rlwi.getRunningLength() > 0) || (rlwj.getRunningLength() > 0)) {
const bool i_is_prey = rlwi.getRunningLength() < rlwj.getRunningLength();
BufferedRunningLengthWord<uword> &prey(i_is_prey ? rlwi : rlwj);
BufferedRunningLengthWord<uword> &predator(i_is_prey ? rlwj : rlwi);
if (((predator.getRunningBit()) && (i_is_prey)) ||
((!predator.getRunningBit()) && (!i_is_prey))) {
container.fastaddStreamOfEmptyWords(false, predator.getRunningLength());
prey.discardFirstWordsWithReload(predator.getRunningLength());
} else if (i_is_prey) {
const size_t index =
prey.discharge(container, predator.getRunningLength());
container.fastaddStreamOfEmptyWords(false, predator.getRunningLength() -
index);
} else {
const size_t index =
prey.dischargeNegated(container, predator.getRunningLength());
container.fastaddStreamOfEmptyWords(true, predator.getRunningLength() -
index);
}
predator.discardRunningWordsWithReload();
}
const uword nbre_literal = std::min(rlwi.getNumberOfLiteralWords(),
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (size_t k = 0; k < nbre_literal; ++k) {
container.addWord(static_cast<uword>(rlwi.getLiteralWordAt(k) & ~rlwj.getLiteralWordAt(k)));
}
rlwi.discardLiteralWordsWithReload(nbre_literal);
rlwj.discardLiteralWordsWithReload(nbre_literal);
}
}
if(rlwi.size() > 0) {
rlwi.discharge(container);
container.setSizeInBits(sizeInBits());
} else {
while(rlwj.size() > 0) {
container.addStreamOfEmptyWords(false, rlwj.size());
if (!rlwj.next()) { break; }
}
container.setSizeInBits(a.sizeInBits());
}
container.assertWordCount("logicalandnot");
}
template <class uword>
size_t EWAHBoolArray<uword>::logicalandnotcount(const EWAHBoolArray &a) const {
EWAHBoolArrayRawIterator<uword> i = raw_iterator();
EWAHBoolArrayRawIterator<uword> j = a.raw_iterator();
if (!j.hasNext()) { // the other fellow is empty
return this->numberOfOnes();
}
if (!(i.hasNext())) { // hopefully this never happens...
return 0;
}
size_t answer = 0;
// at this point, this should be safe:
BufferedRunningLengthWord<uword> &rlwi = i.next();
BufferedRunningLengthWord<uword> &rlwj = j.next();
while ((rlwi.size() > 0) && (rlwj.size() > 0)) {
while ((rlwi.getRunningLength() > 0) || (rlwj.getRunningLength() > 0)) {
const bool i_is_prey = rlwi.getRunningLength() < rlwj.getRunningLength();
BufferedRunningLengthWord<uword> &prey(i_is_prey ? rlwi : rlwj);
BufferedRunningLengthWord<uword> &predator(i_is_prey ? rlwj : rlwi);
if (((predator.getRunningBit()) && (i_is_prey)) ||
((!predator.getRunningBit()) && (!i_is_prey))) {
prey.discardFirstWordsWithReload(predator.getRunningLength());
} else if (i_is_prey) {
prey.dischargeCount(predator.getRunningLength(), &answer);
} else {
const size_t index =
prey.dischargeCountNegated(predator.getRunningLength(), &answer);
answer += (predator.getRunningLength() - index) * wordinbits;
}
predator.discardRunningWordsWithReload();
}
const uword nbre_literal = std::min(rlwi.getNumberOfLiteralWords(),
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (size_t k = 0; k < nbre_literal; ++k) {
answer += countOnes(
(uword)(rlwi.getLiteralWordAt(k) & (~rlwj.getLiteralWordAt(k))));
}
rlwi.discardLiteralWordsWithReload(nbre_literal);
rlwj.discardLiteralWordsWithReload(nbre_literal);
}
}
const bool i_remains = rlwi.size() > 0;
if (i_remains) {
answer += rlwi.dischargeCount();
}
return answer;
}
template <class uword>
size_t EWAHBoolArray<uword>::logicalandcount(const EWAHBoolArray &a) const {
EWAHBoolArrayRawIterator<uword> i = a.raw_iterator();
EWAHBoolArrayRawIterator<uword> j = raw_iterator();
if (!(i.hasNext() and j.hasNext())) { // hopefully this never happens...
return 0;
}
size_t answer = 0;
// at this point, this should be safe:
BufferedRunningLengthWord<uword> &rlwi = i.next();
BufferedRunningLengthWord<uword> &rlwj = j.next();
while ((rlwi.size() > 0) && (rlwj.size() > 0)) {
while ((rlwi.getRunningLength() > 0) || (rlwj.getRunningLength() > 0)) {
const bool i_is_prey = rlwi.getRunningLength() < rlwj.getRunningLength();
BufferedRunningLengthWord<uword> &prey(i_is_prey ? rlwi : rlwj);
BufferedRunningLengthWord<uword> &predator(i_is_prey ? rlwj : rlwi);
if (!predator.getRunningBit()) {
prey.discardFirstWordsWithReload(predator.getRunningLength());
} else {
// const size_t index =
prey.dischargeCount(predator.getRunningLength(), &answer);
}
predator.discardRunningWordsWithReload();
}
const uword nbre_literal = std::min(rlwi.getNumberOfLiteralWords(),
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (size_t k = 0; k < nbre_literal; ++k) {
answer += countOnes(
(uword)(rlwi.getLiteralWordAt(k) & rlwj.getLiteralWordAt(k)));
}
rlwi.discardLiteralWordsWithReload(nbre_literal);
rlwj.discardLiteralWordsWithReload(nbre_literal);
}
}
return answer;
}
template <class uword>
bool EWAHBoolArray<uword>::intersects(const EWAHBoolArray &a) const {
EWAHBoolArrayRawIterator<uword> i = a.raw_iterator();
EWAHBoolArrayRawIterator<uword> j = raw_iterator();
if (!(i.hasNext() and j.hasNext())) { // hopefully this never happens...
return false;
}
// at this point, this should be safe:
BufferedRunningLengthWord<uword> &rlwi = i.next();
BufferedRunningLengthWord<uword> &rlwj = j.next();
while ((rlwi.size() > 0) && (rlwj.size() > 0)) {
while ((rlwi.getRunningLength() > 0) || (rlwj.getRunningLength() > 0)) {
const bool i_is_prey = rlwi.getRunningLength() < rlwj.getRunningLength();
BufferedRunningLengthWord<uword> &prey(i_is_prey ? rlwi : rlwj);
BufferedRunningLengthWord<uword> &predator(i_is_prey ? rlwj : rlwi);
if (!predator.getRunningBit()) {
prey.discardFirstWordsWithReload(predator.getRunningLength());
} else {
size_t index = 0;
bool isnonzero =
prey.nonzero_discharge(predator.getRunningLength(), index);
if (isnonzero)
return true;
}
predator.discardRunningWordsWithReload();
}
const uword nbre_literal = std::min(rlwi.getNumberOfLiteralWords(),
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (size_t k = 0; k < nbre_literal; ++k) {
if ((rlwi.getLiteralWordAt(k) & rlwj.getLiteralWordAt(k)) != 0)
return true;
}
rlwi.discardLiteralWordsWithReload(nbre_literal);
rlwj.discardLiteralWordsWithReload(nbre_literal);
}
}
return false;
}
template <class uword>
BitmapStatistics EWAHBoolArray<uword>::computeStatistics() const {
BitmapStatistics bs;
EWAHBoolArrayRawIterator<uword> i = raw_iterator();
while (i.hasNext()) {
BufferedRunningLengthWord<uword> &brlw(i.next());
++bs.runningwordmarker;
bs.totalliteral += brlw.getNumberOfLiteralWords();
bs.totalcompressed += brlw.getRunningLength();
if (brlw.getRunningLength() ==
RunningLengthWord<uword>::largestrunninglengthcount) {
++bs.maximumofrunningcounterreached;
}
}
return bs;
}
template <class uword> void EWAHBoolArray<uword>::debugprintout() const {
std::cout << "==printing out EWAHBoolArray==" << std::endl;
std::cout << "Number of compressed words: " << buffer.size() << std::endl;
std::cout << "Size in bits: " << sizeinbits << std::endl;
size_t pointer = 0;
while (pointer < buffer.size()) {
ConstRunningLengthWord<uword> rlw(buffer[pointer]);
bool b = rlw.getRunningBit();
const uword rl = rlw.getRunningLength();
const uword lw = rlw.getNumberOfLiteralWords();
std::cout << "pointer = " << pointer << " running bit=" << b
<< " running length=" << rl << " lit. words=" << lw << std::endl;
for (uword j = 0; j < lw; ++j) {
const uword &w = buffer[pointer + j + 1];
std::cout << toBinaryString(w) << std::endl;
}
pointer += lw + 1;
}
std::cout << "==END==" << std::endl;
}
template <class uword>
size_t EWAHBoolArray<uword>::sizeOnDisk(const bool savesizeinbits) const {
return (savesizeinbits ? sizeof(uint64_t) : 0) + sizeof(uint64_t) +
sizeof(uword) * buffer.size();
}
} // namespace ewah
#endif
|