Binary Indexed Sum Tree (BIST); Fenwick proposed "BIT" but that A) collides w/many uses B) takes partial (S)ums as implied, but explicit is better (though products can work) and C) does not rhyme with "dist" (for distribution - what it is mostly about). While Inet has tutorials, to my knowledge no one (yet) collects all these algos in one place. Fenwick1994 itself messed up invCDF, correcting w/a tech report a year later. This code only allocates needed space & uses 0-based array indexing. See https://en.wikipedia.org/wiki/Fenwick_tree
The idea of a standard binary heap with kids(k)@[2k],[2k+1] for dynamic distributions goes back to Wong&Easton 1980 (or earlier?). Fenwick's clever index encoding/overlaid trees idea allows using 1/4 to 1/2 that space (only max index+1 array elements vs 2*lgCeil(n)), a constant factor improvement. Good explanations truly need figures, as in the original Fenwick paper | Wikipedia.
The Bist[T] type in this module is generic over the type of counters used for partial sums|counts. For few total items, you can use a Bist[uint8] while for many you want to use Bist[uint32]. This can be space-optimized up to 2X further with adix/sequint specialized to store an array of B-bit counters. Ranked B-trees are faster for >24..28-bit index spaces as L3 CPU caching fails, but needing >7..8 decimal dynamic ranges is also rare.
Procs
proc invCDF[T](t: Bist[T]; s: T): (int, T)
- For 0 < s <= tot return (i0,s0) so sum(..<i0)=s0 < s and sum(..i0)>=s Source Edit
proc quantile[T](t: Bist[T]; q: float): float
- Parzen-interpolated quantile when no caller index mapping is needed Source Edit
proc quantile[T](t: Bist[T]; q: float; iL, iH: var int): float
- Parzen-interpolated quantile; E.g., q=0.9 => 90th percentile. answer = result*iL + (1-result)*iH, but is left to caller to do { in case it is mapping larger numeric ranges to/from iL,iH }. Tm ~ 2*lg(addrSpace). Unlike other (broken!) quantile-interpolation methods, Parzen's connects midpoints of vertical CDF jumps, not horizontal. This makes more sense, corresponding to Wilcoxon 1945 & later tie mid-ranking recommendations. Source Edit