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
use crate::hash::Hash;
use std::{iter::FromIterator, ops::Deref};
pub const MAX_ENTRIES: usize = 512;
pub use crate::clock::Slot;
pub type SlotHash = (Slot, Hash);
#[repr(C)]
#[derive(Serialize, Deserialize, PartialEq, Debug, Default)]
pub struct SlotHashes(Vec<SlotHash>);
impl SlotHashes {
pub fn add(&mut self, slot: Slot, hash: Hash) {
match self.binary_search_by(|(probe, _)| slot.cmp(&probe)) {
Ok(index) => (self.0)[index] = (slot, hash),
Err(index) => (self.0).insert(index, (slot, hash)),
}
(self.0).truncate(MAX_ENTRIES);
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn get(&self, slot: &Slot) -> Option<&Hash> {
self.binary_search_by(|(probe, _)| slot.cmp(&probe))
.ok()
.map(|index| &self[index].1)
}
pub fn new(slot_hashes: &[SlotHash]) -> Self {
let mut slot_hashes = slot_hashes.to_vec();
slot_hashes.sort_by(|(a, _), (b, _)| b.cmp(a));
Self(slot_hashes)
}
}
impl FromIterator<(Slot, Hash)> for SlotHashes {
fn from_iter<I: IntoIterator<Item = (Slot, Hash)>>(iter: I) -> Self {
Self(iter.into_iter().collect())
}
}
impl Deref for SlotHashes {
type Target = Vec<SlotHash>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::hash::hash;
#[test]
fn test() {
let mut slot_hashes = SlotHashes::new(&[(1, Hash::default()), (3, Hash::default())]);
slot_hashes.add(2, Hash::default());
assert_eq!(
slot_hashes,
SlotHashes(vec![
(3, Hash::default()),
(2, Hash::default()),
(1, Hash::default()),
])
);
let mut slot_hashes = SlotHashes::new(&[]);
for i in 0..MAX_ENTRIES + 1 {
slot_hashes.add(
i as u64,
hash(&[(i >> 24) as u8, (i >> 16) as u8, (i >> 8) as u8, i as u8]),
);
}
for i in 0..MAX_ENTRIES {
assert_eq!(slot_hashes[i].0, (MAX_ENTRIES - i) as u64);
}
assert_eq!(slot_hashes.len(), MAX_ENTRIES);
}
}