|
using System;
using System.Collections.Generic;
using System.Text;
namespace QiHe.CodeLib.Compress
{
public struct SymbolCode
{
public int Code;
public int Base;
public int Offset;
public int ExtraBits;
}
public class SymbolCoding
{
int StartCode;
int[] BaseValues;
int[] ExtraBits;
Dictionary<int, SymbolCode> SymbolCodes;
public SymbolCoding(int startCode, int[] baseValues, int[] extraBits)
{
StartCode = startCode;
BaseValues = baseValues;
ExtraBits = extraBits;
SymbolCodes = new Dictionary<int, SymbolCode>();
}
public SymbolCode this[int symbol]
{
get
{
if (!SymbolCodes.ContainsKey(symbol))
{
SymbolCodes[symbol] = CreateSymbolCode(symbol);
//TraceTool.Debug.Send(SymbolCodes.Count.ToString());
}
return SymbolCodes[symbol];
}
}
SymbolCode CreateSymbolCode(int symbol)
{
SymbolCode symbolCode = new SymbolCode();
int code = GetCode(symbol);
symbolCode.Base = BaseValues[code];
symbolCode.ExtraBits = ExtraBits[code];
symbolCode.Offset = symbol - symbolCode.Base;
symbolCode.Code = StartCode + code;
return symbolCode;
}
private int GetCode(int symbol)
{
int code = -1;
for (int index = 0; index < BaseValues.Length; index++)
{
if (symbol >= BaseValues[index])
{
code = index;
}
else
{
break;
}
}
if (code == -1)
{
throw new Exception("symbol too small.");
}
else
{
return code;
}
}
public int GetSymbolBase(int code)
{
return BaseValues[code - StartCode];
}
public int GetSymbolExtrabits(int code)
{
return ExtraBits[code - StartCode];
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.