#region Using
using System;
#endregion
namespace SharpCompress.Compressor.PPMd.I1
{
///
/// PPM state.
///
///
///
/// This must be a structure rather than a class because several places in the associated code assume that
/// is a value type (meaning that assignment creates a completely new copy of the
/// instance rather than just copying a reference to the same instance).
///
///
/// Note that is a field rather than a property for performance reasons.
///
///
internal struct PpmState
{
public uint Address;
public byte[] Memory;
public static readonly PpmState Zero = new PpmState(0, null);
public const int Size = 6;
///
/// Initializes a new instance of the structure.
///
public PpmState(uint address, byte[] memory)
{
Address = address;
Memory = memory;
}
///
/// Gets or sets the symbol.
///
public byte Symbol
{
get { return Memory[Address]; }
set { Memory[Address] = value; }
}
///
/// Gets or sets the frequency.
///
public byte Frequency
{
get { return Memory[Address + 1]; }
set { Memory[Address + 1] = value; }
}
///
/// Gets or sets the successor.
///
public Model.PpmContext Successor
{
get { return new Model.PpmContext(((uint) Memory[Address + 2]) | ((uint) Memory[Address + 3]) << 8 | ((uint) Memory[Address + 4]) << 16 | ((uint) Memory[Address + 5]) << 24, Memory); }
set
{
Memory[Address + 2] = (byte) value.Address;
Memory[Address + 3] = (byte) (value.Address >> 8);
Memory[Address + 4] = (byte) (value.Address >> 16);
Memory[Address + 5] = (byte) (value.Address >> 24);
}
}
///
/// Gets the at the relative to this
/// .
///
///
///
public PpmState this[int offset]
{
get { return new PpmState((uint) (Address + offset * Size), Memory); }
}
///
/// Allow a pointer to be implicitly converted to a PPM state.
///
///
///
public static implicit operator PpmState(Pointer pointer)
{
return new PpmState(pointer.Address, pointer.Memory);
}
///
/// Allow pointer-like addition on a PPM state.
///
///
///
///
public static PpmState operator +(PpmState state, int offset)
{
state.Address = (uint) (state.Address + offset * Size);
return state;
}
///
/// Allow pointer-like incrementing on a PPM state.
///
///
///
public static PpmState operator ++(PpmState state)
{
state.Address += Size;
return state;
}
///
/// Allow pointer-like subtraction on a PPM state.
///
///
///
///
public static PpmState operator -(PpmState state, int offset)
{
state.Address = (uint) (state.Address - offset * Size);
return state;
}
///
/// Allow pointer-like decrementing on a PPM state.
///
///
///
public static PpmState operator --(PpmState state)
{
state.Address -= Size;
return state;
}
///
/// Compare two PPM states.
///
///
///
///
public static bool operator <=(PpmState state1, PpmState state2)
{
return state1.Address <= state2.Address;
}
///
/// Compare two PPM states.
///
///
///
///
public static bool operator >=(PpmState state1, PpmState state2)
{
return state1.Address >= state2.Address;
}
///
/// Compare two PPM states.
///
///
///
///
public static bool operator ==(PpmState state1, PpmState state2)
{
return state1.Address == state2.Address;
}
///
/// Compare two PPM states.
///
///
///
///
public static bool operator !=(PpmState state1, PpmState state2)
{
return state1.Address != state2.Address;
}
///
/// Indicates whether this instance and a specified object are equal.
///
/// true if obj and this instance are the same type and represent the same value; otherwise, false.
/// Another object to compare to.
public override bool Equals(object obj)
{
if (obj is PpmState)
{
PpmState state = (PpmState) obj;
return state.Address == Address;
}
return base.Equals(obj);
}
///
/// Returns the hash code for this instance.
///
/// A 32-bit signed integer that is the hash code for this instance.
public override int GetHashCode()
{
return Address.GetHashCode();
}
}
}