Median

Trait Median 

Source
pub trait Median<KeyPrefix: FullCodec, MedianValue: Average + LexicographicEncoding>: MedianStore<KeyPrefix, MedianValue> {
    // Required methods
    fn length(key_prefix: impl Copy + EncodeLike<KeyPrefix>) -> u64;
    fn median(
        key_prefix: impl Copy + EncodeLike<KeyPrefix>,
    ) -> Option<MedianValue>;
    fn push(key_prefix: impl Copy + EncodeLike<KeyPrefix>, value: MedianValue);
    fn pop(
        key_prefix: impl Copy + EncodeLike<KeyPrefix>,
        value: MedianValue,
    ) -> bool;
}
Expand description

A median.

The implementation only uses a constant amount of database operations to implement insertion and removal. When instantiated over a database with logarithmic complexities (such as a radix trie), this effects a median with logarithmic memory/computation complexities (not requiring loading all values into memory).

This SHOULD NOT be used for small collections where the linear (or even quadratic) complexities still out-perform how expensive database operations are. In those cases, the collection should be written to a single storage slot, read entirely, sorted, and the median should be immediately taken via indexing the value halfway through the collection.

Required Methods§

Source

fn length(key_prefix: impl Copy + EncodeLike<KeyPrefix>) -> u64

The current length of the median’s list.

Source

fn median(key_prefix: impl Copy + EncodeLike<KeyPrefix>) -> Option<MedianValue>

The current median value.

This returns None if no values are present.

Source

fn push(key_prefix: impl Copy + EncodeLike<KeyPrefix>, value: MedianValue)

Push a new value onto the median.

If the value is already present within the existing values, the amount of times it will be considered present will be incremented.

Source

fn pop( key_prefix: impl Copy + EncodeLike<KeyPrefix>, value: MedianValue, ) -> bool

Pop a value from the median.

This returns true if the value was present and false otherwise.

If the value is present within the existing values multiple times, only a single instance will be removed.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<KeyPrefix: FullCodec, MedianValue: Average + LexicographicEncoding, S: MedianStore<KeyPrefix, MedianValue>> Median<KeyPrefix, MedianValue> for S