Class RefinementList
Defined in File KernelDensity.hpp
Class Documentation
-
class RefinementList
The list of tree nodes a kernel density evaluation is refining, as a sum tree.
Holds a set of entries, each a tree node with a central estimate of some quantity and an uncertainty on it, and provides, each in O(1), the sums of all entries’ estimates and uncertainties and the entry with the largest uncertainty; inserting, replacing, and erasing an entry each cost O(log n).
Entries live in slots, the leaves of a complete binary tree stored as an implicit array (internal node i has children 2i and 2i + 1, and slot s is node capacity + s). Every internal node holds the sums of its two children’s estimates and uncertainties, and which of the slots below it has the largest uncertainty; changing a slot recomputes the nodes on its path to the root. Each sum is thus always recomputed from its two children’s current values, never updated by adding and subtracting changes, so its rounding error is bounded by a few ulps per level of the current values, however many changes have been made. A running total, by contrast, accumulates error from every term ever added and removed: refinement removes parents whose uncertainties can exceed the final one by many orders of magnitude, so this error can grow to swamp the tolerance and keep it from ever being met. Freed slots are reused, and the capacity doubles when every slot is in use.
Public Functions
-
inline explicit RefinementList(const std::size_t capacity = 64)
Construct an empty list.
- Parameters:
capacity – Initial number of slots; rounded up to a power of two (at least 1)
-
inline auto estimate() const -> double
Sum of the entries’ estimates.
- Returns:
The sum (zero if empty)
-
inline auto uncertainty() const -> double
Sum of the entries’ uncertainties.
- Returns:
The sum (zero if empty)
-
inline auto worst() const -> std::size_t
The slot with the largest uncertainty.
- Returns:
Its index; if the list is empty, or all uncertainties are zero, a slot whose uncertainty is zero
-
inline auto uncertainty(const std::size_t slot) const -> double
Uncertainty of the entry in a slot.
- Parameters:
slot – The slot
- Returns:
Its uncertainty (zero if the slot is empty)
-
inline auto node(const std::size_t slot) const -> std::size_t
Tree node of the entry in a slot.
- Parameters:
slot – The slot; must hold an entry
- Returns:
The node’s index
-
inline auto size() const -> std::size_t
Number of entries.
- Returns:
The number of slots in use
-
inline void insert(const std::size_t node, const double est, const double err)
Add an entry, in a free slot.
- Parameters:
node – Its tree node
est – Its estimate
err – Its uncertainty; must be non-negative
-
inline void replace(const std::size_t slot, const std::size_t node, const double est, const double err)
Replace the entry in a slot.
- Parameters:
slot – The slot; must hold an entry
node – The new entry’s tree node
est – Its estimate
err – Its uncertainty; must be non-negative
-
inline void erase(const std::size_t slot)
Remove the entry in a slot, freeing the slot for reuse.
- Parameters:
slot – The slot; must hold an entry
-
inline explicit RefinementList(const std::size_t capacity = 64)