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

Function bits_needed

nodedb-fts/src/codec/bitpack.rs:13–18  ·  view source on GitHub ↗

Variable-width bitpacking for `u32` arrays. Packs an array of u32 values using the minimum number of bits per value. 2-byte block header stores `num_values` (u16) followed by 1-byte `bit_width`. Each value is stored in exactly `bit_width` bits, packed contiguously. Used for both delta-encoded doc IDs and term frequencies. Compute the minimum number of bits needed to represent `max_val`. Returns

(max_val: u32)

Source from the content-addressed store, hash-verified

11/// Compute the minimum number of bits needed to represent `max_val`.
12/// Returns 0 for max_val == 0, 1 for max_val == 1, etc.
13pub fn bits_needed(max_val: u32) -> u8 {
14 if max_val == 0 {
15 return 0;
16 }
17 32 - max_val.leading_zeros() as u8
18}
19
20/// Pack a slice of u32 values into a compact byte buffer.
21///

Callers 1

packFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected