#region Using using System; #endregion namespace SharpCompress.Compressor.PPMd.I1 { /// /// A structure containing a single address. The address represents a location in the /// array. That location in the array contains information itself describing a section /// of the array (ie. a block of memory). /// /// /// /// 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). /// /// /// MemoryNode /// 4 Stamp /// 4 Next /// 4 UnitCount /// /// /// Note that is a field rather than a property for performance reasons. /// /// internal struct MemoryNode { public uint Address; public byte[] Memory; public static readonly MemoryNode Zero = new MemoryNode(0, null); public const int Size = 12; /// /// Initializes a new instance of the structure. /// public MemoryNode(uint address, byte[] memory) { Address = address; Memory = memory; } /// /// Gets or sets the stamp. /// public uint Stamp { get { return ((uint) Memory[Address]) | ((uint) Memory[Address + 1]) << 8 | ((uint) Memory[Address + 2]) << 16 | ((uint) Memory[Address + 3]) << 24; } set { Memory[Address] = (byte) value; Memory[Address + 1] = (byte) (value >> 8); Memory[Address + 2] = (byte) (value >> 16); Memory[Address + 3] = (byte) (value >> 24); } } /// /// Gets or sets the next memory node. /// public MemoryNode Next { get { return new MemoryNode(((uint) Memory[Address + 4]) | ((uint) Memory[Address + 5]) << 8 | ((uint) Memory[Address + 6]) << 16 | ((uint) Memory[Address + 7]) << 24, Memory); } set { Memory[Address + 4] = (byte) value.Address; Memory[Address + 5] = (byte) (value.Address >> 8); Memory[Address + 6] = (byte) (value.Address >> 16); Memory[Address + 7] = (byte) (value.Address >> 24); } } /// /// Gets or sets the unit count. /// public uint UnitCount { get { return ((uint) Memory[Address + 8]) | ((uint) Memory[Address + 9]) << 8 | ((uint) Memory[Address + 10]) << 16 | ((uint) Memory[Address + 11]) << 24; } set { Memory[Address + 8] = (byte) value; Memory[Address + 9] = (byte) (value >> 8); Memory[Address + 10] = (byte) (value >> 16); Memory[Address + 11] = (byte) (value >> 24); } } /// /// Gets whether there is a next memory node available. /// public bool Available { get { return Next.Address != 0; } } /// /// Link in the provided memory node. /// /// public void Link(MemoryNode memoryNode) { memoryNode.Next = Next; Next = memoryNode; } /// /// Unlink this memory node. /// public void Unlink() { Next = Next.Next; } /// /// Insert the memory node into the linked list. /// /// /// public void Insert(MemoryNode memoryNode, uint unitCount) { Link(memoryNode); memoryNode.Stamp = uint.MaxValue; memoryNode.UnitCount = unitCount; Stamp++; } /// /// Remove this memory node from the linked list. /// /// public MemoryNode Remove() { MemoryNode next = Next; Unlink(); Stamp--; return next; } /// /// Allow a pointer to be implicitly converted to a memory node. /// /// /// public static implicit operator MemoryNode(Pointer pointer) { return new MemoryNode(pointer.Address, pointer.Memory); } /// /// Allow pointer-like addition on a memory node. /// /// /// /// public static MemoryNode operator +(MemoryNode memoryNode, int offset) { memoryNode.Address = (uint) (memoryNode.Address + offset * Size); return memoryNode; } /// /// Allow pointer-like addition on a memory node. /// /// /// /// public static MemoryNode operator +(MemoryNode memoryNode, uint offset) { memoryNode.Address += offset * Size; return memoryNode; } /// /// Allow pointer-like subtraction on a memory node. /// /// /// /// public static MemoryNode operator -(MemoryNode memoryNode, int offset) { memoryNode.Address = (uint) (memoryNode.Address - offset * Size); return memoryNode; } /// /// Allow pointer-like subtraction on a memory node. /// /// /// /// public static MemoryNode operator -(MemoryNode memoryNode, uint offset) { memoryNode.Address -= offset * Size; return memoryNode; } /// /// Compare two memory nodes. /// /// /// /// public static bool operator ==(MemoryNode memoryNode1, MemoryNode memoryNode2) { return memoryNode1.Address == memoryNode2.Address; } /// /// Compare two memory nodes. /// /// /// /// public static bool operator !=(MemoryNode memoryNode1, MemoryNode memoryNode2) { return memoryNode1.Address != memoryNode2.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 MemoryNode) { MemoryNode memoryNode = (MemoryNode) obj; return memoryNode.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(); } } }