re_int_histogram/
lib.rs

1//! A histogram with `i64` keys and `u32` counts, supporting both sparse and dense uses.
2//!
3//! It supports high-level summaries of the histogram, so that you can quickly
4//! get a birds-eye view of the data without having to visit every point in the histogram.
5//!
6//! You can also think of the histogram as a multi-set,
7//! where you can insert the same key multiple times and then query
8//! how many times you've inserted it.
9
10mod tree;
11
12pub use tree::{Int64Histogram, Iter};
13
14// -----------------------------------------------------------------------------------
15
16/// We use `u64` keys in the internal structures,
17/// because it is so much easier to work with
18pub(crate) fn u64_key_from_i64_key(key: i64) -> u64 {
19    // key.wrapping_add_unsigned(i64::MIN.unsigned_abs()) // unstable
20    (key as i128 + i64::MAX as i128 + 1) as _
21}
22
23pub(crate) fn i64_key_from_u64_key(key: u64) -> i64 {
24    (key as i128 + i64::MIN as i128) as _
25}
26
27#[test]
28fn test_u64_i64_key_conversions() {
29    assert_eq!(u64_key_from_i64_key(i64::MIN), u64::MIN);
30    assert_eq!(u64_key_from_i64_key(i64::MIN + 1), u64::MIN + 1);
31    assert_eq!(u64_key_from_i64_key(i64::MIN + 2), u64::MIN + 2);
32    assert_eq!(u64_key_from_i64_key(i64::MAX - 2), u64::MAX - 2);
33    assert_eq!(u64_key_from_i64_key(i64::MAX - 1), u64::MAX - 1);
34    assert_eq!(u64_key_from_i64_key(i64::MAX), u64::MAX);
35
36    assert_eq!(i64_key_from_u64_key(u64::MIN), i64::MIN);
37    assert_eq!(i64_key_from_u64_key(u64::MIN + 1), i64::MIN + 1);
38    assert_eq!(i64_key_from_u64_key(u64::MIN + 2), i64::MIN + 2);
39    assert_eq!(i64_key_from_u64_key(u64::MAX - 2), i64::MAX - 2);
40    assert_eq!(i64_key_from_u64_key(u64::MAX - 1), i64::MAX - 1);
41    assert_eq!(i64_key_from_u64_key(u64::MAX), i64::MAX);
42}
43
44// -----------------------------------------------------------------------------------
45
46/// An inclusive range.
47#[derive(Clone, Copy, PartialEq, Eq)]
48pub(crate) struct RangeU64 {
49    /// inclusive
50    pub min: u64,
51
52    /// inclusive
53    pub max: u64,
54}
55
56impl RangeU64 {
57    pub fn new(min: u64, max: u64) -> Self {
58        Self { min, max }
59    }
60
61    pub fn single(value: u64) -> Self {
62        Self {
63            min: value,
64            max: value,
65        }
66    }
67
68    #[inline]
69    pub fn contains(&self, value: u64) -> bool {
70        self.min <= value && value <= self.max
71    }
72
73    #[inline]
74    pub fn contains_all_of(&self, other: Self) -> bool {
75        self.contains(other.min) && self.contains(other.max)
76    }
77
78    #[inline]
79    pub fn intersects(&self, other: Self) -> bool {
80        self.min <= other.max && other.min <= self.max
81    }
82}
83
84impl std::fmt::Debug for RangeU64 {
85    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86        write!(f, "RangeU64[{}, {}]", self.min, self.max)
87    }
88}
89
90// -----------------------------------------------------------------------------------
91
92/// An inclusive range.
93#[derive(Clone, Copy, PartialEq, Eq)]
94pub struct RangeI64 {
95    /// inclusive
96    pub min: i64,
97
98    /// inclusive
99    pub max: i64,
100}
101
102impl RangeI64 {
103    pub fn new(min: i64, max: i64) -> Self {
104        Self { min, max }
105    }
106
107    pub fn single(value: i64) -> Self {
108        Self {
109            min: value,
110            max: value,
111        }
112    }
113
114    #[inline]
115    pub fn contains(&self, value: i64) -> bool {
116        self.min <= value && value <= self.max
117    }
118
119    #[inline]
120    pub fn length(&self) -> u64 {
121        (self.max - self.min + 1) as u64
122    }
123}
124
125impl std::fmt::Debug for RangeI64 {
126    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127        write!(f, "RangeI64[{}, {}]", self.min, self.max)
128    }
129}