mirror of
https://github.com/chylex/Brotli-Builder.git
synced 2025-05-14 02:34:05 +02:00
Rewrite AlphabetSize.BitsPerSymbol to use Log2 utility class
This commit is contained in:
parent
79a96735a5
commit
87011bc853
BrotliLib
UnitTests/Numbers
@ -12,7 +12,7 @@ namespace BrotliLib.Brotli.Components.Header{
|
||||
|
||||
private static readonly BitDeserializer<HuffmanTree<T>, Context> SimpleCodeDeserialize = MarkedBitDeserializer.Wrap<HuffmanTree<T>, Context>(
|
||||
(reader, context) => {
|
||||
byte bitsPerSymbol = context.AlphabetSize.BitsPerSymbol;
|
||||
int bitsPerSymbol = context.AlphabetSize.BitsPerSymbol;
|
||||
int symbolCount = reader.NextChunk(2, "NSYM", value => 1 + value);
|
||||
|
||||
T[] symbols = reader.ReadValueArray(symbolCount, "symbol", () => context.BitsToSymbol(reader.NextChunk(bitsPerSymbol)));
|
||||
@ -24,7 +24,7 @@ namespace BrotliLib.Brotli.Components.Header{
|
||||
);
|
||||
|
||||
private static readonly BitSerializer<HuffmanTree<T>, Context> SimpleCodeSerialize = (writer, obj, context) => {
|
||||
byte bitsPerSymbol = context.AlphabetSize.BitsPerSymbol;
|
||||
int bitsPerSymbol = context.AlphabetSize.BitsPerSymbol;
|
||||
|
||||
writer.WriteChunk(2, obj.Root.SymbolCount - 1);
|
||||
|
||||
|
@ -8,17 +8,13 @@ namespace BrotliLib.Numbers{
|
||||
/// <summary>
|
||||
/// Returns the minimum amount of bits required to represent every symbol in the alphabet.
|
||||
/// </summary>
|
||||
public byte BitsPerSymbol{
|
||||
public int BitsPerSymbol{
|
||||
get{
|
||||
int size = SymbolCount - 1;
|
||||
byte bitsPerSymbol = 0;
|
||||
|
||||
while(size > 0){
|
||||
size >>= 1;
|
||||
++bitsPerSymbol;
|
||||
if (SymbolCount <= 1){
|
||||
return 0;
|
||||
}
|
||||
|
||||
return bitsPerSymbol;
|
||||
return Log2.Floor(SymbolCount - 1) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,23 +7,23 @@ open BrotliLib.Numbers
|
||||
|
||||
module Behavior =
|
||||
[<Theory>]
|
||||
[<InlineData(0, 0uy)>]
|
||||
[<InlineData(1, 0uy)>]
|
||||
[<InlineData(2, 1uy)>]
|
||||
[<InlineData(3, 2uy)>]
|
||||
[<InlineData(4, 2uy)>]
|
||||
[<InlineData(5, 3uy)>]
|
||||
[<InlineData(6, 3uy)>]
|
||||
[<InlineData(7, 3uy)>]
|
||||
[<InlineData(8, 3uy)>]
|
||||
[<InlineData(9, 4uy)>]
|
||||
[<InlineData(16, 4uy)>]
|
||||
[<InlineData(17, 5uy)>]
|
||||
[<InlineData(32, 5uy)>]
|
||||
[<InlineData(33, 6uy)>]
|
||||
[<InlineData(256, 8uy)>]
|
||||
[<InlineData(704, 10uy)>]
|
||||
let ``alphabet of specified size calculates amount of bits per symbol correctly`` (symbols: int, bits: byte) =
|
||||
[<InlineData(0, 0)>]
|
||||
[<InlineData(1, 0)>]
|
||||
[<InlineData(2, 1)>]
|
||||
[<InlineData(3, 2)>]
|
||||
[<InlineData(4, 2)>]
|
||||
[<InlineData(5, 3)>]
|
||||
[<InlineData(6, 3)>]
|
||||
[<InlineData(7, 3)>]
|
||||
[<InlineData(8, 3)>]
|
||||
[<InlineData(9, 4)>]
|
||||
[<InlineData(16, 4)>]
|
||||
[<InlineData(17, 5)>]
|
||||
[<InlineData(32, 5)>]
|
||||
[<InlineData(33, 6)>]
|
||||
[<InlineData(256, 8)>]
|
||||
[<InlineData(704, 10)>]
|
||||
let ``alphabet of specified size calculates amount of bits per symbol correctly`` (symbols: int, bits: int) =
|
||||
Assert.Equal(bits, AlphabetSize(symbols).BitsPerSymbol)
|
||||
|
||||
[<Fact>]
|
||||
|
Loading…
Reference in New Issue
Block a user