using System;
using System.Text;

namespace CPF
{
    class History
    {
        public void Clear()
        {
            for (int i = 0; i < MaxHistory; i++) {
                HistoryTable[i].Parent   = null;
                HistoryTable[i].Child    = null;
            }
        }

        public void Record(State historyParent, State historyState)
        {
            for (int i = 0; i < MaxHistory; i++)
            {
                if ((HistoryTable[i].Parent == null) ||
                    HistoryTable[i].Parent == historyParent)
                {
                    HistoryTable[i].Parent = historyParent;
                    HistoryTable[i].Child = historyState;
                    return;
                }
            }
        }

        public State Retrieve(State historyParent)
        {
            for (int i = 0; i < MaxHistory; i++)
            {
                if (HistoryTable[i].Parent == historyParent)
                {
                    State res = HistoryTable[i].Child;
                    // remove history state from deep history table
                    HistoryTable[i].Parent  = null;
                    HistoryTable[i].Child   = null;
                    // return deep history target
                    return res;
                }
            }
            return null;
        }

        private struct Entry
        {
            public State Parent;
            public State Child;
        }

        private const int  MaxHistory   = 10;
        private Entry[] HistoryTable    = new Entry[MaxHistory];
    }
}
