Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › compress › zstd › Decompress › LiteralsSection › HuffmanTree › build Private Function build Doc Source Private Function std.compress.zstd.Decompress.LiteralsSection.HuffmanTree.build
fn build (weights : *[256 ]u4 , symbol_count : usize ) error Params weights : *[256 ]u4 symbol_count : usize File compress/zstd/Decompress.zig: 1244
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266 fn build (weights : *[256 ]u4 , symbol_count : usize ) error {MalformedHuffmanTree }!HuffmanTree {
var weight_power_sum_big : u32 = 0 ;
for (weights [0 .. symbol_count - 1 ]) |value | {
weight_power_sum_big += (@as (u16 , 1 ) << value ) >> 1 ;
}
if (weight_power_sum_big >= 1 << 11 ) return error .MalformedHuffmanTree ;
const weight_power_sum = @as (u16 , @intCast (weight_power_sum_big ));
// advance to next power of two (even if weight_power_sum is a power of 2)
// TODO: is it valid to have weight_power_sum == 0?
const max_number_of_bits = if (weight_power_sum == 0 ) 1 else std .math .log2_int (u16 , weight_power_sum ) + 1 ;
const next_power_of_two = @as (u16 , 1 ) << max_number_of_bits ;
weights [symbol_count - 1 ] = std .math .log2_int (u16 , next_power_of_two - weight_power_sum ) + 1 ;
var weight_sorted_prefixed_symbols : [256 ]PrefixedSymbol = undefined ;
const prefixed_symbol_count = assignSymbols (weight_sorted_prefixed_symbols [0 ..symbol_count ], weights .*);
const tree : HuffmanTree = .{
.max_bit_count = max_number_of_bits ,
.symbol_count_minus_one = @as (u8 , @intCast (prefixed_symbol_count - 1 )),
.nodes = weight_sorted_prefixed_symbols ,
};
return tree ;
}