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 › Table › build Public Function build Doc Source Public Function std.compress.zstd.Decompress.Table.build
pub fn build (values : []const u16 , entries : []Table .Fse ) !void Params values : []const u16 entries : []Table .Fse File compress/zstd/Decompress.zig: 1490
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542 pub fn build (values : []const u16 , entries : []Table .Fse ) !void {
const total_probability = @as (u16 , @intCast (entries .len ));
const accuracy_log = std .math .log2_int (u16 , total_probability );
assert (total_probability <= 1 << 9 );
var less_than_one_count : usize = 0 ;
for (values , 0 ..) |value , i | {
if (value == 0 ) {
entries [entries .len - 1 - less_than_one_count ] = Table .Fse {
.symbol = @as (u8 , @intCast (i )),
.baseline = 0 ,
.bits = accuracy_log ,
};
less_than_one_count += 1 ;
}
}
var position : usize = 0 ;
var temp_states : [1 << 9 ]u16 = undefined ;
for (values , 0 ..) |value , symbol | {
if (value == 0 or value == 1 ) continue ;
const probability = value - 1 ;
const state_share_dividend = std .math .ceilPowerOfTwo (u16 , probability ) catch
return error .MalformedFseTable ;
const share_size = @divExact (total_probability , state_share_dividend );
const double_state_count = state_share_dividend - probability ;
const single_state_count = probability - double_state_count ;
const share_size_log = std .math .log2_int (u16 , share_size );
for (0 ..probability ) |i | {
temp_states [i ] = @as (u16 , @intCast (position ));
position += (entries .len >> 1 ) + (entries .len >> 3 ) + 3 ;
position &= entries .len - 1 ;
while (position >= entries .len - less_than_one_count ) {
position += (entries .len >> 1 ) + (entries .len >> 3 ) + 3 ;
position &= entries .len - 1 ;
}
}
std .mem .sort (u16 , temp_states [0 ..probability ], {}, std .sort .asc (u16 ));
for (0 ..probability ) |i | {
entries [temp_states [i ]] = if (i < double_state_count ) Table .Fse {
.symbol = @as (u8 , @intCast (symbol )),
.bits = share_size_log + 1 ,
.baseline = single_state_count * share_size + @as (u16 , @intCast (i )) * 2 * share_size ,
} else Table .Fse {
.symbol = @as (u8 , @intCast (symbol )),
.bits = share_size_log ,
.baseline = (@as (u16 , @intCast (i )) - double_state_count ) * share_size ,
};
}
}
}