patrickhuber / Pliant

MIT License
26 stars 4 forks source link

add disambiguation algorithm for iterative tree traversal #97

Open patrickhuber opened 6 years ago

patrickhuber commented 6 years ago

Given that the parse forest is a binary tree, a dictionary can be passed into a disambiguation algorithm that tells each node which child to traverse. The disambiguation algorithm is responsible for tracking the current node index which increments every time the function is called.

namespace Pliant.Forest
{
    public class ChildSelectDisambiguationAlgorithm : IForestDisambiguationAlgorithm
    {
        IList<int> _childIndexes ;
        int _index ;

        public IList<int> ChildIndexes { get { return _childIndexes; } }

        public ChildSelectDisambiguationAlgorithm()
        {
              _childIndexes = new List<int>();
              _index = 0;
        }
        public ChildSelectDisambiguationAlgorithm(IEnumerable<int> childIndexes)
        {
              _childIndexes = new List<int>(childIndexes);
              _index = 0;
        }
        public IAndForestNode GetCurrentAndNode(IInternalForestNode internalNode)
        {
             _index ++;
             childIndex = _childIndexes [index] ;
             return internalNode.Children[childIndex];
        }
    }
}