Exemplo n.º 1
0
        public override IAutomat PurgeStates()
        {
            (uint[] translate, string[] names, uint[] aStates) = base.RemovedStateTranslateTables();

            var newT = new TuringTransformSingleBand();

            foreach (var t2 in Transforms)
            {
                if (translate.Contains(t2.Key.q))
                {
                    if (translate.Contains(t2.Value.qNext))
                    {
                        var tk = new TuringKey(translate.ArrayIndex(t2.Key.q), t2.Key.c);
                        var tv = new TuringVal(translate.ArrayIndex(t2.Value.qNext), t2.Value.c2, t2.Value.Direction);
                        newT.Add(tk, tv);
                    }
                }
            }

            return(new TuringMachineSingleBand($"{Name}_purged", (uint)names.Length, Alphabet, BandAlphabet, newT, translate.ArrayIndex(StartState), BlankSymbol, aStates)
            {
                DefaultAcceptance = this.DefaultAcceptance
            });
        }
Exemplo n.º 2
0
        public static TuringMachineSingleBand FromBinString(string binString)
        {
            var transform = new TuringTransformSingleBand();

            const char S = '1'; // Separator
            const char C = '0'; // Counter

            const char SC = '0';

            int curPos = 0;

            char cmax = SC;
            uint qmax = 0;


            while (curPos < binString.Length)
            {
                uint q = 0;
                char c = SC;

                while (binString[curPos] == C)
                {
                    q++;
                    curPos++;
                }
                if (binString[curPos] != S)
                {
                    throw new Serpen.Uni.Automat.Exception("Format", null);
                }
                else
                {
                    curPos++;
                }

                while (binString[curPos] == C)
                {
                    c++;
                    curPos++;
                }
                if (binString[curPos] != S)
                {
                    throw new Serpen.Uni.Automat.Exception("Format", null);
                }
                else
                {
                    curPos++;
                }

                if (cmax < c)
                {
                    cmax = c;
                }
                if (qmax < q)
                {
                    qmax = q;
                }
                var tmkey = new TuringKey(q - 1, --c);

                uint qnext = 0;
                char c2    = SC;
                uint dir   = 0;

                while (binString[curPos] == C)
                {
                    qnext++;
                    curPos++;
                }
                if (binString[curPos] != S)
                {
                    throw new Serpen.Uni.Automat.Exception("Format", null);
                }
                else
                {
                    curPos++;
                }

                while (binString[curPos] == C)
                {
                    c2++;
                    curPos++;
                }
                if (binString[curPos] != S)
                {
                    throw new Serpen.Uni.Automat.Exception("Format", null);
                }
                else
                {
                    curPos++;
                }

                while (curPos < binString.Length && binString[curPos] == C)
                {
                    dir++;
                    curPos++;
                }

                if (curPos < binString.Length && binString.Substring(curPos, 2) != new string(S, 2))
                {
                    throw new Serpen.Uni.Automat.Exception("Format", null);
                }
                else
                {
                    curPos += 2;
                }

                if (cmax < c2)
                {
                    cmax = c2;
                }
                if (qmax < qnext)
                {
                    qmax = qnext;
                }
                var tmval = new TuringVal(qnext - 1, --c2, (TMDirection)(++dir));

                transform.Add(tmkey, tmval);
            }

            var alp = new char[cmax - SC];

            for (int i = 0; i < alp.Length; i++)
            {
                alp[i] = (char)(SC + i);
            }

            return(new TuringMachineSingleBand("uTM", qmax, alp.SkipLast(1).ToArray(), alp, transform, 0, alp[^ 1], 1));