Delta encoding/decoding for sorted `u32` sequences. Posting lists are sorted by doc ID. Delta encoding stores the first ID absolute and subsequent IDs as `current - previous`. Typical deltas are much smaller than absolute IDs, enabling tighter bitpacking. Delta-encode a sorted slice of u32 values in place. After encoding: `out[0] = values[0]`, `out[i] = values[i] - values[i-1]`. The input MUST b
(values: &[u32])
| 11 | /// After encoding: `out[0] = values[0]`, `out[i] = values[i] - values[i-1]`. |
| 12 | /// The input MUST be sorted ascending. Unsorted input produces garbage. |
| 13 | pub fn encode(values: &[u32]) -> Vec<u32> { |
| 14 | if values.is_empty() { |
| 15 | return Vec::new(); |
| 16 | } |
| 17 | let mut deltas = Vec::with_capacity(values.len()); |
| 18 | deltas.push(values[0]); |
| 19 | for i in 1..values.len() { |
| 20 | deltas.push(values[i] - values[i - 1]); |
| 21 | } |
| 22 | deltas |
| 23 | } |
| 24 | |
| 25 | /// Decode delta-encoded values back to absolute sorted values. |
| 26 | /// |