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 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
|
// ==========================================================================
// SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2026, Knut Reinert, FU Berlin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * 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.
// * Neither the name of Knut Reinert or the FU Berlin nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// 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 KNUT REINERT OR THE FU BERLIN 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.
//
// ==========================================================================
// Author: David Weese <david.weese@fu-berlin.de>
// ==========================================================================
// Access a file like a string by implementing a custom paging/lru mechanism.
// ==========================================================================
#ifndef SEQAN_HEADER_STRING_EXTERNAL_H
#define SEQAN_HEADER_STRING_EXTERNAL_H
/* IOREV
* _nottested_
* _doc_
*
*
* mostly documented (doc for some functions missing)
* not tested in any test case nor used in any app right now
* -> needs testing, especially different iterators
*/
//////////////////////////////////////////////////////////////////////////////
namespace seqan2
{
/*!
* @class ExternalConfig
* @headerfile <seqan/file.h>
* @brief Standard configuration for the @link ExternalString @endlink.
*
* @signature template <[typename TFile[, unsigned SEQAN_PAGESIZE[, unsigned FRAMES]]>
* struct ExternalConfig;
*
* @tparam TFile The @link File @endlink type to use. Default: <tt>File<></tt>.
* @tparam SEQAN_PAGESIZE The number of values in one page. This should be a power of 2 to speed up transfer and
* calculations. Default: 2<sup>20</sup>.
* @tparam FRAMES The number of pages that should reside in internal memory. To enable prefetching and automatic
* swap-out, <tt>frames</tt> should be greater than 1. Default: 2.
*
* When using this configuration, the <tt>Size</tt> type of the ExternalString is <tt>unsigned</tt>. Thus, with this configuration at
* most 4.294.967.296 values can be stored in an ExternalString on a 32 bit system.
*
* For a larger size type use @link ExternalConfigLarge @endlink.
*
* @see ExternalConfigLarge
*/
/*!
* @class ExternalConfigLarge
* @headerfile <seqan/file.h>
* @brief Large size type configuration for the @link ExternalString @endlink.
*
* @signature template <[typename TFile[, unsigned SEQAN_PAGESIZE[, unsigned FRAMES]]>
* struct ExternalConfigLarge;
*
* @tparam TFile The @link File @endlink type to use. Default: <tt>File<></tt>.
* @tparam SEQAN_PAGESIZE The number of values in one page. This should be a power of 2 to speed up transfer and
* calculations. Default: 2<sup>20</sup>.
* @tparam FRAMES The number of pages that should reside in internal memory. To enable prefetching and automatic
* swap-out, <tt>frames</tt> should be greater than 1. Default: 2.
*
* @see ExternalConfig
*
* @section Remarks
*
* When using this configuration ,the <tt>Size</tt> type of the ExternalString is <tt>Size</tt> type of <tt>TFile</tt>. Normally, this is
* a 64 bit integer. For a smaller size type use ExternalConfig.
*
* Some data structures store size types values (e.g. suffix arrays in indices). To save memory, you should think o
* fusing ExternalConfig.
*/
/*!
* @class ExternalConfigSize
* @headerfile <seqan/file.h>
* @brief Arbitrary size type configuration for @link ExternalString @endlink.
*
* @signature template <typename TSize[, typename TFile[, unsigned SEQAN_PAGESIZE[, unsigned FRAMEs]]]>
* class ExternalConfigSize;
*
* @tparam TSize The size type of the ExternalString.
* @tparam TFile Type of file the ExternalString will be based on. Defaults to <tt>File<></tt>.
* @tparam SEQAN_PAGESIZE The number of values in one page. This should be a power of 2 to speed up transfer and
* calculations. Default: 2<sup>20</sup>.
* @tparam FRAMES The number of pages that should reside in internal memory. To enable prefetching and automatic
* swap-out, <tt>frames</tt> should be greater than 1. Default: 2.
*/
/*!
* @class ExternalString External String
* @extends String
* @headerfile <seqan/file.h>
* @brief String that is stored in external memory.
*
* @signature template <typename TValue[, typename TConfig]>
* class String<TValue, External<TConfig> >;
*
* @tparam TValue The type that is used for the items/characters stored in the string.
* @tparam TConfig A structure to configure the external string. Defaults to <tt>ExternalConfigLarge<>. See
* ExternalConfig, ExternalConfigLarge, and ExternalConfigSize.
*
* The External String enables to access sequences larger than the available internal memory (RAM) by using external
* memory (e.g. Hard disk, Network storage, ...) via a File object. Sequences of nearly arbitrary size can be accessed
* even larger than the logically addressable memory, i.e. they can in particular contain more than 2^32 elements on a
* 32bit system (see Tag.ExternalConfigLarge). See the String constructor for more details.
*
* This String also supports fast appending and removing of values at the end (see Block String, appendValue)
*
* The External String implements a LRU mechanism to swap out pages. The External String's Iterator detects a forward or
* backward iteration and asynchronously prefetches pages that certainly will be accessed and automatically swaps out
* pages that certainly won't be accessed any more in the iteration process.
*
* The String is implemented like a virtual memory manager. It divides its character sequence into pages of a fixed
* length (e.g. 4MB) and maintains a page table with information for each page (e.g. resides in memory or was swapped
* out, is dirty and needs to be saved, ...). Besides the page table the String also contains a size-limited list of
* page frames. A page frame is reserved internal memory for a page. When accessing values of a page that is stored in
* external memory, the page is loaded to a page frame first. In case that there is no page frame free, another page is
* swapped out before to free a page frame.
*/
#ifndef _EXTERNAL_STRING_DEFAULT_PAGE_SIZE
#define _EXTERNAL_STRING_DEFAULT_PAGE_SIZE 4 * 1024 * 1024 // 4MTypes per default
#endif
// standard external string
// size is uint32
template < typename TFile_ = File<>, // default file type
unsigned SEQAN_PAGESIZE_ = _EXTERNAL_STRING_DEFAULT_PAGE_SIZE,
unsigned FRAMES_ = 2 > // simultanous frames
struct ExternalConfig {
//IOREV _bug_ doc says default page size is 2^20, but it is 2^22
typedef TFile_ TFile;
typedef unsigned TSize;
enum { SEQAN_PAGESIZE = SEQAN_PAGESIZE_ };
enum { FRAMES = FRAMES_ };
};
// the same as ExternalConfig
// but size type is size type of TFile_ (i.e. uint64)
//
// ATTENTION:
// pipes use the size type
// uint64 blows up your suffix arrays, lcp-tables, ...
template < typename TFile_ = File<>, // default file type
unsigned SEQAN_PAGESIZE_ = _EXTERNAL_STRING_DEFAULT_PAGE_SIZE,
unsigned FRAMES_ = 2 > // simultanous frames
struct ExternalConfigLarge {
//IOREV contains warning in code comments, need to investigate
typedef TFile_ TFile;
typedef typename MakeUnsigned<typename Size<TFile_>::Type>::Type TSize;
enum { SEQAN_PAGESIZE = SEQAN_PAGESIZE_ };
enum { FRAMES = FRAMES_ };
};
// custom size type
template < typename TSize_,
typename TFile_ = File<>, // default file type
unsigned SEQAN_PAGESIZE_ = _EXTERNAL_STRING_DEFAULT_PAGE_SIZE,
unsigned FRAMES_ = 2 > // simultanous frames
struct ExternalConfigSize {
//IOREV
typedef TSize_ TSize;
typedef TFile_ TFile;
enum { SEQAN_PAGESIZE = SEQAN_PAGESIZE_ };
enum { FRAMES = FRAMES_ };
};
template < typename TConfig = ExternalConfigLarge<> >
struct External {};
//IOREV
//////////////////////////////////////////////////////////////////////////////
// random External String iterator
template < typename TExtString >
struct ExtStringIterator
{
//IOREV
typedef ExtStringIterator TIterator;
typedef ExtStringIterator TStdIterator;
typedef typename Value<TExtString>::Type TValue;
typedef typename Size<TExtString>::Type TSize;
typedef typename Difference<TExtString>::Type TDifference;
typedef typename TExtString::TVolatilePtr TVolatilePtr;
enum { SEQAN_PAGESIZE = TExtString::SEQAN_PAGESIZE };
TSize offset;
TExtString *extString;
ExtStringIterator():
offset(0),
extString(NULL) {}
explicit ExtStringIterator(TExtString *_extString, TSize _offset):
extString(_extString),
offset(_offset) {}
//////////////////////////////////////////////////////////////////////////////
// iterator conversion interface
ExtStringIterator(const TStdIterator &I):
offset(I.offset),
extString(I.extString) {}
//////////////////////////////////////////////////////////////////////////////
// iterator arithmetic
inline TDifference operator- (const TIterator &I) const {
return offset - I.offset;
}
inline TIterator operator- (TDifference delta) const {
return TIterator(extString, offset - delta);
}
inline TIterator& operator-= (TDifference delta) const {
offset -= delta;
return *this;
}
inline TIterator operator+ (TDifference delta) const {
return TIterator(extString, offset + delta);
}
inline TIterator& operator+= (TDifference delta) const {
offset += delta;
return *this;
}
inline TValue & operator* () const {
return (*extString)[offset];
}
/* inline TValue const & operator* () const {
return (*extString)[offset];
}
*/
inline TIterator& operator++ () {
++offset; return *this;
}
inline TIterator operator++ (int) {
TIterator before = *this;
++offset; return before;
}
inline TIterator& operator-- () {
--offset; return *this;
}
inline TIterator operator-- (int) {
TIterator before = *this;
--offset; return before;
}
inline bool operator== (const TIterator &I) const {
SEQAN_ASSERT_EQ(extString, I.extString);
return offset == I.offset;
}
inline bool operator!= (const TIterator &I) const {
SEQAN_ASSERT_EQ(extString, I.extString);
return offset != I.offset;
}
inline bool operator< (const TIterator &I) const {
SEQAN_ASSERT_EQ(extString, I.extString);
return offset < I.offset;
}
};
//////////////////////////////////////////////////////////////////////////////
// const random External String iterator
template < typename TExtString >
struct ExtStringConstIterator
{
//IOREV
typedef ExtStringConstIterator TIterator;
typedef ExtStringIterator<TExtString> TStdIterator;
typedef ExtStringConstIterator TStdConstIterator;
typedef typename Value<TExtString>::Type TValue;
typedef typename Size<TExtString>::Type TSize;
typedef typename Difference<TExtString>::Type TDifference;
typedef typename TExtString::TVolatilePtr TVolatilePtr;
enum { SEQAN_PAGESIZE = TExtString::SEQAN_PAGESIZE };
TSize offset;
TExtString *extString;
ExtStringConstIterator():
offset(0),
extString(NULL) {}
explicit ExtStringConstIterator(TExtString *_extString, TSize _offset):
extString(_extString),
offset(_offset) {}
//////////////////////////////////////////////////////////////////////////////
// iterator conversion interface
ExtStringConstIterator(const TStdIterator &I):
offset(I.offset),
extString(I.extString) {}
ExtStringConstIterator(const TStdConstIterator &I):
offset(I.offset),
extString(I.extString) {}
//////////////////////////////////////////////////////////////////////////////
// iterator arithmetic
inline TDifference operator- (const TIterator &I) const {
return offset - I.offset;
}
inline TIterator operator- (TDifference delta) const {
return TIterator(extString, offset - delta);
}
inline TIterator& operator-= (TDifference delta) const {
offset -= delta;
return *this;
}
inline TIterator operator+ (TDifference delta) const {
return TIterator(extString, offset + delta);
}
inline TIterator& operator+= (TDifference delta) const {
offset += delta;
return *this;
}
inline TValue const & operator* () const {
return (*extString)[offset];
}
inline TIterator& operator++ () {
++offset; return *this;
}
inline TIterator operator++ (int) {
TIterator before = *this;
++offset; return before;
}
inline TIterator& operator-- () {
--offset; return *this;
}
inline TIterator operator-- (int) {
TIterator before = *this;
--offset; return before;
}
inline bool operator== (const TIterator &I) const {
SEQAN_ASSERT_EQ(extString, I.extString);
return offset == I.offset;
}
inline bool operator!= (const TIterator &I) const {
SEQAN_ASSERT_EQ(extString, I.extString);
return offset != I.offset;
}
inline bool operator< (const TIterator &I) const {
SEQAN_ASSERT_EQ(extString, I.extString);
return offset < I.offset;
}
};
//////////////////////////////////////////////////////////////////////////////
// forward External String iterator
template < typename TExtString >
struct ExtStringFwdIterator
{
//IOREV
typedef ExtStringFwdIterator TIterator;
typedef ExtStringIterator<TExtString> TStdIterator;
typedef ExtStringConstIterator<TExtString> TStdConstIterator;
typedef typename Value<TExtString>::Type TValue;
typedef typename Size<TExtString>::Type TSize;
typedef typename Difference<TExtString>::Type TDifference;
typedef typename TExtString::TVolatilePtr TVolatilePtr;
enum { SEQAN_PAGESIZE = TExtString::SEQAN_PAGESIZE };
TExtString *extString;
bool dirty;
int pageNo;
unsigned pageOfs;
int prefetch; // -n .. prefetch n pages downwards, n .. prefetch n pages upwards, 0 .. disabled
TVolatilePtr begin;
ExtStringFwdIterator():
extString(NULL),
dirty(true),
pageNo(0),
pageOfs(0),
prefetch(0),
begin(NULL) {}
ExtStringFwdIterator(const TIterator &I):
extString(I.extString),
dirty(true),
pageNo(I.pageNo),
pageOfs(I.pageOfs),
prefetch(I.prefetch),
begin(NULL) {}
explicit ExtStringFwdIterator(TExtString *_extString, TSize _offset):
extString(_extString),
dirty(true),
pageNo(_offset / SEQAN_PAGESIZE),
pageOfs(_offset % SEQAN_PAGESIZE),
prefetch(0),
begin(NULL) {}
explicit ExtStringFwdIterator(TExtString *_extString, TSize _pageNo, TSize _pageOfs):
extString(_extString),
dirty(true),
pageNo(_pageNo),
pageOfs(_pageOfs),
prefetch(0),
begin(NULL) {}
~ExtStringFwdIterator()
{
invalidate();
}
//////////////////////////////////////////////////////////////////////////////
// iterator conversion interface
ExtStringFwdIterator(const TStdIterator &I):
extString(I.extString),
pageNo(I.offset / SEQAN_PAGESIZE),
pageOfs(I.offset % SEQAN_PAGESIZE),
prefetch(0),
begin(NULL) {}
inline TIterator& operator=(TStdIterator const & Right_) {
invalidate();
pageNo = Right_.offset / SEQAN_PAGESIZE;
pageOfs = Right_.offset % SEQAN_PAGESIZE;
extString = Right_.extString;
return *this;
}
inline TSize position() const
{
return (TSize)pageNo * (TSize)SEQAN_PAGESIZE + pageOfs;
}
inline operator TStdIterator() const {
return TStdIterator(extString, position());
}
inline operator TStdConstIterator() const {
return TStdConstIterator(extString, position());
}
inline TIterator& operator=(TIterator const & Right_) {
invalidate();
extString = Right_.extString;
pageNo = Right_.pageNo;
pageOfs = Right_.pageOfs;
prefetch = Right_.prefetch;
return *this;
}
//////////////////////////////////////////////////////////////////////////////
// iterator arithmetic
inline TDifference operator- (const TIterator &I) const
{
return position() - I.position();
}
inline TIterator operator- (TDifference delta) const {
TDifference dPNo = delta / SEQAN_PAGESIZE;
TDifference dPOfs = delta % SEQAN_PAGESIZE;
if (static_cast<TDifference>(pageOfs) >= dPOfs)
return TIterator(extString, pageNo - dPNo, pageOfs - dPOfs);
else
return TIterator(extString, pageNo - dPNo - 1, SEQAN_PAGESIZE + pageOfs - dPOfs);
}
inline TIterator& operator-= (TDifference delta) {
TDifference dPNo = delta / SEQAN_PAGESIZE;
TDifference dPOfs = delta % SEQAN_PAGESIZE;
if (pageOfs < dPOfs) {
++dPNo;
pageOfs = SEQAN_PAGESIZE + pageOfs - dPOfs;
} else
pageOfs -= dPOfs;
if (dPNo) invalidate(0);
pageNo -= dPNo;
return *this;
}
inline TIterator operator+ (TDifference delta) const {
TDifference dPNo = delta / SEQAN_PAGESIZE;
TDifference nPOfs = pageOfs + delta % SEQAN_PAGESIZE;
if (nPOfs < SEQAN_PAGESIZE)
return TIterator(extString, pageNo + dPNo, nPOfs);
else
return TIterator(extString, pageNo + dPNo + 1, nPOfs - SEQAN_PAGESIZE);
}
inline TIterator& operator+= (TDifference delta) {
TDifference dPNo = delta / SEQAN_PAGESIZE;
TDifference nPOfs = pageOfs + delta % SEQAN_PAGESIZE;
if (nPOfs >= SEQAN_PAGESIZE) {
++dPNo;
nPOfs -= SEQAN_PAGESIZE;
}
if (dPNo) invalidate(0);
pageNo += dPNo;
pageOfs = nPOfs;
return *this;
}
inline void validate() const
{
typename TExtString::TPageFrame &pf = extString->getSharedPage(pageNo, prefetch);
const_cast<TIterator*>(this)->dirty = pf.dirty;
const_cast<TIterator*>(this)->begin = pf.begin;
}
inline void invalidate(int _prefetch = 0) const
{
if (begin)
{
const_cast<TIterator*>(this)->begin = NULL;
extString->releasePage(pageNo, (prefetch != 0) || (_prefetch != 0));
const_cast<TIterator*>(this)->prefetch = _prefetch;
}
}
inline TValue & operator* () const
{
if (!begin) validate();
// synchronize PageFrame dirty flag on dirty false->true change
if (!dirty) {
const_cast<TIterator*>(this)->dirty = true;
extString->getPage(pageNo).dirty = true;
}
return const_cast<TIterator*>(this)->begin[pageOfs];
}
/*
inline TValue const & operator* () const {
if (!begin) validate();
return begin[pageOfs];
}
*/
inline TIterator& operator++ () {
if (++pageOfs == SEQAN_PAGESIZE) {
invalidate(1);
pageOfs = 0;
++pageNo;
}
return *this;
}
inline TIterator operator++ (int) {
TIterator before = *this;
if (++pageOfs == SEQAN_PAGESIZE) {
invalidate(1);
pageOfs = 0;
++pageNo;
}
return before;
}
inline TIterator& operator-- () {
if (pageOfs)
--pageOfs;
else {
invalidate(-1);
pageOfs = SEQAN_PAGESIZE - 1;
--pageNo;
}
return *this;
}
inline TIterator operator-- (int) {
TIterator before = *this;
if (pageOfs)
--pageOfs;
else {
invalidate(-1);
pageOfs = SEQAN_PAGESIZE - 1;
--pageNo;
}
return before;
}
inline bool operator== (const TIterator &I) const {
SEQAN_ASSERT_EQ(extString, I.extString);
return pageNo == I.pageNo && pageOfs == I.pageOfs;
}
inline bool operator!= (const TIterator &I) const {
SEQAN_ASSERT_EQ(extString, I.extString);
return pageNo != I.pageNo || pageOfs != I.pageOfs;
}
inline bool operator< (const TIterator &I) const {
SEQAN_ASSERT_EQ(extString, I.extString);
return pageNo < I.pageNo || (pageNo == I.pageNo && pageOfs < I.pageOfs);
}
};
//////////////////////////////////////////////////////////////////////////////
// const forward External String iterator
template < typename TExtString >
struct ExtStringFwdConstIterator
{
//IOREV _nottested_
typedef ExtStringFwdConstIterator TIterator;
typedef ExtStringIterator<TExtString> TStdIterator;
typedef ExtStringConstIterator<TExtString> TStdConstIterator;
typedef ExtStringFwdIterator<TExtString> TFwdIterator;
typedef typename Value<TExtString>::Type TValue;
typedef typename Size<TExtString>::Type TSize;
typedef typename Difference<TExtString>::Type TDifference;
typedef typename TExtString::TVolatilePtr TVolatilePtr;
enum { SEQAN_PAGESIZE = TExtString::SEQAN_PAGESIZE };
TExtString *extString;
int pageNo;
unsigned pageOfs;
int prefetch; // -n .. prefetch n pages downwards, n .. prefetch n pages upwards, 0 .. disabled
TVolatilePtr begin;
ExtStringFwdConstIterator():
extString(NULL),
pageNo(0),
pageOfs(0),
prefetch(0),
begin(NULL) {}
ExtStringFwdConstIterator(const TIterator &I):
extString(I.extString),
pageNo(I.pageNo),
pageOfs(I.pageOfs),
prefetch(I.prefetch),
begin(NULL) {}
ExtStringFwdConstIterator(const TFwdIterator &I):
extString(I.extString),
pageNo(I.pageNo),
pageOfs(I.pageOfs),
prefetch(I.prefetch),
begin(NULL) {}
~ExtStringFwdConstIterator() {
invalidate();
}
ExtStringFwdConstIterator(TExtString *_extString, TSize _offset):
extString(_extString),
pageNo(_offset / SEQAN_PAGESIZE),
pageOfs(_offset % SEQAN_PAGESIZE),
prefetch(0),
begin(NULL) {}
ExtStringFwdConstIterator(TExtString *_extString, TSize _pageNo, TSize _pageOfs):
extString(_extString),
pageNo(_pageNo),
pageOfs(_pageOfs),
prefetch(0),
begin(NULL) {}
//////////////////////////////////////////////////////////////////////////////
// iterator conversion interface
ExtStringFwdConstIterator(TStdIterator &I):
extString(I.extString),
pageNo(I.offset / SEQAN_PAGESIZE),
pageOfs(I.offset % SEQAN_PAGESIZE),
prefetch(0),
begin(NULL) {}
ExtStringFwdConstIterator(TStdConstIterator const &I):
extString(I.extString),
pageNo(I.offset / SEQAN_PAGESIZE),
pageOfs(I.offset % SEQAN_PAGESIZE),
prefetch(0),
begin(NULL) {}
inline TIterator& operator=(TStdIterator const & Right_) {
invalidate();
pageNo = Right_.offset / SEQAN_PAGESIZE;
pageOfs = Right_.offset % SEQAN_PAGESIZE;
extString = Right_.extString;
return *this;
}
inline TIterator& operator=(TStdConstIterator const & Right_) {
invalidate();
pageNo = Right_.offset / SEQAN_PAGESIZE;
pageOfs = Right_.offset % SEQAN_PAGESIZE;
extString = Right_.extString;
return *this;
}
inline TSize position() const
{
return (TSize)pageNo * (TSize)SEQAN_PAGESIZE + pageOfs;
}
inline operator TStdConstIterator() const {
return TStdConstIterator(extString, position());
}
inline TIterator& operator=(TIterator const & Right_) {
invalidate();
extString = Right_.extString;
pageNo = Right_.pageNo;
pageOfs = Right_.pageOfs;
prefetch = Right_.prefetch;
return *this;
}
inline TIterator& operator=(TFwdIterator const & Right_) {
invalidate();
extString = Right_.extString;
pageNo = Right_.pageNo;
pageOfs = Right_.pageOfs;
prefetch = Right_.prefetch;
return *this;
}
//////////////////////////////////////////////////////////////////////////////
// iterator arithmetic
inline TDifference operator- (const TIterator &I) const
{
return position() - I.position();
}
inline TIterator operator- (TDifference delta) const {
TDifference dPNo = delta / SEQAN_PAGESIZE;
TDifference dPOfs = delta % SEQAN_PAGESIZE;
if (pageOfs >= dPOfs)
return TIterator(extString, pageNo - dPNo, pageOfs - dPOfs);
else
return TIterator(extString, pageNo - dPNo - 1, SEQAN_PAGESIZE + pageOfs - dPOfs);
}
inline TIterator& operator-= (TDifference delta) {
TDifference dPNo = delta / SEQAN_PAGESIZE;
TDifference dPOfs = delta % SEQAN_PAGESIZE;
if (pageOfs < dPOfs) {
++dPNo;
pageOfs = SEQAN_PAGESIZE + pageOfs - dPOfs;
} else
pageOfs -= dPOfs;
if (dPNo) invalidate(0);
pageNo -= dPNo;
return *this;
}
inline TIterator operator+ (TDifference delta) const {
TDifference dPNo = delta / SEQAN_PAGESIZE;
TDifference nPOfs = pageOfs + delta % SEQAN_PAGESIZE;
if (nPOfs < SEQAN_PAGESIZE)
return TIterator(extString, pageNo + dPNo, nPOfs);
else
return TIterator(extString, pageNo + dPNo + 1, nPOfs - SEQAN_PAGESIZE);
}
inline TIterator& operator+= (TDifference delta) {
TDifference dPNo = delta / SEQAN_PAGESIZE;
TDifference nPOfs = pageOfs + delta % SEQAN_PAGESIZE;
if (nPOfs >= SEQAN_PAGESIZE) {
++dPNo;
nPOfs -= SEQAN_PAGESIZE;
}
if (dPNo) invalidate(0);
pageNo += dPNo;
pageOfs = nPOfs;
return *this;
}
inline void validate() const {
typename TExtString::TPageFrame &pf = extString->getSharedPage(pageNo, prefetch);
const_cast<TIterator*>(this)->begin = pf.begin;
}
inline void invalidate(int _prefetch = 0) const {
if (begin) {
const_cast<TIterator*>(this)->begin = NULL;
extString->releasePage(pageNo, (prefetch != 0) || (_prefetch != 0));
const_cast<TIterator*>(this)->prefetch = _prefetch;
}
}
inline TValue const & operator* () const {
if (!begin) validate();
return begin[pageOfs];
}
inline TIterator& operator++ () {
if (++pageOfs == SEQAN_PAGESIZE) {
invalidate(1);
pageOfs = 0;
++pageNo;
}
return *this;
}
inline TIterator operator++ (int) {
TIterator before = *this;
if (++pageOfs == SEQAN_PAGESIZE) {
invalidate(1);
pageOfs = 0;
++pageNo;
}
return before;
}
inline TIterator& operator-- () {
if (pageOfs)
--pageOfs;
else {
invalidate(-1);
pageOfs = SEQAN_PAGESIZE - 1;
--pageNo;
}
return *this;
}
inline TIterator operator-- (int) {
TIterator before = *this;
if (pageOfs)
--pageOfs;
else {
invalidate(-1);
pageOfs = SEQAN_PAGESIZE - 1;
--pageNo;
}
return before;
}
inline bool operator== (const TIterator &I) const {
SEQAN_ASSERT_EQ(extString, I.extString);
return pageNo == I.pageNo && pageOfs == I.pageOfs;
}
inline bool operator!= (const TIterator &I) const {
SEQAN_ASSERT_EQ(extString, I.extString);
return pageNo != I.pageNo || pageOfs != I.pageOfs;
}
inline bool operator< (const TIterator &I) const {
SEQAN_ASSERT_EQ(extString, I.extString);
return pageNo < I.pageNo || (pageNo == I.pageNo && pageOfs < I.pageOfs);
}
};
//////////////////////////////////////////////////////////////////////////////
// iterator metafunctions
template < typename TString >
struct Container< ExtStringIterator<TString> > { typedef TString Type; };
//IOREV
template < typename TString >
struct Container< ExtStringConstIterator<TString> > { typedef TString Type; };
//IOREV
template < typename TString >
struct Container< ExtStringFwdIterator<TString> > { typedef TString Type; };
//IOREV
template < typename TString >
struct Container< ExtStringFwdConstIterator<TString> > { typedef TString Type; };
//IOREV
template < typename TString >
struct Value< ExtStringIterator<TString> > { typedef typename Value<TString>::Type Type; };
//IOREV
template < typename TString >
struct Value< ExtStringConstIterator<TString> > { typedef typename Value<TString>::Type Type; };
//IOREV
template < typename TString >
struct Value< ExtStringFwdIterator<TString> > { typedef typename Value<TString>::Type Type; };
//IOREV
template < typename TString >
struct Value< ExtStringFwdConstIterator<TString> > { typedef typename Value<TString>::Type Type; };
//IOREV
template < typename TString >
struct Reference< ExtStringConstIterator<TString> >:
public Reference<TString const> {};
//IOREV
template < typename TString >
struct Reference< ExtStringFwdConstIterator<TString> >:
public Reference<TString const> {};
//IOREV
template < typename TString >
struct Size< ExtStringIterator<TString> > { typedef typename Size<TString>::Type Type; };
//IOREV
template < typename TString >
struct Size< ExtStringConstIterator<TString> > { typedef typename Size<TString>::Type Type; };
//IOREV
template < typename TString >
struct Size< ExtStringFwdIterator<TString> > { typedef typename Size<TString>::Type Type; };
//IOREV
template < typename TString >
struct Size< ExtStringFwdConstIterator<TString> > { typedef typename Size<TString>::Type Type; };
//IOREV
template < typename TString >
struct Position< ExtStringIterator<TString> > { typedef typename Position<TString>::Type Type; };
//IOREV
template < typename TString >
struct Position< ExtStringConstIterator<TString> > { typedef typename Position<TString>::Type Type; };
//IOREV
template < typename TString >
struct Position< ExtStringFwdIterator<TString> > { typedef typename Position<TString>::Type Type; };
//IOREV
template < typename TString >
struct Position< ExtStringFwdConstIterator<TString> > { typedef typename Position<TString>::Type Type; };
//IOREV
template < typename TString >
struct Difference< ExtStringIterator<TString> > { typedef typename Difference<TString>::Type Type; };
//IOREV
template < typename TString >
struct Difference< ExtStringConstIterator<TString> > { typedef typename Difference<TString>::Type Type; };
//IOREV
template < typename TString >
struct Difference< ExtStringFwdIterator<TString> > { typedef typename Difference<TString>::Type Type; };
//IOREV
template < typename TString >
struct Difference< ExtStringFwdConstIterator<TString> > { typedef typename Difference<TString>::Type Type; };
//IOREV
//////////////////////////////////////////////////////////////////////////////
// global interface
template <typename TExtString>
inline TExtString & container(ExtStringIterator<TExtString> &it) { return *(it.extString); }
//IOREV
template <typename TExtString>
inline TExtString & container(ExtStringIterator<TExtString> const &it) { return *(it.extString); }
//IOREV
template <typename TExtString>
inline TExtString & container(ExtStringConstIterator<TExtString> &it) { return *(it.extString); }
//IOREV
template <typename TExtString>
inline TExtString & container(ExtStringConstIterator<TExtString> const &it) { return *(it.extString); }
//IOREV
template <typename TExtString>
inline TExtString & container(ExtStringFwdIterator<TExtString> &it) { return *(it.extString); }
//IOREV
template <typename TExtString>
inline TExtString & container(ExtStringFwdIterator<TExtString> const &it) { return *(it.extString); }
//IOREV
template <typename TExtString>
inline TExtString & container(ExtStringFwdConstIterator<TExtString> &it) { return *(it.extString); }
//IOREV
template <typename TExtString>
inline TExtString & container(ExtStringFwdConstIterator<TExtString> const &it) { return *(it.extString); }
//IOREV
//____________________________________________________________________________
template <typename TExtString>
inline bool atBegin(ExtStringIterator<TExtString> &it) { return it.offset == 0; }
//IOREV
template <typename TExtString>
inline bool atBegin(ExtStringIterator<TExtString> const &it) { return it.offset == 0; }
//IOREV
template <typename TExtString>
inline bool atBegin(ExtStringConstIterator<TExtString> &it) { return it.offset == 0; }
//IOREV
template <typename TExtString>
inline bool atBegin(ExtStringConstIterator<TExtString> const &it) { return it.offset == 0; }
//IOREV
template <typename TExtString>
inline bool atBegin(ExtStringFwdIterator<TExtString> &it) {
//IOREV
return it.pageNo == 0 && it.pageOfs == 0;
}
template <typename TExtString>
inline bool atBegin(ExtStringFwdIterator<TExtString> const &it) {
//IOREV
return it.pageNo == 0 && it.pageOfs == 0;
}
template <typename TExtString>
inline bool atBegin(ExtStringFwdConstIterator<TExtString> &it) {
//IOREV
return it.pageNo == 0 && it.pageOfs == 0;
}
template <typename TExtString>
inline bool atBegin(ExtStringFwdConstIterator<TExtString> const &it) {
//IOREV
return it.pageNo == 0 && it.pageOfs == 0;
}
//____________________________________________________________________________
template <typename TExtString>
inline bool atEnd(ExtStringIterator<TExtString> &it) { return it.offset == it.extString->data_size; }
//IOREV
template <typename TExtString>
inline bool atEnd(ExtStringIterator<TExtString> const &it) { return it.offset == it.extString->data_size; }
//IOREV
template <typename TExtString>
inline bool atEnd(ExtStringConstIterator<TExtString> &it) { return it.offset == it.extString->data_size; }
//IOREV
template <typename TExtString>
inline bool atEnd(ExtStringConstIterator<TExtString> const &it) { return it.offset == it.extString->data_size; }
//IOREV
template <typename TExtString>
inline bool atEnd(ExtStringFwdIterator<TExtString> &it) {
//IOREV
return TExtString::SEQAN_PAGESIZE * it.pageNo + it.pageOfs == it.extString->data_size;
}
template <typename TExtString>
inline bool atEnd(ExtStringFwdIterator<TExtString> const &it) {
//IOREV
return TExtString::SEQAN_PAGESIZE * it.pageNo + it.pageOfs == it.extString->data_size;
}
template <typename TExtString>
inline bool atEnd(ExtStringFwdConstIterator<TExtString> &it) {
//IOREV
return TExtString::SEQAN_PAGESIZE * it.pageNo + it.pageOfs == it.extString->data_size;
}
template <typename TExtString>
inline bool atEnd(ExtStringFwdConstIterator<TExtString> const &it) {
//IOREV
return TExtString::SEQAN_PAGESIZE * it.pageNo + it.pageOfs == it.extString->data_size;
}
//////////////////////////////////////////////////////////////////////////////
// External String
//////////////////////////////////////////////////////////////////////////////
template < typename TValue,
typename TConfig >
class String<TValue, External<TConfig> >
{
//IOREV _doc_ contains TODOs by holtgrew
public:
enum { FRAMES = TConfig::FRAMES,
SEQAN_PAGESIZE = TConfig::SEQAN_PAGESIZE };
typedef typename TConfig::TFile TFile;
typedef typename TConfig::TSize TSize;
typedef String<int> TPageTable;
typedef Buffer<TValue, PageFrame<TFile, Fixed<SEQAN_PAGESIZE> > > TPageFrame;
typedef PageContainer<TPageFrame, FRAMES> TCache;
typedef VolatilePtr<TValue> TVolatilePtr;
TPageTable pager;
TCache cache;
TFile file;
bool _temporary, _ownFile;
TSize data_size;
int lastDiskPage; // the last page on disk and in mem
unsigned lastDiskPageSize; // can be smaller than SEQAN_PAGESIZE
String(TSize size = 0) :
file(NULL), _temporary(true), _ownFile(false), data_size(0),
lastDiskPage(0), // the last two values need not to be initialized here
lastDiskPageSize(0) // because of "write before read"
{
resize(*this, size);
}
/*!
* @fn ExternalString::String
* @brief Constructor
*
* @signature String::String();
* @signature String::String(file);
* @signature String::String(fileName[, openMode]);
*
* @param[in] file The @link File @endlink to use for reading and writing. You must ensure that
* <tt>file</tt> is open as the string will not call <tt>open</tt> and <tt>close</tt>
* on the file.
* @param[in] fileName The path to open. Type: <tt>char const *</tt>
* @param[in,out] openMode The open mode.
*
* @section Remarks
*
* When a file or file name is given, this file will be used for the ExternalString. If the file exists, this file will
* be used and determines the strings length and content. If the file doesn't exist, a new and empty file will be
* created and used for the string. In both cases, the string won't delete the file in the destructor.
*
* When no file is given (default c'tor) the string will be empty and no file is used until the string needs to swap out
* page frames. Then a temporary file will be used which will be deleted when the string is destroyed.
*
* Instead of giving file or fileName to the constructor, you could also use the default constructor and call open or
* openTemp afterwards to reach the same behaviour.
*/
explicit
String(TFile &_file) :
file(NULL), _temporary(true), _ownFile(false), data_size(0), lastDiskPage(0), lastDiskPageSize(0)
{
open(*this, _file);
}
explicit
String(const char *fileName, int openMode = DefaultOpenMode<TFile>::VALUE) :
file(NULL), _temporary(true), _ownFile(false), data_size(0), lastDiskPage(0), lastDiskPageSize(0)
{
open(*this, fileName, openMode);
}
// copy the contents from another string
String(String const & source):
file(NULL), _temporary(true), _ownFile(false), data_size(0), lastDiskPage(0), lastDiskPageSize(0)
{
assign(*this, source);
}
template <typename TSource>
String(TSource const & source) :
file(NULL), _temporary(true), _ownFile(false), data_size(0), lastDiskPage(0), lastDiskPageSize(0)
{
assign(*this, source);
}
~String()
{
close(*this);
}
inline TValue & operator[] (TSize offset) {
TPageFrame &pf = getPage(offset / SEQAN_PAGESIZE);
pf.dirty = true;
return pf[offset % SEQAN_PAGESIZE];
}
inline TValue const & operator[] (TSize offset) const {
return const_cast<String*>(this)->getPage(offset / SEQAN_PAGESIZE)[offset % SEQAN_PAGESIZE];
}
template <typename TSource>
inline String & operator= (TSource const & source)
{
assign(*this, source);
return *this;
}
inline String & operator= (String const & source)
{
assign(*this, source);
return *this;
}
inline operator bool()
{
return file;
}
//////////////////////////////////////////////////////////////////////////////
// swapping interface
// when a page has to be swapped out and file is not open, open a temporary file
inline void _ensureFileIsOpen()
{
if (!file)
{
_temporary = true;
if (!(_ownFile = openTemp(file)))
std::cerr << "External String couldn't open temporary file" << std::endl;
}
}
// for debugging
void _dumpCache()
{
for(int i = 0; i < length(cache); ++i)
{
TPageFrame &pf = cache[i];
std::cerr << "[" << pf.pageNo << "]";
if (pf.dirty)
std::cerr << "*";
else
std::cerr << " ";
if (pf.status == READY)
std::cerr << " ";
else
std::cerr << ". ";
}
std::cerr << std::endl;
}
// return a priority for a page frame (the higher is more persistent)
inline typename TPageFrame::Priority getPriority(int /*pageNo*/) const
{
/* if (keepFirst && pageNo < (int)(length(cache)) - 10) // save 1 for random access
return TPageFrame::PERMANENT_LEVEL;
else*/
return TPageFrame::NORMAL_LEVEL;
}
// write page to disk if dirty and remove from page table now or after finishing IO
inline void flush(TPageFrame &pf)
{
if (pf.status == READY && pf.dirty) { // write if dirty and not i/o transferring
nukeCopies(pf.begin); // proceeding writes should wait and set dirty bit
if (pf.priority > TPageFrame::NORMAL_LEVEL && pf.priority <= TPageFrame::ITERATOR_LEVEL)
cache.upgrade(pf, TPageFrame::PREFETCH_LEVEL);
_ensureFileIsOpen();
if (pf.pageNo != (int)(data_size / (TSize)SEQAN_PAGESIZE))
writePage(pf, pf.pageNo, file);
else {
lastDiskPage = data_size / SEQAN_PAGESIZE;
lastDiskPageSize = data_size % SEQAN_PAGESIZE;
writeLastPage(pf, pf.pageNo, file, lastDiskPageSize);
}
pf.dataStatus = TPageFrame::ON_DISK;
}
}
// write page synchronously to disk if dirty and remove from page table
inline void swapOutAndWait(TPageFrame &pf)
{
nukeCopies(pf.begin); // proceeding writes should wait and set dirty bit
if (pf.status != READY)
{
pager[pf.pageNo] = TPageFrame::ON_DISK; // page is not dirty and on disk
bool waitResult = waitFor(pf); // after finishing I/O transfer
// TODO(weese): Throw an I/O exception
if (!waitResult)
SEQAN_FAIL("%s operation could not be completed: \"%s\"",
_pageFrameStatusString(pf.status),
strerror(errno));
pf.pageNo = -1; // cut back link
return;
}
if (pf.dirty) { // write if dirty
_ensureFileIsOpen();
if (pf.pageNo != (int)(data_size / (TSize)SEQAN_PAGESIZE)) {
writePage(pf, pf.pageNo, file);
if (pf.pageNo >= lastDiskPage)
lastDiskPage = -1; // make lastDiskPage(Size) invalid because file size is aligned
} else {
writeLastPage(pf, pf.pageNo, file, data_size % SEQAN_PAGESIZE);
lastDiskPage = data_size / SEQAN_PAGESIZE;
lastDiskPageSize = data_size % SEQAN_PAGESIZE;
}
pager[pf.pageNo] = TPageFrame::ON_DISK; // page is marked to be on disk
bool waitResult = waitFor(pf); // after finishing I/O transfer
// TODO(weese): Throw an I/O exception
if (!waitResult)
SEQAN_FAIL("%s operation could not be completed: \"%s\"",
_pageFrameStatusString(pf.status),
strerror(errno));
} else
pager[pf.pageNo] = pf.dataStatus; // restore original data status
pf.pageNo = -1; // cut back link
}
struct testIODone : public std::function<bool(TPageFrame&)>
{
String &me;
testIODone(String &_me): me(_me) {}
inline bool operator() (TPageFrame &pf)
{
PageFrameState oldStatus = pf.status;
bool inProgress;
bool waitResult = waitFor(pf, 0, inProgress);
// TODO(weese): Throw an I/O exception
if (!waitResult)
SEQAN_FAIL("%s operation could not be completed: \"%s\"",
_pageFrameStatusString(pf.status),
strerror(errno));
if (!inProgress && (oldStatus != READY))
{
if (pf.pageNo >= me.lastDiskPage)
me.lastDiskPage = -1; // make lastDiskPage(Size) invalid because file size is aligned
}
return !inProgress;
}
};
inline TPageFrame &getPage(
int pageNo,
typename TPageFrame::Priority maxLevel,
typename TPageFrame::Priority newLevel,
int prefetchPages)
{
int frameNo = pager[pageNo];
if (frameNo >= 0) // cache hit
{
TPageFrame &pf = cache[frameNo];
cache.upgrade(
pf,
_max(pf.priority, newLevel)); // update lru order
PageFrameState oldStatus = pf.status;
bool waitResult = waitFor(pf); // wait for I/O transfer to complete
// TODO(weese): Throw an I/O exception
if (!waitResult)
SEQAN_FAIL("%s operation could not be completed: \"%s\"",
_pageFrameStatusString(pf.status),
strerror(errno));
if (oldStatus != READY)
if (pf.pageNo >= lastDiskPage)
lastDiskPage = -1; // make lastDiskPage(Size) invalid because file size is aligned
if (prefetchPages > 0) prefetch(pageNo + 1, pageNo + 1 + prefetchPages, frameNo);
else if (prefetchPages < 0) prefetch(pageNo + prefetchPages, pageNo, frameNo);
return pf;
} else { // cache miss
typename TPageFrame::DataStatus dataStatus = static_cast<typename TPageFrame::DataStatus>(frameNo);
frameNo = cache.mru(testIODone(*this), maxLevel); // try to get an undirty and READY pageframe
if (frameNo < 0) // if there is none,
frameNo = cache.mruDirty(); // get the most recently used dirty frame
TPageFrame &pf = cache[frameNo];
// *** frame is choosen ***
if (pf.begin)
swapOutAndWait(pf); // write synchronously to disk, if page is dirty
else
allocPage(pf, file); // allocate memory if page is virgin
// *** frame is free now ***
pf.dataStatus = dataStatus;
if (dataStatus == TPageFrame::ON_DISK)
{
if (pageNo != lastDiskPage)
readPage(pageNo, pf, file);
else
readLastPage(pageNo, pf, file, lastDiskPageSize);
}
pager[pageNo] = frameNo; // assign new page to page table
pf.pageNo = pageNo; // set back link
cache.upgrade(
pf,
_max(getPriority(pageNo), newLevel)); // update lru order
if (prefetchPages > 0) prefetch(pageNo + 1, pageNo + 1 + prefetchPages, frameNo);
else if (prefetchPages < 0) prefetch(pageNo + prefetchPages, pageNo, frameNo);
bool waitResult = waitFor(pf); // wait for I/O transfer to complete
// TODO(weese): Throw an I/O exception
if (!waitResult)
SEQAN_FAIL("%s operation could not be completed: \"%s\"",
_pageFrameStatusString(pf.status),
strerror(errno));
return pf;
}
}
inline TPageFrame &getPage(int pageNo)
{
return getPage(pageNo, TPageFrame::NORMAL_LEVEL, TPageFrame::NORMAL_LEVEL, 0);
}
// prefetch is non-blocking and should speed up swapping
inline void prefetch(int pageBegin, int pageEnd, int except = -1)
{
if (!file) return;
if (pageBegin < 0) pageBegin = 0;
if (pageEnd >= (int)length(pager)) pageEnd = (int)length(pager) - 1;
for(int pageNo = pageBegin; pageNo < pageEnd; ++pageNo) {
int frameNo = pager[pageNo];
typename TPageFrame::DataStatus dataStatus = static_cast<typename TPageFrame::DataStatus>(frameNo);
if (dataStatus == TPageFrame::ON_DISK && // prefetch only if page is on disk
pageNo != lastDiskPage) // reading the last page is blocking
{
frameNo = cache.mru(
testIODone(*this),
TPageFrame::NORMAL_LEVEL); // choose undirty and ready page
if (frameNo < 0 || frameNo == except) return; // no lowlevel-page left for prefetching
TPageFrame &pf = cache[frameNo];
#ifdef SEQAN_VERBOSE
std::cerr << "prefetch: page " << pageNo << std::endl;
#endif
// *** frame is choosen ***
if (pf.begin)
swapOutAndWait(pf); // write synchronously to disk, if page is dirty
else
allocPage(pf, file); // allocate memory if page is virgin
// *** frame is free now ***
pf.dataStatus = dataStatus;
readPage(pageNo, pf, file);
pager[pageNo] = frameNo; // assign new page to page table
pf.pageNo = pageNo; // set back link
cache.upgrade(pf, TPageFrame::PREFETCH_LEVEL); // update lru order
}
}
}
template < typename T >
inline static int _prefetchIffAsync(int /*prefetchPages*/, T const &) {
return 0;
}
template < typename TSpec >
inline static int _prefetchIffAsync(int prefetchPages, File<Async<TSpec> > const &) {
return prefetchPages;
}
inline TPageFrame &getSharedPage(int pageNo, int prefetchPages = 0)
{
return getPage(
pageNo,
TPageFrame::PREFETCH_LEVEL,
TPageFrame::ITERATOR_LEVEL,
_prefetchIffAsync(prefetchPages, file));
}
inline void releasePage(int pageNo, bool writeThrough = false)
{
int frameNo = pager[pageNo];
if (frameNo >= 0) // release only cached pages
{
TPageFrame &pf = cache[frameNo];
if (pf.begin.isLonely() && pf.priority <= TPageFrame::ITERATOR_LEVEL)
{
cache.upgrade(pf, _max(getPriority(pageNo), TPageFrame::NORMAL_LEVEL));
if (writeThrough)
{
#ifdef SEQAN_VERBOSE
if (pf.dirty)
std::cerr << "writeThrough: page " << pageNo << std::endl;
#endif
flush(pf); // write if dirty
}
}
}
}
inline void rename(unsigned frameNo)
{
TPageFrame &pf = cache[frameNo];
cache.rename(frameNo); // update lru entry
if (pf.pageNo >= 0)
pager[pf.pageNo] = frameNo; // update back link
}
// change the number of in-mem pageframes
// more pages mean less swapping,
// less pages mean more free mem
inline void resizeCache(unsigned newFrames)
{
unsigned oldFrames = length(cache);
if (data_size)
newFrames = _min(newFrames, (unsigned) enclosingBlocks(data_size, (unsigned)SEQAN_PAGESIZE));
if (newFrames < oldFrames) {
flush(*this);
for(unsigned i = newFrames; i < oldFrames; ++i) {
int frameNo = cache.mruDirty(); // get the most recently used frame (can be dirty)
if (frameNo < 0) break;
TPageFrame &pf = cache[frameNo];
// *** frame is choosen ***
if (pf.begin) {
swapOutAndWait(pf); // write synchronously to disk, if page is dirty
freePage(pf, file); // free memory
}
cache.erase(frameNo); // erase page frame from cache
for(unsigned j = frameNo; j < length(cache); ++j)
rename(j); // update remaining pages
}
} else if (oldFrames < newFrames) {
resize(cache, newFrames);
}
}
};
//////////////////////////////////////////////////////////////////////////////
// meta-function interface
template < typename TValue, typename TConfig >
struct Size< String<TValue, External<TConfig> > >
{
//IOREV
typedef typename String<TValue, External<TConfig> >::TSize Type;
};
template < typename TValue, typename TConfig >
struct Difference< String<TValue, External<TConfig> > >
{
//IOREV
typedef typename MakeSigned_<typename String<TValue, External<TConfig> >::TSize>::Type Type;
};
template < typename TValue, typename TConfig, typename TSpec >
struct Iterator< String<TValue, External<TConfig> > const, Tag<TSpec> const >
{
//IOREV
typedef ExtStringFwdConstIterator< String<TValue, External<TConfig> > > Type;
};
template < typename TValue, typename TConfig, typename TSpec >
struct Iterator< String<TValue, External<TConfig> >, Tag<TSpec> const >
{
//IOREV
typedef ExtStringFwdIterator< String<TValue, External<TConfig> > > Type;
};
//____________________________________________________________________________
template < typename TValue, typename TConfig >
struct DefaultOverflowExplicit<String<TValue, External<TConfig> > >
{
//IOREV
typedef Generous Type;
};
template < typename TValue, typename TConfig >
struct DefaultOverflowImplicit<String<TValue, External<TConfig> > >
{
//IOREV
typedef Generous Type;
};
//____________________________________________________________________________
/*
template < typename TValue, typename TConfig >
struct DefaultIteratorSpec< String<TValue, External<TConfig> > > {
typedef Standard Type;
};
template < typename TValue, typename TConfig >
struct DefaultIteratorSpec< String<TValue, External<TConfig> > const > {
typedef Standard Type;
};
*/
template < typename TValue, typename TConfig >
struct AllowsFastRandomAccess< String<TValue, External<TConfig> > >
{
//IOREV
typedef False Type;
enum { VALUE = false };
};
//////////////////////////////////////////////////////////////////////////////
// global interface
//____________________________________________________________________________
template < typename TValue, typename TConfig >
inline void
clear(String<TValue, External<TConfig> > &me)
{
//IOREV
// clear(me.pager);
resize(me, 0);
}
//____________________________________________________________________________
// wait until IO of every page is finished
template < typename TValue, typename TConfig >
inline void
waitForAll(String<TValue, External<TConfig> > &me)
{
//IOREV _nodoc_
typedef typename String<TValue, External<TConfig> >::TCache TCache;
typedef typename Iterator<TCache, Standard>::Type TIter;
TIter f = begin(me.cache, Standard());
TIter fEnd = end(me.cache, Standard());
for(; f != fEnd ; ++f)
{
bool waitResult = waitFor(*f); // wait for I/O transfer to complete
if (!waitResult)
SEQAN_FAIL("%s operation could not be completed: \"%s\"",
_pageFrameStatusString(f->status),
strerror(errno));
}
}
/*!
* @fn ExternalString#flush
* @brief Waits for all open read/write requests to complete.
*
* @signature void flush(str);
*
* @param[in,out] str The ExternalString to flush.
*/
template < typename TValue, typename TConfig >
inline void
flush(String<TValue, External<TConfig> > &me)
{
//IOREV _doc_
typedef typename String<TValue, External<TConfig> >::TCache TCache;
typedef typename Iterator<TCache, Standard>::Type TIter;
// write all dirty pages to disk
if (me.file)
{
TIter f = begin(me.cache, Standard());
TIter fEnd = end(me.cache, Standard());
for(; f != fEnd ; ++f)
if ((*f).begin) me.flush(*f);
waitForAll(me);
}
}
// cancel all transactions
template < typename TValue, typename TConfig >
inline void
cancel(String<TValue, External<TConfig> > &me)
{
//IOREV _nodoc_
typedef typename String<TValue, External<TConfig> >::TCache TCache;
typedef typename Iterator<TCache, Standard>::Type TIter;
if (me.file)
{
TIter f = begin(me.cache, Standard());
TIter fEnd = end(me.cache, Standard());
for(; f != fEnd ; ++f)
if ((*f).begin) cancel(*f, me.file);
}
}
// cancel all transactions and free allocated pages
template < typename TValue, typename TConfig >
inline void
cancelAndFree(String<TValue, External<TConfig> > &me)
{
//IOREV _nodoc_
typedef String<TValue, External<TConfig> > TExtString;
typedef typename TExtString::TPageFrame TPageFrame;
typedef typename String<TValue, External<TConfig> >::TCache TCache;
typedef typename Iterator<TCache, Standard>::Type TIter;
TIter f = begin(me.cache, Standard());
TIter fEnd = end(me.cache, Standard());
for(; f != fEnd ; ++f)
{
if ((me.file) && (*f).begin) cancel(*f, me.file);
if ((*f).pageNo >= 0)
{
me.pager[(*f).pageNo] = (*f).dataStatus;
(*f).pageNo = TPageFrame::UNINITIALIZED;
}
// std::cerr << *f << std::endl;
if ((*f).begin)
freePage(*f, me.file);
}
}
// flush and free all allocated pages
template < typename TValue, typename TConfig >
inline void
flushAndFree(String<TValue, External<TConfig> > &me)
{
//IOREV _nodoc_
typedef String<TValue, External<TConfig> > TExtString;
typedef typename TExtString::TPageFrame TPageFrame;
typedef typename TExtString::TCache TCache;
typedef typename Iterator<TCache, Standard>::Type TIter;
flush(me);
TIter f = begin(me.cache, Standard());
TIter fEnd = end(me.cache, Standard());
for(; f != fEnd ; ++f)
{
if (f->pageNo >= 0)
{
me.pager[f->pageNo] = f->dataStatus;
f->pageNo = TPageFrame::UNINITIALIZED;
}
// std::cerr << *f << std::endl;
if (f->begin) freePage(*f, me.file);
}
}
//____________________________________________________________________________
/*!
* @fn ExternalString#open
* @brief Open the ExternalString's underlying file from a path.
*
* @signature bool open(str, fileName[, openMode]);
*
* @param[in,out] str The ExternalString to open.
* @param[in] fileName Path to the file to open. Type: <tt>char const *</tt>.
* @param[in] openMode The open mode. Type: <tt>int</tt>.
*
* @return bool <tt>true</tt> if the operation succeeded and <tt>false</tt> otherwise.
*/
template < typename TValue, typename TConfig >
inline bool
open(String<TValue, External<TConfig> > &me, const char *fileName, int openMode)
{
//IOREV _doc_
typedef String<TValue, External<TConfig> > TExtString;
typedef typename TExtString::TPageFrame TPageFrame;
me._temporary = false;
if ((me._ownFile = open(me.file, fileName, openMode)))
me.data_size = length(me.file) / sizeof(TValue);
else
me.data_size = 0;
resize(me.pager, enclosingBlocks(me.data_size,
(unsigned)me.SEQAN_PAGESIZE), (me.data_size)?
TPageFrame::ON_DISK:
TPageFrame::UNINITIALIZED);
me.lastDiskPage = me.data_size / me.SEQAN_PAGESIZE;
me.lastDiskPageSize = me.data_size % me.SEQAN_PAGESIZE;
return me._ownFile;
}
template < typename TValue, typename TConfig >
inline bool
open(String<TValue, External<TConfig> > &me, const char *fileName)
{
//IOREV _doc_
typedef String<TValue, External<TConfig> > TExtString;
typedef typename TExtString::TFile TFile;
return open(me, fileName, DefaultOpenMode<TFile>::VALUE);
}
template < typename TValue, typename TConfig >
inline bool
open(String<TValue, External<TConfig> > &me, typename TConfig::TFile file)
{
//IOREV _doc_
typedef String<TValue, External<TConfig> > TExtString;
typedef typename TExtString::TPageFrame TPageFrame;
me.file = file;
me._temporary = false;
me._ownFile = false;
if (me.file)
me.data_size = length(me.file) / sizeof(TValue);
else
me.data_size = 0;
resize(me.pager, enclosingBlocks(me.data_size,
(unsigned)me.SEQAN_PAGESIZE), (me.data_size)?
TPageFrame::ON_DISK:
TPageFrame::UNINITIALIZED);
me.lastDiskPage = me.data_size / me.SEQAN_PAGESIZE;
me.lastDiskPageSize = me.data_size % me.SEQAN_PAGESIZE;
return me._file;
}
/*!
* @fn ExternalString#openTemp
* @brief Open an ExternalString using an temporary file.
*
* @signature bool openTemp(str);
*
* @param[in,out] str The ExternalString to open using temporary file.
*
* @return bool <tt>true</tt> if opening succeeded, <tt>false</tt> otherwise.
*/
template < typename TValue, typename TConfig >
inline bool
openTemp(String<TValue, External<TConfig> > &me)
{
//IOREV _doc_
me._temporary = true;
me.lastDiskPage = 0;
me.lastDiskPageSize = 0;
clear(me.pager);
return me._ownFile = openTemp(me.file);
}
//____________________________________________________________________________
template < typename TValue, typename TConfig >
inline bool
save(String<TValue, External<TConfig> > const &/*me*/, const char * /*fileName*/, int /*openMode*/) {
//IOREV _nodoc_ shouldn't we flush here? in case of abnormal termination...
// External Strings are persistent, thus there is no need to save them
//ExtStringsDontNeedToBeSaved error;
return true;
}
template < typename TValue, typename TConfig >
inline bool
save(String<TValue, External<TConfig> > const &/*me*/, const char * /*fileName*/) {
//IOREV _nodoc_ shouldn't we flush here? in case of abnormal termination...
// External Strings are persistent, thus there is no need to save them
//ExtStringsDontNeedToBeSaved error;
return true;
}
template < typename TValue, typename TConfig >
inline bool
save(String<TValue, External<TConfig> > const &/*me*/, typename TConfig::TFile /*file*/) {
//IOREV _nodoc_ shouldn't we flush here? in case of abnormal termination...
// External Strings are persistent, thus there is no need to save them
//ExtStringsDontNeedToBeSaved error;
return true;
}
//____________________________________________________________________________
/*!
* @fn ExternalString#close
* @brief Close the external string.
*
* @signature bool close(str);
*
* @param[in] str The ExternalString to close the file of.
*
* @return bool <tt>true</tt> if the closing succeeded, <tt>false</tt> otherwise.
*/
template < typename TValue, typename TConfig >
inline bool
close(String<TValue, External<TConfig> > &me)
{
//IOREV _doc_
// close associated file
if (me._temporary)
cancelAndFree(me);
else
flushAndFree(me);
clear(me.pager);
if (me._ownFile)
{
me._ownFile = false;
return close(me.file);
}
else
return true;
}
//____________________________________________________________________________
template < typename TValue, typename TConfig >
inline typename Size< String<TValue, External<TConfig> > >::Type
length(String<TValue, External<TConfig> > const &me)
{
//IOREV
return me.data_size;
}
template < typename TValue, typename TConfig >
inline typename Size< String<TValue, External<TConfig> > >::Type
capacity(String<TValue, External<TConfig> > const &me)
{
//IOREV
typedef typename Size< String<TValue, External<TConfig> > >::Type TSize;
return (TSize)capacity(me.pager) * (TSize)me.SEQAN_PAGESIZE;
}
//____________________________________________________________________________
template < typename TValue, typename TConfig, typename TNewSize, typename TExpand >
inline typename Size< String<TValue, External<TConfig> > >::Type
resize(
String<TValue, External<TConfig> > &me,
TNewSize new_length,
Tag<TExpand> expand)
{
//IOREV
typedef String<TValue, External<TConfig> > TString;
typedef typename TString::TPageFrame TPageFrame;
typedef typename Size<TString>::Type TSize;
typedef typename TString::TCache TCache;
typedef typename Iterator<TCache, Standard>::Type TIter;
resize(me.pager, enclosingBlocks(new_length, (unsigned)me.SEQAN_PAGESIZE), TPageFrame::UNINITIALIZED, expand);
if ((TSize)new_length < me.data_size)
{
TIter f = begin(me.cache, Standard());
TIter fEnd = end(me.cache, Standard());
// remove pages from cache that are behind our new file end
int new_pages = (new_length + me.SEQAN_PAGESIZE - 1) / me.SEQAN_PAGESIZE;
for(; f != fEnd ; ++f)
if (f->begin && f->pageNo >= new_pages)
{
if (f->status != READY)
cancel(*f, me.file);
clear(*f);
}
// before shrinking the file size
me.lastDiskPage = new_length / me.SEQAN_PAGESIZE;
me.lastDiskPageSize = new_length % me.SEQAN_PAGESIZE;
if (me.file)
resize(me.file, (TSize)new_length * (TSize)sizeof(TValue));
}
me.data_size = new_length;
return length(me);
}
//____________________________________________________________________________
template < typename TValue, typename TConfig, typename TSize, typename TExpand >
inline typename Size< String<TValue, External<TConfig> > >::Type
reserve(
String<TValue, External<TConfig> > &me,
TSize new_capacity,
Tag<TExpand> expand)
{
//IOREV
reserve(me.pager, enclosingBlocks(new_capacity, (unsigned)me.SEQAN_PAGESIZE), expand);
return capacity(me);
}
//____________________________________________________________________________
template < typename TValue, typename TConfig, typename TSpec >
inline typename Iterator<String<TValue, External<TConfig> >, Tag<TSpec> const>::Type
begin(String<TValue, External<TConfig> > &me, Tag<TSpec> const)
{
//IOREV
typedef String<TValue, External<TConfig> > TString;
return typename Iterator<TString, Tag<TSpec> const>::Type (&me, 0);
}
template < typename TValue, typename TConfig, typename TSpec >
inline typename Iterator<String<TValue, External<TConfig> > const, Tag<TSpec> const>::Type
begin(String<TValue, External<TConfig> > const &me, Tag<TSpec> const)
{
//IOREV
typedef String<TValue, External<TConfig> > TString;
return typename Iterator<TString const, Tag<TSpec> const>::Type (const_cast<TString*>(&me), 0);
}
template < typename TValue, typename TConfig, typename TSpec >
inline typename Iterator<String<TValue, External<TConfig> >, Tag<TSpec> const>::Type
end(String<TValue, External<TConfig> > &me, Tag<TSpec> const)
{
//IOREV
typedef String<TValue, External<TConfig> > TString;
return typename Iterator<TString, Tag<TSpec> const>::Type (&me, length(me));
}
template < typename TValue, typename TConfig, typename TSpec >
inline typename Iterator<String<TValue, External<TConfig> > const, Tag<TSpec> const>::Type
end(String<TValue, External<TConfig> > const &me, Tag<TSpec> const)
{
//IOREV
typedef String<TValue, External<TConfig> > TString;
return typename Iterator<TString const, Tag<TSpec> const>::Type (const_cast<TString*>(&me), length(me));
}
//____________________________________________________________________________
template < typename TValue, typename TConfig, typename TPos >
inline typename Reference<String<TValue, External<TConfig> > >::Type
value(String<TValue, External<TConfig> > &me, TPos pos)
{
//IOREV
return me[pos];
}
template < typename TValue, typename TConfig, typename TPos >
inline typename Reference<String<TValue, External<TConfig> > const>::Type
value(String<TValue, External<TConfig> > const &me, TPos pos)
{
//IOREV
return me[pos];
}
//____________________________________________________________________________
template < typename TTargetValue, typename TConfig, typename TValue, typename TExpand >
inline void
appendValue(String<TTargetValue, External<TConfig> > &me,
TValue && value,
Tag<TExpand> expand)
{
resize(me, me.data_size + 1, expand);
back(me) = std::forward<TValue>(value);
}
//____________________________________________________________________________
template < typename TValue, typename TConfig, typename TSource, typename TExpand >
inline void
append(String<TValue, External<TConfig> > &target,
TSource const &source,
Tag<TExpand> expand)
{
//IOREV doc says, resize() my invalidate iterators, therefore it_target might be bogus
typedef String<TValue, External<TConfig> > TTarget;
typedef typename Iterator<TSource const, Standard>::Type ISource;
typedef typename Iterator<TTarget, Standard>::Type ITarget;
ITarget it_target = end(target, Standard());
resize(target, length(target) + length(source), expand);
ISource it_source = begin(source, Standard());
ISource it_source_end = end(source, Standard());
for (; it_source != it_source_end; ++it_source, ++it_target)
*it_target = *it_source;
}
template < typename TValue, typename TConfig, typename TSourceValue, typename TExpand >
inline void
append(String<TValue, External<TConfig> > &target,
TSourceValue * source,
Tag<TExpand> expand)
{
//IOREV doc says, resize() my invalidate iterators, therefore it_target might be bogus
typedef String<TValue, External<TConfig> > TTarget;
typedef typename Iterator<TSourceValue *, Standard>::Type ISource;
typedef typename Iterator<TTarget, Standard>::Type ITarget;
ITarget it_target = end(target, Standard());
resize(target, length(target) + length(source), expand);
ISource it_source = begin(source, Standard());
ISource it_source_end = end(source, Standard());
for (; it_source != it_source_end; ++it_source, ++it_target)
*it_target = *it_source;
}
/*
template < typename TSpec >
std::ostream& operator<<(std::ostream &out, String<char, TSpec > &p) {
typename Iterator< String<char, TSpec > >::Type _cur = begin(p), _end = end(p);
while (_cur != _end) {
out << *_cur;
++_cur;
}
return out;
}
template < typename TValue, typename TSpec >
std::ostream& operator<<(std::ostream &out, String<TValue, TSpec > &p) {
typename Iterator< String<TValue, TSpec > >::Type _cur = begin(p), _end = end(p);
while (_cur != _end) {
out << *_cur << " ";
++_cur;
}
return out;
}
*/
//____________________________________________________________________________
// sequence -> external string
template < typename TValue,
typename TConfig,
typename TSource,
typename TExpand >
inline void assign(
String<TValue, External<TConfig> > &target,
TSource const &source,
Tag<TExpand>)
{
//IOREV
typedef String<TValue, External<TConfig> > TTarget;
typedef typename Iterator<TSource const, Standard>::Type ISource;
typedef typename Iterator<TTarget, Standard>::Type ITarget;
resize(target, length(source));
ISource it_source = begin(source, Standard());
ISource it_source_end = end(source, Standard());
ITarget it_target = begin(target, Standard());
for (; it_source != it_source_end; ++it_source, ++it_target)
*it_target = *it_source;
}
/*
template < typename TValue,
typename TConfig,
typename TSourceValue,
typename TExpand >
inline void assign(
String<TValue, External<TConfig> > &target,
TSourceValue const * source,
Tag<TExpand>)
{
//IOREV
typedef String<TValue, External<TConfig> > TTarget;
typedef typename Iterator<TSourceValue *, Standard>::Type ISource;
typedef typename Iterator<TTarget, Standard>::Type ITarget;
resize(target, length(source));
ISource it_source = begin(source, Standard());
ISource it_source_end = end(source, Standard());
ITarget it_target = begin(target, Standard());
for (; it_source != it_source_end; ++it_source, ++it_target)
*it_target = *it_source;
}
*/
//____________________________________________________________________________
template < typename TValue, typename TConfig >
inline void const *
getObjectId(String<TValue, External<TConfig> > const &me)
{
//IOREV
return &(*begin(me.pager));
}
}
#endif
|