public void update_context()
{
Debug.Assert(this.History.Count >= this.Depth, "history is shorter than depth in update_context");
this.Context = new List<int>();
this.Context.Add(this.RootI);
int nodeI = this.RootI;
int updateDepth = 1;
for (int i = History.Count-1; i>=0; i--)
{
int symbol = History[i];
var node = this.Nodes[nodeI];
if (symbol == 1 && node.Child1 != -1) {
nodeI = node.Child1;
}
else if (symbol == 0 && node.Child0 != -1) {
nodeI = node.Child0;
}
else
{
int newNodeI = this.CreateNewNode();
if (symbol == 1) {
this.Nodes[nodeI].Child1 = newNodeI;
}
else if (symbol == 0)
{
this.Nodes[nodeI].Child0 = newNodeI;
}
else {
Debug.Assert(false, "invalid symbol");
}
nodeI = newNodeI;
this.TreeSize += 1;
}
node = this.Nodes[nodeI];
this.Context.Add(nodeI);
updateDepth += 1;
if (updateDepth > this.Depth)
{
break;
}
}
}