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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::Limiter;

/// A limiter for a map which is limited by the number of elements.
#[derive(Copy, Clone, Debug)]
#[repr(transparent)]
pub struct ByLength {
    max_length: u32,
}

impl ByLength {
    /// Creates a new length limiter with a given `max_length`.
    ///
    /// If you don't need to strictly cap the number of elements and just want to limit
    /// the memory usage then prefer using [`ByMemoryUsage`].
    pub const fn new(max_length: u32) -> Self {
        ByLength { max_length }
    }

    /// Returns the max length.
    pub const fn max_length(&self) -> u32 {
        self.max_length
    }
}

impl<K, V> Limiter<K, V> for ByLength {
    type KeyToInsert<'a> = K;
    type LinkType = u32;

    #[inline]
    fn is_over_the_limit(&self, length: usize) -> bool {
        length > self.max_length as usize
    }

    #[inline]
    fn on_insert(&mut self, _length: usize, key: Self::KeyToInsert<'_>, value: V) -> Option<(K, V)> {
        if self.max_length > 0 {
            Some((key, value))
        } else {
            None
        }
    }

    #[inline]
    fn on_replace(
        &mut self,
        _length: usize,
        _old_key: &mut K,
        _new_key: K,
        _old_value: &mut V,
        _new_value: &mut V,
    ) -> bool {
        true
    }

    #[inline]
    fn on_removed(&mut self, _key: &mut K, _value: &mut V) {}

    #[inline]
    fn on_cleared(&mut self) {}

    #[inline]
    fn on_grow(&mut self, _new_memory_usage: usize) -> bool {
        true
    }
}

/// A limiter for a map which is limited by memory usage.
#[derive(Copy, Clone, Debug)]
#[repr(transparent)]
pub struct ByMemoryUsage {
    max_bytes: usize,
}

impl ByMemoryUsage {
    /// Creates a new memory usage limiter with a given limit in bytes.
    pub const fn new(max_bytes: usize) -> Self {
        ByMemoryUsage { max_bytes }
    }

    /// Returns the max memory usage.
    pub const fn max_memory_usage(&self) -> usize {
        self.max_bytes
    }
}

impl<K, V> Limiter<K, V> for ByMemoryUsage {
    type KeyToInsert<'a> = K;
    type LinkType = u32;

    #[inline]
    fn is_over_the_limit(&self, _: usize) -> bool {
        false
    }

    #[inline]
    fn on_insert(&mut self, _length: usize, key: Self::KeyToInsert<'_>, value: V) -> Option<(K, V)> {
        Some((key, value))
    }

    #[inline]
    fn on_replace(
        &mut self,
        _length: usize,
        _old_key: &mut K,
        _new_key: K,
        _old_value: &mut V,
        _new_value: &mut V,
    ) -> bool {
        true
    }

    #[inline]
    fn on_removed(&mut self, _key: &mut K, _value: &mut V) {}

    #[inline]
    fn on_cleared(&mut self) {}

    #[inline]
    fn on_grow(&mut self, new_memory_usage: usize) -> bool {
        new_memory_usage <= self.max_bytes
    }
}

/// A limiter for a map which can hold an unlimited number of elements.
///
/// With this limiter the map will act like a normal ordered hashmap.
///
/// On 64-bit systems it is less efficient and uses more memory than [`UnlimitedCompact`],
/// but can hold a truly unlimited number of elements.
///
/// On 32-bit systems it is exactly the same as [`UnlimitedCompact`].
#[derive(Copy, Clone, Debug, Default)]
pub struct Unlimited;

impl<K, V> Limiter<K, V> for Unlimited {
    type KeyToInsert<'a> = K;
    type LinkType = usize;

    #[inline]
    fn is_over_the_limit(&self, _: usize) -> bool {
        false
    }

    #[inline]
    fn on_insert(&mut self, _length: usize, key: Self::KeyToInsert<'_>, value: V) -> Option<(K, V)> {
        Some((key, value))
    }

    #[inline]
    fn on_replace(
        &mut self,
        _length: usize,
        _old_key: &mut K,
        _new_key: K,
        _old_value: &mut V,
        _new_value: &mut V,
    ) -> bool {
        true
    }

    #[inline]
    fn on_removed(&mut self, _key: &mut K, _value: &mut V) {}

    #[inline]
    fn on_cleared(&mut self) {}

    #[inline]
    fn on_grow(&mut self, _new_memory_usage: usize) -> bool {
        true
    }
}

/// A limiter for a map which can hold an unlimited** number of elements.
///
/// ** - limited to around somewhere between a billion elements and two billion elements.
/// If you need to store more use [`Unlimited`].
#[derive(Copy, Clone, Debug, Default)]
pub struct UnlimitedCompact;

impl<K, V> Limiter<K, V> for UnlimitedCompact {
    type KeyToInsert<'a> = K;
    type LinkType = u32;

    #[inline]
    fn is_over_the_limit(&self, _: usize) -> bool {
        false
    }

    #[inline]
    fn on_insert(&mut self, _length: usize, key: Self::KeyToInsert<'_>, value: V) -> Option<(K, V)> {
        Some((key, value))
    }

    #[inline]
    fn on_replace(
        &mut self,
        _length: usize,
        _old_key: &mut K,
        _new_key: K,
        _old_value: &mut V,
        _new_value: &mut V,
    ) -> bool {
        true
    }

    #[inline]
    fn on_removed(&mut self, _key: &mut K, _value: &mut V) {}

    #[inline]
    fn on_cleared(&mut self) {}

    #[inline]
    fn on_grow(&mut self, _new_memory_usage: usize) -> bool {
        true
    }
}