Beispiel #1
0
        public static DfaState ToDfa3(this State start, out int dfaCount, bool showProgress)
        {
            DfaState dfaStart;

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            if (showProgress)
            {
                Console.WriteLine("NFA to DFA v.3 Converting...");
                Console.WriteLine("{0}", DateTime.Now);
            }

            var dfaStates       = new Dictionary <int[], DfaState>(3000000, new IntArrayComparer());
            var dfaStatesSync   = new object();
            var notMarkedStates = new Queue <DfaState>(500000);
            var notMarkedSync   = new object();

            dfaStart = new DfaState(DfaState.GetNfaIds(start.Eclosure()));
            dfaStates.Add(dfaStart.NfaIds, dfaStart);
            notMarkedStates.Enqueue(dfaStart);

            Thread[] threads = new Thread[Environment.ProcessorCount * 2];

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(ToDfaThread);
                threads[i].Start(new ThreadParams(i == 0, showProgress, dfaStates,
                                                  dfaStatesSync, notMarkedStates, notMarkedSync));

                if (i == 0)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        Thread.Sleep(1000);
                        if (threads[0].IsAlive == false)
                        {
                            break;
                        }
                    }
                    if (threads[0].IsAlive == false)
                    {
                        break;
                    }
                }
            }

            for (int i = 0; i < threads.Length; i++)
            {
                if (threads[i] != null)
                {
                    threads[i].Join();
                }
            }

            dfaCount = dfaStates.Count;
            if (showProgress)
            {
                stopwatch.Stop();
                Console.WriteLine("Done (States: {0}, Elapsed: {1})\t\t\t\t", dfaCount, stopwatch.Elapsed);
            }

            return(dfaStart);
        }
Beispiel #2
0
        public static DfaState ToDfa2(this State start, out int dfaCount, bool showProgress)
        {
            DfaState dfaStart;

            if (showProgress)
            {
                Console.WriteLine("NFA to DFA v.2 Converting...");
                Console.WriteLine("{0}", DateTime.Now);
            }

            var dfaStates       = new Dictionary <int[], DfaState>(3000000, new IntArrayComparer());
            var notMarkedStates = new Queue <DfaState>(500000);

            dfaStart = new DfaState(DfaState.GetNfaIds(start.Eclosure()));
            dfaStates.Add(dfaStart.NfaIds, dfaStart);
            notMarkedStates.Enqueue(dfaStart);

            int i        = 0;
            int time     = Environment.TickCount;
            var allChars = new bool[byte.MaxValue + 1];

            GC.Collect();
            long memStart = GC.GetTotalMemory(true);

            while (notMarkedStates.Count > 0)
            {
                DfaState t = notMarkedStates.Dequeue();

                if (showProgress && i++ % 100 == 0)
                {
                    int memPerState = (int)((GC.GetTotalMemory(true) - memStart) / (long)dfaStates.Count);
                    int time1       = Environment.TickCount;
                    Console.Write("{2}\t({3} ms)\t\t{0} ({4} b)\t\t{1}\t\t", dfaStates.Count, notMarkedStates.Count, dfaStates.Count - notMarkedStates.Count, time1 - time, memPerState);
                    Console.Write("\r");
                    time = time1;
                }

                if (t != null)
                {
                    for (int j = 0; j <= byte.MaxValue; j++)
                    {
                        allChars[j] = false;
                    }
                    foreach (var nfaState in t.NfaStates)
                    {
                        nfaState.Transition.ForEachNotNullKeys((nb) => { allChars[(int)nb] = true; });
                    }

                    for (int j = 0; j <= byte.MaxValue; j++)
                    {
                        if (allChars[j])
                        {
                            byte char1 = (byte)j;

                            var states = t.NfaStates.GetMove2(char1).GetEclosure();

                            var uUnique = DfaState.GetNfaIds(states);

                            DfaState u;
                            if (dfaStates.TryGetValue(uUnique, out u) == false)
                            {
                                u = new DfaState(uUnique);
                                dfaStates.Add(uUnique, u);
                                notMarkedStates.Enqueue(u);
                            }

                            t.AddTransition(char1, u);
                        }
                    }

                    if (i % 1000 == 0)
                    {
                        GC.Collect();
                        GC.WaitForFullGCComplete();
                        GC.WaitForPendingFinalizers();
                    }
                }
            }

            dfaCount = dfaStates.Count;

            if (showProgress)
            {
                Console.WriteLine("Done ({0})\t\t\t\t\t\t\t\t", dfaCount);
            }

            return(dfaStart);
        }