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
//! Maps that only store entries with nonzero values.
//!
//! These types are recommended whenever it is important that the common value is zero, even though
//! most numeric types' [Default] implementations also yield a zero value.

use num_traits::Zero;

use crate::{Commonality, TotalBTreeMap, TotalHashMap};

/// A hash map that only stores entries with non-zero values. All other keys are presumed to be
/// associated with the zero value.
pub type NonZeroHashMap<K, V> = TotalHashMap<K, V, ZeroCommonality>;

/// An ordered map that only stores entries with non-zero values. All other keys are presumed to be
/// associated with the zero value.
pub type NonZeroBTreeMap<K, V> = TotalBTreeMap<K, V, ZeroCommonality>;

/// A [commonality](Commonality) based on the [Zero] trait.
///
/// A [TotalHashMap] or [TotalBTreeMap] using this commonality only stores entries with nonzero
/// values.
pub struct ZeroCommonality(());

impl<T: Zero> Commonality<T> for ZeroCommonality {
    fn common() -> T {
        T::zero()
    }
    fn is_common(value: &T) -> bool {
        value.is_zero()
    }
}