MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / encode

Function encode

nodedb-codec/src/double_delta.rs:117–152  ·  view source on GitHub ↗

Encode a slice of i64 values using DoubleDelta compression.

(values: &[i64])

Source from the content-addressed store, hash-verified

115
116/// Encode a slice of i64 values using DoubleDelta compression.
117pub fn encode(values: &[i64]) -> Vec<u8> {
118 let count = values.len() as u32;
119 let mut out = Vec::with_capacity(20 + values.len() / 4);
120
121 out.extend_from_slice(&count.to_le_bytes());
122
123 if values.is_empty() {
124 return out;
125 }
126
127 out.extend_from_slice(&values[0].to_le_bytes());
128
129 if values.len() == 1 {
130 return out;
131 }
132
133 let first_delta = values[1].wrapping_sub(values[0]);
134 out.extend_from_slice(&first_delta.to_le_bytes());
135
136 if values.len() == 2 {
137 return out;
138 }
139
140 let mut bs = BitWriter::new();
141 let mut prev_delta = first_delta;
142
143 for i in 2..values.len() {
144 let delta = values[i].wrapping_sub(values[i - 1]);
145 let dod = delta.wrapping_sub(prev_delta);
146 encode_dod(&mut bs, dod);
147 prev_delta = delta;
148 }
149
150 out.extend_from_slice(bs.as_bytes());
151 out
152}
153
154/// Decode DoubleDelta-compressed bytes back to i64 values.
155pub fn decode(data: &[u8]) -> Result<Vec<i64>, CodecError> {

Callers 13

finishMethod · 0.70
empty_roundtripFunction · 0.70
single_valueFunction · 0.70
two_valuesFunction · 0.70
constant_rate_timestampsFunction · 0.70
monotonic_with_jitterFunction · 0.70
non_monotonic_valuesFunction · 0.70
negative_valuesFunction · 0.70
large_deltasFunction · 0.70
boundary_valuesFunction · 0.70

Calls 4

encode_dodFunction · 0.85
lenMethod · 0.45
is_emptyMethod · 0.45
as_bytesMethod · 0.45

Tested by 12

empty_roundtripFunction · 0.56
single_valueFunction · 0.56
two_valuesFunction · 0.56
constant_rate_timestampsFunction · 0.56
monotonic_with_jitterFunction · 0.56
non_monotonic_valuesFunction · 0.56
negative_valuesFunction · 0.56
large_deltasFunction · 0.56
boundary_valuesFunction · 0.56
streaming_decoderFunction · 0.56