private static LinkedNode InsertNode(LinkedNode Tail, int Decomp) { LinkedNode node = Tail; LinkedNode prev = Tail.Prev; LinkedNode node3 = new LinkedNode(node.DecompressedValue, node.Weight); node3.Parent = node; LinkedNode newNode = new LinkedNode(Decomp, 0); newNode.Parent = node; node.Child0 = newNode; Tail.Next = node3; node3.Prev = Tail; newNode.Prev = node3; node3.Next = newNode; AdjustTree(newNode); AdjustTree(newNode); return prev; }
private static LinkedNode Decode(BitStream Input, LinkedNode Head) { LinkedNode node = Head; while (node.Child0 != null) { int num = Input.ReadBits(1); if (num == -1) { throw new Exception("Unexpected end of file"); } node = (num == 0) ? node.Child0 : node.Child1; } return node; }
private static LinkedNode BuildList(byte[] PrimeData) { LinkedNode node = new LinkedNode(0x100, 1); node = node.Insert(new LinkedNode(0x101, 1)); for (int i = 0; i < PrimeData.Length; i++) { if (PrimeData[i] != 0) { node = node.Insert(new LinkedNode(i, PrimeData[i])); } } return node; }
private static LinkedNode BuildTree(LinkedNode Tail) { LinkedNode node = Tail; while (node != null) { LinkedNode node2 = node; LinkedNode prev = node.Prev; if (prev == null) { return node; } LinkedNode other = new LinkedNode(0, node2.Weight + prev.Weight); other.Child0 = node2; node2.Parent = other; prev.Parent = other; node.Insert(other); node = node.Prev.Prev; } return node; }
private static void AdjustTree(LinkedNode NewNode) { LinkedNode parent = NewNode; while (parent != null) { LinkedNode prev; parent.Weight++; LinkedNode node2 = parent; while (true) { prev = node2.Prev; if ((prev == null) || (prev.Weight >= parent.Weight)) { break; } node2 = prev; } if (node2 == parent) { parent = parent.Parent; } else { if (node2.Prev != null) { node2.Prev.Next = node2.Next; } node2.Next.Prev = node2.Prev; node2.Next = parent.Next; node2.Prev = parent; if (parent.Next != null) { parent.Next.Prev = node2; } parent.Next = node2; parent.Prev.Next = parent.Next; parent.Next.Prev = parent.Prev; LinkedNode next = prev.Next; parent.Next = next; parent.Prev = prev; next.Prev = parent; prev.Next = parent; LinkedNode node5 = parent.Parent; LinkedNode node6 = node2.Parent; if (node5.Child0 == parent) { node5.Child0 = node2; } if ((node5 != node6) && (node6.Child0 == node2)) { node6.Child0 = parent; } parent.Parent = node6; node2.Parent = node5; parent = parent.Parent; } } }