State.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Text;
  3. using SharpCompress.Converters;
  4. namespace SharpCompress.Compressors.PPMd.H
  5. {
  6. internal class State : Pointer
  7. {
  8. internal const int SIZE = 6;
  9. internal State(byte[] memory)
  10. : base(memory)
  11. {
  12. }
  13. internal int Symbol { get => Memory[Address] & 0xff; set => Memory[Address] = (byte)value; }
  14. internal int Freq { get => Memory[Address + 1] & 0xff; set => Memory[Address + 1] = (byte)value; }
  15. internal State Initialize(byte[] mem)
  16. {
  17. return base.Initialize<State>(mem);
  18. }
  19. internal void IncrementFreq(int dFreq)
  20. {
  21. Memory[Address + 1] = (byte)(Memory[Address + 1] + dFreq);
  22. }
  23. internal int GetSuccessor()
  24. {
  25. return DataConverter.LittleEndian.GetInt32(Memory, Address + 2);
  26. }
  27. internal void SetSuccessor(PpmContext successor)
  28. {
  29. SetSuccessor(successor.Address);
  30. }
  31. internal void SetSuccessor(int successor)
  32. {
  33. DataConverter.LittleEndian.PutBytes(Memory, Address + 2, successor);
  34. }
  35. internal void SetValues(StateRef state)
  36. {
  37. Symbol = state.Symbol;
  38. Freq = state.Freq;
  39. SetSuccessor(state.GetSuccessor());
  40. }
  41. internal void SetValues(State ptr)
  42. {
  43. Array.Copy(ptr.Memory, ptr.Address, Memory, Address, SIZE);
  44. }
  45. internal State DecrementAddress()
  46. {
  47. Address = Address - SIZE;
  48. return this;
  49. }
  50. internal State IncrementAddress()
  51. {
  52. Address = Address + SIZE;
  53. return this;
  54. }
  55. internal static void PpmdSwap(State ptr1, State ptr2)
  56. {
  57. byte[] mem1 = ptr1.Memory, mem2 = ptr2.Memory;
  58. for (int i = 0, pos1 = ptr1.Address, pos2 = ptr2.Address; i < SIZE; i++, pos1++, pos2++)
  59. {
  60. byte temp = mem1[pos1];
  61. mem1[pos1] = mem2[pos2];
  62. mem2[pos2] = temp;
  63. }
  64. }
  65. public override String ToString()
  66. {
  67. StringBuilder buffer = new StringBuilder();
  68. buffer.Append("State[");
  69. buffer.Append("\n Address=");
  70. buffer.Append(Address);
  71. buffer.Append("\n size=");
  72. buffer.Append(SIZE);
  73. buffer.Append("\n symbol=");
  74. buffer.Append(Symbol);
  75. buffer.Append("\n freq=");
  76. buffer.Append(Freq);
  77. buffer.Append("\n successor=");
  78. buffer.Append(GetSuccessor());
  79. buffer.Append("\n]");
  80. return buffer.ToString();
  81. }
  82. }
  83. }