View Source exometer_shallowtree (exometer_core v1.7.1)

Size-constrained leftist tree Inspired by Leftist Trees by Sartaj Sahni.

The purpose of this module is to efficiently store a limited number of values in e.g. a lossy histogram (ex. exometer_slot_slide). The complexity of insert operations is log(N), but once the tree is full, only values higher than the minimum value already in the tree will be inserted, and the old minimum is deleted - i.e. two O(log N) operations. For other values, the cost will be only two comparisons, since the top node in the tree always contains the minimum.

Summary

Functions

Insert value V into tree T.

Returns the maximum number of values for the given tree.
Create an empty tree limited to Size.
Returns the number of values stored in the given tree.

Extract the smallest value from the tree T.

Converts a tree to a list.

Types

-type tree() :: #t{size :: term(), limit :: term(), tree :: term()}.

Functions

-spec insert(number(), any(), tree()) -> tree().

Insert value V into tree T.

If the tree is full and V is smaller than the minimum, this function will return immediately, leaving the tree unchanged.
-spec limit(tree()) -> non_neg_integer().
Returns the maximum number of values for the given tree.
-spec new(pos_integer()) -> tree().
Create an empty tree limited to Size.
-spec size(tree()) -> non_neg_integer().
Returns the number of values stored in the given tree.
-spec take_min(tree()) -> {number(), any(), tree()} | error.

Extract the smallest value from the tree T.

If the tree is empty, error is returned, otherwise {Minimum, NewTree}.
-spec to_list(tree()) -> [{number(), any()}].

Converts a tree to a list.

The list will not be ordered, since the aim is to produce the list as quickly as possible. Also, lists:sort(to_list(Tree)), if to_list/1 uses brute force, seems faster than most approaches for extracting values in order.