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 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <dptabres.hxx>
#include <dptabdat.hxx>
#include <dptabsrc.hxx>
#include <global.hxx>
#include <subtotal.hxx>
#include <globstr.hrc>
#include <scresid.hxx>
#include <dpitemdata.hxx>
#include <generalfunction.hxx>
#include <document.hxx>
#include <dpresfilter.hxx>
#include <dputil.hxx>
#include <o3tl/safeint.hxx>
#include <osl/diagnose.h>
#include <rtl/math.hxx>
#include <sal/log.hxx>
#include <math.h>
#include <float.h>
#include <algorithm>
#include <memory>
#include <unordered_map>
#include <com/sun/star/sheet/DataResultFlags.hpp>
#include <com/sun/star/sheet/MemberResultFlags.hpp>
#include <com/sun/star/sheet/DataPilotFieldReferenceType.hpp>
#include <com/sun/star/sheet/DataPilotFieldReferenceItemType.hpp>
#include <com/sun/star/sheet/DataPilotFieldShowItemsMode.hpp>
#include <com/sun/star/sheet/DataPilotFieldSortMode.hpp>
#include <com/sun/star/sheet/GeneralFunction2.hpp>
using namespace com::sun::star;
using ::std::vector;
using ::std::pair;
using ::com::sun::star::uno::Sequence;
namespace {
const char* aFuncStrIds[] = // matching enum ScSubTotalFunc
{
nullptr, // SUBTOTAL_FUNC_NONE
STR_FUN_TEXT_AVG, // SUBTOTAL_FUNC_AVE
STR_FUN_TEXT_COUNT, // SUBTOTAL_FUNC_CNT
STR_FUN_TEXT_COUNT, // SUBTOTAL_FUNC_CNT2
STR_FUN_TEXT_MAX, // SUBTOTAL_FUNC_MAX
STR_FUN_TEXT_MIN, // SUBTOTAL_FUNC_MIN
STR_FUN_TEXT_PRODUCT, // SUBTOTAL_FUNC_PROD
STR_FUN_TEXT_STDDEV, // SUBTOTAL_FUNC_STD
STR_FUN_TEXT_STDDEV, // SUBTOTAL_FUNC_STDP
STR_FUN_TEXT_SUM, // SUBTOTAL_FUNC_SUM
STR_FUN_TEXT_VAR, // SUBTOTAL_FUNC_VAR
STR_FUN_TEXT_VAR, // SUBTOTAL_FUNC_VARP
STR_FUN_TEXT_MEDIAN, // SUBTOTAL_FUNC_MED
nullptr // SUBTOTAL_FUNC_SELECTION_COUNT - not used for pivot table
};
bool lcl_SearchMember( const std::vector<std::unique_ptr<ScDPResultMember>>& list, SCROW nOrder, SCROW& rIndex)
{
bool bFound = false;
SCROW nLo = 0;
SCROW nHi = list.size() - 1;
SCROW nIndex;
while (nLo <= nHi)
{
nIndex = (nLo + nHi) / 2;
if ( list[nIndex]->GetOrder() < nOrder )
nLo = nIndex + 1;
else
{
nHi = nIndex - 1;
if ( list[nIndex]->GetOrder() == nOrder )
{
bFound = true;
nLo = nIndex;
}
}
}
rIndex = nLo;
return bFound;
}
class FilterStack
{
std::vector<ScDPResultFilter>& mrFilters;
public:
explicit FilterStack(std::vector<ScDPResultFilter>& rFilters) : mrFilters(rFilters) {}
void pushDimName(const OUString& rName, bool bDataLayout)
{
mrFilters.emplace_back(rName, bDataLayout);
}
void pushDimValue(const OUString& rValueName, const OUString& rValue)
{
ScDPResultFilter& rFilter = mrFilters.back();
rFilter.maValueName = rValueName;
rFilter.maValue = rValue;
rFilter.mbHasValue = true;
}
~FilterStack()
{
ScDPResultFilter& rFilter = mrFilters.back();
if (rFilter.mbHasValue)
rFilter.mbHasValue = false;
else
mrFilters.pop_back();
}
};
// function objects for sorting of the column and row members:
class ScDPRowMembersOrder
{
ScDPResultDimension& rDimension;
long nMeasure;
bool bAscending;
public:
ScDPRowMembersOrder( ScDPResultDimension& rDim, long nM, bool bAsc ) :
rDimension(rDim),
nMeasure(nM),
bAscending(bAsc)
{}
bool operator()( sal_Int32 nIndex1, sal_Int32 nIndex2 ) const;
};
class ScDPColMembersOrder
{
ScDPDataDimension& rDimension;
long nMeasure;
bool bAscending;
public:
ScDPColMembersOrder( ScDPDataDimension& rDim, long nM, bool bAsc ) :
rDimension(rDim),
nMeasure(nM),
bAscending(bAsc)
{}
bool operator()( sal_Int32 nIndex1, sal_Int32 nIndex2 ) const;
};
}
static bool lcl_IsLess( const ScDPDataMember* pDataMember1, const ScDPDataMember* pDataMember2, long nMeasure, bool bAscending )
{
// members can be NULL if used for rows
ScDPSubTotalState aEmptyState;
const ScDPAggData* pAgg1 = pDataMember1 ? pDataMember1->GetConstAggData( nMeasure, aEmptyState ) : nullptr;
const ScDPAggData* pAgg2 = pDataMember2 ? pDataMember2->GetConstAggData( nMeasure, aEmptyState ) : nullptr;
bool bError1 = pAgg1 && pAgg1->HasError();
bool bError2 = pAgg2 && pAgg2->HasError();
if ( bError1 )
return false; // errors are always sorted at the end
else if ( bError2 )
return true; // errors are always sorted at the end
else
{
double fVal1 = ( pAgg1 && pAgg1->HasData() ) ? pAgg1->GetResult() : 0.0; // no data is sorted as 0
double fVal2 = ( pAgg2 && pAgg2->HasData() ) ? pAgg2->GetResult() : 0.0;
// compare values
// don't have to check approxEqual, as this is the only sort criterion
return bAscending ? ( fVal1 < fVal2 ) : ( fVal1 > fVal2 );
}
}
static bool lcl_IsEqual( const ScDPDataMember* pDataMember1, const ScDPDataMember* pDataMember2, long nMeasure )
{
// members can be NULL if used for rows
ScDPSubTotalState aEmptyState;
const ScDPAggData* pAgg1 = pDataMember1 ? pDataMember1->GetConstAggData( nMeasure, aEmptyState ) : nullptr;
const ScDPAggData* pAgg2 = pDataMember2 ? pDataMember2->GetConstAggData( nMeasure, aEmptyState ) : nullptr;
bool bError1 = pAgg1 && pAgg1->HasError();
bool bError2 = pAgg2 && pAgg2->HasError();
if ( bError1 )
{
if ( bError2 )
return true; // equal
else
return false;
}
else if ( bError2 )
return false;
else
{
double fVal1 = ( pAgg1 && pAgg1->HasData() ) ? pAgg1->GetResult() : 0.0; // no data is sorted as 0
double fVal2 = ( pAgg2 && pAgg2->HasData() ) ? pAgg2->GetResult() : 0.0;
// compare values
// this is used to find equal data at the end of the AutoShow range, so approxEqual must be used
return rtl::math::approxEqual( fVal1, fVal2 );
}
}
bool ScDPRowMembersOrder::operator()( sal_Int32 nIndex1, sal_Int32 nIndex2 ) const
{
const ScDPResultMember* pMember1 = rDimension.GetMember(nIndex1);
const ScDPResultMember* pMember2 = rDimension.GetMember(nIndex2);
// make the hide item to the largest order.
if ( !pMember1->IsVisible() || !pMember2->IsVisible() )
return pMember1->IsVisible();
const ScDPDataMember* pDataMember1 = pMember1->GetDataRoot() ;
const ScDPDataMember* pDataMember2 = pMember2->GetDataRoot();
// GetDataRoot can be NULL if there was no data.
// IsVisible == false can happen after AutoShow.
return lcl_IsLess( pDataMember1, pDataMember2, nMeasure, bAscending );
}
bool ScDPColMembersOrder::operator()( sal_Int32 nIndex1, sal_Int32 nIndex2 ) const
{
const ScDPDataMember* pDataMember1 = rDimension.GetMember(nIndex1);
const ScDPDataMember* pDataMember2 = rDimension.GetMember(nIndex2);
bool bHide1 = pDataMember1 && !pDataMember1->IsVisible();
bool bHide2 = pDataMember2 && !pDataMember2->IsVisible();
if ( bHide1 || bHide2 )
return !bHide1;
return lcl_IsLess( pDataMember1, pDataMember2, nMeasure, bAscending );
}
ScDPInitState::Member::Member(long nSrcIndex, SCROW nNameIndex) :
mnSrcIndex(nSrcIndex), mnNameIndex(nNameIndex) {}
void ScDPInitState::AddMember( long nSourceIndex, SCROW nMember )
{
maMembers.emplace_back(nSourceIndex, nMember);
}
void ScDPInitState::RemoveMember()
{
OSL_ENSURE(!maMembers.empty(), "ScDPInitState::RemoveMember: Attempt to remove member while empty.");
if (!maMembers.empty())
maMembers.pop_back();
}
namespace {
#if DUMP_PIVOT_TABLE
void dumpRow(
const OUString& rType, const OUString& rName, const ScDPAggData* pAggData,
ScDocument* pDoc, ScAddress& rPos )
{
SCCOL nCol = rPos.Col();
SCROW nRow = rPos.Row();
SCTAB nTab = rPos.Tab();
pDoc->SetString( nCol++, nRow, nTab, rType );
pDoc->SetString( nCol++, nRow, nTab, rName );
while ( pAggData )
{
pDoc->SetValue( nCol++, nRow, nTab, pAggData->GetResult() );
pAggData = pAggData->GetExistingChild();
}
rPos.SetRow( nRow + 1 );
}
void indent( ScDocument* pDoc, SCROW nStartRow, const ScAddress& rPos )
{
SCCOL nCol = rPos.Col();
SCTAB nTab = rPos.Tab();
OUString aString;
for (SCROW nRow = nStartRow; nRow < rPos.Row(); nRow++)
{
aString = pDoc->GetString(nCol, nRow, nTab);
if (!aString.isEmpty())
{
aString = " " + aString;
pDoc->SetString( nCol, nRow, nTab, aString );
}
}
}
#endif
}
ScDPRunningTotalState::ScDPRunningTotalState( ScDPResultMember* pColRoot, ScDPResultMember* pRowRoot ) :
pColResRoot(pColRoot), pRowResRoot(pRowRoot)
{
// These arrays should never be empty as the terminating value must be present at all times.
maColVisible.push_back(-1);
maColSorted.push_back(-1);
maRowVisible.push_back(-1);
maRowSorted.push_back(-1);
}
void ScDPRunningTotalState::AddColIndex( long nVisible, long nSorted )
{
maColVisible.back() = nVisible;
maColVisible.push_back(-1);
maColSorted.back() = nSorted;
maColSorted.push_back(-1);
}
void ScDPRunningTotalState::AddRowIndex( long nVisible, long nSorted )
{
maRowVisible.back() = nVisible;
maRowVisible.push_back(-1);
maRowSorted.back() = nSorted;
maRowSorted.push_back(-1);
}
void ScDPRunningTotalState::RemoveColIndex()
{
OSL_ENSURE(!maColVisible.empty() && !maColSorted.empty(), "ScDPRunningTotalState::RemoveColIndex: array is already empty!");
if (maColVisible.size() >= 2)
{
maColVisible.pop_back();
maColVisible.back() = -1;
}
if (maColSorted.size() >= 2)
{
maColSorted.pop_back();
maColSorted.back() = -1;
}
}
void ScDPRunningTotalState::RemoveRowIndex()
{
OSL_ENSURE(!maRowVisible.empty() && !maRowSorted.empty(), "ScDPRunningTotalState::RemoveRowIndex: array is already empty!");
if (maRowVisible.size() >= 2)
{
maRowVisible.pop_back();
maRowVisible.back() = -1;
}
if (maRowSorted.size() >= 2)
{
maRowSorted.pop_back();
maRowSorted.back() = -1;
}
}
ScDPRelativePos::ScDPRelativePos( long nBase, long nDir ) :
nBasePos( nBase ),
nDirection( nDir )
{
}
void ScDPAggData::Update( const ScDPValue& rNext, ScSubTotalFunc eFunc, const ScDPSubTotalState& rSubState )
{
if (nCount<0) // error?
return; // nothing more...
if (rNext.meType == ScDPValue::Empty)
return;
if ( rSubState.eColForce != SUBTOTAL_FUNC_NONE && rSubState.eRowForce != SUBTOTAL_FUNC_NONE &&
rSubState.eColForce != rSubState.eRowForce )
return;
if ( rSubState.eColForce != SUBTOTAL_FUNC_NONE ) eFunc = rSubState.eColForce;
if ( rSubState.eRowForce != SUBTOTAL_FUNC_NONE ) eFunc = rSubState.eRowForce;
if ( eFunc == SUBTOTAL_FUNC_NONE )
return;
if ( eFunc != SUBTOTAL_FUNC_CNT2 ) // CNT2 counts everything, incl. strings and errors
{
if (rNext.meType == ScDPValue::Error)
{
nCount = -1; // -1 for error (not for CNT2)
return;
}
if (rNext.meType == ScDPValue::String)
return; // ignore
}
++nCount; // for all functions
switch (eFunc)
{
case SUBTOTAL_FUNC_SUM:
case SUBTOTAL_FUNC_AVE:
if ( !SubTotal::SafePlus( fVal, rNext.mfValue ) )
nCount = -1; // -1 for error
break;
case SUBTOTAL_FUNC_PROD:
if ( nCount == 1 ) // copy first value (fVal is initialized to 0)
fVal = rNext.mfValue;
else if ( !SubTotal::SafeMult( fVal, rNext.mfValue ) )
nCount = -1; // -1 for error
break;
case SUBTOTAL_FUNC_CNT:
case SUBTOTAL_FUNC_CNT2:
// nothing more than incrementing nCount
break;
case SUBTOTAL_FUNC_MAX:
if ( nCount == 1 || rNext.mfValue > fVal )
fVal = rNext.mfValue;
break;
case SUBTOTAL_FUNC_MIN:
if ( nCount == 1 || rNext.mfValue < fVal )
fVal = rNext.mfValue;
break;
case SUBTOTAL_FUNC_STD:
case SUBTOTAL_FUNC_STDP:
case SUBTOTAL_FUNC_VAR:
case SUBTOTAL_FUNC_VARP:
maWelford.update( rNext.mfValue);
break;
case SUBTOTAL_FUNC_MED:
{
auto aIter = std::upper_bound(mSortedValues.begin(), mSortedValues.end(), rNext.mfValue);
if (aIter == mSortedValues.end())
mSortedValues.push_back(rNext.mfValue);
else
mSortedValues.insert(aIter, rNext.mfValue);
}
break;
default:
OSL_FAIL("invalid function");
}
}
void ScDPAggData::Calculate( ScSubTotalFunc eFunc, const ScDPSubTotalState& rSubState )
{
// calculate the original result
// (without reference value, used as the basis for reference value calculation)
// called several times at the cross-section of several subtotals - don't calculate twice then
if ( IsCalculated() )
return;
if ( rSubState.eColForce != SUBTOTAL_FUNC_NONE ) eFunc = rSubState.eColForce;
if ( rSubState.eRowForce != SUBTOTAL_FUNC_NONE ) eFunc = rSubState.eRowForce;
if ( eFunc == SUBTOTAL_FUNC_NONE ) // this happens when there is no data dimension
{
nCount = SC_DPAGG_RESULT_EMPTY; // make sure there's a valid state for HasData etc.
return;
}
// check the error conditions for the selected function
bool bError = false;
switch (eFunc)
{
case SUBTOTAL_FUNC_SUM:
case SUBTOTAL_FUNC_PROD:
case SUBTOTAL_FUNC_CNT:
case SUBTOTAL_FUNC_CNT2:
bError = ( nCount < 0 ); // only real errors
break;
case SUBTOTAL_FUNC_AVE:
case SUBTOTAL_FUNC_MED:
case SUBTOTAL_FUNC_MAX:
case SUBTOTAL_FUNC_MIN:
bError = ( nCount <= 0 ); // no data is an error
break;
case SUBTOTAL_FUNC_STDP:
case SUBTOTAL_FUNC_VARP:
bError = ( nCount <= 0 ); // no data is an error
assert(bError || nCount == static_cast<sal_Int64>(maWelford.getCount()));
break;
case SUBTOTAL_FUNC_STD:
case SUBTOTAL_FUNC_VAR:
bError = ( nCount < 2 ); // need at least 2 values
assert(bError || nCount == static_cast<sal_Int64>(maWelford.getCount()));
break;
default:
OSL_FAIL("invalid function");
}
// calculate the selected function
double fResult = 0.0;
if ( !bError )
{
switch (eFunc)
{
case SUBTOTAL_FUNC_MAX:
case SUBTOTAL_FUNC_MIN:
case SUBTOTAL_FUNC_SUM:
case SUBTOTAL_FUNC_PROD:
// different error conditions are handled above
fResult = fVal;
break;
case SUBTOTAL_FUNC_CNT:
case SUBTOTAL_FUNC_CNT2:
fResult = nCount;
break;
case SUBTOTAL_FUNC_AVE:
if ( nCount > 0 )
fResult = fVal / static_cast<double>(nCount);
break;
case SUBTOTAL_FUNC_STD:
if ( nCount >= 2 )
{
fResult = maWelford.getVarianceSample();
if (fResult < 0.0)
bError = true;
else
fResult = sqrt( fResult);
}
break;
case SUBTOTAL_FUNC_VAR:
if ( nCount >= 2 )
fResult = maWelford.getVarianceSample();
break;
case SUBTOTAL_FUNC_STDP:
if ( nCount > 0 )
{
fResult = maWelford.getVariancePopulation();
if (fResult < 0.0)
bError = true;
else
fResult = sqrt( fResult);
}
break;
case SUBTOTAL_FUNC_VARP:
if ( nCount > 0 )
fResult = maWelford.getVariancePopulation();
break;
case SUBTOTAL_FUNC_MED:
{
size_t nSize = mSortedValues.size();
if (nSize > 0)
{
assert(nSize == static_cast<size_t>(nCount));
if ((nSize % 2) == 1)
fResult = mSortedValues[nSize / 2];
else
fResult = (mSortedValues[nSize / 2 - 1] + mSortedValues[nSize / 2]) / 2.0;
}
}
break;
default:
OSL_FAIL("invalid function");
}
}
bool bEmpty = ( nCount == 0 ); // no data
// store the result
// Empty is checked first, so empty results are shown empty even for "average" etc.
// If these results should be treated as errors in reference value calculations,
// a separate state value (EMPTY_ERROR) is needed.
// Now, for compatibility, empty "average" results are counted as 0.
if ( bEmpty )
nCount = SC_DPAGG_RESULT_EMPTY;
else if ( bError )
nCount = SC_DPAGG_RESULT_ERROR;
else
nCount = SC_DPAGG_RESULT_VALID;
if ( bEmpty || bError )
fResult = 0.0; // default, in case the state is later modified
fVal = fResult; // used directly from now on
fAux = 0.0; // used for running total or original result of reference value
}
bool ScDPAggData::IsCalculated() const
{
return ( nCount <= SC_DPAGG_RESULT_EMPTY );
}
double ScDPAggData::GetResult() const
{
assert( IsCalculated() && "ScDPAggData not calculated" );
return fVal; // use calculated value
}
bool ScDPAggData::HasError() const
{
assert( IsCalculated() && "ScDPAggData not calculated" );
return ( nCount == SC_DPAGG_RESULT_ERROR );
}
bool ScDPAggData::HasData() const
{
assert( IsCalculated() && "ScDPAggData not calculated" );
return ( nCount != SC_DPAGG_RESULT_EMPTY ); // values or error
}
void ScDPAggData::SetResult( double fNew )
{
assert( IsCalculated() && "ScDPAggData not calculated" );
fVal = fNew; // don't reset error flag
}
void ScDPAggData::SetError()
{
assert( IsCalculated() && "ScDPAggData not calculated" );
nCount = SC_DPAGG_RESULT_ERROR;
}
void ScDPAggData::SetEmpty( bool bSet )
{
assert( IsCalculated() && "ScDPAggData not calculated" );
if ( bSet )
nCount = SC_DPAGG_RESULT_EMPTY;
else
nCount = SC_DPAGG_RESULT_VALID;
}
double ScDPAggData::GetAuxiliary() const
{
// after Calculate, fAux is used as auxiliary value for running totals and reference values
assert( IsCalculated() && "ScDPAggData not calculated" );
return fAux;
}
void ScDPAggData::SetAuxiliary( double fNew )
{
// after Calculate, fAux is used as auxiliary value for running totals and reference values
assert( IsCalculated() && "ScDPAggData not calculated" );
fAux = fNew;
}
ScDPAggData* ScDPAggData::GetChild()
{
if (!pChild)
pChild.reset( new ScDPAggData );
return pChild.get();
}
void ScDPAggData::Reset()
{
maWelford = WelfordRunner();
fVal = 0.0;
fAux = 0.0;
nCount = SC_DPAGG_EMPTY;
pChild.reset();
}
#if DUMP_PIVOT_TABLE
void ScDPAggData::Dump(int nIndent) const
{
std::string aIndent(nIndent*2, ' ');
std::cout << aIndent << "* ";
if (IsCalculated())
std::cout << GetResult();
else
std::cout << "not calculated";
std::cout << " [val=" << fVal << "; aux=" << fAux << "; count=" << nCount << "]" << std::endl;
}
#endif
ScDPRowTotals::ScDPRowTotals() :
bIsInColRoot( false )
{
}
ScDPRowTotals::~ScDPRowTotals()
{
}
static ScDPAggData* lcl_GetChildTotal( ScDPAggData* pFirst, long nMeasure )
{
OSL_ENSURE( nMeasure >= 0, "GetColTotal: no measure" );
ScDPAggData* pAgg = pFirst;
long nSkip = nMeasure;
// subtotal settings are ignored - column/row totals exist once per measure
for ( long nPos=0; nPos<nSkip; nPos++ )
pAgg = pAgg->GetChild(); // column total is constructed empty - children need to be created
if ( !pAgg->IsCalculated() )
{
// for first use, simulate an empty calculation
ScDPSubTotalState aEmptyState;
pAgg->Calculate( SUBTOTAL_FUNC_SUM, aEmptyState );
}
return pAgg;
}
ScDPAggData* ScDPRowTotals::GetRowTotal( long nMeasure )
{
return lcl_GetChildTotal( &aRowTotal, nMeasure );
}
ScDPAggData* ScDPRowTotals::GetGrandTotal( long nMeasure )
{
return lcl_GetChildTotal( &aGrandTotal, nMeasure );
}
static ScSubTotalFunc lcl_GetForceFunc( const ScDPLevel* pLevel, long nFuncNo )
{
ScSubTotalFunc eRet = SUBTOTAL_FUNC_NONE;
if ( pLevel )
{
//TODO: direct access via ScDPLevel
uno::Sequence<sal_Int16> aSeq = pLevel->getSubTotals();
long nSequence = aSeq.getLength();
if ( nSequence && aSeq[0] != sheet::GeneralFunction2::AUTO )
{
// For manual subtotals, "automatic" is added as first function.
// ScDPResultMember::GetSubTotalCount adds to the count, here NONE has to be
// returned as the first function then.
--nFuncNo; // keep NONE for first (check below), move the other entries
}
if ( nFuncNo >= 0 && nFuncNo < nSequence )
{
ScGeneralFunction eUser = static_cast<ScGeneralFunction>(aSeq.getConstArray()[nFuncNo]);
if (eUser != ScGeneralFunction::AUTO)
eRet = ScDPUtil::toSubTotalFunc(eUser);
}
}
return eRet;
}
ScDPResultData::ScDPResultData( ScDPSource& rSrc ) :
mrSource(rSrc),
bLateInit( false ),
bDataAtCol( false ),
bDataAtRow( false )
{
}
ScDPResultData::~ScDPResultData()
{
}
void ScDPResultData::SetMeasureData(
std::vector<ScSubTotalFunc>& rFunctions, std::vector<sheet::DataPilotFieldReference>& rRefs,
std::vector<sheet::DataPilotFieldOrientation>& rRefOrient, std::vector<OUString>& rNames )
{
// We need to have at least one measure data at all times.
maMeasureFuncs.swap(rFunctions);
if (maMeasureFuncs.empty())
maMeasureFuncs.push_back(SUBTOTAL_FUNC_NONE);
maMeasureRefs.swap(rRefs);
if (maMeasureRefs.empty())
maMeasureRefs.emplace_back(); // default ctor is ok.
maMeasureRefOrients.swap(rRefOrient);
if (maMeasureRefOrients.empty())
maMeasureRefOrients.push_back(sheet::DataPilotFieldOrientation_HIDDEN);
maMeasureNames.swap(rNames);
if (maMeasureNames.empty())
maMeasureNames.push_back(ScResId(STR_EMPTYDATA));
}
void ScDPResultData::SetDataLayoutOrientation( sheet::DataPilotFieldOrientation nOrient )
{
bDataAtCol = ( nOrient == sheet::DataPilotFieldOrientation_COLUMN );
bDataAtRow = ( nOrient == sheet::DataPilotFieldOrientation_ROW );
}
void ScDPResultData::SetLateInit( bool bSet )
{
bLateInit = bSet;
}
long ScDPResultData::GetColStartMeasure() const
{
if (maMeasureFuncs.size() == 1)
return 0;
return bDataAtCol ? SC_DPMEASURE_ALL : SC_DPMEASURE_ANY;
}
long ScDPResultData::GetRowStartMeasure() const
{
if (maMeasureFuncs.size() == 1)
return 0;
return bDataAtRow ? SC_DPMEASURE_ALL : SC_DPMEASURE_ANY;
}
ScSubTotalFunc ScDPResultData::GetMeasureFunction(long nMeasure) const
{
OSL_ENSURE(o3tl::make_unsigned(nMeasure) < maMeasureFuncs.size(), "bumm");
return maMeasureFuncs[nMeasure];
}
const sheet::DataPilotFieldReference& ScDPResultData::GetMeasureRefVal(long nMeasure) const
{
OSL_ENSURE(o3tl::make_unsigned(nMeasure) < maMeasureRefs.size(), "bumm");
return maMeasureRefs[nMeasure];
}
sheet::DataPilotFieldOrientation ScDPResultData::GetMeasureRefOrient(long nMeasure) const
{
OSL_ENSURE(o3tl::make_unsigned(nMeasure) < maMeasureRefOrients.size(), "bumm");
return maMeasureRefOrients[nMeasure];
}
OUString ScDPResultData::GetMeasureString(long nMeasure, bool bForce, ScSubTotalFunc eForceFunc, bool& rbTotalResult) const
{
// with bForce==true, return function instead of "result" for single measure
// with eForceFunc != SUBTOTAL_FUNC_NONE, always use eForceFunc
rbTotalResult = false;
if ( nMeasure < 0 || (maMeasureFuncs.size() == 1 && !bForce && eForceFunc == SUBTOTAL_FUNC_NONE) )
{
// for user-specified subtotal function with all measures,
// display only function name
assert(unsigned(eForceFunc) < SAL_N_ELEMENTS(aFuncStrIds));
if ( eForceFunc != SUBTOTAL_FUNC_NONE )
return ScResId(aFuncStrIds[eForceFunc]);
rbTotalResult = true;
return ScResId(STR_TABLE_ERGEBNIS);
}
else
{
OSL_ENSURE(o3tl::make_unsigned(nMeasure) < maMeasureFuncs.size(), "bumm");
const ScDPDimension* pDataDim = mrSource.GetDataDimension(nMeasure);
if (pDataDim)
{
const std::optional<OUString> & pLayoutName = pDataDim->GetLayoutName();
if (pLayoutName)
return *pLayoutName;
}
ScSubTotalFunc eFunc = ( eForceFunc == SUBTOTAL_FUNC_NONE ) ?
GetMeasureFunction(nMeasure) : eForceFunc;
return ScDPUtil::getDisplayedMeasureName(maMeasureNames[nMeasure], eFunc);
}
}
OUString ScDPResultData::GetMeasureDimensionName(long nMeasure) const
{
if ( nMeasure < 0 )
{
OSL_FAIL("GetMeasureDimensionName: negative");
return "***";
}
return mrSource.GetDataDimName(nMeasure);
}
bool ScDPResultData::IsBaseForGroup( long nDim ) const
{
return mrSource.GetData()->IsBaseForGroup(nDim);
}
long ScDPResultData::GetGroupBase( long nGroupDim ) const
{
return mrSource.GetData()->GetGroupBase(nGroupDim);
}
bool ScDPResultData::IsNumOrDateGroup( long nDim ) const
{
return mrSource.GetData()->IsNumOrDateGroup(nDim);
}
bool ScDPResultData::IsInGroup( SCROW nGroupDataId, long nGroupIndex,
const ScDPItemData& rBaseData, long nBaseIndex ) const
{
const ScDPItemData* pGroupData = mrSource.GetItemDataById(nGroupIndex , nGroupDataId);
if ( pGroupData )
return mrSource.GetData()->IsInGroup(*pGroupData, nGroupIndex, rBaseData, nBaseIndex);
else
return false;
}
bool ScDPResultData::HasCommonElement( SCROW nFirstDataId, long nFirstIndex,
const ScDPItemData& rSecondData, long nSecondIndex ) const
{
const ScDPItemData* pFirstData = mrSource.GetItemDataById(nFirstIndex , nFirstDataId);
if ( pFirstData )
return mrSource.GetData()->HasCommonElement(*pFirstData, nFirstIndex, rSecondData, nSecondIndex);
else
return false;
}
ResultMembers& ScDPResultData::GetDimResultMembers(long nDim, const ScDPDimension* pDim, ScDPLevel* pLevel) const
{
if (nDim < static_cast<long>(maDimMembers.size()) && maDimMembers[nDim])
return *maDimMembers[nDim];
if (nDim >= static_cast<long>(maDimMembers.size()))
maDimMembers.resize(nDim+1);
std::unique_ptr<ResultMembers> pResultMembers(new ResultMembers());
// global order is used to initialize aMembers, so it doesn't have to be looked at later
const ScMemberSortOrder& rGlobalOrder = pLevel->GetGlobalOrder();
ScDPMembers* pMembers = pLevel->GetMembersObject();
long nMembCount = pMembers->getCount();
for (long i = 0; i < nMembCount; ++i)
{
long nSorted = rGlobalOrder.empty() ? i : rGlobalOrder[i];
ScDPMember* pMember = pMembers->getByIndex(nSorted);
if (!pResultMembers->FindMember(pMember->GetItemDataId()))
{
ScDPParentDimData aNew(i, pDim, pLevel, pMember);
pResultMembers->InsertMember(aNew);
}
}
maDimMembers[nDim] = std::move(pResultMembers);
return *maDimMembers[nDim];
}
ScDPResultMember::ScDPResultMember(
const ScDPResultData* pData, const ScDPParentDimData& rParentDimData ) :
pResultData( pData ),
aParentDimData( rParentDimData ),
bHasElements( false ),
bForceSubTotal( false ),
bHasHiddenDetails( false ),
bInitialized( false ),
bAutoHidden( false ),
nMemberStep( 1 )
{
// pParentLevel/pMemberDesc is 0 for root members
}
ScDPResultMember::ScDPResultMember(
const ScDPResultData* pData, bool bForceSub ) :
pResultData( pData ),
bHasElements( false ),
bForceSubTotal( bForceSub ),
bHasHiddenDetails( false ),
bInitialized( false ),
bAutoHidden( false ),
nMemberStep( 1 )
{
}
ScDPResultMember::~ScDPResultMember()
{
}
OUString ScDPResultMember::GetName() const
{
const ScDPMember* pMemberDesc = GetDPMember();
if (pMemberDesc)
return pMemberDesc->GetNameStr( false );
else
return ScResId(STR_PIVOT_TOTAL); // root member
}
OUString ScDPResultMember::GetDisplayName( bool bLocaleIndependent ) const
{
const ScDPMember* pDPMember = GetDPMember();
if (!pDPMember)
return OUString();
ScDPItemData aItem(pDPMember->FillItemData());
if (aParentDimData.mpParentDim)
{
long nDim = aParentDimData.mpParentDim->GetDimension();
return pResultData->GetSource().GetData()->GetFormattedString(nDim, aItem, bLocaleIndependent);
}
return aItem.GetString();
}
ScDPItemData ScDPResultMember::FillItemData() const
{
const ScDPMember* pMemberDesc = GetDPMember();
if (pMemberDesc)
return pMemberDesc->FillItemData();
return ScDPItemData(ScResId(STR_PIVOT_TOTAL)); // root member
}
bool ScDPResultMember::IsNamedItem( SCROW nIndex ) const
{
//TODO: store ScDPMember pointer instead of ScDPMember ???
const ScDPMember* pMemberDesc = GetDPMember();
if (pMemberDesc)
return pMemberDesc->IsNamedItem(nIndex);
return false;
}
bool ScDPResultMember::IsValidEntry( const vector< SCROW >& aMembers ) const
{
if ( !IsValid() )
return false;
const ScDPResultDimension* pChildDim = GetChildDimension();
if (pChildDim)
{
if (aMembers.size() < 2)
return false;
vector<SCROW>::const_iterator itr = aMembers.begin();
vector<SCROW> aChildMembers(++itr, aMembers.end());
return pChildDim->IsValidEntry(aChildMembers);
}
else
return true;
}
void ScDPResultMember::InitFrom( const vector<ScDPDimension*>& ppDim, const vector<ScDPLevel*>& ppLev,
size_t nPos, ScDPInitState& rInitState ,
bool bInitChild )
{
// with LateInit, initialize only those members that have data
if ( pResultData->IsLateInit() )
return;
bInitialized = true;
if (nPos >= ppDim.size())
return;
// skip child dimension if details are not shown
if ( GetDPMember() && !GetDPMember()->getShowDetails() )
{
// Show DataLayout dimension
nMemberStep = 1;
while ( nPos < ppDim.size() )
{
if ( ppDim[nPos]->getIsDataLayoutDimension() )
{
if ( !pChildDimension )
pChildDimension.reset( new ScDPResultDimension( pResultData ) );
pChildDimension->InitFrom( ppDim, ppLev, nPos, rInitState , false );
return;
}
else
{ //find next dim
nPos ++;
nMemberStep ++;
}
}
bHasHiddenDetails = true; // only if there is a next dimension
return;
}
if ( bInitChild )
{
pChildDimension.reset( new ScDPResultDimension( pResultData ) );
pChildDimension->InitFrom(ppDim, ppLev, nPos, rInitState);
}
}
void ScDPResultMember::LateInitFrom(
LateInitParams& rParams, const vector<SCROW>& pItemData, size_t nPos, ScDPInitState& rInitState)
{
// without LateInit, everything has already been initialized
if ( !pResultData->IsLateInit() )
return;
bInitialized = true;
if ( rParams.IsEnd( nPos ) /*nPos >= ppDim.size()*/)
// No next dimension. Bail out.
return;
// skip child dimension if details are not shown
if ( GetDPMember() && !GetDPMember()->getShowDetails() )
{
// Show DataLayout dimension
nMemberStep = 1;
while ( !rParams.IsEnd( nPos ) )
{
if ( rParams.GetDim( nPos )->getIsDataLayoutDimension() )
{
if ( !pChildDimension )
pChildDimension.reset( new ScDPResultDimension( pResultData ) );
// #i111462# reset InitChild flag only for this child dimension's LateInitFrom call,
// not for following members of parent dimensions
bool bWasInitChild = rParams.GetInitChild();
rParams.SetInitChild( false );
pChildDimension->LateInitFrom( rParams, pItemData, nPos, rInitState );
rParams.SetInitChild( bWasInitChild );
return;
}
else
{ //find next dim
nPos ++;
nMemberStep ++;
}
}
bHasHiddenDetails = true; // only if there is a next dimension
return;
}
// LateInitFrom is called several times...
if ( rParams.GetInitChild() )
{
if ( !pChildDimension )
pChildDimension.reset( new ScDPResultDimension( pResultData ) );
pChildDimension->LateInitFrom( rParams, pItemData, nPos, rInitState );
}
}
bool ScDPResultMember::IsSubTotalInTitle(long nMeasure) const
{
bool bRet = false;
if ( pChildDimension && /*pParentLevel*/GetParentLevel() &&
/*pParentLevel*/GetParentLevel()->IsOutlineLayout() && /*pParentLevel*/GetParentLevel()->IsSubtotalsAtTop() )
{
long nUserSubStart;
long nSubTotals = GetSubTotalCount( &nUserSubStart );
nSubTotals -= nUserSubStart; // visible count
if ( nSubTotals )
{
if ( nMeasure == SC_DPMEASURE_ALL )
nSubTotals *= pResultData->GetMeasureCount(); // number of subtotals that will be inserted
// only a single subtotal row will be shown in the outline title row
if ( nSubTotals == 1 )
bRet = true;
}
}
return bRet;
}
long ScDPResultMember::GetSize(long nMeasure) const
{
if ( !IsVisible() )
return 0;
const ScDPLevel* pParentLevel = GetParentLevel();
long nExtraSpace = 0;
if ( pParentLevel && pParentLevel->IsAddEmpty() )
++nExtraSpace;
if ( pChildDimension )
{
// outline layout takes up an extra row for the title only if subtotals aren't shown in that row
if ( pParentLevel && pParentLevel->IsOutlineLayout() && !IsSubTotalInTitle( nMeasure ) )
++nExtraSpace;
long nSize = pChildDimension->GetSize(nMeasure);
long nUserSubStart;
long nUserSubCount = GetSubTotalCount( &nUserSubStart );
nUserSubCount -= nUserSubStart; // for output size, use visible count
if ( nUserSubCount )
{
if ( nMeasure == SC_DPMEASURE_ALL )
nSize += pResultData->GetMeasureCount() * nUserSubCount;
else
nSize += nUserSubCount;
}
return nSize + nExtraSpace;
}
else
{
if ( nMeasure == SC_DPMEASURE_ALL )
return pResultData->GetMeasureCount() + nExtraSpace;
else
return 1 + nExtraSpace;
}
}
bool ScDPResultMember::IsVisible() const
{
if (!bInitialized)
return false;
if (!IsValid())
return false;
if (bHasElements)
return true;
// not initialized -> shouldn't be there at all
// (allocated only to preserve ordering)
const ScDPLevel* pParentLevel = GetParentLevel();
return (pParentLevel && pParentLevel->getShowEmpty());
}
bool ScDPResultMember::IsValid() const
{
// non-Valid members are left out of calculation
// was member set no invisible at the DataPilotSource?
const ScDPMember* pMemberDesc = GetDPMember();
if ( pMemberDesc && !pMemberDesc->isVisible() )
return false;
if ( bAutoHidden )
return false;
return true;
}
long ScDPResultMember::GetSubTotalCount( long* pUserSubStart ) const
{
if ( pUserSubStart )
*pUserSubStart = 0; // default
const ScDPLevel* pParentLevel = GetParentLevel();
if ( bForceSubTotal ) // set if needed for root members
return 1; // grand total is always "automatic"
else if ( pParentLevel )
{
//TODO: direct access via ScDPLevel
uno::Sequence<sal_Int16> aSeq = pParentLevel->getSubTotals();
long nSequence = aSeq.getLength();
if ( nSequence && aSeq[0] != sheet::GeneralFunction2::AUTO )
{
// For manual subtotals, always add "automatic" as first function
// (used for calculation, but not for display, needed for sorting, see lcl_GetForceFunc)
++nSequence;
if ( pUserSubStart )
*pUserSubStart = 1; // visible subtotals start at 1
}
return nSequence;
}
else
return 0;
}
void ScDPResultMember::ProcessData( const vector< SCROW >& aChildMembers, const ScDPResultDimension* pDataDim,
const vector< SCROW >& aDataMembers, const vector<ScDPValue>& aValues )
{
SetHasElements();
if (pChildDimension)
pChildDimension->ProcessData( aChildMembers, pDataDim, aDataMembers, aValues );
if ( !pDataRoot )
{
pDataRoot.reset( new ScDPDataMember( pResultData, nullptr ) );
if ( pDataDim )
pDataRoot->InitFrom( pDataDim ); // recursive
}
ScDPSubTotalState aSubState; // initial state
long nUserSubCount = GetSubTotalCount();
// Calculate at least automatic if no subtotals are selected,
// show only own values if there's no child dimension (innermost).
if ( !nUserSubCount || !pChildDimension )
nUserSubCount = 1;
const ScDPLevel* pParentLevel = GetParentLevel();
for (long nUserPos=0; nUserPos<nUserSubCount; nUserPos++) // including hidden "automatic"
{
// #i68338# if nUserSubCount is 1 (automatic only), don't set nRowSubTotalFunc
if ( pChildDimension && nUserSubCount > 1 )
{
aSubState.nRowSubTotalFunc = nUserPos;
aSubState.eRowForce = lcl_GetForceFunc( pParentLevel, nUserPos );
}
pDataRoot->ProcessData( aDataMembers, aValues, aSubState );
}
}
/**
* Parse subtotal string and replace all occurrences of '?' with the caption
* string. Do ensure that escaped characters are not translated.
*/
static OUString lcl_parseSubtotalName(const OUString& rSubStr, const OUString& rCaption)
{
OUStringBuffer aNewStr;
sal_Int32 n = rSubStr.getLength();
bool bEscaped = false;
for (sal_Int32 i = 0; i < n; ++i)
{
sal_Unicode c = rSubStr[i];
if (!bEscaped && c == '\\')
{
bEscaped = true;
continue;
}
if (!bEscaped && c == '?')
aNewStr.append(rCaption);
else
aNewStr.append(c);
bEscaped = false;
}
return aNewStr.makeStringAndClear();
}
void ScDPResultMember::FillMemberResults(
uno::Sequence<sheet::MemberResult>* pSequences, long& rPos, long nMeasure, bool bRoot,
const OUString* pMemberName, const OUString* pMemberCaption )
{
// IsVisible() test is in ScDPResultDimension::FillMemberResults
// (not on data layout dimension)
if (!pSequences->hasElements())
// empty sequence. Bail out.
return;
long nSize = GetSize(nMeasure);
sheet::MemberResult* pArray = pSequences->getArray();
OSL_ENSURE( rPos+nSize <= pSequences->getLength(), "bumm" );
bool bIsNumeric = false;
double fValue;
rtl::math::setNan(&fValue);
OUString aName;
if ( pMemberName ) // if pMemberName != NULL, use instead of real member name
{
aName = *pMemberName;
}
else
{
ScDPItemData aItemData(FillItemData());
if (aParentDimData.mpParentDim)
{
long nDim = aParentDimData.mpParentDim->GetDimension();
aName = pResultData->GetSource().GetData()->GetFormattedString(nDim, aItemData, false);
}
else
{
long nDim = -1;
const ScDPMember* pMem = GetDPMember();
if (pMem)
nDim = pMem->GetDim();
aName = pResultData->GetSource().GetData()->GetFormattedString(nDim, aItemData, false);
}
ScDPItemData::Type eType = aItemData.GetType();
bIsNumeric = eType == ScDPItemData::Value || eType == ScDPItemData::GroupValue;
// IsValue() is not identical to bIsNumeric, i.e.
// ScDPItemData::GroupValue is excluded and not stored in the double,
// so even if the item is numeric the Value may be NaN.
if (aItemData.IsValue())
fValue = aItemData.GetValue();
}
const ScDPDimension* pParentDim = GetParentDim();
if ( bIsNumeric && pParentDim && pResultData->IsNumOrDateGroup( pParentDim->GetDimension() ) )
{
// Numeric group dimensions use numeric entries for proper sorting,
// but the group titles must be output as text.
bIsNumeric = false;
}
OUString aCaption = aName;
const ScDPMember* pMemberDesc = GetDPMember();
if (pMemberDesc)
{
const std::optional<OUString> & pLayoutName = pMemberDesc->GetLayoutName();
if (pLayoutName)
{
aCaption = *pLayoutName;
bIsNumeric = false; // layout name is always non-numeric.
}
}
if ( pMemberCaption ) // use pMemberCaption if != NULL
aCaption = *pMemberCaption;
if (aCaption.isEmpty())
aCaption = ScResId(STR_EMPTYDATA);
if (bIsNumeric)
pArray[rPos].Flags |= sheet::MemberResultFlags::NUMERIC;
else
pArray[rPos].Flags &= ~sheet::MemberResultFlags::NUMERIC;
const ScDPLevel* pParentLevel = GetParentLevel();
if ( nSize && !bRoot ) // root is overwritten by first dimension
{
pArray[rPos].Name = aName;
pArray[rPos].Caption = aCaption;
pArray[rPos].Flags |= sheet::MemberResultFlags::HASMEMBER;
pArray[rPos].Value = fValue;
// set "continue" flag (removed for subtotals later)
for (long i=1; i<nSize; i++)
pArray[rPos+i].Flags |= sheet::MemberResultFlags::CONTINUE;
if ( pParentLevel && pParentLevel->getRepeatItemLabels() )
{
long nSizeNonEmpty = nSize;
if ( pParentLevel->IsAddEmpty() )
--nSizeNonEmpty;
for (long i=1; i<nSizeNonEmpty; i++)
{
pArray[rPos+i].Name = aName;
pArray[rPos+i].Caption = aCaption;
pArray[rPos+i].Flags |= sheet::MemberResultFlags::HASMEMBER;
pArray[rPos+i].Value = fValue;
}
}
}
long nExtraSpace = 0;
if ( pParentLevel && pParentLevel->IsAddEmpty() )
++nExtraSpace;
bool bTitleLine = false;
if ( pParentLevel && pParentLevel->IsOutlineLayout() )
bTitleLine = true;
// if the subtotals are shown at the top (title row) in outline layout,
// no extra row for the subtotals is needed
bool bSubTotalInTitle = IsSubTotalInTitle( nMeasure );
bool bHasChild = ( pChildDimension != nullptr );
if (bHasChild)
{
if ( bTitleLine ) // in tabular layout the title is on a separate row
++rPos; // -> fill child dimension one row below
if (bRoot) // same sequence for root member
pChildDimension->FillMemberResults( pSequences, rPos, nMeasure );
else
pChildDimension->FillMemberResults( pSequences + nMemberStep/*1*/, rPos, nMeasure );
if ( bTitleLine ) // title row is included in GetSize, so the following
--rPos; // positions are calculated with the normal values
}
rPos += nSize;
long nUserSubStart;
long nUserSubCount = GetSubTotalCount(&nUserSubStart);
if ( nUserSubCount && pChildDimension && !bSubTotalInTitle )
{
long nMemberMeasure = nMeasure;
long nSubSize = pResultData->GetCountForMeasure(nMeasure);
rPos -= nSubSize * (nUserSubCount - nUserSubStart); // GetSize includes space for SubTotal
rPos -= nExtraSpace; // GetSize includes the empty line
for (long nUserPos=nUserSubStart; nUserPos<nUserSubCount; nUserPos++)
{
for ( long nSubCount=0; nSubCount<nSubSize; nSubCount++ )
{
if ( nMeasure == SC_DPMEASURE_ALL )
nMemberMeasure = nSubCount;
ScSubTotalFunc eForce = SUBTOTAL_FUNC_NONE;
if (bHasChild)
eForce = lcl_GetForceFunc( pParentLevel, nUserPos );
bool bTotalResult = false;
OUString aSubStr = aCaption + " " + pResultData->GetMeasureString(nMemberMeasure, false, eForce, bTotalResult);
if (bTotalResult)
{
if (pMemberDesc)
{
// single data field layout.
const std::optional<OUString> & pSubtotalName = pParentDim->GetSubtotalName();
if (pSubtotalName)
aSubStr = lcl_parseSubtotalName(*pSubtotalName, aCaption);
pArray[rPos].Flags &= ~sheet::MemberResultFlags::GRANDTOTAL;
}
else
{
// root member - subtotal (grand total?) for multi-data field layout.
const std::optional<OUString> & pGrandTotalName = pResultData->GetSource().GetGrandTotalName();
if (pGrandTotalName)
aSubStr = *pGrandTotalName;
pArray[rPos].Flags |= sheet::MemberResultFlags::GRANDTOTAL;
}
}
rtl::math::setNan(&fValue); /* TODO: any numeric value to obtain? */
pArray[rPos].Name = aName;
pArray[rPos].Caption = aSubStr;
pArray[rPos].Flags = ( pArray[rPos].Flags |
( sheet::MemberResultFlags::HASMEMBER | sheet::MemberResultFlags::SUBTOTAL) ) &
~sheet::MemberResultFlags::CONTINUE;
pArray[rPos].Value = fValue;
if ( nMeasure == SC_DPMEASURE_ALL )
{
// data layout dimension is (direct/indirect) child of this.
// data layout dimension must have name for all entries.
uno::Sequence<sheet::MemberResult>* pLayoutSeq = pSequences;
if (!bRoot)
++pLayoutSeq;
ScDPResultDimension* pLayoutDim = pChildDimension.get();
while ( pLayoutDim && !pLayoutDim->IsDataLayout() )
{
pLayoutDim = pLayoutDim->GetFirstChildDimension();
++pLayoutSeq;
}
if ( pLayoutDim )
{
sheet::MemberResult* pLayoutArray = pLayoutSeq->getArray();
pLayoutArray[rPos].Name = pResultData->GetMeasureDimensionName(nMemberMeasure);
}
}
rPos += 1;
}
}
rPos += nExtraSpace; // add again (subtracted above)
}
}
void ScDPResultMember::FillDataResults(
const ScDPResultMember* pRefMember,
ScDPResultFilterContext& rFilterCxt, uno::Sequence<uno::Sequence<sheet::DataResult> >& rSequence,
long nMeasure) const
{
std::unique_ptr<FilterStack> pFilterStack;
const ScDPMember* pDPMember = GetDPMember();
if (pDPMember)
{
// Root result has no corresponding DP member. Only take the non-root results.
pFilterStack.reset(new FilterStack(rFilterCxt.maFilters));
pFilterStack->pushDimValue( GetDisplayName( false), GetDisplayName( true));
}
// IsVisible() test is in ScDPResultDimension::FillDataResults
// (not on data layout dimension)
const ScDPLevel* pParentLevel = GetParentLevel();
long nStartRow = rFilterCxt.mnRow;
long nExtraSpace = 0;
if ( pParentLevel && pParentLevel->IsAddEmpty() )
++nExtraSpace;
bool bTitleLine = false;
if ( pParentLevel && pParentLevel->IsOutlineLayout() )
bTitleLine = true;
bool bSubTotalInTitle = IsSubTotalInTitle( nMeasure );
bool bHasChild = ( pChildDimension != nullptr );
if (bHasChild)
{
if ( bTitleLine ) // in tabular layout the title is on a separate row
++rFilterCxt.mnRow; // -> fill child dimension one row below
long nOldRow = rFilterCxt.mnRow;
pChildDimension->FillDataResults(pRefMember, rFilterCxt, rSequence, nMeasure);
rFilterCxt.mnRow = nOldRow; // Revert to the original row before the call.
rFilterCxt.mnRow += GetSize( nMeasure );
if ( bTitleLine ) // title row is included in GetSize, so the following
--rFilterCxt.mnRow; // positions are calculated with the normal values
}
long nUserSubStart;
long nUserSubCount = GetSubTotalCount(&nUserSubStart);
if ( nUserSubCount || !bHasChild )
{
// Calculate at least automatic if no subtotals are selected,
// show only own values if there's no child dimension (innermost).
if ( !nUserSubCount || !bHasChild )
{
nUserSubCount = 1;
nUserSubStart = 0;
}
long nMemberMeasure = nMeasure;
long nSubSize = pResultData->GetCountForMeasure(nMeasure);
if (bHasChild)
{
rFilterCxt.mnRow -= nSubSize * ( nUserSubCount - nUserSubStart ); // GetSize includes space for SubTotal
rFilterCxt.mnRow -= nExtraSpace; // GetSize includes the empty line
}
long nMoveSubTotal = 0;
if ( bSubTotalInTitle )
{
nMoveSubTotal = rFilterCxt.mnRow - nStartRow; // force to first (title) row
rFilterCxt.mnRow = nStartRow;
}
if ( pDataRoot )
{
ScDPSubTotalState aSubState; // initial state
for (long nUserPos=nUserSubStart; nUserPos<nUserSubCount; nUserPos++)
{
if ( bHasChild && nUserSubCount > 1 )
{
aSubState.nRowSubTotalFunc = nUserPos;
aSubState.eRowForce = lcl_GetForceFunc( /*pParentLevel*/GetParentLevel() , nUserPos );
}
for ( long nSubCount=0; nSubCount<nSubSize; nSubCount++ )
{
if ( nMeasure == SC_DPMEASURE_ALL )
nMemberMeasure = nSubCount;
else if ( pResultData->GetColStartMeasure() == SC_DPMEASURE_ALL )
nMemberMeasure = SC_DPMEASURE_ALL;
OSL_ENSURE( rFilterCxt.mnRow < rSequence.getLength(), "bumm" );
rFilterCxt.mnCol = 0;
if (pRefMember->IsVisible())
{
uno::Sequence<sheet::DataResult>& rSubSeq = rSequence.getArray()[rFilterCxt.mnRow];
pDataRoot->FillDataRow(pRefMember, rFilterCxt, rSubSeq, nMemberMeasure, bHasChild, aSubState);
}
rFilterCxt.mnRow += 1;
}
}
}
else
rFilterCxt.mnRow += nSubSize * ( nUserSubCount - nUserSubStart ); // empty rows occur when ShowEmpty is true
// add extra space again if subtracted from GetSize above,
// add to own size if no children
rFilterCxt.mnRow += nExtraSpace;
rFilterCxt.mnRow += nMoveSubTotal;
}
}
void ScDPResultMember::UpdateDataResults( const ScDPResultMember* pRefMember, long nMeasure ) const
{
// IsVisible() test is in ScDPResultDimension::FillDataResults
// (not on data layout dimension)
bool bHasChild = ( pChildDimension != nullptr );
long nUserSubCount = GetSubTotalCount();
// process subtotals even if not shown
// Calculate at least automatic if no subtotals are selected,
// show only own values if there's no child dimension (innermost).
if (!nUserSubCount || !bHasChild)
nUserSubCount = 1;
long nMemberMeasure = nMeasure;
long nSubSize = pResultData->GetCountForMeasure(nMeasure);
if (pDataRoot)
{
ScDPSubTotalState aSubState; // initial state
for (long nUserPos = 0; nUserPos < nUserSubCount; ++nUserPos) // including hidden "automatic"
{
if (bHasChild && nUserSubCount > 1)
{
aSubState.nRowSubTotalFunc = nUserPos;
aSubState.eRowForce = lcl_GetForceFunc(GetParentLevel(), nUserPos);
}
for (long nSubCount = 0; nSubCount < nSubSize; ++nSubCount)
{
if (nMeasure == SC_DPMEASURE_ALL)
nMemberMeasure = nSubCount;
else if (pResultData->GetColStartMeasure() == SC_DPMEASURE_ALL)
nMemberMeasure = SC_DPMEASURE_ALL;
pDataRoot->UpdateDataRow(pRefMember, nMemberMeasure, bHasChild, aSubState);
}
}
}
if (bHasChild) // child dimension must be processed last, so the column total is known
{
pChildDimension->UpdateDataResults( pRefMember, nMeasure );
}
}
void ScDPResultMember::SortMembers( ScDPResultMember* pRefMember )
{
bool bHasChild = ( pChildDimension != nullptr );
if (bHasChild)
pChildDimension->SortMembers( pRefMember ); // sorting is done at the dimension
if ( IsRoot() && pDataRoot )
{
// use the row root member to sort columns
// sub total count is always 1
pDataRoot->SortMembers( pRefMember );
}
}
void ScDPResultMember::DoAutoShow( ScDPResultMember* pRefMember )
{
bool bHasChild = ( pChildDimension != nullptr );
if (bHasChild)
pChildDimension->DoAutoShow( pRefMember ); // sorting is done at the dimension
if ( IsRoot()&& pDataRoot )
{
// use the row root member to sort columns
// sub total count is always 1
pDataRoot->DoAutoShow( pRefMember );
}
}
void ScDPResultMember::ResetResults()
{
if (pDataRoot)
pDataRoot->ResetResults();
if (pChildDimension)
pChildDimension->ResetResults();
}
void ScDPResultMember::UpdateRunningTotals( const ScDPResultMember* pRefMember, long nMeasure,
ScDPRunningTotalState& rRunning, ScDPRowTotals& rTotals ) const
{
// IsVisible() test is in ScDPResultDimension::FillDataResults
// (not on data layout dimension)
rTotals.SetInColRoot( IsRoot() );
bool bHasChild = ( pChildDimension != nullptr );
long nUserSubCount = GetSubTotalCount();
//if ( nUserSubCount || !bHasChild )
{
// Calculate at least automatic if no subtotals are selected,
// show only own values if there's no child dimension (innermost).
if ( !nUserSubCount || !bHasChild )
nUserSubCount = 1;
long nMemberMeasure = nMeasure;
long nSubSize = pResultData->GetCountForMeasure(nMeasure);
if ( pDataRoot )
{
ScDPSubTotalState aSubState; // initial state
for (long nUserPos=0; nUserPos<nUserSubCount; nUserPos++) // including hidden "automatic"
{
if ( bHasChild && nUserSubCount > 1 )
{
aSubState.nRowSubTotalFunc = nUserPos;
aSubState.eRowForce = lcl_GetForceFunc(GetParentLevel(), nUserPos);
}
for ( long nSubCount=0; nSubCount<nSubSize; nSubCount++ )
{
if ( nMeasure == SC_DPMEASURE_ALL )
nMemberMeasure = nSubCount;
else if ( pResultData->GetColStartMeasure() == SC_DPMEASURE_ALL )
nMemberMeasure = SC_DPMEASURE_ALL;
if (pRefMember->IsVisible())
pDataRoot->UpdateRunningTotals(
pRefMember, nMemberMeasure, bHasChild, aSubState, rRunning, rTotals, *this);
}
}
}
}
if (bHasChild) // child dimension must be processed last, so the column total is known
{
pChildDimension->UpdateRunningTotals( pRefMember, nMeasure, rRunning, rTotals );
}
}
#if DUMP_PIVOT_TABLE
void ScDPResultMember::DumpState( const ScDPResultMember* pRefMember, ScDocument* pDoc, ScAddress& rPos ) const
{
dumpRow("ScDPResultMember", GetName(), nullptr, pDoc, rPos);
SCROW nStartRow = rPos.Row();
if (pDataRoot)
pDataRoot->DumpState( pRefMember, pDoc, rPos );
if (pChildDimension)
pChildDimension->DumpState( pRefMember, pDoc, rPos );
indent(pDoc, nStartRow, rPos);
}
void ScDPResultMember::Dump(int nIndent) const
{
std::string aIndent(nIndent*2, ' ');
std::cout << aIndent << "-- result member '" << GetName() << "'" << std::endl;
std::cout << aIndent << " column totals" << std::endl;
for (const ScDPAggData* p = &aColTotal; p; p = p->GetExistingChild())
p->Dump(nIndent+1);
if (pChildDimension)
pChildDimension->Dump(nIndent+1);
if (pDataRoot)
{
std::cout << aIndent << " data root" << std::endl;
pDataRoot->Dump(nIndent+1);
}
}
#endif
ScDPAggData* ScDPResultMember::GetColTotal( long nMeasure ) const
{
return lcl_GetChildTotal( const_cast<ScDPAggData*>(&aColTotal), nMeasure );
}
void ScDPResultMember::FillVisibilityData(ScDPResultVisibilityData& rData) const
{
if (pChildDimension)
pChildDimension->FillVisibilityData(rData);
}
ScDPDataMember::ScDPDataMember( const ScDPResultData* pData, const ScDPResultMember* pRes ) :
pResultData( pData ),
pResultMember( pRes )
{
// pResultMember is 0 for root members
}
ScDPDataMember::~ScDPDataMember()
{
}
OUString ScDPDataMember::GetName() const
{
if (pResultMember)
return pResultMember->GetName();
else
return EMPTY_OUSTRING;
}
bool ScDPDataMember::IsVisible() const
{
if (pResultMember)
return pResultMember->IsVisible();
else
return false;
}
bool ScDPDataMember::IsNamedItem( SCROW nRow ) const
{
if (pResultMember)
return pResultMember->IsNamedItem(nRow);
else
return false;
}
bool ScDPDataMember::HasHiddenDetails() const
{
if (pResultMember)
return pResultMember->HasHiddenDetails();
else
return false;
}
void ScDPDataMember::InitFrom( const ScDPResultDimension* pDim )
{
if ( !pChildDimension )
pChildDimension.reset( new ScDPDataDimension(pResultData) );
pChildDimension->InitFrom(pDim);
}
const long SC_SUBTOTALPOS_AUTO = -1; // default
const long SC_SUBTOTALPOS_SKIP = -2; // don't use
static long lcl_GetSubTotalPos( const ScDPSubTotalState& rSubState )
{
if ( rSubState.nColSubTotalFunc >= 0 && rSubState.nRowSubTotalFunc >= 0 &&
rSubState.nColSubTotalFunc != rSubState.nRowSubTotalFunc )
{
// #i68338# don't return the same index for different combinations (leading to repeated updates),
// return a "don't use" value instead
return SC_SUBTOTALPOS_SKIP;
}
long nRet = SC_SUBTOTALPOS_AUTO;
if ( rSubState.nColSubTotalFunc >= 0 ) nRet = rSubState.nColSubTotalFunc;
if ( rSubState.nRowSubTotalFunc >= 0 ) nRet = rSubState.nRowSubTotalFunc;
return nRet;
}
void ScDPDataMember::UpdateValues( const vector<ScDPValue>& aValues, const ScDPSubTotalState& rSubState )
{
//TODO: find out how many and which subtotals are used
ScDPAggData* pAgg = &aAggregate;
long nSubPos = lcl_GetSubTotalPos(rSubState);
if (nSubPos == SC_SUBTOTALPOS_SKIP)
return;
if (nSubPos > 0)
{
long nSkip = nSubPos * pResultData->GetMeasureCount();
for (long i=0; i<nSkip; i++)
pAgg = pAgg->GetChild(); // created if not there
}
size_t nCount = aValues.size();
for (size_t nPos = 0; nPos < nCount; ++nPos)
{
pAgg->Update(aValues[nPos], pResultData->GetMeasureFunction(nPos), rSubState);
pAgg = pAgg->GetChild();
}
}
void ScDPDataMember::ProcessData( const vector< SCROW >& aChildMembers, const vector<ScDPValue>& aValues,
const ScDPSubTotalState& rSubState )
{
if ( pResultData->IsLateInit() && !pChildDimension && pResultMember && pResultMember->GetChildDimension() )
{
// if this DataMember doesn't have a child dimension because the ResultMember's
// child dimension wasn't there yet during this DataMembers's creation,
// create the child dimension now
InitFrom( pResultMember->GetChildDimension() );
}
long nUserSubCount = pResultMember ? pResultMember->GetSubTotalCount() : 0;
// Calculate at least automatic if no subtotals are selected,
// show only own values if there's no child dimension (innermost).
if ( !nUserSubCount || !pChildDimension )
nUserSubCount = 1;
ScDPSubTotalState aLocalSubState = rSubState; // keep row state, modify column
for (long nUserPos=0; nUserPos<nUserSubCount; nUserPos++) // including hidden "automatic"
{
if ( pChildDimension && nUserSubCount > 1 )
{
const ScDPLevel* pForceLevel = pResultMember ? pResultMember->GetParentLevel() : nullptr;
aLocalSubState.nColSubTotalFunc = nUserPos;
aLocalSubState.eColForce = lcl_GetForceFunc( pForceLevel, nUserPos );
}
UpdateValues( aValues, aLocalSubState );
}
if (pChildDimension)
pChildDimension->ProcessData( aChildMembers, aValues, rSubState ); // with unmodified subtotal state
}
bool ScDPDataMember::HasData( long nMeasure, const ScDPSubTotalState& rSubState ) const
{
if ( rSubState.eColForce != SUBTOTAL_FUNC_NONE && rSubState.eRowForce != SUBTOTAL_FUNC_NONE &&
rSubState.eColForce != rSubState.eRowForce )
return false;
// HasData can be different between measures!
const ScDPAggData* pAgg = GetConstAggData( nMeasure, rSubState );
if (!pAgg)
return false; //TODO: error?
return pAgg->HasData();
}
bool ScDPDataMember::HasError( long nMeasure, const ScDPSubTotalState& rSubState ) const
{
const ScDPAggData* pAgg = GetConstAggData( nMeasure, rSubState );
if (!pAgg)
return true;
return pAgg->HasError();
}
double ScDPDataMember::GetAggregate( long nMeasure, const ScDPSubTotalState& rSubState ) const
{
const ScDPAggData* pAgg = GetConstAggData( nMeasure, rSubState );
if (!pAgg)
return DBL_MAX; //TODO: error?
return pAgg->GetResult();
}
ScDPAggData* ScDPDataMember::GetAggData( long nMeasure, const ScDPSubTotalState& rSubState )
{
OSL_ENSURE( nMeasure >= 0, "GetAggData: no measure" );
ScDPAggData* pAgg = &aAggregate;
long nSkip = nMeasure;
long nSubPos = lcl_GetSubTotalPos(rSubState);
if (nSubPos == SC_SUBTOTALPOS_SKIP)
return nullptr;
if (nSubPos > 0)
nSkip += nSubPos * pResultData->GetMeasureCount();
for ( long nPos=0; nPos<nSkip; nPos++ )
pAgg = pAgg->GetChild(); //TODO: need to create children here?
return pAgg;
}
const ScDPAggData* ScDPDataMember::GetConstAggData( long nMeasure, const ScDPSubTotalState& rSubState ) const
{
OSL_ENSURE( nMeasure >= 0, "GetConstAggData: no measure" );
const ScDPAggData* pAgg = &aAggregate;
long nSkip = nMeasure;
long nSubPos = lcl_GetSubTotalPos(rSubState);
if (nSubPos == SC_SUBTOTALPOS_SKIP)
return nullptr;
if (nSubPos > 0)
nSkip += nSubPos * pResultData->GetMeasureCount();
for ( long nPos=0; nPos<nSkip; nPos++ )
{
pAgg = pAgg->GetExistingChild();
if (!pAgg)
return nullptr;
}
return pAgg;
}
void ScDPDataMember::FillDataRow(
const ScDPResultMember* pRefMember, ScDPResultFilterContext& rFilterCxt,
uno::Sequence<sheet::DataResult>& rSequence, long nMeasure, bool bIsSubTotalRow,
const ScDPSubTotalState& rSubState) const
{
std::unique_ptr<FilterStack> pFilterStack;
if (pResultMember)
{
// Topmost data member (pResultMember=NULL) doesn't need to be handled
// since its immediate parent result member is linked to the same
// dimension member.
pFilterStack.reset(new FilterStack(rFilterCxt.maFilters));
pFilterStack->pushDimValue( pResultMember->GetDisplayName( false), pResultMember->GetDisplayName( true));
}
OSL_ENSURE( pRefMember == pResultMember || !pResultMember, "bla" );
long nStartCol = rFilterCxt.mnCol;
const ScDPDataDimension* pDataChild = GetChildDimension();
const ScDPResultDimension* pRefChild = pRefMember->GetChildDimension();
const ScDPLevel* pRefParentLevel = pRefMember->GetParentLevel();
long nExtraSpace = 0;
if ( pRefParentLevel && pRefParentLevel->IsAddEmpty() )
++nExtraSpace;
bool bTitleLine = false;
if ( pRefParentLevel && pRefParentLevel->IsOutlineLayout() )
bTitleLine = true;
bool bSubTotalInTitle = pRefMember->IsSubTotalInTitle( nMeasure );
// leave space for children even if the DataMember hasn't been initialized
// (pDataChild is null then, this happens when no values for it are in this row)
bool bHasChild = ( pRefChild != nullptr );
if ( bHasChild )
{
if ( bTitleLine ) // in tabular layout the title is on a separate column
++rFilterCxt.mnCol; // -> fill child dimension one column below
if ( pDataChild )
{
long nOldCol = rFilterCxt.mnCol;
pDataChild->FillDataRow(pRefChild, rFilterCxt, rSequence, nMeasure, bIsSubTotalRow, rSubState);
rFilterCxt.mnCol = nOldCol; // Revert to the old column value before the call.
}
rFilterCxt.mnCol += static_cast<sal_uInt16>(pRefMember->GetSize( nMeasure ));
if ( bTitleLine ) // title column is included in GetSize, so the following
--rFilterCxt.mnCol; // positions are calculated with the normal values
}
long nUserSubStart;
long nUserSubCount = pRefMember->GetSubTotalCount(&nUserSubStart);
if ( nUserSubCount || !bHasChild )
{
// Calculate at least automatic if no subtotals are selected,
// show only own values if there's no child dimension (innermost).
if ( !nUserSubCount || !bHasChild )
{
nUserSubCount = 1;
nUserSubStart = 0;
}
ScDPSubTotalState aLocalSubState(rSubState); // keep row state, modify column
long nMemberMeasure = nMeasure;
long nSubSize = pResultData->GetCountForMeasure(nMeasure);
if (bHasChild)
{
rFilterCxt.mnCol -= nSubSize * ( nUserSubCount - nUserSubStart ); // GetSize includes space for SubTotal
rFilterCxt.mnCol -= nExtraSpace; // GetSize includes the empty line
}
long nMoveSubTotal = 0;
if ( bSubTotalInTitle )
{
nMoveSubTotal = rFilterCxt.mnCol - nStartCol; // force to first (title) column
rFilterCxt.mnCol = nStartCol;
}
for (long nUserPos=nUserSubStart; nUserPos<nUserSubCount; nUserPos++)
{
if ( pChildDimension && nUserSubCount > 1 )
{
const ScDPLevel* pForceLevel = pResultMember ? pResultMember->GetParentLevel() : nullptr;
aLocalSubState.nColSubTotalFunc = nUserPos;
aLocalSubState.eColForce = lcl_GetForceFunc( pForceLevel, nUserPos );
}
for ( long nSubCount=0; nSubCount<nSubSize; nSubCount++ )
{
if ( nMeasure == SC_DPMEASURE_ALL )
nMemberMeasure = nSubCount;
OSL_ENSURE( rFilterCxt.mnCol < rSequence.getLength(), "bumm" );
sheet::DataResult& rRes = rSequence.getArray()[rFilterCxt.mnCol];
if ( HasData( nMemberMeasure, aLocalSubState ) )
{
if ( HasError( nMemberMeasure, aLocalSubState ) )
{
rRes.Value = 0;
rRes.Flags |= sheet::DataResultFlags::ERROR;
}
else
{
rRes.Value = GetAggregate( nMemberMeasure, aLocalSubState );
rRes.Flags |= sheet::DataResultFlags::HASDATA;
}
}
if ( bHasChild || bIsSubTotalRow )
rRes.Flags |= sheet::DataResultFlags::SUBTOTAL;
rFilterCxt.maFilterSet.add(rFilterCxt.maFilters, rRes.Value);
rFilterCxt.mnCol += 1;
}
}
// add extra space again if subtracted from GetSize above,
// add to own size if no children
rFilterCxt.mnCol += nExtraSpace;
rFilterCxt.mnCol += nMoveSubTotal;
}
}
void ScDPDataMember::UpdateDataRow(
const ScDPResultMember* pRefMember, long nMeasure, bool bIsSubTotalRow,
const ScDPSubTotalState& rSubState )
{
OSL_ENSURE( pRefMember == pResultMember || !pResultMember, "bla" );
// Calculate must be called even if not visible (for use as reference value)
const ScDPDataDimension* pDataChild = GetChildDimension();
const ScDPResultDimension* pRefChild = pRefMember->GetChildDimension();
// leave space for children even if the DataMember hasn't been initialized
// (pDataChild is null then, this happens when no values for it are in this row)
bool bHasChild = ( pRefChild != nullptr );
// process subtotals even if not shown
long nUserSubCount = pRefMember->GetSubTotalCount();
// Calculate at least automatic if no subtotals are selected,
// show only own values if there's no child dimension (innermost).
if ( !nUserSubCount || !bHasChild )
nUserSubCount = 1;
ScDPSubTotalState aLocalSubState(rSubState); // keep row state, modify column
long nMemberMeasure = nMeasure;
long nSubSize = pResultData->GetCountForMeasure(nMeasure);
for (long nUserPos=0; nUserPos<nUserSubCount; nUserPos++) // including hidden "automatic"
{
if ( pChildDimension && nUserSubCount > 1 )
{
const ScDPLevel* pForceLevel = pResultMember ? pResultMember->GetParentLevel() : nullptr;
aLocalSubState.nColSubTotalFunc = nUserPos;
aLocalSubState.eColForce = lcl_GetForceFunc( pForceLevel, nUserPos );
}
for ( long nSubCount=0; nSubCount<nSubSize; nSubCount++ )
{
if ( nMeasure == SC_DPMEASURE_ALL )
nMemberMeasure = nSubCount;
// update data...
ScDPAggData* pAggData = GetAggData( nMemberMeasure, aLocalSubState );
if (pAggData)
{
//TODO: aLocalSubState?
ScSubTotalFunc eFunc = pResultData->GetMeasureFunction( nMemberMeasure );
sheet::DataPilotFieldReference aReferenceValue = pResultData->GetMeasureRefVal( nMemberMeasure );
sal_Int32 eRefType = aReferenceValue.ReferenceType;
// calculate the result first - for all members, regardless of reference value
pAggData->Calculate( eFunc, aLocalSubState );
if ( eRefType == sheet::DataPilotFieldReferenceType::ITEM_DIFFERENCE ||
eRefType == sheet::DataPilotFieldReferenceType::ITEM_PERCENTAGE ||
eRefType == sheet::DataPilotFieldReferenceType::ITEM_PERCENTAGE_DIFFERENCE )
{
// copy the result into auxiliary value, so differences can be
// calculated in any order
pAggData->SetAuxiliary( pAggData->GetResult() );
}
// column/row percentage/index is now in UpdateRunningTotals, so it doesn't disturb sorting
}
}
}
if ( bHasChild ) // child dimension must be processed last, so the row total is known
{
if ( pDataChild )
pDataChild->UpdateDataRow( pRefChild, nMeasure, bIsSubTotalRow, rSubState );
}
}
void ScDPDataMember::SortMembers( ScDPResultMember* pRefMember )
{
OSL_ENSURE( pRefMember == pResultMember || !pResultMember, "bla" );
if ( pRefMember->IsVisible() ) //TODO: here or in ScDPDataDimension ???
{
ScDPDataDimension* pDataChild = GetChildDimension();
ScDPResultDimension* pRefChild = pRefMember->GetChildDimension();
if ( pRefChild && pDataChild )
pDataChild->SortMembers( pRefChild ); // sorting is done at the dimension
}
}
void ScDPDataMember::DoAutoShow( ScDPResultMember* pRefMember )
{
OSL_ENSURE( pRefMember == pResultMember || !pResultMember, "bla" );
if ( pRefMember->IsVisible() ) //TODO: here or in ScDPDataDimension ???
{
ScDPDataDimension* pDataChild = GetChildDimension();
ScDPResultDimension* pRefChild = pRefMember->GetChildDimension();
if ( pRefChild && pDataChild )
pDataChild->DoAutoShow( pRefChild ); // sorting is done at the dimension
}
}
void ScDPDataMember::ResetResults()
{
aAggregate.Reset();
ScDPDataDimension* pDataChild = GetChildDimension();
if ( pDataChild )
pDataChild->ResetResults();
}
void ScDPDataMember::UpdateRunningTotals(
const ScDPResultMember* pRefMember, long nMeasure, bool bIsSubTotalRow,
const ScDPSubTotalState& rSubState, ScDPRunningTotalState& rRunning,
ScDPRowTotals& rTotals, const ScDPResultMember& rRowParent )
{
OSL_ENSURE( pRefMember == pResultMember || !pResultMember, "bla" );
const ScDPDataDimension* pDataChild = GetChildDimension();
const ScDPResultDimension* pRefChild = pRefMember->GetChildDimension();
bool bIsRoot = ( pResultMember == nullptr || pResultMember->GetParentLevel() == nullptr );
// leave space for children even if the DataMember hasn't been initialized
// (pDataChild is null then, this happens when no values for it are in this row)
bool bHasChild = ( pRefChild != nullptr );
long nUserSubCount = pRefMember->GetSubTotalCount();
{
// Calculate at least automatic if no subtotals are selected,
// show only own values if there's no child dimension (innermost).
if ( !nUserSubCount || !bHasChild )
nUserSubCount = 1;
ScDPSubTotalState aLocalSubState(rSubState); // keep row state, modify column
long nMemberMeasure = nMeasure;
long nSubSize = pResultData->GetCountForMeasure(nMeasure);
for (long nUserPos=0; nUserPos<nUserSubCount; nUserPos++) // including hidden "automatic"
{
if ( pChildDimension && nUserSubCount > 1 )
{
const ScDPLevel* pForceLevel = pResultMember ? pResultMember->GetParentLevel() : nullptr;
aLocalSubState.nColSubTotalFunc = nUserPos;
aLocalSubState.eColForce = lcl_GetForceFunc( pForceLevel, nUserPos );
}
for ( long nSubCount=0; nSubCount<nSubSize; nSubCount++ )
{
if ( nMeasure == SC_DPMEASURE_ALL )
nMemberMeasure = nSubCount;
// update data...
ScDPAggData* pAggData = GetAggData( nMemberMeasure, aLocalSubState );
if (pAggData)
{
//TODO: aLocalSubState?
sheet::DataPilotFieldReference aReferenceValue = pResultData->GetMeasureRefVal( nMemberMeasure );
sal_Int32 eRefType = aReferenceValue.ReferenceType;
if ( eRefType == sheet::DataPilotFieldReferenceType::RUNNING_TOTAL ||
eRefType == sheet::DataPilotFieldReferenceType::ITEM_DIFFERENCE ||
eRefType == sheet::DataPilotFieldReferenceType::ITEM_PERCENTAGE ||
eRefType == sheet::DataPilotFieldReferenceType::ITEM_PERCENTAGE_DIFFERENCE )
{
bool bRunningTotal = ( eRefType == sheet::DataPilotFieldReferenceType::RUNNING_TOTAL );
bool bRelative =
( aReferenceValue.ReferenceItemType != sheet::DataPilotFieldReferenceItemType::NAMED && !bRunningTotal );
long nRelativeDir = bRelative ?
( ( aReferenceValue.ReferenceItemType == sheet::DataPilotFieldReferenceItemType::PREVIOUS ) ? -1 : 1 ) : 0;
const ScDPRunningTotalState::IndexArray& rColVisible = rRunning.GetColVisible();
const ScDPRunningTotalState::IndexArray& rColSorted = rRunning.GetColSorted();
const ScDPRunningTotalState::IndexArray& rRowVisible = rRunning.GetRowVisible();
const ScDPRunningTotalState::IndexArray& rRowSorted = rRunning.GetRowSorted();
OUString aRefFieldName = aReferenceValue.ReferenceField;
//TODO: aLocalSubState?
sheet::DataPilotFieldOrientation nRefOrient = pResultData->GetMeasureRefOrient( nMemberMeasure );
bool bRefDimInCol = ( nRefOrient == sheet::DataPilotFieldOrientation_COLUMN );
bool bRefDimInRow = ( nRefOrient == sheet::DataPilotFieldOrientation_ROW );
ScDPResultDimension* pSelectDim = nullptr;
long nRowPos = 0;
long nColPos = 0;
// find the reference field in column or row dimensions
if ( bRefDimInRow ) // look in row dimensions
{
pSelectDim = rRunning.GetRowResRoot()->GetChildDimension();
while ( pSelectDim && pSelectDim->GetName() != aRefFieldName )
{
long nIndex = rRowSorted[nRowPos];
if ( nIndex >= 0 && nIndex < pSelectDim->GetMemberCount() )
pSelectDim = pSelectDim->GetMember(nIndex)->GetChildDimension();
else
pSelectDim = nullptr;
++nRowPos;
}
// child dimension of innermost member?
if ( pSelectDim && rRowSorted[nRowPos] < 0 )
pSelectDim = nullptr;
}
if ( bRefDimInCol ) // look in column dimensions
{
pSelectDim = rRunning.GetColResRoot()->GetChildDimension();
while ( pSelectDim && pSelectDim->GetName() != aRefFieldName )
{
long nIndex = rColSorted[nColPos];
if ( nIndex >= 0 && nIndex < pSelectDim->GetMemberCount() )
pSelectDim = pSelectDim->GetMember(nIndex)->GetChildDimension();
else
pSelectDim = nullptr;
++nColPos;
}
// child dimension of innermost member?
if ( pSelectDim && rColSorted[nColPos] < 0 )
pSelectDim = nullptr;
}
bool bNoDetailsInRef = false;
if ( pSelectDim && bRunningTotal )
{
// Running totals:
// If details are hidden for this member in the reference dimension,
// don't show or sum up the value. Otherwise, for following members,
// the running totals of details and subtotals wouldn't match.
long nMyIndex = bRefDimInCol ? rColSorted[nColPos] : rRowSorted[nRowPos];
if ( nMyIndex >= 0 && nMyIndex < pSelectDim->GetMemberCount() )
{
const ScDPResultMember* pMyRefMember = pSelectDim->GetMember(nMyIndex);
if ( pMyRefMember && pMyRefMember->HasHiddenDetails() )
{
pSelectDim = nullptr; // don't calculate
bNoDetailsInRef = true; // show error, not empty
}
}
}
if ( bRelative )
{
// Difference/Percentage from previous/next:
// If details are hidden for this member in the innermost column/row
// dimension (the orientation of the reference dimension), show an
// error value.
// - If the no-details dimension is the reference dimension, its
// members will be skipped when finding the previous/next member,
// so there must be no results for its members.
// - If the no-details dimension is outside of the reference dimension,
// no calculation in the reference dimension is possible.
// - Otherwise, the error isn't strictly necessary, but shown for
// consistency.
bool bInnerNoDetails = bRefDimInCol ? HasHiddenDetails() :
( !bRefDimInRow || rRowParent.HasHiddenDetails() );
if ( bInnerNoDetails )
{
pSelectDim = nullptr;
bNoDetailsInRef = true; // show error, not empty
}
}
if ( !bRefDimInCol && !bRefDimInRow ) // invalid dimension specified
bNoDetailsInRef = true; // pSelectDim is then already NULL
// get the member for the reference item and do the calculation
if ( bRunningTotal )
{
// running total in (dimension) -> find first existing member
if ( pSelectDim )
{
ScDPDataMember* pSelectMember;
if ( bRefDimInCol )
pSelectMember = ScDPResultDimension::GetColReferenceMember( nullptr, nullptr,
nColPos, rRunning );
else
{
const long* pRowSorted = rRowSorted.data();
const long* pColSorted = rColSorted.data();
pRowSorted += nRowPos + 1; // including the reference dimension
pSelectMember = pSelectDim->GetRowReferenceMember(
nullptr, nullptr, pRowSorted, pColSorted);
}
if ( pSelectMember )
{
// The running total is kept as the auxiliary value in
// the first available member for the reference dimension.
// Members are visited in final order, so each one's result
// can be used and then modified.
ScDPAggData* pSelectData = pSelectMember->
GetAggData( nMemberMeasure, aLocalSubState );
if ( pSelectData )
{
double fTotal = pSelectData->GetAuxiliary();
fTotal += pAggData->GetResult();
pSelectData->SetAuxiliary( fTotal );
pAggData->SetResult( fTotal );
pAggData->SetEmpty(false); // always display
}
}
else
pAggData->SetError();
}
else if (bNoDetailsInRef)
pAggData->SetError();
else
pAggData->SetEmpty(true); // empty (dim set to 0 above)
}
else
{
// difference/percentage -> find specified member
if ( pSelectDim )
{
OUString aRefItemName = aReferenceValue.ReferenceItemName;
ScDPRelativePos aRefItemPos( 0, nRelativeDir ); // nBasePos is modified later
const OUString* pRefName = nullptr;
const ScDPRelativePos* pRefPos = nullptr;
if ( bRelative )
pRefPos = &aRefItemPos;
else
pRefName = &aRefItemName;
ScDPDataMember* pSelectMember;
if ( bRefDimInCol )
{
aRefItemPos.nBasePos = rColVisible[nColPos]; // without sort order applied
pSelectMember = ScDPResultDimension::GetColReferenceMember( pRefPos, pRefName,
nColPos, rRunning );
}
else
{
aRefItemPos.nBasePos = rRowVisible[nRowPos]; // without sort order applied
const long* pRowSorted = rRowSorted.data();
const long* pColSorted = rColSorted.data();
pRowSorted += nRowPos + 1; // including the reference dimension
pSelectMember = pSelectDim->GetRowReferenceMember(
pRefPos, pRefName, pRowSorted, pColSorted);
}
// difference or perc.difference is empty for the reference item itself
if ( pSelectMember == this &&
eRefType != sheet::DataPilotFieldReferenceType::ITEM_PERCENTAGE )
{
pAggData->SetEmpty(true);
}
else if ( pSelectMember )
{
const ScDPAggData* pOtherAggData = pSelectMember->
GetConstAggData( nMemberMeasure, aLocalSubState );
OSL_ENSURE( pOtherAggData, "no agg data" );
if ( pOtherAggData )
{
// Reference member may be visited before or after this one,
// so the auxiliary value is used for the original result.
double fOtherResult = pOtherAggData->GetAuxiliary();
double fThisResult = pAggData->GetResult();
bool bError = false;
switch ( eRefType )
{
case sheet::DataPilotFieldReferenceType::ITEM_DIFFERENCE:
fThisResult = fThisResult - fOtherResult;
break;
case sheet::DataPilotFieldReferenceType::ITEM_PERCENTAGE:
if ( fOtherResult == 0.0 )
bError = true;
else
fThisResult = fThisResult / fOtherResult;
break;
case sheet::DataPilotFieldReferenceType::ITEM_PERCENTAGE_DIFFERENCE:
if ( fOtherResult == 0.0 )
bError = true;
else
fThisResult = ( fThisResult - fOtherResult ) / fOtherResult;
break;
default:
OSL_FAIL("invalid calculation type");
}
if ( bError )
{
pAggData->SetError();
}
else
{
pAggData->SetResult(fThisResult);
pAggData->SetEmpty(false); // always display
}
//TODO: errors in data?
}
}
else if (bRelative && !bNoDetailsInRef)
pAggData->SetEmpty(true); // empty
else
pAggData->SetError(); // error
}
else if (bNoDetailsInRef)
pAggData->SetError(); // error
else
pAggData->SetEmpty(true); // empty
}
}
else if ( eRefType == sheet::DataPilotFieldReferenceType::ROW_PERCENTAGE ||
eRefType == sheet::DataPilotFieldReferenceType::COLUMN_PERCENTAGE ||
eRefType == sheet::DataPilotFieldReferenceType::TOTAL_PERCENTAGE ||
eRefType == sheet::DataPilotFieldReferenceType::INDEX )
{
// set total values when they are encountered (always before their use)
ScDPAggData* pColTotalData = pRefMember->GetColTotal( nMemberMeasure );
ScDPAggData* pRowTotalData = rTotals.GetRowTotal( nMemberMeasure );
ScDPAggData* pGrandTotalData = rTotals.GetGrandTotal( nMemberMeasure );
double fTotalValue = pAggData->HasError() ? 0 : pAggData->GetResult();
if ( bIsRoot && rTotals.IsInColRoot() && pGrandTotalData )
pGrandTotalData->SetAuxiliary( fTotalValue );
if ( bIsRoot && pRowTotalData )
pRowTotalData->SetAuxiliary( fTotalValue );
if ( rTotals.IsInColRoot() && pColTotalData )
pColTotalData->SetAuxiliary( fTotalValue );
// find relation to total values
switch ( eRefType )
{
case sheet::DataPilotFieldReferenceType::ROW_PERCENTAGE:
case sheet::DataPilotFieldReferenceType::COLUMN_PERCENTAGE:
case sheet::DataPilotFieldReferenceType::TOTAL_PERCENTAGE:
{
double nTotal;
if ( eRefType == sheet::DataPilotFieldReferenceType::ROW_PERCENTAGE )
nTotal = pRowTotalData ? pRowTotalData->GetAuxiliary() : 0.0;
else if ( eRefType == sheet::DataPilotFieldReferenceType::COLUMN_PERCENTAGE )
nTotal = pColTotalData ? pColTotalData->GetAuxiliary() : 0.0;
else
nTotal = pGrandTotalData ? pGrandTotalData->GetAuxiliary() : 0.0;
if ( nTotal == 0.0 )
pAggData->SetError();
else
pAggData->SetResult( pAggData->GetResult() / nTotal );
}
break;
case sheet::DataPilotFieldReferenceType::INDEX:
{
double nColTotal = pColTotalData ? pColTotalData->GetAuxiliary() : 0.0;
double nRowTotal = pRowTotalData ? pRowTotalData->GetAuxiliary() : 0.0;
double nGrandTotal = pGrandTotalData ? pGrandTotalData->GetAuxiliary() : 0.0;
if ( nRowTotal == 0.0 || nColTotal == 0.0 )
pAggData->SetError();
else
pAggData->SetResult(
( pAggData->GetResult() * nGrandTotal ) /
( nRowTotal * nColTotal ) );
}
break;
}
}
}
}
}
}
if ( bHasChild ) // child dimension must be processed last, so the row total is known
{
if ( pDataChild )
pDataChild->UpdateRunningTotals( pRefChild, nMeasure,
bIsSubTotalRow, rSubState, rRunning, rTotals, rRowParent );
}
}
#if DUMP_PIVOT_TABLE
void ScDPDataMember::DumpState( const ScDPResultMember* pRefMember, ScDocument* pDoc, ScAddress& rPos ) const
{
dumpRow("ScDPDataMember", GetName(), &aAggregate, pDoc, rPos);
SCROW nStartRow = rPos.Row();
const ScDPDataDimension* pDataChild = GetChildDimension();
const ScDPResultDimension* pRefChild = pRefMember->GetChildDimension();
if ( pDataChild && pRefChild )
pDataChild->DumpState( pRefChild, pDoc, rPos );
indent(pDoc, nStartRow, rPos);
}
void ScDPDataMember::Dump(int nIndent) const
{
std::string aIndent(nIndent*2, ' ');
std::cout << aIndent << "-- data member '"
<< (pResultMember ? pResultMember->GetName() : OUString()) << "'" << std::endl;
for (const ScDPAggData* pAgg = &aAggregate; pAgg; pAgg = pAgg->GetExistingChild())
pAgg->Dump(nIndent+1);
if (pChildDimension)
pChildDimension->Dump(nIndent+1);
}
#endif
// Helper class to select the members to include in
// ScDPResultDimension::InitFrom or LateInitFrom if groups are used
namespace {
class ScDPGroupCompare
{
private:
const ScDPResultData* pResultData;
const ScDPInitState& rInitState;
long nDimSource;
bool bIncludeAll;
bool bIsBase;
long nGroupBase;
public:
ScDPGroupCompare( const ScDPResultData* pData, const ScDPInitState& rState, long nDimension );
bool IsIncluded( const ScDPMember& rMember ) { return bIncludeAll || TestIncluded( rMember ); }
bool TestIncluded( const ScDPMember& rMember );
};
}
ScDPGroupCompare::ScDPGroupCompare( const ScDPResultData* pData, const ScDPInitState& rState, long nDimension ) :
pResultData( pData ),
rInitState( rState ),
nDimSource( nDimension )
{
bIsBase = pResultData->IsBaseForGroup( nDimSource );
nGroupBase = pResultData->GetGroupBase( nDimSource ); //TODO: get together in one call?
// if bIncludeAll is set, TestIncluded doesn't need to be called
bIncludeAll = !( bIsBase || nGroupBase >= 0 );
}
bool ScDPGroupCompare::TestIncluded( const ScDPMember& rMember )
{
bool bInclude = true;
if ( bIsBase )
{
// need to check all previous groups
//TODO: get array of groups (or indexes) before loop?
ScDPItemData aMemberData(rMember.FillItemData());
const std::vector<ScDPInitState::Member>& rMemStates = rInitState.GetMembers();
bInclude = std::all_of(rMemStates.begin(), rMemStates.end(),
[this, &aMemberData](const ScDPInitState::Member& rMem) {
return (pResultData->GetGroupBase(rMem.mnSrcIndex) != nDimSource)
|| pResultData->IsInGroup(rMem.mnNameIndex, rMem.mnSrcIndex, aMemberData, nDimSource);
});
}
else if ( nGroupBase >= 0 )
{
// base isn't used in preceding fields
// -> look for other groups using the same base
//TODO: get array of groups (or indexes) before loop?
ScDPItemData aMemberData(rMember.FillItemData());
const std::vector<ScDPInitState::Member>& rMemStates = rInitState.GetMembers();
bInclude = std::all_of(rMemStates.begin(), rMemStates.end(),
[this, &aMemberData](const ScDPInitState::Member& rMem) {
// coverity[copy_paste_error : FALSE] - same base (hierarchy between
// the two groups is irrelevant)
return (pResultData->GetGroupBase(rMem.mnSrcIndex) != nGroupBase)
|| pResultData->HasCommonElement(rMem.mnNameIndex, rMem.mnSrcIndex, aMemberData, nDimSource);
});
}
return bInclude;
}
ScDPResultDimension::ScDPResultDimension( const ScDPResultData* pData ) :
pResultData( pData ),
nSortMeasure( 0 ),
bIsDataLayout( false ),
bSortByData( false ),
bSortAscending( false ),
bAutoShow( false ),
bAutoTopItems( false ),
bInitialized( false ),
nAutoMeasure( 0 ),
nAutoCount( 0 )
{
}
ScDPResultDimension::~ScDPResultDimension()
{
}
ScDPResultMember *ScDPResultDimension::FindMember( SCROW iData ) const
{
if( bIsDataLayout )
return maMemberArray[0].get();
MemberHash::const_iterator aRes = maMemberHash.find( iData );
if( aRes != maMemberHash.end()) {
if ( aRes->second->IsNamedItem( iData ) )
return aRes->second;
OSL_FAIL("problem! hash result is not the same as IsNamedItem");
}
unsigned int i;
unsigned int nCount = maMemberArray.size();
for( i = 0; i < nCount ; i++ )
{
ScDPResultMember* pResultMember = maMemberArray[i].get();
if ( pResultMember->IsNamedItem( iData ) )
return pResultMember;
}
return nullptr;
}
void ScDPResultDimension::InitFrom(
const vector<ScDPDimension*>& ppDim, const vector<ScDPLevel*>& ppLev,
size_t nPos, ScDPInitState& rInitState, bool bInitChild )
{
if (nPos >= ppDim.size() || nPos >= ppLev.size())
{
bInitialized = true;
return;
}
ScDPDimension* pThisDim = ppDim[nPos];
ScDPLevel* pThisLevel = ppLev[nPos];
if (!pThisDim || !pThisLevel)
{
bInitialized = true;
return;
}
bIsDataLayout = pThisDim->getIsDataLayoutDimension(); // member
aDimensionName = pThisDim->getName(); // member
// Check the autoshow setting. If it's enabled, store the settings.
const sheet::DataPilotFieldAutoShowInfo& rAutoInfo = pThisLevel->GetAutoShow();
if ( rAutoInfo.IsEnabled )
{
bAutoShow = true;
bAutoTopItems = ( rAutoInfo.ShowItemsMode == sheet::DataPilotFieldShowItemsMode::FROM_TOP );
nAutoMeasure = pThisLevel->GetAutoMeasure();
nAutoCount = rAutoInfo.ItemCount;
}
// Check the sort info, and store the settings if appropriate.
const sheet::DataPilotFieldSortInfo& rSortInfo = pThisLevel->GetSortInfo();
if ( rSortInfo.Mode == sheet::DataPilotFieldSortMode::DATA )
{
bSortByData = true;
bSortAscending = rSortInfo.IsAscending;
nSortMeasure = pThisLevel->GetSortMeasure();
}
// global order is used to initialize aMembers, so it doesn't have to be looked at later
const ScMemberSortOrder& rGlobalOrder = pThisLevel->GetGlobalOrder();
long nDimSource = pThisDim->GetDimension(); //TODO: check GetSourceDim?
ScDPGroupCompare aCompare( pResultData, rInitState, nDimSource );
// Now, go through all members and initialize them.
ScDPMembers* pMembers = pThisLevel->GetMembersObject();
long nMembCount = pMembers->getCount();
for ( long i=0; i<nMembCount; i++ )
{
long nSorted = rGlobalOrder.empty() ? i : rGlobalOrder[i];
ScDPMember* pMember = pMembers->getByIndex(nSorted);
if ( aCompare.IsIncluded( *pMember ) )
{
ScDPParentDimData aData( i, pThisDim, pThisLevel, pMember);
ScDPResultMember* pNew = AddMember( aData );
rInitState.AddMember(nDimSource, pNew->GetDataId());
pNew->InitFrom( ppDim, ppLev, nPos+1, rInitState, bInitChild );
rInitState.RemoveMember();
}
}
bInitialized = true;
}
void ScDPResultDimension::LateInitFrom(
LateInitParams& rParams, const vector<SCROW>& pItemData, size_t nPos, ScDPInitState& rInitState)
{
if ( rParams.IsEnd( nPos ) )
return;
if (nPos >= pItemData.size())
{
SAL_WARN("sc.core", "pos " << nPos << ", but vector size is " << pItemData.size());
return;
}
SCROW rThisData = pItemData[nPos];
ScDPDimension* pThisDim = rParams.GetDim( nPos );
ScDPLevel* pThisLevel = rParams.GetLevel( nPos );
if (!pThisDim || !pThisLevel)
return;
long nDimSource = pThisDim->GetDimension(); //TODO: check GetSourceDim?
bool bShowEmpty = pThisLevel->getShowEmpty();
if ( !bInitialized )
{ // init some values
// create all members at the first call (preserve order)
bIsDataLayout = pThisDim->getIsDataLayoutDimension();
aDimensionName = pThisDim->getName();
const sheet::DataPilotFieldAutoShowInfo& rAutoInfo = pThisLevel->GetAutoShow();
if ( rAutoInfo.IsEnabled )
{
bAutoShow = true;
bAutoTopItems = ( rAutoInfo.ShowItemsMode == sheet::DataPilotFieldShowItemsMode::FROM_TOP );
nAutoMeasure = pThisLevel->GetAutoMeasure();
nAutoCount = rAutoInfo.ItemCount;
}
const sheet::DataPilotFieldSortInfo& rSortInfo = pThisLevel->GetSortInfo();
if ( rSortInfo.Mode == sheet::DataPilotFieldSortMode::DATA )
{
bSortByData = true;
bSortAscending = rSortInfo.IsAscending;
nSortMeasure = pThisLevel->GetSortMeasure();
}
}
bool bLateInitAllMembers= bIsDataLayout || rParams.GetInitAllChild() || bShowEmpty;
if ( !bLateInitAllMembers )
{
ResultMembers& rMembers = pResultData->GetDimResultMembers(nDimSource, pThisDim, pThisLevel);
bLateInitAllMembers = rMembers.IsHasHideDetailsMembers();
SAL_INFO("sc.core", aDimensionName << (rMembers.IsHasHideDetailsMembers() ? " HasHideDetailsMembers" : ""));
rMembers.SetHasHideDetailsMembers( false );
}
bool bNewAllMembers = (!rParams.IsRow()) || nPos == 0 || bLateInitAllMembers;
if (bNewAllMembers )
{
// global order is used to initialize aMembers, so it doesn't have to be looked at later
if ( !bInitialized )
{ //init all members
const ScMemberSortOrder& rGlobalOrder = pThisLevel->GetGlobalOrder();
ScDPGroupCompare aCompare( pResultData, rInitState, nDimSource );
ScDPMembers* pMembers = pThisLevel->GetMembersObject();
long nMembCount = pMembers->getCount();
for ( long i=0; i<nMembCount; i++ )
{
long nSorted = rGlobalOrder.empty() ? i : rGlobalOrder[i];
ScDPMember* pMember = pMembers->getByIndex(nSorted);
if ( aCompare.IsIncluded( *pMember ) )
{ // add all members
ScDPParentDimData aData( i, pThisDim, pThisLevel, pMember );
AddMember( aData );
}
}
bInitialized = true; // don't call again, even if no members were included
}
// initialize only specific member (or all if "show empty" flag is set)
if ( bLateInitAllMembers )
{
long nCount = maMemberArray.size();
for (long i=0; i<nCount; i++)
{
ScDPResultMember* pResultMember = maMemberArray[i].get();
// check show empty
bool bAllChildren = false;
if( bShowEmpty )
{
bAllChildren = !pResultMember->IsNamedItem( rThisData );
}
rParams.SetInitAllChildren( bAllChildren );
rInitState.AddMember( nDimSource, pResultMember->GetDataId() );
pResultMember->LateInitFrom( rParams, pItemData, nPos+1, rInitState );
rInitState.RemoveMember();
}
}
else
{
ScDPResultMember* pResultMember = FindMember( rThisData );
if( nullptr != pResultMember )
{
rInitState.AddMember( nDimSource, pResultMember->GetDataId() );
pResultMember->LateInitFrom( rParams, pItemData, nPos+1, rInitState );
rInitState.RemoveMember();
}
}
}
else
InitWithMembers( rParams, pItemData, nPos, rInitState );
}
long ScDPResultDimension::GetSize(long nMeasure) const
{
long nTotal = 0;
long nMemberCount = maMemberArray.size();
if (bIsDataLayout)
{
OSL_ENSURE(nMeasure == SC_DPMEASURE_ALL || pResultData->GetMeasureCount() == 1,
"DataLayout dimension twice?");
// repeat first member...
nTotal = nMemberCount * maMemberArray[0]->GetSize(0); // all measures have equal size
}
else
{
// add all members
for (long nMem=0; nMem<nMemberCount; nMem++)
nTotal += maMemberArray[nMem]->GetSize(nMeasure);
}
return nTotal;
}
bool ScDPResultDimension::IsValidEntry( const vector< SCROW >& aMembers ) const
{
if (aMembers.empty())
return false;
const ScDPResultMember* pMember = FindMember( aMembers[0] );
if ( nullptr != pMember )
return pMember->IsValidEntry( aMembers );
#if OSL_DEBUG_LEVEL > 0
SAL_INFO("sc.core", "IsValidEntry: Member not found, DimNam = " << GetName());
#endif
return false;
}
void ScDPResultDimension::ProcessData( const vector< SCROW >& aMembers,
const ScDPResultDimension* pDataDim,
const vector< SCROW >& aDataMembers,
const vector<ScDPValue>& aValues ) const
{
if (aMembers.empty())
return;
ScDPResultMember* pMember = FindMember( aMembers[0] );
if ( nullptr != pMember )
{
vector<SCROW> aChildMembers;
if (aMembers.size() > 1)
{
vector<SCROW>::const_iterator itr = aMembers.begin();
aChildMembers.insert(aChildMembers.begin(), ++itr, aMembers.end());
}
pMember->ProcessData( aChildMembers, pDataDim, aDataMembers, aValues );
return;
}
OSL_FAIL("ProcessData: Member not found");
}
void ScDPResultDimension::FillMemberResults( uno::Sequence<sheet::MemberResult>* pSequences,
long nStart, long nMeasure )
{
long nPos = nStart;
long nCount = maMemberArray.size();
for (long i=0; i<nCount; i++)
{
long nSorted = aMemberOrder.empty() ? i : aMemberOrder[i];
ScDPResultMember* pMember = maMemberArray[nSorted].get();
// in data layout dimension, use first member with different measures/names
if ( bIsDataLayout )
{
bool bTotalResult = false;
OUString aMbrName = pResultData->GetMeasureDimensionName( nSorted );
OUString aMbrCapt = pResultData->GetMeasureString( nSorted, false, SUBTOTAL_FUNC_NONE, bTotalResult );
maMemberArray[0]->FillMemberResults( pSequences, nPos, nSorted, false, &aMbrName, &aMbrCapt );
}
else if ( pMember->IsVisible() )
{
pMember->FillMemberResults( pSequences, nPos, nMeasure, false, nullptr, nullptr );
}
// nPos is modified
}
}
void ScDPResultDimension::FillDataResults(
const ScDPResultMember* pRefMember, ScDPResultFilterContext& rFilterCxt,
uno::Sequence< uno::Sequence<sheet::DataResult> >& rSequence, long nMeasure) const
{
FilterStack aFilterStack(rFilterCxt.maFilters);
aFilterStack.pushDimName(GetName(), bIsDataLayout);
long nMemberMeasure = nMeasure;
long nCount = maMemberArray.size();
for (long i=0; i<nCount; i++)
{
long nSorted = aMemberOrder.empty() ? i : aMemberOrder[i];
const ScDPResultMember* pMember;
if (bIsDataLayout)
{
OSL_ENSURE(nMeasure == SC_DPMEASURE_ALL || pResultData->GetMeasureCount() == 1,
"DataLayout dimension twice?");
pMember = maMemberArray[0].get();
nMemberMeasure = nSorted;
}
else
pMember = maMemberArray[nSorted].get();
if ( pMember->IsVisible() )
pMember->FillDataResults(pRefMember, rFilterCxt, rSequence, nMemberMeasure);
}
}
void ScDPResultDimension::UpdateDataResults( const ScDPResultMember* pRefMember, long nMeasure ) const
{
long nMemberMeasure = nMeasure;
long nCount = maMemberArray.size();
for (long i=0; i<nCount; i++)
{
const ScDPResultMember* pMember;
if (bIsDataLayout)
{
OSL_ENSURE(nMeasure == SC_DPMEASURE_ALL || pResultData->GetMeasureCount() == 1,
"DataLayout dimension twice?");
pMember = maMemberArray[0].get();
nMemberMeasure = i;
}
else
pMember = maMemberArray[i].get();
if ( pMember->IsVisible() )
pMember->UpdateDataResults( pRefMember, nMemberMeasure );
}
}
void ScDPResultDimension::SortMembers( ScDPResultMember* pRefMember )
{
long nCount = maMemberArray.size();
if ( bSortByData )
{
// sort members
OSL_ENSURE( aMemberOrder.empty(), "sort twice?" );
aMemberOrder.resize( nCount );
for (long nPos=0; nPos<nCount; nPos++)
aMemberOrder[nPos] = nPos;
ScDPRowMembersOrder aComp( *this, nSortMeasure, bSortAscending );
::std::sort( aMemberOrder.begin(), aMemberOrder.end(), aComp );
}
// handle children
// for data layout, call only once - sorting measure is always taken from settings
long nLoopCount = bIsDataLayout ? 1 : nCount;
for (long i=0; i<nLoopCount; i++)
{
ScDPResultMember* pMember = maMemberArray[i].get();
if ( pMember->IsVisible() )
pMember->SortMembers( pRefMember );
}
}
void ScDPResultDimension::DoAutoShow( ScDPResultMember* pRefMember )
{
long nCount = maMemberArray.size();
// handle children first, before changing the visible state
// for data layout, call only once - sorting measure is always taken from settings
long nLoopCount = bIsDataLayout ? 1 : nCount;
for (long i=0; i<nLoopCount; i++)
{
ScDPResultMember* pMember = maMemberArray[i].get();
if ( pMember->IsVisible() )
pMember->DoAutoShow( pRefMember );
}
if ( bAutoShow && nAutoCount > 0 && nAutoCount < nCount )
{
// establish temporary order, hide remaining members
ScMemberSortOrder aAutoOrder;
aAutoOrder.resize( nCount );
long nPos;
for (nPos=0; nPos<nCount; nPos++)
aAutoOrder[nPos] = nPos;
ScDPRowMembersOrder aComp( *this, nAutoMeasure, !bAutoTopItems );
::std::sort( aAutoOrder.begin(), aAutoOrder.end(), aComp );
// look for equal values to the last included one
long nIncluded = nAutoCount;
const ScDPResultMember* pMember1 = maMemberArray[aAutoOrder[nIncluded - 1]].get();
const ScDPDataMember* pDataMember1 = pMember1->IsVisible() ? pMember1->GetDataRoot() : nullptr;
bool bContinue = true;
while ( bContinue )
{
bContinue = false;
if ( nIncluded < nCount )
{
const ScDPResultMember* pMember2 = maMemberArray[aAutoOrder[nIncluded]].get();
const ScDPDataMember* pDataMember2 = pMember2->IsVisible() ? pMember2->GetDataRoot() : nullptr;
if ( lcl_IsEqual( pDataMember1, pDataMember2, nAutoMeasure ) )
{
++nIncluded; // include more members if values are equal
bContinue = true;
}
}
}
// hide the remaining members
for (nPos = nIncluded; nPos < nCount; nPos++)
{
ScDPResultMember* pMember = maMemberArray[aAutoOrder[nPos]].get();
pMember->SetAutoHidden();
}
}
}
void ScDPResultDimension::ResetResults()
{
long nCount = maMemberArray.size();
for (long i=0; i<nCount; i++)
{
// sort order doesn't matter
ScDPResultMember* pMember = maMemberArray[bIsDataLayout ? 0 : i].get();
pMember->ResetResults();
}
}
long ScDPResultDimension::GetSortedIndex( long nUnsorted ) const
{
return aMemberOrder.empty() ? nUnsorted : aMemberOrder[nUnsorted];
}
void ScDPResultDimension::UpdateRunningTotals( const ScDPResultMember* pRefMember, long nMeasure,
ScDPRunningTotalState& rRunning, ScDPRowTotals& rTotals ) const
{
const ScDPResultMember* pMember;
long nMemberMeasure = nMeasure;
long nCount = maMemberArray.size();
for (long i=0; i<nCount; i++)
{
long nSorted = aMemberOrder.empty() ? i : aMemberOrder[i];
if (bIsDataLayout)
{
OSL_ENSURE(nMeasure == SC_DPMEASURE_ALL || pResultData->GetMeasureCount() == 1,
"DataLayout dimension twice?");
pMember = maMemberArray[0].get();
nMemberMeasure = nSorted;
}
else
pMember = maMemberArray[nSorted].get();
if ( pMember->IsVisible() )
{
if ( bIsDataLayout )
rRunning.AddRowIndex( 0, 0 );
else
rRunning.AddRowIndex( i, nSorted );
pMember->UpdateRunningTotals( pRefMember, nMemberMeasure, rRunning, rTotals );
rRunning.RemoveRowIndex();
}
}
}
ScDPDataMember* ScDPResultDimension::GetRowReferenceMember(
const ScDPRelativePos* pRelativePos, const OUString* pName,
const long* pRowIndexes, const long* pColIndexes ) const
{
// get named, previous/next, or first member of this dimension (first existing if pRelativePos and pName are NULL)
OSL_ENSURE( pRelativePos == nullptr || pName == nullptr, "can't use position and name" );
ScDPDataMember* pColMember = nullptr;
bool bFirstExisting = ( pRelativePos == nullptr && pName == nullptr );
long nMemberCount = maMemberArray.size();
long nMemberIndex = 0; // unsorted
long nDirection = 1; // forward if no relative position is used
if ( pRelativePos )
{
nDirection = pRelativePos->nDirection;
nMemberIndex = pRelativePos->nBasePos + nDirection; // bounds are handled below
OSL_ENSURE( nDirection == 1 || nDirection == -1, "Direction must be 1 or -1" );
}
else if ( pName )
{
// search for named member
const ScDPResultMember* pRowMember = maMemberArray[GetSortedIndex(nMemberIndex)].get();
//TODO: use ScDPItemData, as in ScDPDimension::IsValidPage?
while ( pRowMember && pRowMember->GetName() != *pName )
{
++nMemberIndex;
if ( nMemberIndex < nMemberCount )
pRowMember = maMemberArray[GetSortedIndex(nMemberIndex)].get();
else
pRowMember = nullptr;
}
}
bool bContinue = true;
while ( bContinue && nMemberIndex >= 0 && nMemberIndex < nMemberCount )
{
const ScDPResultMember* pRowMember = maMemberArray[GetSortedIndex(nMemberIndex)].get();
// get child members by given indexes
const long* pNextRowIndex = pRowIndexes;
while ( *pNextRowIndex >= 0 && pRowMember )
{
const ScDPResultDimension* pRowChild = pRowMember->GetChildDimension();
if ( pRowChild && *pNextRowIndex < pRowChild->GetMemberCount() )
pRowMember = pRowChild->GetMember( *pNextRowIndex );
else
pRowMember = nullptr;
++pNextRowIndex;
}
if ( pRowMember && pRelativePos )
{
// Skip the member if it has hidden details
// (because when looking for the details, it is skipped, too).
// Also skip if the member is invisible because it has no data,
// for consistent ordering.
if ( pRowMember->HasHiddenDetails() || !pRowMember->IsVisible() )
pRowMember = nullptr;
}
if ( pRowMember )
{
pColMember = pRowMember->GetDataRoot();
const long* pNextColIndex = pColIndexes;
while ( *pNextColIndex >= 0 && pColMember )
{
ScDPDataDimension* pColChild = pColMember->GetChildDimension();
if ( pColChild && *pNextColIndex < pColChild->GetMemberCount() )
pColMember = pColChild->GetMember( *pNextColIndex );
else
pColMember = nullptr;
++pNextColIndex;
}
}
// continue searching only if looking for first existing or relative position
bContinue = ( pColMember == nullptr && ( bFirstExisting || pRelativePos ) );
nMemberIndex += nDirection;
}
return pColMember;
}
ScDPDataMember* ScDPResultDimension::GetColReferenceMember(
const ScDPRelativePos* pRelativePos, const OUString* pName,
long nRefDimPos, const ScDPRunningTotalState& rRunning )
{
OSL_ENSURE( pRelativePos == nullptr || pName == nullptr, "can't use position and name" );
const long* pColIndexes = rRunning.GetColSorted().data();
const long* pRowIndexes = rRunning.GetRowSorted().data();
// get own row member using all indexes
const ScDPResultMember* pRowMember = rRunning.GetRowResRoot();
ScDPDataMember* pColMember = nullptr;
const long* pNextRowIndex = pRowIndexes;
while ( *pNextRowIndex >= 0 && pRowMember )
{
const ScDPResultDimension* pRowChild = pRowMember->GetChildDimension();
if ( pRowChild && *pNextRowIndex < pRowChild->GetMemberCount() )
pRowMember = pRowChild->GetMember( *pNextRowIndex );
else
pRowMember = nullptr;
++pNextRowIndex;
}
// get column (data) members before the reference field
//TODO: pass rRowParent from ScDPDataMember::UpdateRunningTotals instead
if ( pRowMember )
{
pColMember = pRowMember->GetDataRoot();
const long* pNextColIndex = pColIndexes;
long nColSkipped = 0;
while ( *pNextColIndex >= 0 && pColMember && nColSkipped < nRefDimPos )
{
ScDPDataDimension* pColChild = pColMember->GetChildDimension();
if ( pColChild && *pNextColIndex < pColChild->GetMemberCount() )
pColMember = pColChild->GetMember( *pNextColIndex );
else
pColMember = nullptr;
++pNextColIndex;
++nColSkipped;
}
}
// get column member for the reference field
if ( pColMember )
{
ScDPDataDimension* pReferenceDim = pColMember->GetChildDimension();
if ( pReferenceDim )
{
long nReferenceCount = pReferenceDim->GetMemberCount();
bool bFirstExisting = ( pRelativePos == nullptr && pName == nullptr );
long nMemberIndex = 0; // unsorted
long nDirection = 1; // forward if no relative position is used
pColMember = nullptr; // don't use parent dimension's member if none found
if ( pRelativePos )
{
nDirection = pRelativePos->nDirection;
nMemberIndex = pRelativePos->nBasePos + nDirection; // bounds are handled below
}
else if ( pName )
{
// search for named member
pColMember = pReferenceDim->GetMember( pReferenceDim->GetSortedIndex( nMemberIndex ) );
//TODO: use ScDPItemData, as in ScDPDimension::IsValidPage?
while ( pColMember && pColMember->GetName() != *pName )
{
++nMemberIndex;
if ( nMemberIndex < nReferenceCount )
pColMember = pReferenceDim->GetMember( pReferenceDim->GetSortedIndex( nMemberIndex ) );
else
pColMember = nullptr;
}
}
bool bContinue = true;
while ( bContinue && nMemberIndex >= 0 && nMemberIndex < nReferenceCount )
{
pColMember = pReferenceDim->GetMember( pReferenceDim->GetSortedIndex( nMemberIndex ) );
// get column members below the reference field
const long* pNextColIndex = pColIndexes + nRefDimPos + 1;
while ( *pNextColIndex >= 0 && pColMember )
{
ScDPDataDimension* pColChild = pColMember->GetChildDimension();
if ( pColChild && *pNextColIndex < pColChild->GetMemberCount() )
pColMember = pColChild->GetMember( *pNextColIndex );
else
pColMember = nullptr;
++pNextColIndex;
}
if ( pColMember && pRelativePos )
{
// Skip the member if it has hidden details
// (because when looking for the details, it is skipped, too).
// Also skip if the member is invisible because it has no data,
// for consistent ordering.
if ( pColMember->HasHiddenDetails() || !pColMember->IsVisible() )
pColMember = nullptr;
}
// continue searching only if looking for first existing or relative position
bContinue = ( pColMember == nullptr && ( bFirstExisting || pRelativePos ) );
nMemberIndex += nDirection;
}
}
else
pColMember = nullptr;
}
return pColMember;
}
#if DUMP_PIVOT_TABLE
void ScDPResultDimension::DumpState( const ScDPResultMember* pRefMember, ScDocument* pDoc, ScAddress& rPos ) const
{
OUString aDimName = bIsDataLayout ? OUString("(data layout)") : GetName();
dumpRow("ScDPResultDimension", aDimName, nullptr, pDoc, rPos);
SCROW nStartRow = rPos.Row();
long nCount = bIsDataLayout ? 1 : maMemberArray.size();
for (long i=0; i<nCount; i++)
{
const ScDPResultMember* pMember = maMemberArray[i].get();
pMember->DumpState( pRefMember, pDoc, rPos );
}
indent(pDoc, nStartRow, rPos);
}
void ScDPResultDimension::Dump(int nIndent) const
{
std::string aIndent(nIndent*2, ' ');
std::cout << aIndent << "-- dimension '" << GetName() << "'" << std::endl;
for (const auto& rxMember : maMemberArray)
{
const ScDPResultMember* p = rxMember.get();
p->Dump(nIndent+1);
}
}
#endif
long ScDPResultDimension::GetMemberCount() const
{
return maMemberArray.size();
}
const ScDPResultMember* ScDPResultDimension::GetMember(long n) const
{
return maMemberArray[n].get();
}
ScDPResultMember* ScDPResultDimension::GetMember(long n)
{
return maMemberArray[n].get();
}
ScDPResultDimension* ScDPResultDimension::GetFirstChildDimension() const
{
if ( !maMemberArray.empty() )
return maMemberArray[0]->GetChildDimension();
else
return nullptr;
}
void ScDPResultDimension::FillVisibilityData(ScDPResultVisibilityData& rData) const
{
if (IsDataLayout())
return;
for (const auto& rxMember : maMemberArray)
{
ScDPResultMember* pMember = rxMember.get();
if (pMember->IsValid())
{
ScDPItemData aItem(pMember->FillItemData());
rData.addVisibleMember(GetName(), aItem);
pMember->FillVisibilityData(rData);
}
}
}
ScDPDataDimension::ScDPDataDimension( const ScDPResultData* pData ) :
pResultData( pData ),
pResultDimension( nullptr ),
bIsDataLayout( false )
{
}
ScDPDataDimension::~ScDPDataDimension()
{
}
void ScDPDataDimension::InitFrom( const ScDPResultDimension* pDim )
{
if (!pDim)
return;
pResultDimension = pDim;
bIsDataLayout = pDim->IsDataLayout();
// Go through all result members under the given result dimension, and
// create a new data member instance for each result member.
long nCount = pDim->GetMemberCount();
for (long i=0; i<nCount; i++)
{
const ScDPResultMember* pResMem = pDim->GetMember(i);
ScDPDataMember* pNew = new ScDPDataMember( pResultData, pResMem );
maMembers.emplace_back( pNew);
if ( !pResultData->IsLateInit() )
{
// with LateInit, pResMem hasn't necessarily been initialized yet,
// so InitFrom for the new result member is called from its ProcessData method
const ScDPResultDimension* pChildDim = pResMem->GetChildDimension();
if ( pChildDim )
pNew->InitFrom( pChildDim );
}
}
}
void ScDPDataDimension::ProcessData( const vector< SCROW >& aDataMembers, const vector<ScDPValue>& aValues,
const ScDPSubTotalState& rSubState )
{
// the ScDPItemData array must contain enough entries for all dimensions - this isn't checked
long nCount = maMembers.size();
for (long i=0; i<nCount; i++)
{
ScDPDataMember* pMember = maMembers[static_cast<sal_uInt16>(i)].get();
// always first member for data layout dim
if ( bIsDataLayout || ( !aDataMembers.empty() && pMember->IsNamedItem(aDataMembers[0]) ) )
{
vector<SCROW> aChildDataMembers;
if (aDataMembers.size() > 1)
{
vector<SCROW>::const_iterator itr = aDataMembers.begin();
aChildDataMembers.insert(aChildDataMembers.begin(), ++itr, aDataMembers.end());
}
pMember->ProcessData( aChildDataMembers, aValues, rSubState );
return;
}
}
OSL_FAIL("ProcessData: Member not found");
}
void ScDPDataDimension::FillDataRow(
const ScDPResultDimension* pRefDim, ScDPResultFilterContext& rFilterCxt,
uno::Sequence<sheet::DataResult>& rSequence, long nMeasure, bool bIsSubTotalRow,
const ScDPSubTotalState& rSubState) const
{
OUString aDimName;
bool bDataLayout = false;
if (pResultDimension)
{
aDimName = pResultDimension->GetName();
bDataLayout = pResultDimension->IsDataLayout();
}
FilterStack aFilterStack(rFilterCxt.maFilters);
aFilterStack.pushDimName(aDimName, bDataLayout);
OSL_ENSURE( pRefDim && static_cast<size_t>(pRefDim->GetMemberCount()) == maMembers.size(), "dimensions don't match" );
OSL_ENSURE( pRefDim == pResultDimension, "wrong dim" );
const ScMemberSortOrder& rMemberOrder = pRefDim->GetMemberOrder();
long nMemberMeasure = nMeasure;
long nCount = maMembers.size();
for (long i=0; i<nCount; i++)
{
long nSorted = rMemberOrder.empty() ? i : rMemberOrder[i];
long nMemberPos = nSorted;
if (bIsDataLayout)
{
OSL_ENSURE(nMeasure == SC_DPMEASURE_ALL || pResultData->GetMeasureCount() == 1,
"DataLayout dimension twice?");
nMemberPos = 0;
nMemberMeasure = nSorted;
}
const ScDPResultMember* pRefMember = pRefDim->GetMember(nMemberPos);
if ( pRefMember->IsVisible() ) //TODO: here or in ScDPDataMember::FillDataRow ???
{
const ScDPDataMember* pDataMember = maMembers[static_cast<sal_uInt16>(nMemberPos)].get();
pDataMember->FillDataRow(pRefMember, rFilterCxt, rSequence, nMemberMeasure, bIsSubTotalRow, rSubState);
}
}
}
void ScDPDataDimension::UpdateDataRow( const ScDPResultDimension* pRefDim,
long nMeasure, bool bIsSubTotalRow,
const ScDPSubTotalState& rSubState ) const
{
OSL_ENSURE( pRefDim && static_cast<size_t>(pRefDim->GetMemberCount()) == maMembers.size(), "dimensions don't match" );
OSL_ENSURE( pRefDim == pResultDimension, "wrong dim" );
long nMemberMeasure = nMeasure;
long nCount = maMembers.size();
for (long i=0; i<nCount; i++)
{
long nMemberPos = i;
if (bIsDataLayout)
{
OSL_ENSURE(nMeasure == SC_DPMEASURE_ALL || pResultData->GetMeasureCount() == 1,
"DataLayout dimension twice?");
nMemberPos = 0;
nMemberMeasure = i;
}
// Calculate must be called even if the member is not visible (for use as reference value)
const ScDPResultMember* pRefMember = pRefDim->GetMember(nMemberPos);
ScDPDataMember* pDataMember = maMembers[static_cast<sal_uInt16>(nMemberPos)].get();
pDataMember->UpdateDataRow( pRefMember, nMemberMeasure, bIsSubTotalRow, rSubState );
}
}
void ScDPDataDimension::SortMembers( ScDPResultDimension* pRefDim )
{
long nCount = maMembers.size();
if ( pRefDim->IsSortByData() )
{
// sort members
ScMemberSortOrder& rMemberOrder = pRefDim->GetMemberOrder();
OSL_ENSURE( rMemberOrder.empty(), "sort twice?" );
rMemberOrder.resize( nCount );
for (long nPos=0; nPos<nCount; nPos++)
rMemberOrder[nPos] = nPos;
ScDPColMembersOrder aComp( *this, pRefDim->GetSortMeasure(), pRefDim->IsSortAscending() );
::std::sort( rMemberOrder.begin(), rMemberOrder.end(), aComp );
}
// handle children
OSL_ENSURE( pRefDim && static_cast<size_t>(pRefDim->GetMemberCount()) == maMembers.size(), "dimensions don't match" );
OSL_ENSURE( pRefDim == pResultDimension, "wrong dim" );
// for data layout, call only once - sorting measure is always taken from settings
long nLoopCount = bIsDataLayout ? 1 : nCount;
for (long i=0; i<nLoopCount; i++)
{
ScDPResultMember* pRefMember = pRefDim->GetMember(i);
if ( pRefMember->IsVisible() ) //TODO: here or in ScDPDataMember ???
{
ScDPDataMember* pDataMember = maMembers[static_cast<sal_uInt16>(i)].get();
pDataMember->SortMembers( pRefMember );
}
}
}
void ScDPDataDimension::DoAutoShow( ScDPResultDimension* pRefDim )
{
long nCount = maMembers.size();
// handle children first, before changing the visible state
OSL_ENSURE( pRefDim && static_cast<size_t>(pRefDim->GetMemberCount()) == maMembers.size(), "dimensions don't match" );
OSL_ENSURE( pRefDim == pResultDimension, "wrong dim" );
// for data layout, call only once - sorting measure is always taken from settings
long nLoopCount = bIsDataLayout ? 1 : nCount;
for (long i=0; i<nLoopCount; i++)
{
ScDPResultMember* pRefMember = pRefDim->GetMember(i);
if ( pRefMember->IsVisible() ) //TODO: here or in ScDPDataMember ???
{
ScDPDataMember* pDataMember = maMembers[i].get();
pDataMember->DoAutoShow( pRefMember );
}
}
if ( pRefDim->IsAutoShow() && pRefDim->GetAutoCount() > 0 && pRefDim->GetAutoCount() < nCount )
{
// establish temporary order, hide remaining members
ScMemberSortOrder aAutoOrder;
aAutoOrder.resize( nCount );
long nPos;
for (nPos=0; nPos<nCount; nPos++)
aAutoOrder[nPos] = nPos;
ScDPColMembersOrder aComp( *this, pRefDim->GetAutoMeasure(), !pRefDim->IsAutoTopItems() );
::std::sort( aAutoOrder.begin(), aAutoOrder.end(), aComp );
// look for equal values to the last included one
long nIncluded = pRefDim->GetAutoCount();
ScDPDataMember* pDataMember1 = maMembers[aAutoOrder[nIncluded - 1]].get();
if ( !pDataMember1->IsVisible() )
pDataMember1 = nullptr;
bool bContinue = true;
while ( bContinue )
{
bContinue = false;
if ( nIncluded < nCount )
{
ScDPDataMember* pDataMember2 = maMembers[aAutoOrder[nIncluded]].get();
if ( !pDataMember2->IsVisible() )
pDataMember2 = nullptr;
if ( lcl_IsEqual( pDataMember1, pDataMember2, pRefDim->GetAutoMeasure() ) )
{
++nIncluded; // include more members if values are equal
bContinue = true;
}
}
}
// hide the remaining members
for (nPos = nIncluded; nPos < nCount; nPos++)
{
ScDPResultMember* pMember = pRefDim->GetMember(aAutoOrder[nPos]);
pMember->SetAutoHidden();
}
}
}
void ScDPDataDimension::ResetResults()
{
long nCount = maMembers.size();
for (long i=0; i<nCount; i++)
{
// sort order doesn't matter
long nMemberPos = bIsDataLayout ? 0 : i;
ScDPDataMember* pDataMember = maMembers[nMemberPos].get();
pDataMember->ResetResults();
}
}
long ScDPDataDimension::GetSortedIndex( long nUnsorted ) const
{
if (!pResultDimension)
return nUnsorted;
const ScMemberSortOrder& rMemberOrder = pResultDimension->GetMemberOrder();
return rMemberOrder.empty() ? nUnsorted : rMemberOrder[nUnsorted];
}
void ScDPDataDimension::UpdateRunningTotals( const ScDPResultDimension* pRefDim,
long nMeasure, bool bIsSubTotalRow,
const ScDPSubTotalState& rSubState, ScDPRunningTotalState& rRunning,
ScDPRowTotals& rTotals, const ScDPResultMember& rRowParent ) const
{
OSL_ENSURE( pRefDim && static_cast<size_t>(pRefDim->GetMemberCount()) == maMembers.size(), "dimensions don't match" );
OSL_ENSURE( pRefDim == pResultDimension, "wrong dim" );
long nMemberMeasure = nMeasure;
long nCount = maMembers.size();
for (long i=0; i<nCount; i++)
{
const ScMemberSortOrder& rMemberOrder = pRefDim->GetMemberOrder();
long nSorted = rMemberOrder.empty() ? i : rMemberOrder[i];
long nMemberPos = nSorted;
if (bIsDataLayout)
{
OSL_ENSURE(nMeasure == SC_DPMEASURE_ALL || pResultData->GetMeasureCount() == 1,
"DataLayout dimension twice?");
nMemberPos = 0;
nMemberMeasure = nSorted;
}
const ScDPResultMember* pRefMember = pRefDim->GetMember(nMemberPos);
if ( pRefMember->IsVisible() )
{
if ( bIsDataLayout )
rRunning.AddColIndex( 0, 0 );
else
rRunning.AddColIndex( i, nSorted );
ScDPDataMember* pDataMember = maMembers[nMemberPos].get();
pDataMember->UpdateRunningTotals(
pRefMember, nMemberMeasure, bIsSubTotalRow, rSubState, rRunning, rTotals, rRowParent);
rRunning.RemoveColIndex();
}
}
}
#if DUMP_PIVOT_TABLE
void ScDPDataDimension::DumpState( const ScDPResultDimension* pRefDim, ScDocument* pDoc, ScAddress& rPos ) const
{
OUString aDimName = bIsDataLayout ? OUString("(data layout)") : OUString("(unknown)");
dumpRow("ScDPDataDimension", aDimName, nullptr, pDoc, rPos);
SCROW nStartRow = rPos.Row();
long nCount = bIsDataLayout ? 1 : maMembers.size();
for (long i=0; i<nCount; i++)
{
const ScDPResultMember* pRefMember = pRefDim->GetMember(i);
const ScDPDataMember* pDataMember = maMembers[i].get();
pDataMember->DumpState( pRefMember, pDoc, rPos );
}
indent(pDoc, nStartRow, rPos);
}
void ScDPDataDimension::Dump(int nIndent) const
{
std::string aIndent(nIndent*2, ' ');
std::cout << aIndent << "-- data dimension '"
<< (pResultDimension ? pResultDimension->GetName() : OUString()) << "'" << std::endl;
for (auto& rxMember : maMembers)
rxMember->Dump(nIndent+1);
}
#endif
long ScDPDataDimension::GetMemberCount() const
{
return maMembers.size();
}
const ScDPDataMember* ScDPDataDimension::GetMember(long n) const
{
return maMembers[n].get();
}
ScDPDataMember* ScDPDataDimension::GetMember(long n)
{
return maMembers[n].get();
}
ScDPResultVisibilityData::ScDPResultVisibilityData(
ScDPSource* pSource) :
mpSource(pSource)
{
}
ScDPResultVisibilityData::~ScDPResultVisibilityData()
{
}
void ScDPResultVisibilityData::addVisibleMember(const OUString& rDimName, const ScDPItemData& rMemberItem)
{
DimMemberType::iterator itr = maDimensions.find(rDimName);
if (itr == maDimensions.end())
{
pair<DimMemberType::iterator, bool> r = maDimensions.emplace(
rDimName, VisibleMemberType());
if (!r.second)
// insertion failed.
return;
itr = r.first;
}
VisibleMemberType& rMem = itr->second;
rMem.insert(rMemberItem);
}
void ScDPResultVisibilityData::fillFieldFilters(vector<ScDPFilteredCache::Criterion>& rFilters) const
{
typedef std::unordered_map<OUString, long> FieldNameMapType;
FieldNameMapType aFieldNames;
ScDPTableData* pData = mpSource->GetData();
long nColumnCount = pData->GetColumnCount();
for (long i = 0; i < nColumnCount; ++i)
{
aFieldNames.emplace(pData->getDimensionName(i), i);
}
const ScDPDimensions* pDims = mpSource->GetDimensionsObject();
for (const auto& [rDimName, rMem] : maDimensions)
{
ScDPFilteredCache::Criterion aCri;
FieldNameMapType::const_iterator itrField = aFieldNames.find(rDimName);
if (itrField == aFieldNames.end())
// This should never happen!
continue;
long nDimIndex = itrField->second;
aCri.mnFieldIndex = static_cast<sal_Int32>(nDimIndex);
aCri.mpFilter = std::make_shared<ScDPFilteredCache::GroupFilter>();
ScDPFilteredCache::GroupFilter* pGrpFilter =
static_cast<ScDPFilteredCache::GroupFilter*>(aCri.mpFilter.get());
for (const ScDPItemData& rMemItem : rMem)
{
pGrpFilter->addMatchItem(rMemItem);
}
ScDPDimension* pDim = pDims->getByIndex(nDimIndex);
ScDPMembers* pMembers = pDim->GetHierarchiesObject()->getByIndex(0)->
GetLevelsObject()->getByIndex(0)->GetMembersObject();
if (pGrpFilter->getMatchItemCount() < o3tl::make_unsigned(pMembers->getCount()))
rFilters.push_back(aCri);
}
}
size_t ScDPResultVisibilityData::MemberHash::operator() (const ScDPItemData& r) const
{
if (r.IsValue())
return static_cast<size_t>(::rtl::math::approxFloor(r.GetValue()));
else
return r.GetString().hashCode();
}
SCROW ScDPResultMember::GetDataId( ) const
{
const ScDPMember* pMemberDesc = GetDPMember();
if (pMemberDesc)
return pMemberDesc->GetItemDataId();
return -1;
}
ScDPResultMember* ScDPResultDimension::AddMember(const ScDPParentDimData &aData )
{
ScDPResultMember* pMember = new ScDPResultMember( pResultData, aData );
SCROW nDataIndex = pMember->GetDataId();
maMemberArray.emplace_back( pMember );
maMemberHash.emplace( nDataIndex, pMember );
return pMember;
}
ScDPResultMember* ScDPResultDimension::InsertMember(const ScDPParentDimData *pMemberData)
{
SCROW nInsert = 0;
if ( !lcl_SearchMember( maMemberArray, pMemberData->mnOrder , nInsert ) )
{
ScDPResultMember* pNew = new ScDPResultMember( pResultData, *pMemberData );
maMemberArray.emplace( maMemberArray.begin()+nInsert, pNew );
SCROW nDataIndex = pMemberData->mpMemberDesc->GetItemDataId();
maMemberHash.emplace( nDataIndex, pNew );
return pNew;
}
return maMemberArray[ nInsert ].get();
}
void ScDPResultDimension::InitWithMembers(
LateInitParams& rParams, const std::vector<SCROW>& pItemData, size_t nPos,
ScDPInitState& rInitState)
{
if ( rParams.IsEnd( nPos ) )
return;
ScDPDimension* pThisDim = rParams.GetDim( nPos );
ScDPLevel* pThisLevel = rParams.GetLevel( nPos );
SCROW nDataID = pItemData[nPos];
if (pThisDim && pThisLevel)
{
long nDimSource = pThisDim->GetDimension(); //TODO: check GetSourceDim?
// create all members at the first call (preserve order)
ResultMembers& rMembers = pResultData->GetDimResultMembers(nDimSource, pThisDim, pThisLevel);
ScDPGroupCompare aCompare( pResultData, rInitState, nDimSource );
// initialize only specific member (or all if "show empty" flag is set)
ScDPResultMember* pResultMember = nullptr;
if ( bInitialized )
pResultMember = FindMember( nDataID );
else
bInitialized = true;
if ( pResultMember == nullptr )
{ //only insert found item
const ScDPParentDimData* pMemberData = rMembers.FindMember( nDataID );
if ( pMemberData && aCompare.IsIncluded( *( pMemberData->mpMemberDesc ) ) )
pResultMember = InsertMember( pMemberData );
}
if ( pResultMember )
{
rInitState.AddMember( nDimSource, pResultMember->GetDataId() );
pResultMember->LateInitFrom(rParams, pItemData, nPos+1, rInitState);
rInitState.RemoveMember();
}
}
}
ScDPParentDimData::ScDPParentDimData() :
mnOrder(-1), mpParentDim(nullptr), mpParentLevel(nullptr), mpMemberDesc(nullptr) {}
ScDPParentDimData::ScDPParentDimData(
SCROW nIndex, const ScDPDimension* pDim, const ScDPLevel* pLev, const ScDPMember* pMember) :
mnOrder(nIndex), mpParentDim(pDim), mpParentLevel(pLev), mpMemberDesc(pMember) {}
const ScDPParentDimData* ResultMembers::FindMember( SCROW nIndex ) const
{
auto aRes = maMemberHash.find( nIndex );
if( aRes != maMemberHash.end()) {
if ( aRes->second.mpMemberDesc && aRes->second.mpMemberDesc->GetItemDataId()==nIndex )
return &aRes->second;
}
return nullptr;
}
void ResultMembers::InsertMember( const ScDPParentDimData& rNew )
{
if ( !rNew.mpMemberDesc->getShowDetails() )
mbHasHideDetailsMember = true;
maMemberHash.emplace( rNew.mpMemberDesc->GetItemDataId(), rNew );
}
ResultMembers::ResultMembers():
mbHasHideDetailsMember( false )
{
}
ResultMembers::~ResultMembers()
{
}
LateInitParams::LateInitParams(
const vector<ScDPDimension*>& ppDim, const vector<ScDPLevel*>& ppLev, bool bRow ) :
mppDim( ppDim ),
mppLev( ppLev ),
mbRow( bRow ),
mbInitChild( true ),
mbAllChildren( false )
{
}
LateInitParams::~LateInitParams()
{
}
bool LateInitParams::IsEnd( size_t nPos ) const
{
return nPos >= mppDim.size();
}
void ScDPResultDimension::CheckShowEmpty( bool bShow )
{
long nCount = maMemberArray.size();
for (long i=0; i<nCount; i++)
{
ScDPResultMember* pMember = maMemberArray.at(i).get();
pMember->CheckShowEmpty(bShow);
}
}
void ScDPResultMember::CheckShowEmpty( bool bShow )
{
if (bHasElements)
{
ScDPResultDimension* pChildDim = GetChildDimension();
if (pChildDim)
pChildDim->CheckShowEmpty();
}
else if (IsValid() && bInitialized)
{
bShow = bShow || (GetParentLevel() && GetParentLevel()->getShowEmpty());
if (bShow)
{
SetHasElements();
ScDPResultDimension* pChildDim = GetChildDimension();
if (pChildDim)
pChildDim->CheckShowEmpty(true);
}
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|