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
use std::io;
use std::fmt;
use database::{FromDatabaseValue, IntoDatabaseValue};
use beserial::{Deserialize, Serialize, DeserializeWithLength, SerializeWithLength, SerializingError, ReadBytesExt, WriteBytesExt};
#[derive(Clone)]
pub struct SuperBlockCounts {
counts: Vec<u32>
}
impl SuperBlockCounts {
pub const NUM_COUNTS: usize = 256;
pub fn zero() -> SuperBlockCounts {
SuperBlockCounts { counts: Vec::new() }
}
fn expand(&mut self, depth: u8) {
while self.counts.len() <= depth as usize {
self.counts.push(0);
}
}
pub fn add(&mut self, depth: u8) {
self.expand(depth);
for i in 0..=(depth as usize) {
self.counts[i] += 1;
}
}
pub fn subtract(&mut self, depth: u8) {
assert!((depth as usize) < self.counts.len());
for i in 0..=(depth as usize) {
self.counts[i] = self.counts[i].checked_sub(1).unwrap_or_else(|| panic!("Superblock count for level {} is already 0 and can't be decreased", i));
}
}
pub fn copy_and_add(&self, depth: u8) -> SuperBlockCounts {
let mut c = self.clone();
c.add(depth);
c
}
pub fn copy_and_subtract(&self, depth: u8) -> SuperBlockCounts {
let mut c = self.clone();
c.subtract(depth);
c
}
pub fn get(&self, depth: u8) -> u32 {
if (depth as usize) < self.counts.len() { self.counts[depth as usize] }
else { 0 }
}
pub fn get_candidate_depth(&self, m: u32) -> u8 {
assert!(Self::NUM_COUNTS - 1 <= (std::u8::MAX as usize));
if m == 0 {
return (Self::NUM_COUNTS - 1) as u8;
}
for (i, count) in self.counts.iter().enumerate().rev() {
if *count >= m {
return i as u8;
}
}
0u8
}
}
impl Default for SuperBlockCounts {
fn default() -> SuperBlockCounts {
SuperBlockCounts::zero()
}
}
impl PartialEq for SuperBlockCounts {
fn eq(&self, other: &SuperBlockCounts) -> bool {
self.counts.iter()
.zip(&other.counts)
.all(|(&left, right)| left == *right)
&&
if self.counts.len() < other.counts.len() { &other.counts[self.counts.len()..] }
else { &self.counts[other.counts.len()..] }.iter()
.all(|count| *count == 0)
}
}
impl Eq for SuperBlockCounts {}
impl fmt::Debug for SuperBlockCounts {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[")?;
for (i, count) in self.counts.iter().enumerate() {
write!(f, "{}", count)?;
if i < Self::NUM_COUNTS - 1 {
write!(f, ",")?;
}
}
if self.counts.len() < Self::NUM_COUNTS - 1 {
write!(f, "...")?;
}
write!(f, "]")
}
}
impl From<Vec<u32>> for SuperBlockCounts {
fn from(counts: Vec<u32>) -> SuperBlockCounts {
assert!(counts.len() < Self::NUM_COUNTS, "Vector must not be larger than {} items.", Self::NUM_COUNTS);
SuperBlockCounts { counts }
}
}
impl Serialize for SuperBlockCounts {
fn serialize<W: WriteBytesExt>(&self, writer: &mut W) -> Result<usize, SerializingError> {
SerializeWithLength::serialize::<u8, W>(&self.counts, writer)
}
fn serialized_size(&self) -> usize {
SerializeWithLength::serialized_size::<u8>(&self.counts)
}
}
impl Deserialize for SuperBlockCounts {
fn deserialize<R: ReadBytesExt>(reader: &mut R) -> Result<Self, SerializingError> {
Ok(SuperBlockCounts { counts: DeserializeWithLength::deserialize::<u8, R>(reader)? })
}
}
impl IntoDatabaseValue for SuperBlockCounts {
fn database_byte_size(&self) -> usize {
self.serialized_size()
}
fn copy_into_database(&self, mut bytes: &mut [u8]) {
Serialize::serialize(&self, &mut bytes).unwrap();
}
}
impl FromDatabaseValue for SuperBlockCounts {
fn copy_from_database(bytes: &[u8]) -> io::Result<Self> where Self: Sized {
let mut cursor = io::Cursor::new(bytes);
Ok(Deserialize::deserialize(&mut cursor)?)
}
}