/// <summary> /// This will take a visit from a byte. This will create new code entries as /// necessary. /// </summary> /// <param name="data">The byte to get a visit from.</param> public void Visit(byte data) { buffer.WriteByte(data); byte[] curBuffer = buffer.ToArray(); LzwNode previous = null; LzwNode current = this.Root; bool createNewCode = false; for (int i = 0; i < curBuffer.Length && current != null; i++) { previous = current; current = current.GetNode(curBuffer[i]); if (current == null) { createNewCode = true; current = new LzwNode(); previous.SetNode(curBuffer[i], current); } } if (createNewCode) { long code = nextCode++; current.Code = code; this.Add(code, curBuffer); buffer = new MemoryStream(); buffer.WriteByte(data); resetCodeSize(); } }
public LzwNode GetNode(byte[] data) { LzwNode current = this; for (int i = 0; i < data.Length && current != null; i++) { current = current.GetNode(data[i]); } return(current); }