public long CalcOffset(RedBlackTree <TreeNode> .RedBlackTreeNode Node) { RedBlackTree <TreeNode> .RedBlackTreeNode cur = Node; long offset = cur.left != null ? cur.left.value.TotalLength : 0; while (cur.parent != null) { if (cur == cur.parent.right) { if (cur.parent.left != null && cur.parent.left.value != null) { offset += cur.parent.left.value.TotalLength; } if (Node.parent.value != null) { offset += cur.parent.value.Length; } } cur = cur.parent; } return(offset); }
public void Insert(long offset, int addBufferOffset, long addLength) { RedBlackTree <TreeNode> .RedBlackTreeNode node = GetTreeNodeAtOffset(offset); long oldNodeOffset = node.value.CalcOffset(node); long newLength = offset - oldNodeOffset; TreeNode splittedNode = node.value.SplitRight(newLength); ChangeLength(node, newLength); RedBlackTree <TreeNode> .RedBlackTreeNode newNode = InsertAfter(node, new DataTreeNode(addBufferOffset, addLength)); if (splittedNode.Length > 0) { InsertAfter(newNode, splittedNode); } if (newLength == 0) { RemoveNode(node); } }
void UpdateNode(RedBlackTree <TreeNode> .RedBlackTreeNode node) { if (node == null) { return; } long currentTotalLength = node.value.Length; if (node.left != null) { currentTotalLength += node.left.value.TotalLength; } if (node.right != null) { currentTotalLength += node.right.value.TotalLength; } if (currentTotalLength != node.value.TotalLength) { node.value.TotalLength = currentTotalLength; UpdateNode(node.parent); } }
void ChangeLength(RedBlackTree <TreeNode> .RedBlackTreeNode node, long newLength) { node.value.Length = newLength; UpdateNode(node); }