See2Context.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #region Using
  2. #endregion
  3. namespace SharpCompress.Compressors.PPMd.I1
  4. {
  5. /// <summary>
  6. /// SEE2 (secondary escape estimation) contexts for PPM contexts with masked symbols.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// This must be a class rather than a structure because MakeEscapeFrequency returns a See2Context
  11. /// instance from the see2Contexts array. The caller (for example, EncodeSymbol2) then updates the
  12. /// returned See2Context instance and expects the updates to be reflected in the see2Contexts array.
  13. /// This would not happen if this were a structure.
  14. /// </para>
  15. /// <remarks>
  16. /// Note that in most cases fields are used rather than properties for performance reasons (for example,
  17. /// <see cref="_shift"/> is a field rather than a property).
  18. /// </remarks>
  19. /// </remarks>
  20. internal class See2Context
  21. {
  22. private const byte PERIOD_BIT_COUNT = 7;
  23. public ushort _summary;
  24. public byte _shift;
  25. public byte _count;
  26. public void Initialize(uint initialValue)
  27. {
  28. _shift = PERIOD_BIT_COUNT - 4;
  29. _summary = (ushort)(initialValue << _shift);
  30. _count = 7;
  31. }
  32. public uint Mean()
  33. {
  34. uint value = (uint)(_summary >> _shift);
  35. _summary = (ushort)(_summary - value);
  36. return (uint)(value + ((value == 0) ? 1 : 0));
  37. }
  38. public void Update()
  39. {
  40. if (_shift < PERIOD_BIT_COUNT && --_count == 0)
  41. {
  42. _summary += _summary;
  43. _count = (byte)(3 << _shift++);
  44. }
  45. }
  46. }
  47. }