File size: 46,982 Bytes
2e3030d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 | /*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2022 NKI/AVL, Netherlands Cancer Institute
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cif++/category.hpp"
#include "cif++/datablock.hpp"
#include "cif++/parser.hpp"
#include "cif++/utilities.hpp"
#include <numeric>
#include <stack>
// TODO: Find out what the rules are exactly for linked items, the current implementation
// is inconsistent. It all depends whether a link is satified if a item taking part in the
// set of linked items is null at one side and not null in the other.
namespace cif
{
const uint32_t kMaxLineLength = 132;
// --------------------------------------------------------------------
class row_comparator
{
public:
row_comparator(category &cat)
{
auto cv = cat.get_cat_validator();
for (auto &k : cv->m_keys)
{
uint16_t ix = cat.add_item(k);
auto iv = cv->get_validator_for_item(k);
if (iv == nullptr)
throw std::runtime_error("Incomplete dictionary, no Item Validator for Key " + k);
auto tv = iv->m_type;
if (tv == nullptr)
throw std::runtime_error("Incomplete dictionary, no type Validator for Item " + k);
using namespace std::placeholders;
m_comparator.emplace_back(ix, std::bind(&type_validator::compare, tv, _1, _2));
}
}
int operator()(const category &cat, const row *a, const row *b) const
{
assert(a);
assert(b);
row_handle rha(cat, *a);
row_handle rhb(cat, *b);
int d = 0;
for (const auto &[k, f] : m_comparator)
{
std::string_view ka = rha[k].text();
std::string_view kb = rhb[k].text();
d = f(ka, kb);
if (d != 0)
break;
}
return d;
}
int operator()(const category &cat, const row_initializer &a, const row *b) const
{
assert(b);
row_handle rhb(cat, *b);
int d = 0;
auto ai = a.begin();
for (const auto &[k, f] : m_comparator)
{
assert(ai != a.end());
std::string_view ka = ai->value();
std::string_view kb = rhb[k].text();
d = f(ka, kb);
if (d != 0)
break;
++ai;
}
return d;
}
private:
using compareFunc = std::function<int(std::string_view, std::string_view)>;
using key_comparator = std::tuple<uint16_t, compareFunc>;
std::vector<key_comparator> m_comparator;
};
// --------------------------------------------------------------------
//
// class to keep an index on the keys of a category. This is a red/black
// tree implementation.
class category_index
{
public:
category_index(category &cat);
~category_index()
{
delete m_root;
}
row *find(const category &cat, row *k) const;
row *find_by_value(const category &cat, row_initializer k) const;
void insert(category &cat, row *r);
void erase(category &cat, row *r);
// reorder the row's and returns new head and tail
std::tuple<row *, row *> reorder()
{
std::tuple<row *, row *> result = std::make_tuple(nullptr, nullptr);
if (m_root != nullptr)
{
entry *head = find_min(m_root);
entry *tail = reorder(m_root);
tail->m_row->m_next = nullptr;
result = std::make_tuple(head->m_row, tail->m_row);
}
return result;
}
size_t size() const;
// bool isValid() const;
private:
struct entry
{
entry(row *r)
: m_row(r)
, m_left(nullptr)
, m_right(nullptr)
, m_red(true)
{
}
~entry()
{
delete m_left;
delete m_right;
}
row *m_row;
entry *m_left;
entry *m_right;
bool m_red;
};
entry *insert(category &cat, entry *h, row *v);
entry *erase(category &cat, entry *h, row *k);
// void validate(entry* h, bool isParentRed, uint32_t blackDepth, uint32_t& minBlack, uint32_t& maxBlack) const;
entry *rotateLeft(entry *h)
{
entry *x = h->m_right;
h->m_right = x->m_left;
x->m_left = h;
x->m_red = h->m_red;
h->m_red = true;
return x;
}
entry *rotateRight(entry *h)
{
entry *x = h->m_left;
h->m_left = x->m_right;
x->m_right = h;
x->m_red = h->m_red;
h->m_red = true;
return x;
}
void flipColour(entry *h)
{
h->m_red = not h->m_red;
if (h->m_left != nullptr)
h->m_left->m_red = not h->m_left->m_red;
if (h->m_right != nullptr)
h->m_right->m_red = not h->m_right->m_red;
}
constexpr bool is_red(entry *h) const
{
return h != nullptr and h->m_red;
}
entry *move_red_left(entry *h)
{
flipColour(h);
if (h->m_right != nullptr and is_red(h->m_right->m_left))
{
h->m_right = rotateRight(h->m_right);
h = rotateLeft(h);
flipColour(h);
}
return h;
}
entry *move_red_right(entry *h)
{
flipColour(h);
if (h->m_left != nullptr and is_red(h->m_left->m_left))
{
h = rotateRight(h);
flipColour(h);
}
return h;
}
entry *fix_up(entry *h)
{
if (is_red(h->m_right))
h = rotateLeft(h);
if (is_red(h->m_left) and is_red(h->m_left->m_left))
h = rotateRight(h);
if (is_red(h->m_left) and is_red(h->m_right))
flipColour(h);
return h;
}
entry *find_min(entry *h)
{
while (h->m_left != nullptr)
h = h->m_left;
return h;
}
entry *erase_min(entry *h)
{
if (h->m_left == nullptr)
{
delete h;
h = nullptr;
}
else
{
if (not is_red(h->m_left) and not is_red(h->m_left->m_left))
h = move_red_left(h);
h->m_left = erase_min(h->m_left);
h = fix_up(h);
}
return h;
}
// Fix m_next items for rows in order of this index
entry *reorder(entry *e)
{
auto result = e;
if (e->m_left != nullptr)
{
auto l = reorder(e->m_left);
l->m_row->m_next = e->m_row;
}
if (e->m_right != nullptr)
{
auto mr = find_min(e->m_right);
e->m_row->m_next = mr->m_row;
result = reorder(e->m_right);
}
return result;
}
row_comparator m_row_comparator;
entry *m_root;
};
category_index::category_index(category &cat)
: m_row_comparator(cat)
, m_root(nullptr)
{
for (auto r : cat)
insert(cat, r.get_row());
}
row *category_index::find(const category &cat, row *k) const
{
const entry *r = m_root;
while (r != nullptr)
{
int d = m_row_comparator(cat, k, r->m_row);
if (d < 0)
r = r->m_left;
else if (d > 0)
r = r->m_right;
else
break;
}
return r ? r->m_row : nullptr;
}
row *category_index::find_by_value(const category &cat, row_initializer k) const
{
// sort the values in k first
row_initializer k2;
for (auto &f : cat.key_item_indices())
{
auto fld = cat.get_item_name(f);
auto ki = find_if(k.begin(), k.end(), [&fld](auto &i)
{ return i.name() == fld; });
if (ki == k.end())
k2.emplace_back(fld, "");
else
k2.emplace_back(*ki);
}
const entry *r = m_root;
while (r != nullptr)
{
int d = m_row_comparator(cat, k2, r->m_row);
if (d < 0)
r = r->m_left;
else if (d > 0)
r = r->m_right;
else
break;
}
return r ? r->m_row : nullptr;
}
void category_index::insert(category &cat, row *k)
{
m_root = insert(cat, m_root, k);
m_root->m_red = false;
}
category_index::entry *category_index::insert(category &cat, entry *h, row *v)
{
if (h == nullptr)
return new entry(v);
int d = m_row_comparator(cat, v, h->m_row);
if (d < 0)
h->m_left = insert(cat, h->m_left, v);
else if (d > 0)
h->m_right = insert(cat, h->m_right, v);
else
{
row_handle rh(cat, *v);
std::ostringstream os;
for (auto col : cat.key_items())
{
if (rh[col])
os << col << ": " << std::quoted(rh[col].text()) << "; ";
}
throw duplicate_key_error("Duplicate Key violation, cat: " + cat.name() + " values: " + os.str());
}
if (is_red(h->m_right) and not is_red(h->m_left))
h = rotateLeft(h);
if (is_red(h->m_left) and is_red(h->m_left->m_left))
h = rotateRight(h);
if (is_red(h->m_left) and is_red(h->m_right))
flipColour(h);
return h;
}
void category_index::erase(category &cat, row *k)
{
assert(find(cat, k) == k);
m_root = erase(cat, m_root, k);
if (m_root != nullptr)
m_root->m_red = false;
}
category_index::entry *category_index::erase(category &cat, entry *h, row *k)
{
if (m_row_comparator(cat, k, h->m_row) < 0)
{
if (h->m_left != nullptr)
{
if (not is_red(h->m_left) and not is_red(h->m_left->m_left))
h = move_red_left(h);
h->m_left = erase(cat, h->m_left, k);
}
}
else
{
if (is_red(h->m_left))
h = rotateRight(h);
if (m_row_comparator(cat, k, h->m_row) == 0 and h->m_right == nullptr)
{
delete h;
return nullptr;
}
if (h->m_right != nullptr)
{
if (not is_red(h->m_right) and not is_red(h->m_right->m_left))
h = move_red_right(h);
if (m_row_comparator(cat, k, h->m_row) == 0)
{
h->m_row = find_min(h->m_right)->m_row;
h->m_right = erase_min(h->m_right);
}
else
h->m_right = erase(cat, h->m_right, k);
}
}
return fix_up(h);
}
size_t category_index::size() const
{
std::stack<entry *> s;
s.push(m_root);
size_t result = 0;
while (not s.empty())
{
entry *e = s.top();
s.pop();
if (e == nullptr)
continue;
++result;
s.push(e->m_left);
s.push(e->m_right);
}
return result;
}
// --------------------------------------------------------------------
category::category(std::string_view name)
: m_name(name)
{
}
category::category(const category &rhs)
: m_name(rhs.m_name)
, m_items(rhs.m_items)
, m_cascade(rhs.m_cascade)
{
for (auto r = rhs.m_head; r != nullptr; r = r->m_next)
insert_impl(end(), clone_row(*r));
m_validator = rhs.m_validator;
m_cat_validator = rhs.m_cat_validator;
if (m_cat_validator != nullptr and m_index == nullptr)
m_index = new category_index(*this);
}
void swap(category &a, category &b) noexcept
{
std::swap(a.m_name, b.m_name);
std::swap(a.m_items, b.m_items);
std::swap(a.m_validator, b.m_validator);
std::swap(a.m_cat_validator, b.m_cat_validator);
std::swap(a.m_parent_links, b.m_parent_links);
std::swap(a.m_child_links, b.m_child_links);
std::swap(a.m_cascade, b.m_cascade);
std::swap(a.m_index, b.m_index);
std::swap(a.m_head, b.m_head);
std::swap(a.m_tail, b.m_tail);
}
category::~category()
{
clear();
}
// --------------------------------------------------------------------
void category::remove_item(std::string_view item_name)
{
for (size_t ix = 0; ix < m_items.size(); ++ix)
{
if (not iequals(item_name, m_items[ix].m_name))
continue;
for (row *r = m_head; r != nullptr; r = r->m_next)
{
if (r->size() > ix)
r->erase(r->begin() + ix);
}
m_items.erase(m_items.begin() + ix);
break;
}
}
void category::rename_item(std::string_view from_name, std::string_view to_name)
{
for (size_t ix = 0; ix < m_items.size(); ++ix)
{
if (not iequals(from_name, m_items[ix].m_name))
continue;
m_items[ix].m_name = to_name;
m_items[ix].m_validator = m_cat_validator ? m_cat_validator->get_validator_for_item(to_name) : nullptr;
break;
}
}
iset category::get_items() const
{
iset result;
for (auto &col : m_items)
result.insert(col.m_name);
return result;
}
iset category::key_items() const
{
if (m_validator == nullptr)
throw std::runtime_error("No Validator specified");
if (m_cat_validator == nullptr)
throw validation_exception(validation_error::undefined_category);
iset result;
for (auto &iv : m_cat_validator->m_item_validators)
result.insert(iv.m_item_name);
return result;
}
std::set<uint16_t> category::key_item_indices() const
{
if (m_validator == nullptr)
throw std::runtime_error("No Validator specified");
if (m_cat_validator == nullptr)
throw validation_exception(validation_error::undefined_category);
std::set<uint16_t> result;
for (auto &k : m_cat_validator->m_keys)
result.insert(get_item_ix(k));
return result;
}
// --------------------------------------------------------------------
void category::set_validator(const validator *v, datablock &db)
{
m_validator = v;
if (m_index != nullptr)
{
delete m_index;
m_index = nullptr;
}
if (m_validator != nullptr)
{
m_cat_validator = m_validator->get_validator_for_category(m_name);
if (m_cat_validator != nullptr)
{
std::set<std::string> missing;
if (not empty())
{
std::vector<uint16_t> kix;
for (auto k : m_cat_validator->m_keys)
{
kix.push_back(get_item_ix(k));
if (kix.back() >= m_items.size())
missing.insert(k);
}
}
if (missing.empty())
m_index = new category_index(*this);
else
{
std::ostringstream msg;
msg << "Cannot construct index since the key item" << (missing.size() > 1 ? "s" : "") << " "
<< cif::join(missing, ", ") << " in " << m_name << " " << (missing.size() == 1 ? "is" : "are") << " missing\n";
throw missing_key_error(msg.str(), *missing.begin());
}
}
}
else
m_cat_validator = nullptr;
for (auto &&[item, cv] : m_items)
cv = m_cat_validator ? m_cat_validator->get_validator_for_item(item) : nullptr;
update_links(db);
}
void category::update_links(const datablock &db)
{
m_child_links.clear();
m_parent_links.clear();
if (m_validator != nullptr)
{
for (auto link : m_validator->get_links_for_parent(m_name))
{
auto childCat = const_cast<category *>(db.get(link->m_child_category));
if (childCat == nullptr)
continue;
m_child_links.emplace_back(childCat, link);
}
for (auto link : m_validator->get_links_for_child(m_name))
{
auto parentCat = const_cast<category *>(db.get(link->m_parent_category));
if (parentCat == nullptr)
continue;
m_parent_links.emplace_back(parentCat, link);
}
}
}
bool category::is_valid() const
{
bool result = true;
if (m_validator == nullptr)
throw std::runtime_error("no Validator specified");
if (empty())
{
if (VERBOSE > 2)
std::cerr << "Skipping validation of empty category " << m_name << '\n';
return true;
}
if (m_cat_validator == nullptr)
{
m_validator->report_error(validation_error::undefined_category, m_name, {}, false);
return false;
}
auto mandatory = m_cat_validator->m_mandatory_items;
for (auto &col : m_items)
{
auto iv = m_cat_validator->get_validator_for_item(col.m_name);
if (iv == nullptr)
{
m_validator->report_error(validation_error::unknown_item, col.m_name, m_name, false);
result = false;
}
// col.m_validator = iv;
if (col.m_validator != iv)
m_validator->report_error(validation_error::incorrect_item_validator, true);
mandatory.erase(col.m_name);
}
if (not mandatory.empty())
{
m_validator->report_error(validation_error::missing_mandatory_items, m_name, join(mandatory, ", "), false);
result = false;
}
if (m_cat_validator->m_keys.empty() == false and m_index == nullptr)
{
std::set<std::string> missing;
for (auto k : m_cat_validator->m_keys)
{
if (get_item_ix(k) >= m_items.size())
missing.insert(k);
}
m_validator->report_error(validation_error::missing_key_items, m_name, join(missing, ", "), false);
result = false;
}
// #if not defined(NDEBUG)
// // check index?
// if (m_index)
// {
// if (m_index->size() != size())
// m_validator->report_error("size of index is not equal to size of category " + m_name, true);
// // m_index->validate();
// for (auto r : *this)
// {
// auto p = r.get_row();
// if (m_index->find(*this, p) != p)
// m_validator->report_error("Key not found in index for category " + m_name, true);
// }
// }
// #endif
// validate all values
mandatory = m_cat_validator->m_mandatory_items;
for (auto ri = m_head; ri != nullptr; ri = ri->m_next)
{
for (uint16_t cix = 0; cix < m_items.size(); ++cix)
{
bool seen = false;
auto iv = m_items[cix].m_validator;
if (iv == nullptr)
{
// no need to report, should have been reported already above
result = false;
continue;
}
auto vi = ri->get(cix);
if (vi != nullptr)
{
seen = true;
std::error_code ec;
iv->validate_value(vi->text(), ec);
if ((bool)ec)
{
m_validator->report_error(ec, m_name, m_items[cix].m_name, false);
continue;
}
}
if (seen or ri != m_head)
continue;
if (iv != nullptr and iv->m_mandatory)
{
m_validator->report_error(validation_error::missing_mandatory_items, m_name, m_items[cix].m_name, false);
result = false;
}
}
}
return result;
}
bool category::validate_links() const
{
if (not m_validator)
return false;
bool result = true;
for (auto &link : m_parent_links)
{
auto parent = link.linked;
if (parent == nullptr)
continue;
// this particular case should be skipped, that's because it is wrong:
// there are atoms that are not part of a polymer, and thus will have no
// parent in that category.
if (name() == "atom_site" and (parent->name() == "pdbx_poly_seq_scheme" or parent->name() == "entity_poly_seq"))
continue;
size_t missing = 0;
category first_missing_rows(name());
for (auto r : *this)
{
auto cond = get_parents_condition(r, *parent);
if (not cond)
continue;
if (not parent->contains(std::move(cond)))
{
++missing;
if (VERBOSE and first_missing_rows.size() < 5)
first_missing_rows.emplace(r);
}
}
if (missing)
{
result = false;
std::cerr << "Links for " << link.v->m_link_group_label << " are incomplete\n"
<< " There are " << missing << " items in " << m_name << " that don't have matching parent items in " << parent->m_name << '\n';
if (VERBOSE)
{
std::cerr << "showing first " << first_missing_rows.size() << " rows\n"
<< '\n';
first_missing_rows.write(std::cerr, link.v->m_child_keys, false);
std::cerr << '\n';
}
}
}
return result;
}
// --------------------------------------------------------------------
row_handle category::operator[](const key_type &key)
{
row_handle result{};
if (not empty())
{
if (m_index == nullptr)
throw std::logic_error("Category " + m_name + " does not have an index");
auto row = m_index->find_by_value(*this, key);
if (row != nullptr)
result = { *this, *row };
}
return result;
}
// --------------------------------------------------------------------
condition category::get_parents_condition(row_handle rh, const category &parentCat) const
{
if (m_validator == nullptr or m_cat_validator == nullptr)
throw std::runtime_error("No validator known for category " + m_name);
condition result;
auto links = m_validator->get_links_for_child(m_name);
links.erase(remove_if(links.begin(), links.end(), [n = parentCat.m_name](auto &l)
{ return l->m_parent_category != n; }),
links.end());
if (not links.empty())
{
for (auto &link : links)
{
condition cond;
for (size_t ix = 0; ix < link->m_child_keys.size(); ++ix)
{
auto childValue = rh[link->m_child_keys[ix]];
if (childValue.empty())
continue;
cond = std::move(cond) and key(link->m_parent_keys[ix]) == childValue.text();
}
result = std::move(result) or std::move(cond);
}
}
else if (cif::VERBOSE > 0)
std::cerr << "warning: no child to parent links were found for child " << parentCat.name() << " and parent " << name() << '\n';
return result;
}
condition category::get_children_condition(row_handle rh, const category &childCat) const
{
if (m_validator == nullptr or m_cat_validator == nullptr)
throw std::runtime_error("No validator known for category " + m_name);
condition result;
iset mandatoryChildItems;
auto childCatValidator = m_validator->get_validator_for_category(childCat.name());
if (childCatValidator != nullptr)
mandatoryChildItems = childCatValidator->m_mandatory_items;
auto links = m_validator->get_links_for_parent(m_name);
links.erase(remove_if(links.begin(), links.end(), [n = childCat.m_name](auto &l)
{ return l->m_child_category != n; }),
links.end());
if (not links.empty())
{
for (auto &link : links)
{
condition cond;
for (size_t ix = 0; ix < link->m_parent_keys.size(); ++ix)
{
auto childKey = link->m_child_keys[ix];
auto parentKey = link->m_parent_keys[ix];
auto parentValue = rh[parentKey];
if (parentValue.empty())
cond = std::move(cond) and key(childKey) == null;
else if (link->m_parent_keys.size() > 1 and not mandatoryChildItems.contains(childKey))
cond = std::move(cond) and (key(childKey) == parentValue.text() or key(childKey) == null);
else
cond = std::move(cond) and key(childKey) == parentValue.text();
}
result = std::move(result) or std::move(cond);
}
}
else if (cif::VERBOSE > 0)
std::cerr << "warning: no parent to child links were found for parent " << name() << " and child " << childCat.name() << '\n';
return result;
}
bool category::has_children(row_handle r) const
{
bool result = false;
for (auto &&[childCat, link] : m_child_links)
{
if (not childCat->contains(get_children_condition(r, *childCat)))
continue;
result = true;
break;
}
return result;
}
bool category::has_parents(row_handle r) const
{
bool result = false;
for (auto &&[parentCat, link] : m_parent_links)
{
if (not parentCat->contains(get_parents_condition(r, *parentCat)))
continue;
result = true;
break;
}
return result;
}
std::vector<row_handle> category::get_children(row_handle r, const category &childCat) const
{
if (m_validator == nullptr or m_cat_validator == nullptr)
throw std::runtime_error("No validator known for category " + m_name);
std::vector<row_handle> result;
for (auto child : childCat.find(get_children_condition(r, childCat)))
{
if (std::find(result.begin(), result.end(), child) == result.end())
result.push_back(child);
}
return result;
}
std::vector<row_handle> category::get_parents(row_handle r, const category &parentCat) const
{
assert(m_validator != nullptr);
assert(m_cat_validator != nullptr);
std::vector<row_handle> result;
for (auto parent : parentCat.find(get_parents_condition(r, parentCat)))
{
if (std::find(result.begin(), result.end(), parent) == result.end())
result.push_back(parent);
}
return result;
}
std::vector<row_handle> category::get_linked(row_handle r, const category &cat) const
{
std::vector<row_handle> result = get_children(r, cat);
if (result.empty())
result = get_parents(r, cat);
return result;
}
// --------------------------------------------------------------------
category::iterator category::erase(iterator pos)
{
row_handle rh = *pos;
row *r = rh.get_row();
iterator result = ++pos;
if (m_head == nullptr)
throw std::runtime_error("erase");
if (m_index != nullptr)
m_index->erase(*this, r);
if (r == m_head)
{
m_head = m_head->m_next;
r->m_next = nullptr;
}
else
{
for (auto pi = m_head; pi != nullptr; pi = pi->m_next)
{
if (pi->m_next == r)
{
pi->m_next = r->m_next;
r->m_next = nullptr;
break;
}
}
}
// links are created based on the _pdbx_item_linked_group_list entries
// in mmcif_pdbx.dic dictionary.
//
// For each link group in _pdbx_item_linked_group_list
// a set of keys from one category is mapped to another.
// If all values in a child are the same as the specified parent ones
// the child is removed as well, recursively of course.
if (m_validator != nullptr)
{
for (auto &&[childCat, link] : m_child_links)
childCat->erase_orphans(get_children_condition(rh, *childCat), *this);
}
delete_row(r);
// reset mTail, if needed
if (r == m_tail)
{
m_tail = m_head;
if (m_tail != nullptr)
while (m_tail->m_next != nullptr)
m_tail = m_tail->m_next;
}
return result;
}
template <typename T>
class save_value
{
public:
save_value(T &v, const T nv = {})
: m_v(v)
, m_sv(std::exchange(m_v, nv))
{
}
~save_value()
{
m_v = m_sv;
}
private:
T &m_v;
const T m_sv;
};
size_t category::erase(condition &&cond)
{
return erase(std::move(cond), {});
}
size_t category::erase(condition &&cond, std::function<void(row_handle)> &&visit)
{
size_t result = 0;
cond.prepare(*this);
std::map<category *, condition> potential_orphans;
auto ri = begin();
while (ri != end())
{
if (cond(*ri))
{
if (visit)
visit(*ri);
for (auto &&[childCat, link] : m_child_links)
{
auto ccond = get_children_condition(*ri, *childCat);
if (not ccond)
continue;
potential_orphans[childCat] = std::move(potential_orphans[childCat]) or std::move(ccond);
}
save_value sv(m_validator);
ri = erase(ri);
++result;
}
else
++ri;
}
for (auto &&[childCat, condition] : potential_orphans)
childCat->erase_orphans(std::move(condition), *this);
return result;
}
void category::clear()
{
auto i = m_head;
while (i != nullptr)
{
auto t = i;
i = i->m_next;
delete_row(t);
}
m_head = m_tail = nullptr;
delete m_index;
m_index = nullptr;
}
void category::erase_orphans(condition &&cond, category &parent)
{
std::vector<row *> remove;
cond.prepare(*this);
for (auto r : *this)
{
if (not cond(r))
continue;
if (parent.contains(get_parents_condition(r, parent)))
continue;
if (VERBOSE > 1)
{
category c(m_name);
c.emplace(r);
std::cerr << "Removing orphaned record: \n"
<< c << '\n'
<< '\n';
}
remove.emplace_back(r.m_row);
}
for (auto r : remove)
erase(iterator(*this, r));
}
std::string category::get_unique_id(std::function<std::string(int)> generator)
{
using namespace cif::literals;
// calling size() often is a waste of resources
if (m_last_unique_num == 0)
m_last_unique_num = static_cast<uint32_t>(size());
std::string result = generator(static_cast<int>(m_last_unique_num++));
std::string id_name = "id";
if (m_cat_validator != nullptr and m_cat_validator->m_keys.size() == 1)
{
id_name = m_cat_validator->m_keys.front();
if (m_index == nullptr and m_cat_validator != nullptr)
m_index = new category_index(*this);
for (;;)
{
if (m_index->find_by_value(*this, { { id_name, result } }) == nullptr)
break;
result = generator(static_cast<int>(m_last_unique_num++));
}
}
else
{
for (;;)
{
if (not contains(key(id_name) == result))
break;
result = generator(static_cast<int>(m_last_unique_num++));
}
}
return result;
}
std::string category::get_unique_value(std::string_view item_name)
{
std::string result;
if (m_validator and m_cat_validator)
{
auto iv = m_cat_validator->get_validator_for_item(item_name);
if (iv and iv->m_type and iv->m_type->m_primitive_type == DDL_PrimitiveType::Numb)
{
uint64_t v = find_max<uint64_t>(item_name);
result = std::to_string(v + 1);
}
}
if (result.empty())
{
// brain-dead implementation
for (size_t ix = 0; ix < size(); ++ix)
{
// result = m_name + "-" + std::to_string(ix);
result = cif_id_for_number(ix);
if (not contains(key(item_name) == result))
break;
}
}
return result;
}
void category::update_value(const std::vector<row_handle> &rows, std::string_view item_name,
value_provider_type &&value_provider)
{
using namespace std::literals;
if (rows.empty())
return;
auto colIx = get_item_ix(item_name);
if (colIx >= m_items.size())
throw validation_exception(validation_error::unknown_item, m_name, item_name);
auto &col = m_items[colIx];
// this is expensive, but better throw early on
// check the value
if (col.m_validator)
{
for (auto row : rows)
{
std::string value{ value_provider(row[item_name].text()) };
std::error_code ec;
col.m_validator->validate_value(value, ec);
if (ec)
throw validation_exception(ec, m_name, item_name);
}
}
// update and see if we need to update any child categories that depend on this value
for (auto parent : rows)
{
std::string oldValue{ parent[item_name].text() };
std::string value{ value_provider(oldValue) };
parent.assign(colIx, value, false);
for (auto &&[childCat, linked] : m_child_links)
{
if (std::find(linked->m_parent_keys.begin(), linked->m_parent_keys.end(), item_name) == linked->m_parent_keys.end())
continue;
condition cond;
std::string childItemName;
for (size_t ix = 0; ix < linked->m_parent_keys.size(); ++ix)
{
std::string pk = linked->m_parent_keys[ix];
std::string ck = linked->m_child_keys[ix];
if (pk == item_name)
{
childItemName = ck;
cond = std::move(cond) && key(ck) == oldValue;
}
else
cond = std::move(cond) && key(ck) == parent[pk].text();
}
auto children = childCat->find(std::move(cond));
if (children.empty())
continue;
std::vector<row_handle> child_rows;
std::copy(children.begin(), children.end(), std::back_inserter(child_rows));
// now be careful. If we search back from child to parent and still find a valid parent row
// we cannot simply rename the child but will have to create a new child. Unless that new
// child already exists of course.
std::vector<row_handle> process;
for (auto child : child_rows)
{
condition cond_c;
for (size_t ix = 0; ix < linked->m_parent_keys.size(); ++ix)
{
std::string pk = linked->m_parent_keys[ix];
std::string ck = linked->m_child_keys[ix];
cond_c = std::move(cond_c) && key(pk) == child[ck].text();
}
auto parents = find(std::move(cond_c));
if (parents.empty())
{
process.push_back(child);
continue;
}
// oops, we need to split this child, unless a row already exists for the new value
condition check;
for (size_t ix = 0; ix < linked->m_parent_keys.size(); ++ix)
{
std::string pk = linked->m_parent_keys[ix];
std::string ck = linked->m_child_keys[ix];
if (pk == item_name)
check = std::move(check) && key(ck) == value;
else
check = std::move(check) && key(ck) == parent[pk].text();
}
if (childCat->contains(std::move(check))) // phew..., narrow escape
continue;
// create the actual copy, if we can...
if (childCat->m_cat_validator != nullptr and childCat->m_cat_validator->m_keys.size() == 1)
{
auto copy = childCat->create_copy(child);
if (copy != child)
{
process.push_back(child);
continue;
}
}
// cannot update this...
if (cif::VERBOSE > 0)
std::cerr << "Cannot update child " << childCat->m_name << "." << childItemName << " with value " << value << '\n';
}
// finally, update the children
if (not process.empty())
childCat->update_value(process, childItemName, value);
}
}
}
void category::update_value(row *row, uint16_t item, std::string_view value, bool updateLinked, bool validate)
{
// make sure we have an index, if possible
if ((updateLinked or validate) and m_index == nullptr and m_cat_validator != nullptr)
m_index = new category_index(*this);
auto &col = m_items[item];
std::string_view oldValue;
auto ival = row->get(item);
if (ival != nullptr)
oldValue = ival->text();
if (value == oldValue) // no need to update
return;
std::string oldStrValue{ oldValue };
// check the value
if (col.m_validator and validate)
col.m_validator->operator()(value);
// If the item is part of the Key for this category, remove it from the index
// before updating
bool reinsert = false;
if (updateLinked and // an update of an Item's value
m_index != nullptr and key_item_indices().count(item))
{
reinsert = m_index->find(*this, row);
if (reinsert)
m_index->erase(*this, row);
}
// first remove old value with cix
if (ival != nullptr)
row->remove(item);
if (not value.empty())
row->append(item, { value });
if (reinsert and m_index != nullptr)
m_index->insert(*this, row);
// see if we need to update any child categories that depend on this value
auto iv = col.m_validator;
if (updateLinked and iv != nullptr /*and m_cascade*/)
{
row_handle rh(*this, *row);
for (auto &&[childCat, linked] : m_child_links)
{
if (std::find(linked->m_parent_keys.begin(), linked->m_parent_keys.end(), iv->m_item_name) == linked->m_parent_keys.end())
continue;
condition cond;
std::string childItemName;
for (size_t ix = 0; ix < linked->m_parent_keys.size(); ++ix)
{
std::string pk = linked->m_parent_keys[ix];
std::string ck = linked->m_child_keys[ix];
// TODO: add code to *NOT* test mandatory items for Empty
if (pk == iv->m_item_name)
{
childItemName = ck;
cond = std::move(cond) and key(ck) == oldStrValue;
}
else
{
std::string_view pk_value = rh[pk].text();
if (pk_value.empty())
cond = std::move(cond) and key(ck) == null;
else
cond = std::move(cond) and ((key(ck) == pk_value) or key(ck) == null);
}
}
auto rows = childCat->find(std::move(cond));
if (rows.empty())
continue;
// if (cif::VERBOSE > 2)
// {
// std::cerr << "Parent: " << linked->mParentcategory << " Child: " << linked->m_child_category << '\n'
// << cond << '\n';
// }
// Now, suppose there are already rows in child that conform to the new value,
// we then skip this rename
condition cond_n;
for (size_t ix = 0; ix < linked->m_parent_keys.size(); ++ix)
{
std::string pk = linked->m_parent_keys[ix];
std::string ck = linked->m_child_keys[ix];
if (pk == iv->m_item_name)
cond_n = std::move(cond_n) and key(ck) == value;
else
{
std::string_view pk_value = rh[pk].text();
if (pk_value.empty())
cond_n = std::move(cond_n) and key(ck) == null;
else
cond_n = std::move(cond_n) and key(ck) == pk_value;
}
}
auto rows_n = childCat->find(std::move(cond_n));
if (not rows_n.empty())
{
if (cif::VERBOSE > 0)
std::cerr << "Will not rename in child category since there are already rows that link to the parent\n";
continue;
}
for (auto cr : rows)
cr.assign(childItemName, value, false);
}
}
}
row *category::clone_row(const row &r)
{
row *result = create_row();
try
{
for (uint16_t ix = 0; ix < r.size(); ++ix)
{
auto &i = r[ix];
if (not i)
continue;
result->append(ix, { i.text() });
}
}
catch (...)
{
delete_row(result);
throw;
}
return result;
}
void category::delete_row(row *r)
{
if (r != nullptr)
{
row_allocator_type ra(get_allocator());
row_allocator_traits::destroy(ra, r);
row_allocator_traits::deallocate(ra, r, 1);
}
}
row_handle category::create_copy(row_handle r)
{
// copy the values
std::vector<item> items;
for (uint16_t ix = 0; ix < r.m_row->size(); ++ix)
{
auto i = r.m_row->get(ix);
if (i != nullptr)
items.emplace_back(m_items[ix].m_name, i->text());
}
if (m_cat_validator and m_cat_validator->m_keys.size() == 1)
{
auto key = m_cat_validator->m_keys.front();
auto kv = m_cat_validator->get_validator_for_item(key);
for (auto &item : items)
{
if (item.name() != key)
continue;
if (kv->m_type->m_primitive_type == DDL_PrimitiveType::Numb)
item.value(get_unique_id(""));
else
item.value(get_unique_id(m_name + "_id_"));
break;
}
}
return emplace(items.begin(), items.end());
}
// proxy methods for every insertion
category::iterator category::insert_impl(const_iterator pos, row *n)
{
if (m_index == nullptr and m_cat_validator != nullptr)
m_index = new category_index(*this);
assert(n != nullptr);
assert(n->m_next == nullptr);
if (n == nullptr)
throw std::runtime_error("Invalid pointer passed to insert");
// #ifndef NDEBUG
// if (m_validator)
// is_valid();
// #endif
try
{
// First, make sure all mandatory items are supplied
if (m_cat_validator != nullptr)
{
for (uint16_t ix = 0; ix < static_cast<uint16_t>(m_items.size()); ++ix)
{
const auto &[item, iv] = m_items[ix];
if (iv == nullptr)
continue;
bool seen = false;
auto i = n->get(ix);
if (i != nullptr)
{
iv->operator()(i->text());
seen = true;
}
if (not seen and iv->m_mandatory)
throw std::runtime_error("missing mandatory item " + item + " for category " + m_name);
}
}
if (m_index != nullptr)
m_index->insert(*this, n);
// insert at end, most often this is the case
if (pos.m_current.m_row == nullptr)
{
if (m_head == nullptr)
m_tail = m_head = n;
else
m_tail = m_tail->m_next = n;
}
else
{
assert(m_head != nullptr);
if (pos.m_current.m_row == m_head)
m_head = n->m_next = m_head;
else
n = n->m_next = m_head->m_next;
}
return iterator(*this, n);
}
catch (const std::exception &e)
{
delete_row(n);
throw;
}
// #ifndef NDEBUG
// if (m_validator)
// is_valid();
// #endif
}
void category::swap_item(uint16_t item_ix, row_handle &a, row_handle &b)
{
assert(this == a.m_category);
assert(this == b.m_category);
auto &ra = *a.m_row;
auto &rb = *b.m_row;
std::swap(ra.at(item_ix), rb.at(item_ix));
}
void category::sort(std::function<int(row_handle, row_handle)> f)
{
if (m_head == nullptr)
return;
std::vector<row_handle> rows;
for (auto itemRow = m_head; itemRow != nullptr; itemRow = itemRow->m_next)
rows.emplace_back(*this, *itemRow);
std::stable_sort(rows.begin(), rows.end(),
[&f](row_handle ia, row_handle ib)
{
return f(ia, ib) < 0;
});
m_head = rows.front().get_row();
m_tail = rows.back().get_row();
auto r = m_head;
for (size_t i = 1; i < rows.size(); ++i)
r = r->m_next = rows[i].get_row();
r->m_next = nullptr;
assert(r == m_tail);
assert(size() == rows.size());
}
void category::reorder_by_index()
{
if (m_index)
std::tie(m_head, m_tail) = m_index->reorder();
}
namespace detail
{
size_t write_value(std::ostream &os, std::string_view value, size_t offset, size_t width, bool right_aligned)
{
if (value.find('\n') != std::string::npos or width == 0 or value.length() > 132) // write as text item
{
if (offset > 0)
os << '\n';
os << ';';
char pc = 0;
for (auto ch : value)
{
if (pc == '\n' and ch == ';')
os << '\\';
os << ch;
pc = ch;
}
if (value.back() != '\n')
os << '\n';
os << ';' << '\n';
offset = 0;
}
else if (sac_parser::is_unquoted_string(value))
{
if (right_aligned)
{
if (value.length() < width)
{
os << std::string(width - value.length() - 1, ' ');
offset += width;
}
else
offset += value.length() + 1;
}
os << value;
if (right_aligned)
os << ' ';
else
{
if (value.length() < width)
{
os << std::string(width - value.length(), ' ');
offset += width;
}
else
{
os << ' ';
offset += value.length() + 1;
}
}
}
else
{
bool done = false;
for (char q : { '\'', '"' })
{
auto p = value.find(q); // see if we can use the quote character
while (p != std::string::npos and sac_parser::is_non_blank(value[p + 1]) and value[p + 1] != q)
p = value.find(q, p + 1);
if (p != std::string::npos)
continue;
os << q << value << q;
if (value.length() + 2 < width)
{
os << std::string(width - value.length() - 2, ' ');
offset += width;
}
else
{
os << ' ';
offset += value.length() + 1;
}
done = true;
break;
}
if (not done)
{
if (offset > 0)
os << '\n';
os << ';' << value << '\n'
<< ';' << '\n';
offset = 0;
}
}
return offset;
}
} // namespace detail
std::vector<std::string> category::get_item_order() const
{
std::vector<std::string> result;
for (auto &c : m_items)
result.push_back("_" + m_name + "." + c.m_name);
return result;
}
void category::write(std::ostream &os) const
{
std::vector<uint16_t> order(m_items.size());
iota(order.begin(), order.end(), static_cast<uint16_t>(0));
write(os, order, false);
}
void category::write(std::ostream &os, const std::vector<std::string> &items, bool addMissingItems)
{
// make sure all items are present
for (auto &c : items)
add_item(c);
std::vector<uint16_t> order;
order.reserve(m_items.size());
for (auto &c : items)
order.push_back(get_item_ix(c));
if (addMissingItems)
{
for (uint16_t i = 0; i < m_items.size(); ++i)
{
if (std::find(order.begin(), order.end(), i) == order.end())
order.push_back(i);
}
}
write(os, order, true);
}
void category::write(std::ostream &os, const std::vector<uint16_t> &order, bool includeEmptyItems) const
{
if (empty())
return;
// If the first Row has a next, we need a loop_
bool needLoop = (m_head->m_next != nullptr);
std::vector<bool> right_aligned(m_items.size(), false);
if (m_cat_validator != nullptr)
{
for (auto cix : order)
{
auto &col = m_items[cix];
right_aligned[cix] = col.m_validator != nullptr and
col.m_validator->m_type != nullptr and
col.m_validator->m_type->m_primitive_type == cif::DDL_PrimitiveType::Numb;
}
}
if (needLoop)
{
os << "loop_\n";
std::vector<size_t> itemWidths(m_items.size());
for (auto cix : order)
{
auto &col = m_items[cix];
os << '_';
if (not m_name.empty())
os << m_name << '.';
os << col.m_name << ' ' << '\n';
itemWidths[cix] = 2;
}
for (auto r = m_head; r != nullptr; r = r->m_next)
{
for (uint16_t ix = 0; ix < r->size(); ++ix)
{
auto v = r->get(ix);
if (v == nullptr)
continue;
if (v->text().find('\n') == std::string_view::npos)
{
size_t l = v->text().length();
if (not sac_parser::is_unquoted_string(v->text()))
l += 2;
if (l > 132)
continue;
if (itemWidths[ix] < l + 1)
itemWidths[ix] = l + 1;
}
}
}
for (auto r = m_head; r != nullptr; r = r->m_next) // loop over rows
{
size_t offset = 0;
for (uint16_t cix : order)
{
size_t w = itemWidths[cix];
std::string_view s;
auto iv = r->get(cix);
if (iv != nullptr)
s = iv->text();
if (s.empty())
s = "?";
size_t l = s.length();
if (not sac_parser::is_unquoted_string(s))
l += 2;
if (l < w)
l = w;
if (offset + l > 132 and offset > 0)
{
os << '\n';
offset = 0;
}
offset = detail::write_value(os, s, offset, w, right_aligned[cix]);
if (offset > 132)
{
os << '\n';
offset = 0;
}
}
if (offset > 0)
os << '\n';
}
}
else
{
// first find the indent level
size_t l = 0;
for (auto &col : m_items)
{
std::string item_name = '_' + m_name + '.' + col.m_name;
if (l < item_name.length())
l = item_name.length();
}
l += 3;
size_t width = 1;
for (auto cix : order)
{
if (not right_aligned[cix])
continue;
std::string_view s;
auto iv = m_head->get(cix);
if (iv != nullptr)
s = iv->text();
if (s.empty())
s = "?";
size_t l2 = s.length();
if (not sac_parser::is_unquoted_string(s))
l2 += 2;
if (width < l2)
width = l2;
}
for (uint16_t cix : order)
{
auto &col = m_items[cix];
os << '_';
if (not m_name.empty())
os << m_name << '.';
os << col.m_name << std::string(l - col.m_name.length() - m_name.length() - 2, ' ');
std::string_view s;
auto iv = m_head->get(cix);
if (iv != nullptr)
s = iv->text();
if (s.empty())
s = "?";
size_t offset = l;
if (s.length() + l >= kMaxLineLength)
{
os << '\n';
offset = 0;
}
if (detail::write_value(os, s, offset, width, s.empty() or right_aligned[cix]) != 0)
os << '\n';
}
}
os << "# \n";
}
bool category::operator==(const category &rhs) const
{
// shortcut
if (this == &rhs)
return true;
auto &a = *this;
auto &b = rhs;
using namespace std::placeholders;
// set<std::string> item_namesA(a.items()), item_namesB(b.items());
//
// if (item_namesA != item_namesB)
// std::cout << "Unequal number of items\n";
const category_validator *catValidator = nullptr;
auto validator = a.get_validator();
if (validator != nullptr)
catValidator = validator->get_validator_for_category(a.name());
typedef std::function<int(std::string_view, std::string_view)> compType;
std::vector<std::tuple<std::string, compType>> item_names;
std::vector<std::string> keys;
std::vector<size_t> keyIx;
if (catValidator == nullptr)
{
for (auto &item_name : a.get_items())
{
item_names.push_back(std::make_tuple(item_name, [](std::string_view va, std::string_view vb)
{ return va.compare(vb); }));
keyIx.push_back(keys.size());
keys.push_back(item_name);
}
}
else
{
keys = catValidator->m_keys;
for (auto &item_name : a.key_items())
{
auto iv = catValidator->get_validator_for_item(item_name);
if (iv == nullptr)
throw std::runtime_error("missing item validator");
auto tv = iv->m_type;
if (tv == nullptr)
throw std::runtime_error("missing type validator");
item_names.push_back(std::make_tuple(item_name, std::bind(&cif::type_validator::compare, tv, std::placeholders::_1, std::placeholders::_2)));
auto pred = [item_name](const std::string &s) -> bool
{
return cif::iequals(item_name, s) == 0;
};
if (find_if(keys.begin(), keys.end(), pred) == keys.end())
keyIx.push_back(item_names.size() - 1);
}
}
// a.reorderByIndex();
// b.reorderByIndex();
auto rowEqual = [&](const row_handle &a, const row_handle &b)
{
int d = 0;
for (auto kix : keyIx)
{
std::string item_name;
compType compare;
std::tie(item_name, compare) = item_names[kix];
d = compare(a[item_name].text(), b[item_name].text());
if (d != 0)
break;
}
return d == 0;
};
auto ai = a.begin(), bi = b.begin();
while (ai != a.end() or bi != b.end())
{
if (ai == a.end() or bi == b.end())
return false;
auto ra = *ai, rb = *bi;
if (not rowEqual(ra, rb))
return false;
std::vector<std::string> missingA, missingB, different;
for (auto &tt : item_names)
{
std::string item_name;
compType compare;
std::tie(item_name, compare) = tt;
// make it an option to compare unapplicable to empty or something
auto ta = ra[item_name].text();
if (ta == "." or ta == "?")
ta = "";
auto tb = rb[item_name].text();
if (tb == "." or tb == "?")
tb = "";
if (compare(ta, tb) != 0)
return false;
}
++ai;
++bi;
}
return true;
}
} // namespace cif |