datasketches 0.6.0

A software library of stochastic streaming algorithms (a.k.a. sketches)
Documentation
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
// 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
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use std::cmp::Ordering;
use std::num::NonZeroU64;

use crate::codec::SketchBytes;
use crate::codec::SketchSlice;
use crate::codec::assert::ensure_preamble_longs_in;
use crate::codec::assert::ensure_serial_version_is;
use crate::codec::assert::insufficient_data;
use crate::codec::family::Family;
use crate::error::Error;
use crate::tdigest::serialization::COMPAT_DOUBLE;
use crate::tdigest::serialization::COMPAT_FLOAT;
use crate::tdigest::serialization::FLAGS_IS_EMPTY;
use crate::tdigest::serialization::FLAGS_IS_SINGLE_VALUE;
use crate::tdigest::serialization::FLAGS_REVERSE_MERGE;
use crate::tdigest::serialization::PREAMBLE_LONGS_EMPTY_OR_SINGLE;
use crate::tdigest::serialization::PREAMBLE_LONGS_MULTIPLE;
use crate::tdigest::serialization::SERIAL_VERSION;

/// The default value of K if one is not specified.
const DEFAULT_K: u16 = 200;
/// Multiplier for unmerged values relative to the target number of centroids.
const UNMERGED_MULTIPLIER: usize = 4;
/// Unmerged-value capacity allocated by the first update to a digest.
const INITIAL_UNMERGED_CAPACITY: usize = 8;
/// Default weight for single values.
const DEFAULT_WEIGHT: NonZeroU64 = NonZeroU64::new(1).unwrap();

// Centroids are stored as `[compressed prefix | unmerged unit-weight tail]`. Carrying a unit weight
// for each unmerged value lets updates, compression, and merge reuse one allocation instead of
// converting raw values into a second vector during compression.
#[derive(Debug, Clone, Default)]
struct TDigestBuffer {
    centroids: Vec<Centroid>,
    unmerged_tail_len: usize,
}

impl TDigestBuffer {
    fn new(centroids: Vec<Centroid>, unmerged_tail_len: usize) -> Self {
        debug_assert!(unmerged_tail_len <= centroids.len());
        TDigestBuffer {
            centroids,
            unmerged_tail_len,
        }
    }

    fn len(&self) -> usize {
        self.centroids.len()
    }

    fn is_empty(&self) -> bool {
        self.centroids.is_empty()
    }

    fn unmerged_len(&self) -> usize {
        self.unmerged_tail_len
    }

    fn compressed_prefix_len(&self) -> usize {
        self.centroids.len() - self.unmerged_tail_len
    }

    fn push_unmerged(&mut self, value: f64, max_unmerged: usize) {
        debug_assert!(self.unmerged_tail_len < max_unmerged);
        if self.centroids.len() == self.centroids.capacity() {
            let target_unmerged = if self.unmerged_tail_len == 0 {
                INITIAL_UNMERGED_CAPACITY
            } else if self.unmerged_tail_len == INITIAL_UNMERGED_CAPACITY {
                // Once a digest outgrows a tiny group, skip an extra allocator round trip while
                // keeping the first allocation small.
                (INITIAL_UNMERGED_CAPACITY * UNMERGED_MULTIPLIER * UNMERGED_MULTIPLIER)
                    .min(max_unmerged)
            } else {
                self.unmerged_tail_len
                    .saturating_mul(UNMERGED_MULTIPLIER)
                    .min(max_unmerged)
            };
            let target_capacity = self.compressed_prefix_len().saturating_add(target_unmerged);
            self.centroids
                .reserve_exact(target_capacity.saturating_sub(self.centroids.len()));
        }

        self.centroids.push(Centroid {
            mean: value,
            weight: DEFAULT_WEIGHT,
        });
        self.unmerged_tail_len += 1;
    }

    /// Returns all centroids in the tie order expected by stable compression sorting.
    ///
    /// The buffer is rotated from `[compressed | unmerged]` to `[unmerged | compressed]`, so new
    /// values stay before existing centroids when their means are equal.
    fn into_centroids_for_compression(mut self) -> Vec<Centroid> {
        debug_assert_ne!(self.unmerged_tail_len, 0);
        let compressed_prefix_len = self.compressed_prefix_len();
        self.centroids.rotate_left(compressed_prefix_len);
        self.centroids
    }

    /// Combines this buffer with a non-empty borrowed buffer in stable mean order.
    fn into_merged_centroids(mut self, other: &TDigestBuffer) -> Vec<Centroid> {
        debug_assert!(!other.is_empty(), "an empty right-hand buffer is a no-op");
        if self.unmerged_tail_len == 0 && other.unmerged_tail_len == 0 {
            // Compression and deserialization both establish this invariant.
            debug_assert!(centroids_are_sorted(&self.centroids));
            debug_assert!(centroids_are_sorted(&other.centroids));
            merge_sorted_centroids(&mut self.centroids, &other.centroids);
            return self.centroids;
        }

        let compressed_prefix_len = self.compressed_prefix_len();
        self.centroids.reserve(other.len());
        let other_prefix_len = other.compressed_prefix_len();
        self.centroids
            .extend_from_slice(&other.centroids[other_prefix_len..]);
        self.centroids
            .extend_from_slice(&other.centroids[..other_prefix_len]);
        // Preserve the stable tie order: left unmerged, right unmerged and compressed, then the
        // left compressed prefix.
        self.centroids.rotate_left(compressed_prefix_len);
        self.centroids.sort_by(centroid_cmp);
        self.centroids
    }

    fn compressed_centroids(&self) -> &[Centroid] {
        assert_eq!(
            self.unmerged_tail_len, 0,
            "t-digest buffer must be compressed before reading centroids"
        );
        &self.centroids
    }

    fn into_compressed_centroids(self) -> Vec<Centroid> {
        assert_eq!(
            self.unmerged_tail_len, 0,
            "t-digest buffer must be compressed before reading centroids"
        );
        self.centroids
    }

    /// Returns an estimate of memory usage in bytes.
    fn estimated_size(&self) -> usize {
        self.centroids.capacity() * size_of::<Centroid>()
    }
}

/// T-Digest sketch for estimating quantiles and ranks.
///
/// See the [module level documentation](super) for more.
#[derive(Debug, Clone)]
pub struct TDigestMut {
    k: u16,

    reverse_merge: bool,
    min: f64,
    max: f64,

    buffer: TDigestBuffer,
    // Weight represented by the compressed prefix. The unmerged tail contributes one per
    // centroid and is counted separately by `TDigestBuffer::unmerged_len`.
    compressed_weight: u64,
}

impl Default for TDigestMut {
    fn default() -> Self {
        Self::make(
            DEFAULT_K,
            false,
            f64::INFINITY,
            f64::NEG_INFINITY,
            TDigestBuffer::default(),
            0,
        )
    }
}

impl TDigestMut {
    /// Creates a mutable t-digest with the given `k` value.
    ///
    /// # Errors
    ///
    /// Returns an error if `k` is less than `10`.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let sketch = TDigestMut::new(100).unwrap();
    /// assert_eq!(sketch.k(), 100);
    /// ```
    pub fn new(k: u16) -> Result<Self, Error> {
        if k < 10 {
            return Err(Error::invalid_argument(format!(
                "k must be at least 10, got {k}"
            )));
        }

        Ok(Self::make(
            k,
            false,
            f64::INFINITY,
            f64::NEG_INFINITY,
            TDigestBuffer::default(),
            0,
        ))
    }

    fn make(
        k: u16,
        reverse_merge: bool,
        min: f64,
        max: f64,
        buffer: TDigestBuffer,
        compressed_weight: u64,
    ) -> Self {
        debug_assert!(k >= 10, "k must be at least 10");
        debug_assert!(buffer.unmerged_tail_len <= buffer.centroids.len());
        debug_assert!(buffer.compressed_prefix_len() != 0 || compressed_weight == 0);

        TDigestMut {
            k,
            reverse_merge,
            min,
            max,
            buffer,
            compressed_weight,
        }
    }

    fn target_centroids(&self) -> usize {
        let fudge = if self.k < 30 { 30 } else { 10 };
        (usize::from(self.k) * 2) + fudge
    }

    fn max_unmerged(&self) -> usize {
        self.target_centroids() * UNMERGED_MULTIPLIER
    }

    fn target_retained_capacity(&self) -> usize {
        self.target_centroids() + self.max_unmerged()
    }

    /// Updates this t-digest with the given value.
    ///
    /// [f64::NAN], [f64::INFINITY], and [f64::NEG_INFINITY] values are ignored.
    ///
    /// # Panics
    ///
    /// Panics without modifying the digest if the total weight would exceed `u64::MAX`.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// sketch.update(1.0);
    /// assert!(sketch.total_weight() >= 1);
    /// ```
    pub fn update(&mut self, value: f64) {
        if !value.is_finite() {
            return;
        }
        assert!(self.total_weight() < u64::MAX, "total weight overflow");

        let max_unmerged = self.max_unmerged();
        if self.buffer.unmerged_len() >= max_unmerged {
            self.compress();
        }
        self.buffer.push_unmerged(value, max_unmerged);
        self.min = self.min.min(value);
        self.max = self.max.max(value);
    }

    /// Returns the compression parameter `k` used to configure this t-digest.
    pub fn k(&self) -> u16 {
        self.k
    }

    /// Returns `true` if this t-digest has not seen any data.
    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }

    /// Returns the minimum value seen by this t-digest, or `None` if it is empty.
    pub fn min_value(&self) -> Option<f64> {
        if self.is_empty() {
            None
        } else {
            Some(self.min)
        }
    }

    /// Returns the maximum value seen by this t-digest, or `None` if it is empty.
    pub fn max_value(&self) -> Option<f64> {
        if self.is_empty() {
            None
        } else {
            Some(self.max)
        }
    }

    /// Returns the total weight.
    pub fn total_weight(&self) -> u64 {
        self.compressed_weight + self.buffer.unmerged_len() as u64
    }

    /// Merges the given t-digest into this one.
    ///
    /// Retains this digest's `k`, even if the other digest uses a different value.
    /// The borrowed input remains available for reuse. Collecting owned inputs into a
    /// [`TDigestMut`] with [`Iterator::collect`] combines a batch with one compression pass.
    ///
    /// # Panics
    ///
    /// Panics without modifying the digest if the combined total weight would exceed `u64::MAX`.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut left = TDigestMut::new(100).unwrap();
    /// let mut right = TDigestMut::new(100).unwrap();
    /// left.update(1.0);
    /// right.update(2.0);
    /// left.merge(&right);
    /// assert_eq!(left.total_weight(), 2);
    /// ```
    pub fn merge(&mut self, other: &TDigestMut) {
        if other.is_empty() {
            return;
        }
        let total_weight = self
            .total_weight()
            .checked_add(other.total_weight())
            .expect("total weight overflow");

        // Preserve true extrema from `other`. Compression only sees centroid means, which can
        // differ from `min`/`max` after ordinary compression or deserialization.
        self.min = self.min.min(other.min);
        self.max = self.max.max(other.max);

        let centroids = std::mem::take(&mut self.buffer).into_merged_centroids(&other.buffer);
        self.compress_sorted_centroids(centroids, total_weight);
    }

    /// Converts this mutable t-digest into an immutable one.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// sketch.update(1.0);
    /// let frozen = sketch.freeze();
    /// assert!(!frozen.is_empty());
    /// ```
    pub fn freeze(mut self) -> TDigest {
        self.compress();
        let mut centroids = self.buffer.into_compressed_centroids();
        // A mutable digest retains update workspace for reuse. The immutable form cannot use that
        // spare capacity, so release it at this consuming boundary.
        centroids.shrink_to_fit();
        TDigest {
            k: self.k,
            reverse_merge: self.reverse_merge,
            min: self.min,
            max: self.max,
            centroids,
            centroids_weight: self.compressed_weight,
        }
    }

    fn view(&mut self) -> TDigestView<'_> {
        self.compress(); // side effect
        TDigestView {
            min: self.min,
            max: self.max,
            centroids: self.buffer.compressed_centroids(),
            centroids_weight: self.compressed_weight,
        }
    }

    /// Returns the cumulative distribution approximation described by [`TDigest::cdf`].
    ///
    /// Returns `Ok(None)` if this t-digest is empty.
    ///
    /// # Errors
    ///
    /// Returns `InvalidArgument` if `split_points` contains NaN or is not strictly increasing, even
    /// if this t-digest is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// for value in [1.0, 2.0, 3.0] {
    ///     sketch.update(value);
    /// }
    /// let cdf = sketch.cdf(&[1.5]).unwrap().unwrap();
    /// assert_eq!(cdf.len(), 2);
    /// ```
    pub fn cdf(&mut self, split_points: &[f64]) -> Result<Option<Vec<f64>>, Error> {
        check_split_points(split_points)?;

        if self.is_empty() {
            return Ok(None);
        }

        Ok(self.view().cdf(split_points))
    }

    /// Returns the probability mass approximation described by [`TDigest::pmf`].
    ///
    /// Returns `Ok(None)` if this t-digest is empty.
    ///
    /// # Errors
    ///
    /// Returns `InvalidArgument` if `split_points` contains NaN or is not strictly increasing, even
    /// if this t-digest is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// for value in [1.0, 2.0, 3.0] {
    ///     sketch.update(value);
    /// }
    /// let pmf = sketch.pmf(&[1.5]).unwrap().unwrap();
    /// assert_eq!(pmf.len(), 2);
    /// ```
    pub fn pmf(&mut self, split_points: &[f64]) -> Result<Option<Vec<f64>>, Error> {
        check_split_points(split_points)?;

        if self.is_empty() {
            return Ok(None);
        }

        Ok(self.view().pmf(split_points))
    }

    /// Returns the normalized rank described by [`TDigest::rank`].
    ///
    /// Returns `Ok(None)` if this t-digest is empty.
    ///
    /// # Errors
    ///
    /// Returns `InvalidArgument` if `value` is NaN, even if this t-digest is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// for value in [1.0, 2.0, 3.0] {
    ///     sketch.update(value);
    /// }
    /// let rank = sketch.rank(2.0).unwrap().unwrap();
    /// assert!((0.0..=1.0).contains(&rank));
    /// ```
    pub fn rank(&mut self, value: f64) -> Result<Option<f64>, Error> {
        if value.is_nan() {
            return Err(Error::invalid_argument("value must not be NaN"));
        }

        if self.is_empty() {
            return Ok(None);
        }
        if value < self.min {
            return Ok(Some(0.0));
        }
        if value > self.max {
            return Ok(Some(1.0));
        }
        if self.min == self.max {
            return Ok(Some(0.5));
        }

        Ok(self.view().rank(value))
    }

    /// Returns the quantile described by [`TDigest::quantile`].
    ///
    /// Returns `Ok(None)` if this t-digest is empty.
    ///
    /// # Errors
    ///
    /// Returns `InvalidArgument` if `rank` is NaN or outside `[0.0, 1.0]`, even if this t-digest is
    /// empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// for value in [1.0, 2.0, 3.0] {
    ///     sketch.update(value);
    /// }
    /// let median = sketch.quantile(0.5).unwrap().unwrap();
    /// assert!((1.0..=3.0).contains(&median));
    /// ```
    pub fn quantile(&mut self, rank: f64) -> Result<Option<f64>, Error> {
        check_rank(rank)?;

        if self.is_empty() {
            return Ok(None);
        }

        Ok(self.view().quantile(rank))
    }

    /// Returns the quantiles described by [`TDigest::quantiles`].
    ///
    /// Returns `Ok(None)` if this t-digest is empty. Otherwise, an empty `ranks` slice returns
    /// `Ok(Some(vec![]))`.
    ///
    /// # Errors
    ///
    /// Returns `InvalidArgument` if any rank is NaN or outside `[0.0, 1.0]`, even if this t-digest
    /// is empty.
    pub fn quantiles(&mut self, ranks: &[f64]) -> Result<Option<Vec<f64>>, Error> {
        check_ranks(ranks)?;

        if self.is_empty() {
            return Ok(None);
        }

        Ok(self.view().quantiles(ranks))
    }

    /// Serializes this mutable t-digest to bytes in double precision.
    ///
    /// The serialized data can be read by other DataSketches implementations.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// sketch.update(1.0);
    /// let bytes = sketch.serialize();
    /// let decoded = TDigestMut::deserialize(&bytes).unwrap();
    /// assert_eq!(decoded.max_value(), Some(1.0));
    /// ```
    pub fn serialize(&mut self) -> Vec<u8> {
        self.compress();
        serialize_compressed(
            self.k,
            self.reverse_merge,
            self.min,
            self.max,
            self.buffer.compressed_centroids(),
            self.compressed_weight,
        )
    }

    /// Deserializes a mutable t-digest from the standard double-precision format.
    ///
    /// Also accepts data serialized by other DataSketches implementations.
    ///
    /// The format of the [reference implementation](https://github.com/tdunning/t-digest) is
    /// auto-detected. Use [`deserialize_f32()`](Self::deserialize_f32) for the compact
    /// DataSketches C++ `tdigest<float>` format.
    ///
    /// # Errors
    ///
    /// Returns `InvalidData` if the image is truncated, has an unsupported format, or contains
    /// invalid extrema, centroids, or weights.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// sketch.update(1.0);
    /// sketch.update(2.0);
    /// let bytes = sketch.serialize();
    /// let decoded = TDigestMut::deserialize(&bytes).unwrap();
    /// assert_eq!(decoded.max_value(), Some(2.0));
    /// ```
    pub fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
        Self::deserialize_impl(bytes, false)
    }

    /// Deserializes a mutable t-digest from the compact single-precision DataSketches format.
    ///
    /// Accepts data serialized by `tdigest<float>` in DataSketches C++. This format stores centroid
    /// means and weights as `(f32, u32)`. Its header does not identify the scalar width, so callers
    /// must select this entry point explicitly.
    ///
    /// # Errors
    ///
    /// Returns `InvalidData` if the image is truncated, has an unsupported format, or contains
    /// invalid extrema, centroids, or weights.
    pub fn deserialize_f32(bytes: &[u8]) -> Result<Self, Error> {
        Self::deserialize_impl(bytes, true)
    }

    fn deserialize_impl(bytes: &[u8], is_f32: bool) -> Result<Self, Error> {
        let mut cursor = SketchSlice::new(bytes);

        let preamble_longs = cursor
            .read_u8()
            .map_err(insufficient_data("preamble_longs"))?;
        let serial_version = cursor
            .read_u8()
            .map_err(insufficient_data("serial_version"))?;
        let family_id = cursor.read_u8().map_err(insufficient_data("family_id"))?;
        if let Err(err) = Family::TDIGEST.validate_id(family_id) {
            return if preamble_longs == 0 && serial_version == 0 && family_id == 0 {
                Self::deserialize_compat(bytes)
            } else {
                Err(err)
            };
        }
        ensure_serial_version_is(SERIAL_VERSION, serial_version)?;
        let k = cursor.read_u16_le().map_err(insufficient_data("k"))?;
        if k < 10 {
            return Err(Error::deserial(format!("k must be at least 10, got {k}")));
        }
        let flags = cursor.read_u8().map_err(insufficient_data("flags"))?;
        let known_flags = FLAGS_IS_EMPTY | FLAGS_IS_SINGLE_VALUE | FLAGS_REVERSE_MERGE;
        if flags & !known_flags != 0 {
            return Err(Error::deserial(format!(
                "malformed data: unknown TDigest flags 0x{:02x}",
                flags & !known_flags
            )));
        }
        let is_empty = (flags & FLAGS_IS_EMPTY) != 0;
        let is_single_value = (flags & FLAGS_IS_SINGLE_VALUE) != 0;
        if is_empty && is_single_value {
            return Err(Error::deserial(
                "malformed data: empty and single-value flags are mutually exclusive",
            ));
        }
        let expected_preamble_longs = if is_empty || is_single_value {
            PREAMBLE_LONGS_EMPTY_OR_SINGLE
        } else {
            PREAMBLE_LONGS_MULTIPLE
        };
        ensure_preamble_longs_in(&[expected_preamble_longs], preamble_longs)?;
        cursor
            .read_u16_le()
            .map_err(insufficient_data("<unused>"))?; // unused
        if is_empty {
            return TDigestMut::new(k);
        }

        let reverse_merge = (flags & FLAGS_REVERSE_MERGE) != 0;
        if is_single_value {
            let value = if is_f32 {
                cursor
                    .read_f32_le()
                    .map_err(insufficient_data("single_value"))? as f64
            } else {
                cursor
                    .read_f64_le()
                    .map_err(insufficient_data("single_value"))?
            };
            check_non_nan(value, "single_value")?;
            check_finite(value, "single_value")?;
            return Ok(TDigestMut::make(
                k,
                reverse_merge,
                value,
                value,
                TDigestBuffer::new(
                    vec![Centroid {
                        mean: value,
                        weight: DEFAULT_WEIGHT,
                    }],
                    0,
                ),
                1,
            ));
        }
        let num_centroids = cursor
            .read_u32_le()
            .map_err(insufficient_data("num_centroids"))? as usize;
        let num_buffered = cursor
            .read_u32_le()
            .map_err(insufficient_data("num_buffered"))? as usize;
        let (min, max) = if is_f32 {
            (
                cursor.read_f32_le().map_err(insufficient_data("min"))? as f64,
                cursor.read_f32_le().map_err(insufficient_data("max"))? as f64,
            )
        } else {
            (
                cursor.read_f64_le().map_err(insufficient_data("min"))?,
                cursor.read_f64_le().map_err(insufficient_data("max"))?,
            )
        };
        check_extrema(min, max, "TDigest")?;
        let (centroid_bytes, buffered_value_bytes) = if is_f32 {
            (size_of::<f32>() + size_of::<u32>(), size_of::<f32>())
        } else {
            (size_of::<f64>() + size_of::<u64>(), size_of::<f64>())
        };
        let centroid_payload_bytes = num_centroids
            .checked_mul(centroid_bytes)
            .ok_or_else(|| Error::deserial("TDigest payload size exceeds the supported size"))?;
        let buffered_payload_bytes = num_buffered
            .checked_mul(buffered_value_bytes)
            .ok_or_else(|| Error::deserial("TDigest payload size exceeds the supported size"))?;
        let required_payload_bytes = centroid_payload_bytes
            .checked_add(buffered_payload_bytes)
            .ok_or_else(|| Error::deserial("TDigest payload size exceeds the supported size"))?;
        // Check the whole payload once so fixed-width records can be decoded without per-field I/O.
        let available_bytes = cursor.remaining().len();
        if available_bytes < required_payload_bytes {
            return Err(Error::insufficient_data_of(
                "TDigest payload",
                format_args!("expected {required_payload_bytes} bytes, got {available_bytes}"),
            ));
        }
        let payload = &cursor.remaining()[..required_payload_bytes];
        let (centroid_payload, buffered_payload) = payload.split_at(centroid_payload_bytes);
        let stored_centroids = num_centroids.checked_add(num_buffered).ok_or_else(|| {
            Error::deserial("num_centroids and num_buffered exceed the supported size")
        })?;
        if stored_centroids == 0 {
            return Err(Error::deserial(
                "malformed data: non-empty TDigest must contain a centroid or buffered value",
            ));
        }
        let mut centroids = Vec::with_capacity(stored_centroids);
        let mut compressed_weight = 0u64;
        let mut previous_mean = min;
        let mut centroid_means_valid = true;
        for bytes in centroid_payload.chunks_exact(centroid_bytes) {
            let (mean, weight) = if is_f32 {
                (
                    f32::from_le_bytes(bytes[..4].try_into().unwrap()) as f64,
                    u32::from_le_bytes(bytes[4..].try_into().unwrap()) as u64,
                )
            } else {
                (
                    f64::from_le_bytes(bytes[..8].try_into().unwrap()),
                    u64::from_le_bytes(bytes[8..].try_into().unwrap()),
                )
            };
            centroid_means_valid &= mean.is_finite() & (mean >= previous_mean) & (mean <= max);
            previous_mean = mean;
            let weight = check_nonzero(weight, "centroid weight")?;
            compressed_weight = checked_weight_sum(compressed_weight, weight.get())?;
            centroids.push(Centroid { mean, weight });
        }
        if !centroid_means_valid {
            return Err(Error::deserial(
                "malformed data: centroid means must be finite, within extrema, and nondecreasing",
            ));
        }
        let total_weight = checked_weight_sum(compressed_weight, num_buffered as u64)?;
        check_single_sample_extrema(min, max, total_weight)?;
        let mut buffered_values_valid = true;
        for bytes in buffered_payload.chunks_exact(buffered_value_bytes) {
            let value = if is_f32 {
                f32::from_le_bytes(bytes.try_into().unwrap()) as f64
            } else {
                f64::from_le_bytes(bytes.try_into().unwrap())
            };
            buffered_values_valid &= value.is_finite() & (value >= min) & (value <= max);
            centroids.push(Centroid {
                mean: value,
                weight: DEFAULT_WEIGHT,
            });
        }
        if !buffered_values_valid {
            return Err(Error::deserial(
                "malformed data: buffered values must be finite and within extrema",
            ));
        }
        Ok(TDigestMut::make(
            k,
            reverse_merge,
            min,
            max,
            TDigestBuffer::new(centroids, num_buffered),
            compressed_weight,
        ))
    }

    // compatibility with the format of the reference implementation
    // default byte order of ByteBuffer is used there, which is big endian
    fn deserialize_compat(bytes: &[u8]) -> Result<Self, Error> {
        fn make_error(tag: &'static str) -> impl FnOnce(std::io::Error) -> Error {
            move |_| Error::insufficient_data_of("compat format", tag)
        }

        let mut cursor = SketchSlice::new(bytes);

        let ty = cursor.read_u32_be().map_err(make_error("type"))?;
        match ty {
            COMPAT_DOUBLE => {
                fn make_error(tag: &'static str) -> impl FnOnce(std::io::Error) -> Error {
                    move |_| Error::insufficient_data_of("compat double format", tag)
                }
                // compatibility with asBytes()
                let min = cursor.read_f64_be().map_err(make_error("min"))?;
                let max = cursor.read_f64_be().map_err(make_error("max"))?;
                check_extrema(min, max, "compat double TDigest")?;
                let k = cursor.read_f64_be().map_err(make_error("k"))? as u16;
                if k < 10 {
                    return Err(Error::deserial(format!(
                        "k must be at least 10 in compat double format, got {k}"
                    )));
                }
                let num_centroids =
                    cursor.read_u32_be().map_err(make_error("num_centroids"))? as usize;
                if num_centroids == 0 {
                    return Err(Error::deserial(
                        "malformed data: compat double TDigest must contain a centroid",
                    ));
                }
                let mut total_weight = 0u64;
                let mut centroids = Vec::with_capacity(num_centroids);
                let mut previous_mean = min;
                let mut centroid_means_valid = true;
                for _ in 0..num_centroids {
                    let weight = cursor.read_f64_be().map_err(make_error("weight"))?;
                    let mean = cursor.read_f64_be().map_err(make_error("mean"))?;
                    let weight =
                        check_compat_weight(weight, "centroid weight in compat double format")?;
                    centroid_means_valid &=
                        mean.is_finite() & (mean >= previous_mean) & (mean <= max);
                    previous_mean = mean;
                    total_weight = checked_weight_sum(total_weight, weight.get())?;
                    centroids.push(Centroid { mean, weight });
                }
                if !centroid_means_valid {
                    return Err(Error::deserial(
                        "malformed data: centroid means in compat double format must be finite, within extrema, and nondecreasing",
                    ));
                }
                check_single_sample_extrema(min, max, total_weight)?;
                Ok(TDigestMut::make(
                    k,
                    false,
                    min,
                    max,
                    TDigestBuffer::new(centroids, 0),
                    total_weight,
                ))
            }
            COMPAT_FLOAT => {
                fn make_error(tag: &'static str) -> impl FnOnce(std::io::Error) -> Error {
                    move |_| Error::insufficient_data_of("compat float format", tag)
                }
                // COMPAT_FLOAT: compatibility with asSmallBytes()
                // reference implementation uses doubles for min and max
                let min = cursor.read_f64_be().map_err(make_error("min"))?;
                let max = cursor.read_f64_be().map_err(make_error("max"))?;
                check_extrema(min, max, "compat float TDigest")?;
                let k = cursor.read_f32_be().map_err(make_error("k"))? as u16;
                if k < 10 {
                    return Err(Error::deserial(format!(
                        "k must be at least 10 in compat float format, got {k}"
                    )));
                }
                // reference implementation stores capacities of the array of centroids and the
                // buffer as shorts they can be derived from k in the constructor
                cursor.read_u32_be().map_err(make_error("<unused>"))?;
                let num_centroids =
                    cursor.read_u16_be().map_err(make_error("num_centroids"))? as usize;
                if num_centroids == 0 {
                    return Err(Error::deserial(
                        "malformed data: compat float TDigest must contain a centroid",
                    ));
                }
                let mut total_weight = 0u64;
                let mut centroids = Vec::with_capacity(num_centroids);
                let mut previous_mean = min;
                let mut centroid_means_valid = true;
                for _ in 0..num_centroids {
                    let weight = cursor.read_f32_be().map_err(make_error("weight"))? as f64;
                    let mean = cursor.read_f32_be().map_err(make_error("mean"))? as f64;
                    let weight =
                        check_compat_weight(weight, "centroid weight in compat float format")?;
                    centroid_means_valid &=
                        mean.is_finite() & (mean >= previous_mean) & (mean <= max);
                    previous_mean = mean;
                    total_weight = checked_weight_sum(total_weight, weight.get())?;
                    centroids.push(Centroid { mean, weight });
                }
                if !centroid_means_valid {
                    return Err(Error::deserial(
                        "malformed data: centroid means in compat float format must be finite, within extrema, and nondecreasing",
                    ));
                }
                check_single_sample_extrema(min, max, total_weight)?;
                Ok(TDigestMut::make(
                    k,
                    false,
                    min,
                    max,
                    TDigestBuffer::new(centroids, 0),
                    total_weight,
                ))
            }
            ty => Err(Error::deserial(format!("unknown TDigest compat type {ty}"))),
        }
    }

    /// Processes unmerged values and merges centroids if needed.
    fn compress(&mut self) {
        if self.buffer.unmerged_len() == 0 {
            // Also preserves fully compressed deserialized images verbatim.
            return;
        }
        let total_weight = self.total_weight();
        let mut centroids = std::mem::take(&mut self.buffer).into_centroids_for_compression();
        centroids.sort_by(centroid_cmp);
        self.compress_sorted_centroids(centroids, total_weight);
    }

    /// Compresses nonempty, sorted centroids whose combined weight is `total_weight`.
    ///
    /// Includes all retained and incoming values, with finite means and nonzero weights.
    /// Callers ensure the total fits in `u64` before taking the buffer.
    fn compress_sorted_centroids(&mut self, mut centroids: Vec<Centroid>, total_weight: u64) {
        debug_assert!(!centroids.is_empty());
        debug_assert!(centroids_are_sorted(&centroids));
        if self.reverse_merge {
            centroids.reverse();
        }
        self.compressed_weight = total_weight;

        let mut num_centroids = 1;
        let len = centroids.len();
        // No two positive integer weights fit when n <= k / 2. For a positive K_2
        // normalizer, the maximum limit is n / (4 * normalizer) = r * (ln(r) + 6),
        // where r = n / (2 * k) <= 1/4: the limit stays below 1.154, hence below 2.
        // A nonpositive normalizer also rejects all merges at these small counts.
        if total_weight <= u64::from(self.k) / 2 {
            num_centroids = len;
        } else {
            let compressed_weight = self.compressed_weight as f64;
            let normalizer = scale_function::normalizer(2.0 * f64::from(self.k), compressed_weight);
            let mut current = 1;
            let mut weight_so_far = 0.;
            while current < len {
                let c = centroids[current];
                let proposed_weight = centroids[num_centroids - 1].weight() + c.weight();
                if should_merge_centroid(
                    current,
                    len,
                    weight_so_far,
                    proposed_weight,
                    compressed_weight,
                    normalizer,
                ) {
                    // merge into existing centroid
                    centroids[num_centroids - 1].add(c);
                } else {
                    // copy to a new centroid
                    weight_so_far += centroids[num_centroids - 1].weight();
                    centroids[num_centroids] = c;
                    num_centroids += 1;
                }
                current += 1;
            }
        }

        centroids.truncate(num_centroids);
        if self.reverse_merge {
            centroids.reverse();
        }
        self.min = self.min.min(centroids[0].mean);
        self.max = self.max.max(centroids[num_centroids - 1].mean);
        self.reverse_merge = !self.reverse_merge;
        self.reduce_retained_capacity(&mut centroids);
        self.buffer = TDigestBuffer::new(centroids, 0);
    }

    fn reduce_retained_capacity(&self, centroids: &mut Vec<Centroid>) {
        let target_capacity = self.target_retained_capacity().max(centroids.len());
        if centroids.capacity() <= target_capacity {
            return;
        }

        // A merge can temporarily exceed the update-path target. Shrink after compaction so one
        // unusually large input does not pin that peak capacity for the rest of the digest's life.
        centroids.shrink_to(target_capacity);
    }

    /// Returns an estimate of the sketch's memory usage in bytes.
    pub fn estimated_size(&self) -> usize {
        self.buffer.estimated_size()
    }
}

/// Collects owned t-digests into one result with a single compression pass.
///
/// Empty inputs are ignored; if all inputs are empty, returns [`TDigestMut::default()`]. The result
/// uses the smallest `k` among the non-empty inputs. A single non-empty input is returned
/// unchanged.
///
/// Collecting consumes each digest without cloning it. Callers that need to retain their inputs
/// can use [`TDigestMut::merge`] or explicitly clone them before collection.
///
/// Batch collection can save repeated compression work when combining many small partials, but
/// is not always faster than repeated [`TDigestMut::merge`] calls. Larger compressed inputs can
/// favor incremental merging. Input count, retained centroid count, and value distribution all
/// affect performance; the number of original samples alone does not determine the cost.
///
/// Collection retains all non-empty input buffers before compression, even with a lazy iterator.
/// Fully compressed inputs are merged directly into the result; inputs with buffered updates also
/// require a combined centroid buffer and sorting workspace. Repeated `merge` calls or smaller
/// batches limit the number of inputs held at once, at the cost of additional compression passes.
/// Different merge groupings can produce different estimates.
///
/// # Panics
///
/// Panics if the combined total weight exceeds `u64::MAX` or the combined centroid count exceeds
/// `usize::MAX`.
///
/// # Examples
///
/// ```
/// use datasketches::tdigest::TDigestMut;
///
/// let partials = [1.0, 2.0].map(|value| {
///     let mut digest = TDigestMut::new(100).unwrap();
///     digest.update(value);
///     digest
/// });
/// let merged = partials.into_iter().collect::<TDigestMut>();
/// assert_eq!(merged.total_weight(), 2);
/// ```
///
/// Serialized states can be decoded in batches, retaining only one batch of decoded inputs at a
/// time. A batch size limits the number of digests, not their byte size; a strict memory budget
/// needs to account for varying input sizes and merge workspace.
///
/// ```
/// use datasketches::tdigest::TDigestMut;
///
/// let partial_states = [1.0, 2.0, 3.0, 4.0].map(|value| {
///     let mut digest = TDigestMut::new(100).unwrap();
///     digest.update(value);
///     digest.serialize()
/// });
///
/// let mut merged = TDigestMut::new(100).unwrap();
/// for batch in partial_states.chunks(2) {
///     let batch = batch
///         .iter()
///         .map(|bytes| TDigestMut::deserialize(bytes))
///         .collect::<Result<TDigestMut, _>>()
///         .unwrap();
///     merged.merge(&batch);
/// }
/// assert_eq!(merged.total_weight(), 4);
/// assert_eq!(merged.min_value(), Some(1.0));
/// assert_eq!(merged.max_value(), Some(4.0));
/// ```
impl FromIterator<TDigestMut> for TDigestMut {
    fn from_iter<T: IntoIterator<Item = TDigestMut>>(iter: T) -> Self {
        let mut digests = iter.into_iter();
        let Some(first) = digests.find(|digest| !digest.is_empty()) else {
            return TDigestMut::default();
        };
        let Some(second) = digests.find(|digest| !digest.is_empty()) else {
            return first;
        };

        // Each entry owns its input buffer and releases it when that input is exhausted.
        struct MergeInput {
            // Cache the next centroid so heap comparisons only touch heap storage.
            next_centroid: Centroid,
            centroids: std::vec::IntoIter<Centroid>,
            unmerged_len: usize,
            input_index: usize,
        }

        fn sift_down_merge_heap(heap: &mut [MergeInput], mut position: usize, reverse: bool) {
            // Equal means follow input order, as in a stable sort of concatenated inputs.
            // Reverse compression reverses that entire order, including ties.
            let precedes = |left: &MergeInput, right: &MergeInput| match centroid_cmp(
                &left.next_centroid,
                &right.next_centroid,
            ) {
                Ordering::Less => !reverse,
                Ordering::Greater => reverse,
                Ordering::Equal if reverse => left.input_index > right.input_index,
                Ordering::Equal => left.input_index < right.input_index,
            };
            loop {
                let left = (position * 2) + 1;
                if left >= heap.len() {
                    return;
                }
                let right = left + 1;
                let next = if right < heap.len() && precedes(&heap[right], &heap[left]) {
                    right
                } else {
                    left
                };
                if !precedes(&heap[next], &heap[position]) {
                    return;
                }
                heap.swap(position, next);
                position = next;
            }
        }

        let reverse = first.reverse_merge;
        let mut merged = TDigestMut::make(
            first.k,
            reverse,
            first.min,
            first.max,
            TDigestBuffer::default(),
            0,
        );
        let digests = [first, second].into_iter().chain(digests);
        let mut heap = Vec::with_capacity(digests.size_hint().0);
        let mut total_weight = 0u64;
        let mut num_centroids = 0usize;
        let mut all_compressed = true;
        for (input_index, digest) in digests.enumerate() {
            if digest.is_empty() {
                continue;
            }
            total_weight = total_weight
                .checked_add(digest.total_weight())
                .expect("combined t-digest weight exceeds u64::MAX");
            num_centroids = num_centroids
                .checked_add(digest.buffer.len())
                .expect("combined t-digest centroid count exceeds usize::MAX");
            merged.k = merged.k.min(digest.k);
            merged.min = merged.min.min(digest.min);
            merged.max = merged.max.max(digest.max);
            let unmerged_len = digest.buffer.unmerged_len();
            all_compressed &= unmerged_len == 0;
            let next_centroid = if reverse {
                *digest.buffer.centroids.last().unwrap()
            } else {
                digest.buffer.centroids[0]
            };
            heap.push(MergeInput {
                next_centroid,
                centroids: digest.buffer.centroids.into_iter(),
                unmerged_len,
                input_index,
            });
        }

        if !all_compressed {
            // Raw tails precede compressed prefixes to preserve stable equal-mean ordering.
            let mut centroids = Vec::with_capacity(num_centroids);
            for input in &heap {
                let prefix_len = input.centroids.len() - input.unmerged_len;
                centroids.extend_from_slice(&input.centroids.as_slice()[prefix_len..]);
            }
            for input in heap {
                let prefix_len = input.centroids.len() - input.unmerged_len;
                centroids.extend(input.centroids.take(prefix_len));
            }
            centroids.sort_by(centroid_cmp);
            merged.compress_sorted_centroids(centroids, total_weight);
            return merged;
        }

        for index in (0..heap.len() / 2).rev() {
            sift_down_merge_heap(&mut heap, index, reverse);
        }
        let compressed_weight = total_weight as f64;
        let normalizer = scale_function::normalizer(2.0 * f64::from(merged.k), compressed_weight);
        // Reserve compressed output only; later updates can grow their own workspace.
        let mut retained: Vec<Centroid> =
            Vec::with_capacity(merged.target_centroids().min(num_centroids));
        let mut weight_so_far = 0.;
        // Advancing the selected input can only move it down the heap; the other inputs stay put.
        for current in 0..num_centroids {
            let input = &mut heap[0];
            let centroid = input.next_centroid;
            let _ = if reverse {
                input.centroids.next_back()
            } else {
                input.centroids.next()
            };
            if input.centroids.len() == 0 {
                heap.swap_remove(0);
            } else {
                input.next_centroid = if reverse {
                    *input.centroids.as_slice().last().unwrap()
                } else {
                    input.centroids.as_slice()[0]
                };
            }
            sift_down_merge_heap(&mut heap, 0, reverse);

            if let Some(last) = retained.last_mut() {
                let proposed_weight = last.weight() + centroid.weight();
                if should_merge_centroid(
                    current,
                    num_centroids,
                    weight_so_far,
                    proposed_weight,
                    compressed_weight,
                    normalizer,
                ) {
                    last.add(centroid);
                } else {
                    weight_so_far += last.weight();
                    retained.push(centroid);
                }
            } else {
                retained.push(centroid);
            }
        }

        debug_assert!(retained.len() <= merged.target_centroids());
        if reverse {
            retained.reverse();
        }
        debug_assert_eq!(
            retained
                .iter()
                .map(|centroid| centroid.weight.get())
                .sum::<u64>(),
            total_weight,
            "compressed centroids must preserve total weight"
        );
        merged.compressed_weight = total_weight;
        merged.reverse_merge = !reverse;
        merged.buffer = TDigestBuffer::new(retained, 0);
        merged
    }
}

fn serialize_compressed(
    k: u16,
    reverse_merge: bool,
    min: f64,
    max: f64,
    centroids: &[Centroid],
    total_weight: u64,
) -> Vec<u8> {
    let is_empty = centroids.is_empty();
    let is_single_value = total_weight == 1;
    let mut total_size = if is_empty || is_single_value {
        // Preamble, serial version, family, k, flags, and two unused bytes.
        size_of::<u64>()
    } else {
        // The short header plus centroid and buffered-value counts.
        size_of::<u64>() * 2
    };
    if is_single_value {
        total_size += size_of::<f64>();
    } else if !is_empty {
        total_size += size_of::<f64>() * 2;
        total_size += centroids.len() * (size_of::<f64>() + size_of::<u64>());
    }

    let mut bytes = SketchBytes::with_capacity(total_size);
    bytes.write_u8(if is_empty || is_single_value {
        PREAMBLE_LONGS_EMPTY_OR_SINGLE
    } else {
        PREAMBLE_LONGS_MULTIPLE
    });
    bytes.write_u8(SERIAL_VERSION);
    bytes.write_u8(Family::TDIGEST.id);
    bytes.write_u16_le(k);
    bytes.write_u8({
        let mut flags = 0;
        if is_empty {
            flags |= FLAGS_IS_EMPTY;
        }
        if is_single_value {
            flags |= FLAGS_IS_SINGLE_VALUE;
        }
        if reverse_merge {
            flags |= FLAGS_REVERSE_MERGE;
        }
        flags
    });
    bytes.write_u16_le(0); // unused
    if is_empty {
        return bytes.into_bytes();
    }
    if is_single_value {
        bytes.write_f64_le(min);
        return bytes.into_bytes();
    }
    bytes.write_u32_le(centroids.len() as u32);
    bytes.write_u32_le(0); // no buffered values
    bytes.write_f64_le(min);
    bytes.write_f64_le(max);
    for centroid in centroids {
        bytes.write_f64_le(centroid.mean);
        bytes.write_u64_le(centroid.weight.get());
    }
    bytes.into_bytes()
}

/// Immutable (frozen) T-Digest sketch for estimating quantiles and ranks.
///
/// See the [module level documentation](super) for more.
pub struct TDigest {
    k: u16,

    reverse_merge: bool,
    min: f64,
    max: f64,

    centroids: Vec<Centroid>,
    centroids_weight: u64,
}

impl TDigest {
    /// Returns the compression parameter `k` used to configure this t-digest.
    pub fn k(&self) -> u16 {
        self.k
    }

    /// Returns `true` if this t-digest has not seen any data.
    pub fn is_empty(&self) -> bool {
        self.centroids.is_empty()
    }

    /// Returns the minimum value seen by this t-digest, or `None` if it is empty.
    pub fn min_value(&self) -> Option<f64> {
        if self.is_empty() {
            None
        } else {
            Some(self.min)
        }
    }

    /// Returns the maximum value seen by this t-digest, or `None` if it is empty.
    pub fn max_value(&self) -> Option<f64> {
        if self.is_empty() {
            None
        } else {
            Some(self.max)
        }
    }

    /// Returns the total weight.
    pub fn total_weight(&self) -> u64 {
        self.centroids_weight
    }

    /// Serializes this immutable t-digest to bytes in double precision.
    ///
    /// The serialized data can be read by other DataSketches implementations.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigest;
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// sketch.update(1.0);
    /// let digest = sketch.freeze();
    /// let bytes = digest.serialize();
    /// let decoded = TDigest::deserialize(&bytes).unwrap();
    /// assert_eq!(decoded.max_value(), Some(1.0));
    /// ```
    pub fn serialize(&self) -> Vec<u8> {
        serialize_compressed(
            self.k,
            self.reverse_merge,
            self.min,
            self.max,
            &self.centroids,
            self.centroids_weight,
        )
    }

    /// Deserializes an immutable t-digest from the standard double-precision format.
    ///
    /// Also accepts data serialized by other DataSketches implementations.
    ///
    /// The format of the [reference implementation](https://github.com/tdunning/t-digest) is
    /// auto-detected. Use [`deserialize_f32()`](Self::deserialize_f32) for the compact
    /// DataSketches C++ `tdigest<float>` format.
    ///
    /// # Errors
    ///
    /// Returns `InvalidData` under the same conditions as [`TDigestMut::deserialize`].
    pub fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
        Ok(TDigestMut::deserialize(bytes)?.freeze())
    }

    /// Deserializes an immutable t-digest from the compact single-precision DataSketches format.
    ///
    /// Accepts data serialized by `tdigest<float>` in DataSketches C++; see
    /// [`TDigestMut::deserialize_f32`] for the encoding details.
    ///
    /// # Errors
    ///
    /// Returns `InvalidData` under the same conditions as [`TDigestMut::deserialize_f32`].
    pub fn deserialize_f32(bytes: &[u8]) -> Result<Self, Error> {
        Ok(TDigestMut::deserialize_f32(bytes)?.freeze())
    }

    fn view(&self) -> TDigestView<'_> {
        TDigestView {
            min: self.min,
            max: self.max,
            centroids: &self.centroids,
            centroids_weight: self.centroids_weight,
        }
    }

    /// Returns an approximation to the Cumulative Distribution Function (CDF), which is the
    /// cumulative analog of the PMF, of the input stream given a set of split points.
    ///
    /// # Arguments
    ///
    /// * `split_points`: An array of _m_ unique, monotonically increasing values that divide the
    ///   input domain into _m+1_ consecutive disjoint intervals.
    ///
    /// # Returns
    ///
    /// An array of m+1 doubles, which are a consecutive approximation to the CDF of the input
    /// stream given the split points. The value at array position j of the returned CDF array
    /// is the sum of the returned values in positions 0 through j of the returned PMF array.
    /// This can be viewed as array of ranks of the given split points plus one more value that
    /// is always 1. For a non-empty t-digest, an empty `split_points` slice returns
    /// `Ok(Some(vec![1.0]))`.
    ///
    /// Returns `Ok(None)` if this t-digest is empty.
    ///
    /// # Errors
    ///
    /// Returns `InvalidArgument` if `split_points` contains NaN or is not strictly increasing, even
    /// if this t-digest is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// for value in [1.0, 2.0, 3.0] {
    ///     sketch.update(value);
    /// }
    /// let digest = sketch.freeze();
    /// let cdf = digest.cdf(&[1.5]).unwrap().unwrap();
    /// assert_eq!(cdf.len(), 2);
    /// ```
    pub fn cdf(&self, split_points: &[f64]) -> Result<Option<Vec<f64>>, Error> {
        check_split_points(split_points)?;
        Ok(self.view().cdf(split_points))
    }

    /// Returns an approximation to the Probability Mass Function (PMF) of the input stream
    /// given a set of split points.
    ///
    /// # Arguments
    ///
    /// * `split_points`: An array of _m_ unique, monotonically increasing values that divide the
    ///   input domain into _m+1_ consecutive disjoint intervals (bins).
    ///
    /// # Returns
    ///
    /// An array of m+1 doubles each of which is an approximation to the fraction of the input
    /// stream values (the mass) that fall into one of those intervals.
    /// For a non-empty t-digest, an empty `split_points` slice returns `Ok(Some(vec![1.0]))`.
    ///
    /// Returns `Ok(None)` if this t-digest is empty.
    ///
    /// # Errors
    ///
    /// Returns `InvalidArgument` if `split_points` contains NaN or is not strictly increasing, even
    /// if this t-digest is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// for value in [1.0, 2.0, 3.0] {
    ///     sketch.update(value);
    /// }
    /// let digest = sketch.freeze();
    /// let pmf = digest.pmf(&[1.5]).unwrap().unwrap();
    /// assert_eq!(pmf.len(), 2);
    /// ```
    pub fn pmf(&self, split_points: &[f64]) -> Result<Option<Vec<f64>>, Error> {
        check_split_points(split_points)?;
        Ok(self.view().pmf(split_points))
    }

    /// Returns the approximate normalized rank in `[0.0, 1.0]` of the given value.
    ///
    /// Returns `Ok(None)` if this t-digest is empty.
    ///
    /// # Errors
    ///
    /// Returns `InvalidArgument` if `value` is NaN, even if this t-digest is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// for value in [1.0, 2.0, 3.0] {
    ///     sketch.update(value);
    /// }
    /// let digest = sketch.freeze();
    /// let rank = digest.rank(2.0).unwrap().unwrap();
    /// assert!((0.0..=1.0).contains(&rank));
    /// ```
    pub fn rank(&self, value: f64) -> Result<Option<f64>, Error> {
        if value.is_nan() {
            return Err(Error::invalid_argument("value must not be NaN"));
        }
        Ok(self.view().rank(value))
    }

    /// Returns the approximate quantile for the given normalized rank.
    ///
    /// Ranks `0.0` and `1.0` return the stored minimum and maximum, respectively.
    ///
    /// Returns `Ok(None)` if this t-digest is empty.
    ///
    /// # Errors
    ///
    /// Returns `InvalidArgument` if `rank` is NaN or outside `[0.0, 1.0]`, even if this t-digest is
    /// empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// for value in [1.0, 2.0, 3.0] {
    ///     sketch.update(value);
    /// }
    /// let digest = sketch.freeze();
    /// let q = digest.quantile(0.5).unwrap().unwrap();
    /// assert!((1.0..=3.0).contains(&q));
    /// ```
    pub fn quantile(&self, rank: f64) -> Result<Option<f64>, Error> {
        check_rank(rank)?;
        Ok(self.view().quantile(rank))
    }

    /// Returns approximate quantiles for the given normalized ranks.
    ///
    /// Ranks in nondecreasing order are answered with one centroid scan. Ranks in any other order
    /// are accepted and results are returned in the same order as the input.
    ///
    /// Returns `Ok(None)` if this t-digest is empty. Otherwise, an empty `ranks` slice returns
    /// `Ok(Some(vec![]))`.
    ///
    /// # Errors
    ///
    /// Returns `InvalidArgument` if any rank is NaN or outside `[0.0, 1.0]`, even if this t-digest
    /// is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// for value in [1.0, 2.0, 3.0] {
    ///     sketch.update(value);
    /// }
    /// let digest = sketch.freeze();
    /// let quantiles = digest.quantiles(&[0.25, 0.5, 0.75]).unwrap().unwrap();
    /// assert_eq!(quantiles.len(), 3);
    /// ```
    pub fn quantiles(&self, ranks: &[f64]) -> Result<Option<Vec<f64>>, Error> {
        check_ranks(ranks)?;
        Ok(self.view().quantiles(ranks))
    }

    /// Converts this immutable t-digest into a mutable one.
    ///
    /// # Examples
    ///
    /// ```
    /// use datasketches::tdigest::TDigestMut;
    ///
    /// let mut sketch = TDigestMut::new(100).unwrap();
    /// sketch.update(1.0);
    /// let digest = sketch.freeze();
    /// let mut mutable = digest.unfreeze();
    /// mutable.update(2.0);
    /// assert_eq!(mutable.total_weight(), 2);
    /// ```
    pub fn unfreeze(self) -> TDigestMut {
        TDigestMut::make(
            self.k,
            self.reverse_merge,
            self.min,
            self.max,
            TDigestBuffer::new(self.centroids, 0),
            self.centroids_weight,
        )
    }

    /// Returns an estimate of the sketch's memory usage in bytes.
    pub fn estimated_size(&self) -> usize {
        self.centroids.capacity() * size_of::<Centroid>()
    }
}

struct TDigestView<'a> {
    min: f64,
    max: f64,
    centroids: &'a [Centroid],
    centroids_weight: u64,
}

impl TDigestView<'_> {
    fn pmf(&self, split_points: &[f64]) -> Option<Vec<f64>> {
        let mut buckets = self.cdf(split_points)?;
        for i in (1..buckets.len()).rev() {
            buckets[i] -= buckets[i - 1];
        }
        Some(buckets)
    }

    fn cdf(&self, split_points: &[f64]) -> Option<Vec<f64>> {
        if self.centroids.is_empty() {
            return None;
        }

        // Split points increase, so their lower centroid indexes never move backwards.
        // Accumulate integer weights only as far as each query needs.
        let mut previous_lower = 0;
        let mut weight_below = 0;

        let mut ranks = Vec::with_capacity(split_points.len() + 1);
        for &p in split_points {
            ranks.push(self.rank_with_weights(p, |lower| {
                weight_below += self.centroids[previous_lower..lower]
                    .iter()
                    .map(|c| c.weight.get())
                    .sum::<u64>();
                previous_lower = lower;
                weight_below
            }));
        }
        ranks.push(1.0);
        Some(ranks)
    }

    fn rank(&self, value: f64) -> Option<f64> {
        debug_assert!(!value.is_nan(), "value must not be NaN");

        if self.centroids.is_empty() {
            return None;
        }
        Some(self.rank_with_weights(value, |lower| {
            self.centroids[..lower].iter().map(|c| c.weight.get()).sum()
        }))
    }

    /// Rank within a non-empty centroid sequence, using the weight before its lower anchor.
    fn rank_with_weights(&self, value: f64, weight_before: impl FnOnce(usize) -> u64) -> f64 {
        if value < self.min {
            return 0.0;
        }
        if value > self.max {
            return 1.0;
        }
        if self.min == self.max {
            return 0.5;
        }

        let centroids_weight = self.centroids_weight as f64;
        let num_centroids = self.centroids.len();

        // Reserve at most one sample for each stored extremum, without passing the adjacent
        // centroid center. Updates can place a unit-weight centroid inside the stored extrema,
        // leaving no mass to interpolate in that tail.
        let first = &self.centroids[0];
        if value < first.mean {
            let center = centroid_center(0, first.weight.get());
            return if value == self.min {
                0.5 / centroids_weight
            } else {
                interpolate(
                    center.min(1.),
                    center,
                    interpolation_fraction(value, self.min, first.mean),
                ) / centroids_weight
            };
        }

        let last = &self.centroids[num_centroids - 1];
        if value > last.mean {
            let center =
                centroid_center(self.centroids_weight - last.weight.get(), last.weight.get());
            return if value == self.max {
                // Round the maximum's mass in the same coordinates as the centroid centers.
                centroid_center(self.centroids_weight - 1, 1) / centroids_weight
            } else {
                interpolate(
                    center,
                    center.max((self.centroids_weight - 1) as f64),
                    interpolation_fraction(value, last.mean, self.max),
                ) / centroids_weight
            };
        }

        // The tail checks place value within the centroid means. Between means, the first
        // mean >= value and its predecessor bracket it; at a mean, use the entire equal run.
        let index = self.centroids.partition_point(|c| c.mean < value);
        let (lower, upper) = if self.centroids[index].mean == value {
            let end = self.centroids.partition_point(|c| c.mean <= value);
            (index, end - 1)
        } else {
            (index - 1, index)
        };

        let weight_below = weight_before(lower);
        let weight_between: u64 = self.centroids[lower..upper]
            .iter()
            .map(|c| c.weight.get())
            .sum();
        let left = &self.centroids[lower];
        let right = &self.centroids[upper];
        let fraction = if left.mean < right.mean {
            interpolation_fraction(value, left.mean, right.mean)
        } else {
            0.5
        };
        interpolate(
            centroid_center(weight_below, left.weight.get()),
            centroid_center(weight_below + weight_between, right.weight.get()),
            fraction,
        ) / centroids_weight
    }

    fn quantile(&self, rank: f64) -> Option<f64> {
        debug_assert!((0.0..=1.0).contains(&rank), "rank must be in [0.0, 1.0]");

        if self.centroids.is_empty() {
            return None;
        }

        let mut quantile = [0.];
        self.fill_quantiles(std::iter::once((0, rank)), &mut quantile);
        Some(quantile[0])
    }

    fn quantiles(&self, ranks: &[f64]) -> Option<Vec<f64>> {
        debug_assert!(
            ranks.iter().all(|rank| (0.0..=1.0).contains(rank)),
            "ranks must be in [0.0, 1.0]"
        );

        if self.centroids.is_empty() {
            return None;
        }

        let mut quantiles = vec![0.; ranks.len()];
        if ranks.is_sorted() {
            self.fill_quantiles(ranks.iter().copied().enumerate(), &mut quantiles);
            return Some(quantiles);
        }

        // The scan only moves forward. Sort indices so queries become monotonic without changing
        // the caller's output order.
        let mut rank_order = (0..ranks.len()).collect::<Vec<_>>();
        rank_order.sort_unstable_by(|&left, &right| {
            // Ranks have already been validated, so neither value can be NaN.
            ranks[left].partial_cmp(&ranks[right]).unwrap()
        });
        self.fill_quantiles(
            rank_order.into_iter().map(|index| (index, ranks[index])),
            &mut quantiles,
        );
        Some(quantiles)
    }

    /// Answers nondecreasing ranks at their original output indices.
    fn fill_quantiles(
        &self,
        ranks: impl DoubleEndedIterator<Item = (usize, f64)>,
        quantiles: &mut [f64],
    ) {
        if self.min == self.max {
            quantiles.fill(self.min);
            return;
        }

        let centroids_weight = self.centroids_weight as f64;
        let mut queries = ranks
            .map(|(index, rank)| (index, rank * centroids_weight))
            .rev()
            .peekable();

        // Consume the right tail from the back, so the centroid scan can omit all tail checks.
        while let Some((index, _)) = queries.next_if(|&(_, weight)| centroids_weight - weight < 1.)
        {
            quantiles[index] = self.max;
        }
        if queries.peek().is_none() {
            return;
        }

        let last = self.centroids.last().unwrap();
        let last_weight = last.weight.get();
        let last_center = centroid_center(self.centroids_weight - last_weight, last_weight);
        if last_weight > 1 {
            let tail_end = (self.centroids_weight - 1) as f64;
            while let Some((index, weight)) = queries.next_if(|&(_, weight)| weight >= last_center)
            {
                // Integer counts can round a wider tail to zero width as well. At this
                // boundary prefer the maximum, as for an exact two-sample tail.
                quantiles[index] = if last_center == tail_end {
                    self.max
                } else {
                    interpolate(
                        last.mean,
                        self.max,
                        (weight - last_center) / (tail_end - last_center),
                    )
                };
            }
        }

        let mut queries = queries.rev().peekable();
        while let Some((index, _)) = queries.next_if(|&(_, weight)| weight < 1.) {
            quantiles[index] = self.min;
        }
        if queries.peek().is_none() {
            return;
        }

        let first = &self.centroids[0];
        let first_weight = first.weight.get();
        let first_center = centroid_center(0, first_weight);
        if first_weight > 1 {
            // At first_weight == 2, the weight < 1 pre-pass already consumed this interval.
            while let Some((index, weight)) = queries.next_if(|&(_, weight)| weight < first_center)
            {
                quantiles[index] =
                    interpolate(self.min, first.mean, (weight - 1.) / (first_center - 1.));
            }
        }
        if queries.peek().is_none() {
            return;
        }

        // Answer the remaining queries by centroid interval; both streams advance only forward.
        let mut weight_before = 0;
        let mut left_center = first_center;
        for pair in self.centroids.windows(2) {
            let left = &pair[0];
            let right = &pair[1];
            let right_before = weight_before + left.weight.get();
            let right_center = centroid_center(right_before, right.weight.get());
            loop {
                let Some(&(index, weight)) = queries.peek() else {
                    return;
                };
                if right_center <= weight {
                    break;
                }
                queries.next();
                let mut start = left_center;
                if left.weight.get() == 1 {
                    start = right_before as f64;
                    if weight < start {
                        quantiles[index] = left.mean;
                        continue;
                    }
                }
                let mut end = right_center;
                if right.weight.get() == 1 {
                    end = right_before as f64;
                    if weight >= end {
                        quantiles[index] = right.mean;
                        continue;
                    }
                }
                quantiles[index] =
                    interpolate(left.mean, right.mean, (weight - start) / (end - start));
            }
            weight_before = right_before;
            left_center = right_center;
        }

        // Rounding at large total weights can exhaust the scan near the maximum.
        for (index, _) in queries {
            quantiles[index] = self.max;
        }
    }
}

/// Checks the sequential validity of the given array of double values.
/// They must be unique, monotonically increasing and not NaN.
fn check_split_points(split_points: &[f64]) -> Result<(), Error> {
    if split_points.iter().any(|split_point| split_point.is_nan()) {
        return Err(Error::invalid_argument(
            "split_points must not contain NaN values",
        ));
    }
    if !split_points.windows(2).all(|pair| pair[0] < pair[1]) {
        return Err(Error::invalid_argument(
            "split_points must be unique and monotonically increasing",
        ));
    }
    Ok(())
}

fn check_rank(rank: f64) -> Result<(), Error> {
    if !(0.0..=1.0).contains(&rank) {
        return Err(Error::invalid_argument(format!(
            "rank must be in [0.0, 1.0], got {rank}"
        )));
    }
    Ok(())
}

fn check_ranks(ranks: &[f64]) -> Result<(), Error> {
    for &rank in ranks {
        check_rank(rank)?;
    }
    Ok(())
}

fn centroid_cmp(a: &Centroid, b: &Centroid) -> Ordering {
    match a.mean.partial_cmp(&b.mean) {
        Some(order) => order,
        None => unreachable!("NaN values should never be present in centroids"),
    }
}

fn centroids_are_sorted(centroids: &[Centroid]) -> bool {
    centroids
        .windows(2)
        .all(|pair| centroid_cmp(&pair[0], &pair[1]) != Ordering::Greater)
}

/// Whether the last output centroid can absorb the next input under the K_2 weight limit.
///
/// `input_index` (zero-based) and `input_len` count input centroids in scan order.
/// `weight_before` sums the finalized output weights, excluding the last output centroid;
/// `proposed_weight` combines that centroid and the next input. `total_weight` sums all
/// input weights, and `normalizer` is the K_2 normalizer for compression `2 * sketch.k`.
///
/// The [paper] limits a centroid with weight > 1 to a scale span of at most one (Section 2.3,
/// Eq. 4). As in the [reference implementation], we use a conservative weight limit instead of
/// evaluating that span: K_2's reciprocal slope, `q * (1 - q) / normalizer`, bounds the
/// allowed fraction of total weight. Both ends of the proposed rank interval must allow
/// the merge, so we take the smaller limit. This keeps centroids finer near the tails;
/// symmetry under `q -> 1 - q` also permits scanning in reverse. See Section 2.8, Eq. 8 for K_2.
///
/// [paper]: https://arxiv.org/abs/1902.04023
/// [reference implementation]: https://github.com/tdunning/t-digest/blob/eca1125a39c13e918d7054105dc89bcda11cfa44/core/src/main/java/com/tdunning/math/stats/MergingDigest.java#L421-L436
fn should_merge_centroid(
    input_index: usize,
    input_len: usize,
    weight_before: f64,
    proposed_weight: f64,
    total_weight: f64,
    normalizer: f64,
) -> bool {
    // Index 0 already seeded the output: refusing index 1 and the last index preserves
    // the first and last input centroids without further merging.
    if input_index == 1 || input_index == input_len - 1 {
        return false;
    }
    let q_start = weight_before / total_weight;
    let q_end = (weight_before + proposed_weight) / total_weight;
    let weight_limit = total_weight
        * scale_function::max(q_start, normalizer).min(scale_function::max(q_end, normalizer));
    proposed_weight <= weight_limit
}

fn merge_sorted_centroids(left: &mut Vec<Centroid>, right: &[Centroid]) {
    debug_assert!(!right.is_empty());
    debug_assert!(centroids_are_sorted(left));
    debug_assert!(centroids_are_sorted(right));

    let mut left_index = left.len();
    let mut right_index = right.len();
    let mut output_index = left_index + right_index;
    left.reserve(right.len());
    left.resize(output_index, right[0]);

    while left_index > 0 && right_index > 0 {
        let left_centroid = left[left_index - 1];
        let right_centroid = right[right_index - 1];
        output_index -= 1;
        // Taking the left side on ties while filling backward keeps the right side first in the
        // final order, matching a stable sort after rotating the compressed left prefix.
        if centroid_cmp(&left_centroid, &right_centroid) != Ordering::Less {
            left_index -= 1;
            left[output_index] = left_centroid;
        } else {
            right_index -= 1;
            left[output_index] = right_centroid;
        }
    }
    if right_index > 0 {
        left[..right_index].copy_from_slice(&right[..right_index]);
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
struct Centroid {
    mean: f64,
    weight: NonZeroU64,
}

impl Centroid {
    fn add(&mut self, other: Centroid) {
        let (self_weight, other_weight) = (self.weight(), other.weight());
        let total_weight = self_weight + other_weight;
        self.weight = self
            .weight
            .checked_add(other.weight.get())
            .expect("weight overflow");

        // Start at the heavier centroid so a small contribution is not rounded away in 1 - ratio.
        let (start, end, fraction) = if self_weight >= other_weight {
            (self.mean, other.mean, other_weight / total_weight)
        } else {
            (other.mean, self.mean, self_weight / total_weight)
        };
        debug_assert!(start.is_finite() && end.is_finite() && fraction > 0. && fraction <= 0.5);

        // Same-sign means within a factor of two in magnitude subtract exactly. For wider
        // gaps, a step of at most 1/2 cannot cross either endpoint even if subtraction rounds.
        // Queries can interpolate farther and still need the general bounds.
        self.mean = if start.is_sign_positive() == end.is_sign_positive() {
            (end - start).mul_add(fraction, start)
        } else if 1. - fraction == 1. {
            // Both corrections can matter even when 1 - fraction rounds to 1.
            // Halving before subtraction avoids overflow; the FMA combines the
            // correction with start before rounding the result.
            (end * 0.5 - start * 0.5).mul_add(2. * fraction, start)
        } else {
            interpolate(start, end, fraction)
        };
    }

    fn weight(&self) -> f64 {
        self.weight.get() as f64
    }
}

fn check_non_nan(value: f64, tag: &'static str) -> Result<(), Error> {
    if value.is_nan() {
        return Err(Error::deserial(format!(
            "malformed data: {tag} cannot be NaN"
        )));
    }

    Ok(())
}

fn check_finite(value: f64, tag: &'static str) -> Result<(), Error> {
    if value.is_infinite() {
        return Err(Error::deserial(format!(
            "malformed data: {tag} cannot be infinite"
        )));
    }

    Ok(())
}

#[inline]
fn check_extrema(min: f64, max: f64, format: &'static str) -> Result<(), Error> {
    if !min.is_finite() || !max.is_finite() {
        return Err(Error::deserial(format!(
            "malformed data: {format} extrema must be finite"
        )));
    }
    if min > max {
        return Err(Error::deserial(format!(
            "malformed data: {format} min {min} exceeds max {max}"
        )));
    }
    Ok(())
}

fn check_single_sample_extrema(min: f64, max: f64, total_weight: u64) -> Result<(), Error> {
    // Tail interpolation assumes that distinct extrema represent at least two samples.
    if total_weight == 1 && min != max {
        return Err(Error::deserial(
            "malformed data: a single sample must have equal extrema",
        ));
    }
    Ok(())
}

fn check_nonzero(value: u64, tag: &'static str) -> Result<NonZeroU64, Error> {
    NonZeroU64::new(value)
        .ok_or_else(|| Error::deserial(format!("malformed data: {tag} cannot be zero")))
}

fn check_compat_weight(value: f64, tag: &'static str) -> Result<NonZeroU64, Error> {
    check_non_nan(value, tag)?;
    check_finite(value, tag)?;
    if !(1.0..u64::MAX as f64).contains(&value) {
        return Err(Error::deserial(format!(
            "malformed data: {tag} must be representable as a positive u64"
        )));
    }
    if value.trunc() != value {
        return Err(Error::deserial(format!(
            "malformed data: {tag} must not have a fractional part"
        )));
    }
    check_nonzero(value as u64, tag)
}

fn checked_weight_sum(total_weight: u64, weight: u64) -> Result<u64, Error> {
    total_weight
        .checked_add(weight)
        .ok_or_else(|| Error::deserial("malformed data: total weight overflow"))
}

/// Generates cluster sizes proportional to `q*(1-q)`.
///
/// The use of a normalizing function results in a strictly bounded number of clusters no matter
/// how many samples.
///
/// Corresponds to K_2 in the reference implementation
mod scale_function {
    pub fn max(q: f64, normalizer: f64) -> f64 {
        q * (1. - q) / normalizer
    }

    pub fn normalizer(compression: f64, n: f64) -> f64 {
        compression / z(compression, n)
    }

    pub fn z(compression: f64, n: f64) -> f64 {
        4. * (n / compression).ln() + 24.
    }
}

/// Keeps cumulative counts exact until a centroid's midpoint is needed for interpolation.
/// All intervals and tails use this same center to avoid inconsistent boundary rounding.
fn centroid_center(weight_before: u64, weight: u64) -> f64 {
    (weight_before + weight / 2) as f64 + (weight % 2) as f64 * 0.5
}

/// Interpolates finite endpoints at a fraction in `[0, 1]`, preserving their bounds.
///
/// The sign split follows WG21 P0811R3:
/// <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0811r3.html>.
fn interpolate(start: f64, end: f64, fraction: f64) -> f64 {
    debug_assert!(start.is_finite() && end.is_finite() && (0.0..=1.0).contains(&fraction));
    // Callers guarantee the fraction's range, but division may round it to an endpoint.
    // Even an FMA need not reproduce `end` at 1: its subtracted difference already rounded.
    if fraction <= 0. {
        return start;
    }
    if fraction >= 1. {
        return end;
    }
    if start.is_sign_positive() != end.is_sign_positive() {
        // Avoid the potentially overflowing difference of opposite-signed endpoints.
        start * (1. - fraction) + end * fraction
    } else {
        // Same-sign subtraction is finite; rounding must not overshoot the end.
        let value = (end - start).mul_add(fraction, start);
        if start < end {
            value.min(end)
        } else {
            value.max(end)
        }
    }
}

/// Locates a value between distinct finite endpoints.
fn interpolation_fraction(value: f64, start: f64, end: f64) -> f64 {
    let width = end - start;
    if width.is_finite() {
        (value - start) / width
    } else {
        // Scaling is needed only when opposite-signed endpoints overflow their difference.
        (value * 0.5 - start * 0.5) / (end * 0.5 - start * 0.5)
    }
}

#[cfg(test)]
mod tests {
    use super::interpolate;

    #[test]
    fn interpolation_preserves_endpoints_after_subtraction_rounds() {
        let start = f64::EPSILON / 2.;
        let end = 1_f64.next_up();
        // end - start rounds to 1, and even an FMA at fraction 1 then returns 1, not end.
        // The endpoint branch also retains the sign of a zero endpoint.
        for (start, end) in [(start, end), (end, start), (-0., 1.), (-1., 0.)] {
            assert_eq!(interpolate(start, end, 0.).to_bits(), start.to_bits());
            assert_eq!(interpolate(start, end, 1.).to_bits(), end.to_bits());
        }
    }
}