private TextAnchorNode FindNode(ref int offset)
{
var n = root;
while (true)
{
if (n.left != null)
{
if (offset < n.left.totalLength)
{
n = n.left; // descend into left subtree
continue;
}
offset -= n.left.totalLength; // skip left subtree
}
if (!n.IsAlive)
MarkNodeForDelete(n);
if (offset < n.length)
{
return n; // found correct node
}
offset -= n.length; // skip this node
if (n.right != null)
{
n = n.right; // descend into right subtree
}
else
{
// didn't find any node containing the offset
return null;
}
}
}