/* * Reference arithmetic coding * Copyright (c) Project Nayuki * * https://www.nayuki.io/page/reference-arithmetic-coding * https://github.com/nayuki/Reference-arithmetic-coding */ /// /// An immutable frequency table where every symbol has the same frequency of 1. /// Useful as a fallback model when no statistics are available. /// public sealed class FlatFrequencyTable : FrequencyTable { /*---- Fields ----*/ // Total number of symbols, which is at least 1. private readonly int numSymbols; /*---- Constructor ----*/ /// /// Constructs a flat frequency table with the specified number of symbols. /// the number of symbols, which must be at least 1 /// if the number of symbols is less than 1 public FlatFrequencyTable(int numSyms) { if (numSyms < 1) { throw new System.ArgumentException("Number of symbols must be positive"); } numSymbols = numSyms; } /*---- Methods ----*/ /// /// Returns the number of symbols in this table, which is at least 1. /// the number of symbols in this table public int SymbolLimit { get { return numSymbols; } } /// /// Returns the frequency of the specified symbol, which is always 1. /// the symbol to query /// the frequency of the symbol, which is 1 /// if {@code symbol} < 0 or {@code symbol} ≥ {@code getSymbolLimit()} public int get(int symbol) { checkSymbol(symbol); return 1; } /// /// Returns the total of all symbol frequencies, which is /// always equal to the number of symbols in this table. /// the total of all symbol frequencies, which is {@code getSymbolLimit()} public int Total { get { return numSymbols; } } /// /// Returns the sum of the frequencies of all the symbols strictly below /// the specified symbol value. The returned value is equal to {@code symbol}. /// the symbol to query /// the sum of the frequencies of all the symbols below {@code symbol}, which is {@code symbol} /// if {@code symbol} < 0 or {@code symbol} ≥ {@code getSymbolLimit()} public int getLow(int symbol) { checkSymbol(symbol); return symbol; } /// /// Returns the sum of the frequencies of the specified symbol and all /// the symbols below. The returned value is equal to {@code symbol + 1}. /// the symbol to query /// the sum of the frequencies of {@code symbol} and all symbols below, which is {@code symbol + 1} /// if {@code symbol} < 0 or {@code symbol} ≥ {@code getSymbolLimit()} public int getHigh(int symbol) { checkSymbol(symbol); return symbol + 1; } // Returns silently if 0 <= symbol < numSymbols, otherwise throws an exception. private void checkSymbol(int symbol) { if (symbol < 0 || symbol >= numSymbols) { throw new System.ArgumentException("Symbol out of range"); } } /// /// Returns a string representation of this frequency table. The format is subject to change. /// a string representation of this frequency table public override string ToString() { return "FlatFrequencyTable=" + numSymbols; } /// /// Unsupported operation, because this frequency table is immutable. /// ignored /// ignored /// because this frequency table is immutable public void set(int symbol, int freq) { throw new System.NotSupportedException(); } /// /// Unsupported operation, because this frequency table is immutable. /// ignored /// because this frequency table is immutable public void increment(int symbol) { throw new System.NotSupportedException(); } }