Ejemplo n.º 1
0
    /// <summary>
    /// https://msdn.microsoft.com/en-us/library/dd460721(v=vs.110).aspx
    /// Loops through the list really fast done in 5 seconds!!! Good one...
    /// </summary>
    /// <returns></returns>
    static double ParallelLoop()
    {
        var        my = Enumerable.Range(1, 200000000).ToList();
        List <int> p  = new List <int>()
        {
            9, 999, 99999, 999999, 99999999, 199999900
        };

        ConcurrentStack <double> results = new ConcurrentStack <double>();

        Parallel.For(0, my.Count(), (i, loopState) =>
        {
            if (results.Count() < p.Count())
            {
                if (p.Contains(my[i]))
                {
                    results.Push(my[i]);
                    Console.WriteLine(my[i]);
                }
            }
            else
            {
                loopState.Stop();
                return;
            }
        });
        return(results.Count());
    }
Ejemplo n.º 2
0
 //Updating Listbox elements for Bartender ListBox
 private void UpdateBartenderList(string info)
 {
     Dispatcher.Invoke(() =>
     {
         ListBartender.Items.Insert(0, info);
         LblGlassCount.Content = $"Glasses on shelf: {cleanGlassStack.Count()} ({glasses} total)";
         LblChairCount.Content = $"Vacant chairs: {freeChairStack.Count()} ({chairs} total)";
     });
 }
Ejemplo n.º 3
0
 //Updating Listbox elements for Patron ListBox
 private void UpdatePatronList(string info)
 {
     Dispatcher.Invoke(() =>
     {
         ListPatron.Items.Insert(0, info);
         LblPatronCount.Content = $"Patrons in bar: {uiPatronCountQueue.Count()}";
         LblChairCount.Content  = $"Vacant chairs: {freeChairStack.Count()} ({chairs} total)";
     });
 }
Ejemplo n.º 4
0
 public void ConcatenateLogs()
 {
     while (true)
     {
         var stopwatch = new Stopwatch();
         stopwatch.Start();
         var evaluatedLog = _currentActiveLog;
         var totalObjects = CommandsQueue.Count();
         totalObjects = totalObjects < 50 ? totalObjects : 50;
         if (totalObjects > 0)
         {
             BaseCommand[] totalCommands = new BaseCommand[totalObjects];
             CommandsQueue.TryPopRange(totalCommands, 0, totalObjects);
             Logs.TryAdd(evaluatedLog, new LogEntry()
             {
                 Commands = totalCommands.ToList(),
                 Term     = CurrentTerm,
                 Index    = evaluatedLog
             });
             _currentActiveLog += 1;
             Save();
             Logger.LogDebug("Adding logs for index " + evaluatedLog + " Log concatenation took " + stopwatch.ElapsedMilliseconds + " to add " + totalObjects);
             //Console.WriteLine("Adding logs for index " + evaluatedLog + " Log concatenation took " + stopwatch.ElapsedMilliseconds + " to add " + totalObjects);
         }
         Thread.Sleep(10);
     }
 }
Ejemplo n.º 5
0
        public void Work(Action <string> callback, ConcurrentStack <Glass> dirtyGlassStack,
                         ConcurrentStack <Glass> cleanGlassStack, bool bouncerIsWorking, ConcurrentQueue <Patron> patronQueue,
                         int waiterWashingSec, int waiterPickingGlassesSec, int glasses)
        {
            this.Callback        = callback;
            this.DirtyGlassStack = dirtyGlassStack;
            this.CleanGlassStack = cleanGlassStack;
            this.BarIsOpen       = bouncerIsWorking;
            this.PatronQueue     = patronQueue;

            Task.Run(() =>
            {
                while (BarIsOpen)
                {
                    while (CleanGlassStack.Count() != glasses)
                    {
                        if (!DirtyGlassStack.IsEmpty)
                        {
                            Callback("The waiter picks up dirty glasses from a table.");
                            Thread.Sleep(waiterWashingSec / waiterSpeed);
                            Callback("The waiter is washing glasses.");
                            Thread.Sleep(waiterPickingGlassesSec / waiterSpeed);
                            Callback("The waiter places the clean glasses back on the shelf.");
                            for (int i = 0; i < DirtyGlassStack.Count(); i++)
                            {
                                DirtyGlassStack.TryPop(out Glass g);
                                CleanGlassStack.Push(new Glass());
                            }
                        }
                    }
                }
                callback("The waiter goes home.");
            });
        }
Ejemplo n.º 6
0
        public IEnumerable <IRemuneracao> AbaterValorExcedenteDasRemuneracoes(ILimiteDeRemuneracao limiteOriginario, IEnumerable <IRemuneracao> remuneracoes, Dinheiro valorExcedente)
        {
            //TODO funciona apenas para real
            Dinheiro valorTotal = remuneracoes.Select(rem => rem.ValorAjustado).SomaDinheiro();

            ConcurrentStack <IRemuneracao> remuneracoesAbatidas = new ConcurrentStack <IRemuneracao>();

            Dinheiro totalAbatido = Dinheiro.Zero;

            Parallel.ForEach(remuneracoes, (rem) =>
            {
                decimal percentualDeAbatimento = decimal.Round(rem.ValorAjustado / valorTotal, 7);

                Dinheiro valorDoAbatimento = percentualDeAbatimento * valorExcedente;

                Dinheiro valorDoAbatimentoArrendondado = Dinheiro.Arredondar(valorDoAbatimento, 7);

                IRemuneracao remuneracaoDivididaEmPartesAbatidas = AbaterRemuneracao(rem, valorDoAbatimentoArrendondado, limiteOriginario);

                if (limiteOriginario.Tipo.Id.Equals("LIMITE_MINIMO") && remuneracaoDivididaEmPartesAbatidas.ValorAjustado > Dinheiro.Zero || limiteOriginario.Tipo.Id.Equals("LIMITE_MAXIMO"))
                {
                    remuneracoesAbatidas.Push(remuneracaoDivididaEmPartesAbatidas);
                    totalAbatido += valorDoAbatimentoArrendondado;
                }
            });

            Dinheiro diferencaDeExcedente = Dinheiro.Arredondar(valorExcedente - totalAbatido, 6);

            if (diferencaDeExcedente != Dinheiro.Zero && remuneracoesAbatidas.Count() > 0)
            {
                CorrecaoDeArredondamentoDeExcedente(remuneracoesAbatidas.First(), diferencaDeExcedente);
            }

            return(remuneracoesAbatidas);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Checks if Value is for actual Block of attempts allowed and insertes the value if true
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        internal bool AddValueToVersuche(sbyte value)
        {
            if (CountOfVersuche() < 24)
            {
                if (MassenVorne.Count() < 6 && IsMassValue(value))
                {
                    MassenVorne.Push(Convert.ToByte(value));
                }
                else if (CountOfVersuche() >= 6 && Schüsse.Count() < 6 && IsSchussValue(value))
                {
                    Schüsse.Push(Convert.ToByte(value));
                }
                else if (CountOfVersuche() >= 12 && MassenHinten.Count() < 6 && IsMassValue(value))
                {
                    MassenHinten.Push(Convert.ToByte(value));
                }
                else if (CountOfVersuche() >= 18 && Kombinieren.Count() < 6 && IsMassValue(value))
                {
                    Kombinieren.Push(Convert.ToByte(value));
                }
                else
                {
                    return(false);
                }

                SaveTurnsToLocalSettings();
                return(true);
            }

            return(false);
        }
Ejemplo n.º 8
0
        public void ValidateAllAuthorsInDatabase()
        {
            string authorDirectory = Path.Combine(this.WorkingDirectoryPath, "authors/");

            if (!Directory.Exists(authorDirectory))
            {
                Directory.CreateDirectory(authorDirectory);
            }

            ConcurrentStack <Author> allAuthorsInDatabase = new ConcurrentStack <Author>();

            using (ApplicationDatabase ctxt = new ApplicationDatabase())
            {
                allAuthorsInDatabase = new ConcurrentStack <Author>(ctxt.Authors.OrderBy(o => o.Id));
            }
            int maxThreads = 12; //Change this to the amount of cores on the target PC

            int totalAmountOfAuthorsInDatabase = allAuthorsInDatabase.Count();
            int totalAuthorsValidated          = 0;

            System.Timers.Timer titleBarUpdateTimer = new System.Timers.Timer();
            titleBarUpdateTimer.Elapsed += (sender, args) => Console.Title = $"{totalAuthorsValidated}/{totalAmountOfAuthorsInDatabase}";
            titleBarUpdateTimer.Interval = 1000;
            titleBarUpdateTimer.Start();

            for (int i = 0; i < maxThreads; i++)
            {
                Console.WriteLine($"Starting author validator {i + 1} of {maxThreads}");
                ThreadPool.QueueUserWorkItem((Action) =>
                {
                    while (true)
                    {
                        try
                        {
                            Author toHandle = null;
                            while (toHandle == null)
                            {
                                allAuthorsInDatabase.TryPop(out toHandle);
                            }
                            ValidateAuthor(toHandle, authorDirectory);
                            Interlocked.Increment(ref totalAuthorsValidated);
                        }
                        catch (Exception e)
                        {
                            try { File.AppendAllText(@"C:\ScratchScrapeData\AuthorScraper\errors\errors.txt", e.Message); }
                            catch (Exception) { }
                        }
                    }
                }, null);
            }
        }
            public void Interceptors_can_be_added_removed_and_dispatched_to_concurrently()
            {
                var interceptors = new ConcurrentStack <InterceptorForThreads>();
                var dispatcher   = new InternalDispatcher <InterceptorForThreads>();

                const int interceptorCount = 20;
                const int dispatchCount    = 10;

                // Add in parallel
                ExecuteInParallel(
                    () =>
                {
                    var interceptor = new InterceptorForThreads();
                    interceptors.Push(interceptor);
                    dispatcher.Add(interceptor);
                }, interceptorCount);

                Assert.Equal(interceptorCount, interceptors.Count);

                // Dispatch in parallel
                var calledInterceptors = new ConcurrentStack <InterceptorForThreads>();

                ExecuteInParallel(() => dispatcher.Dispatch(calledInterceptors.Push), dispatchCount);

                Assert.Equal(dispatchCount * interceptorCount, calledInterceptors.Count);
                interceptors.Each(i => Assert.Equal(dispatchCount, calledInterceptors.Count(c => c == i)));

                var toRemove = new ConcurrentStack <InterceptorForThreads>(interceptors);

                // Add, remove, and dispatch in parallel
                ExecuteInParallel(
                    () =>
                {
                    dispatcher.Dispatch(i => { });
                    InterceptorForThreads interceptor;
                    toRemove.TryPop(out interceptor);
                    dispatcher.Remove(interceptor);
                    dispatcher.Add(interceptor);
                }, interceptorCount);

                // Dispatch in parallel
                calledInterceptors = new ConcurrentStack <InterceptorForThreads>();
                ExecuteInParallel(() => dispatcher.Dispatch(calledInterceptors.Push), dispatchCount);

                Assert.Equal(dispatchCount * interceptorCount, calledInterceptors.Count);
                interceptors.Each(i => Assert.Equal(dispatchCount, calledInterceptors.Count(c => c == i)));
            }
            public void Interceptors_can_be_added_removed_and_dispatched_to_concurrently()
            {
                var interceptors = new ConcurrentStack<InterceptorForThreads>();
                var dispatcher = new InternalDispatcher<InterceptorForThreads>();

                const int interceptorCount = 20;
                const int dispatchCount = 10;

                // Add in parallel
                ExecuteInParallel(
                    () =>
                        {
                            var interceptor = new InterceptorForThreads();
                            interceptors.Push(interceptor);
                            dispatcher.Add(interceptor);
                        }, interceptorCount);

                Assert.Equal(interceptorCount, interceptors.Count);

                // Dispatch in parallel
                var calledInterceptors = new ConcurrentStack<InterceptorForThreads>();
                ExecuteInParallel(() => dispatcher.Dispatch(calledInterceptors.Push), dispatchCount);

                Assert.Equal(dispatchCount * interceptorCount, calledInterceptors.Count);
                interceptors.Each(i => Assert.Equal(dispatchCount, calledInterceptors.Count(c => c == i)));

                var toRemove = new ConcurrentStack<InterceptorForThreads>(interceptors);

                // Add, remove, and dispatch in parallel
                ExecuteInParallel(
                    () =>
                        {
                            dispatcher.Dispatch(i => { });
                            InterceptorForThreads interceptor;
                            toRemove.TryPop(out interceptor);
                            dispatcher.Remove(interceptor);
                            dispatcher.Add(interceptor);
                        }, interceptorCount);

                // Dispatch in parallel
                calledInterceptors = new ConcurrentStack<InterceptorForThreads>();
                ExecuteInParallel(() => dispatcher.Dispatch(calledInterceptors.Push), dispatchCount);

                Assert.Equal(dispatchCount * interceptorCount, calledInterceptors.Count);
                interceptors.Each(i => Assert.Equal(dispatchCount, calledInterceptors.Count(c => c == i)));
            }
Ejemplo n.º 11
0
        void mInvokeTimer_Tick(object sender, EventArgs e)
        {
            int count = mValueStack.Count();

            if (count > 0)
            {
                float[] val = new float[count];
                mValueStack.TryPopRange(val);
                mValueStack.Clear();
                mValue = val.Max();
            }
            else
            {
                mValue = 0.0f;
            }

            if (mValue > mMaxValue)
            {
                // New max value
                mMaxValue       = mValue;
                mHoldTimerTicks = HOLD_TIME_MS / REFRESH_INTERVAL_MS;
            }
            else
            {
                if (mHoldTimerTicks > 0)
                {
                    mHoldTimerTicks--;
                }
                else
                {
                    // Decrement max value
                    const float STEP = 0.03f;
                    if (mMaxValue > STEP)
                    {
                        mMaxValue -= STEP;
                    }
                    else
                    {
                        mMaxValue = 0.0f;
                    }
                }
            }

            this.Invalidate();
        }
Ejemplo n.º 12
0
        private static async Task Run()
        {
            var result = new ConcurrentStack <bool>();
            var tasks  = new List <Task>();

            for (var index = 0; index < 105; index++)
            {
                var number = index;
                tasks.Add(Task.Run(async() =>
                {
                    result.Push(await _service.GetTicket(number));
                    Console.WriteLine($"add {number}");
                }));
            }
            await Task.WhenAll(tasks);

            Console.WriteLine($"success count: {result.Count(r => r)}");
        }
Ejemplo n.º 13
0
        public async Task <int> Get()
        {
            var result = new ConcurrentStack <bool>();
            var tasks  = new List <Task>();

            for (var index = 0; index < 200; index++)
            {
                var task = Task.Run(async() =>
                {
                    var ticket = await _service.GetTicket();
                    result.Push(ticket);
                });
                tasks.Add(task);
            }
            await Task.WhenAll(tasks);

            return(result.Count(r => r));
        }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            var sw = Stopwatch.StartNew();

            _byteArraysStack = new ConcurrentStack <byte[]>();
            _keysStack       = new ConcurrentStack <string>();
            _validKeys       = new ConcurrentStack <string>();

            // This code requires at least 3 logical cores
            int taskAESKeysMax = Environment.ProcessorCount / 2;
            // Use the remaining logical cores - 1
            // to create parallelized tasks to run many consumers
            int taskHexStringsMax = Environment.ProcessorCount - taskAESKeysMax - 1;
            var taskAESKeys       = Task.Factory.StartNew(() => ParallelPartitionGenerateAESKeys(taskAESKeysMax));

            Task[] tasksHexStrings = new Task[taskHexStringsMax];
            for (int i = 0; i < taskHexStringsMax; i++)
            {
                tasksHexStrings[i] = Task.Factory.StartNew(() =>
                {
                    // Increment tasksHexStringsRunning as an atomic operation
                    System.Threading.Interlocked.Increment(ref tasksHexStringsRunning);
                    try
                    {
                        ConvertAESKeysToHex(taskAESKeys);
                    }
                    finally
                    {
                        System.Threading.Interlocked.Decrement(ref tasksHexStringsRunning);
                    }
                });
            }
            var taskValidateKeys = Task.Factory.StartNew(() => ValidateKeys());

            Task.WaitAll(taskValidateKeys);

            Console.WriteLine("Number of keys in the list: {0}", _keysStack.Count());
            Console.WriteLine("Number of valid keys: {0}", _validKeys.Count());

            Debug.WriteLine(sw.Elapsed.ToString());
            //  Display the results and wait for the user to press a key
            Console.WriteLine("Finished!");
            Console.ReadLine();
        }
Ejemplo n.º 15
0
        public override void Scrape()
        {
            Console.WriteLine("Starting project scraping...");
            const int maxThreads = 8;
            ConcurrentStack <Author> toHandle = new ConcurrentStack <Author>();

            using (ApplicationDatabase ctxt = new ApplicationDatabase())
            {
                if (!Directory.Exists(Path.Combine(this.WorkingDirectoryPath, "projects")))
                {
                    Console.WriteLine($"Creating initial projects directory at: {Path.Combine(this.WorkingDirectoryPath, "projects")}");
                    try
                    {
                        Directory.CreateDirectory(Path.Combine(this.WorkingDirectoryPath, "projects"));
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                        return;
                    }
                }

                Console.WriteLine("Enumerating existing projects... (this may take a very long time!)");
                string[] fileNames = Directory.GetFiles(Path.Combine(this.WorkingDirectoryPath, "projects")).Select(o => Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(o))).ToArray();
                Console.WriteLine($"Enumeration completed. Found {fileNames.Length} already scraped projects.\nGenerating projects cache...");
                downloadedProjectsCache = new ConcurrentDictionary <string, string>(fileNames.ToDictionary(x => x.ToString(), x => ""));
                Console.WriteLine("Generating projects cache completed.\nBuilding stack of authors to scrape...");

                foreach (Author a in ctxt.Authors.Where(o => o.Projects.Count() <= 0).OrderBy(o => o.Id))
                {
                    toHandle.Push(a);
                }
                Console.WriteLine("Building stack done. Starting scrapers...");
            }
            System.Timers.Timer titleBarUpdateTimer = new System.Timers.Timer();
            titleBarUpdateTimer.Elapsed += (sender, args) => Console.Title = $"{toHandle.Count()}";
            titleBarUpdateTimer.Interval = 10000;
            titleBarUpdateTimer.Start();


            int maxToHandle = toHandle.Count();

            for (int i = 0; i < maxThreads; i++)
            {
                ThreadPool.QueueUserWorkItem((Action) =>
                {
                    Console.WriteLine($"Project scraper {Guid.NewGuid().ToString()} of {maxThreads} active.");
                    using (ApplicationDatabase ctxt2 = new ApplicationDatabase())
                    {
                        while (true)
                        {
                            Author handling = null;
                            while (handling == null)
                            {
                                toHandle.TryPop(out handling);
                            }
                            foreach (Project project in GetProjectsByUsername(handling.Username, false))
                            {
                                if (!ctxt2.Projects.AsNoTracking().Any(o => o.Id == project.Id))
                                {
                                    project.AuthorId = handling.Id;
                                    ctxt2.Projects.Add(project);
                                }
                                DownloadProjectToFile(project.Id.ToString());
                            }
                            ctxt2.SaveChanges();
                        }
                    }
                }, null);
            }
        }
Ejemplo n.º 16
0
        static void Main(string[] args)
        {
            int[] arr5 = { 101, 202, 303, 404, 505, 606, 707 };

            ConcurrentBag <int> bag = new ConcurrentBag <int>();

            foreach (var items in arr5)
            {
                bag.Add(items);
                log.Debug("Adding multiple items in the bag" + items);
            }

            log.Info("Removing items from the bag using TryPeek method:");
            int itemss;

            bag.TryPeek(out itemss);
            log.Debug("The element peeked out is:" + itemss);
            bag.TryPeek(out itemss);
            log.Debug("The element peeked out is:" + itemss);
            bag.TryPeek(out itemss);
            log.Debug("The element peeked out is:" + itemss);

            log.Info("Removing items from the bag using TryTake method:");
            int itemsss;

            bag.TryTake(out itemsss);
            log.Debug("The element Taken out is:" + itemsss);
            bag.TryTake(out itemsss);
            log.Debug("The element taken out is:" + itemsss);
            bag.TryTake(out itemsss);
            log.Debug("The element taken out is:" + itemsss);

            foreach (var ele in bag)
            {
                log.Debug(ele);
            }


            ConcurrentStack <int> stack = new ConcurrentStack <int>();

            stack.Push(1);
            stack.Push(2);
            stack.Push(3);

            foreach (var l in stack)
            {
                log.Debug("The Stack ELements are:" + l);
            }


            int[] ints = { 5, 6, 8 };
            stack.PushRange(ints);
            foreach (var k in stack)
            {
                log.Debug("This is an example of pushing multiple elements into the stack:" + k);
            }

            int count = stack.Count();

            log.Info("Total number of element in the stack is:" + count);

            int item;

            stack.TryPeek(out item);                                             //Using Try peek method
            log.Debug("Removeing element from thr stack USing TryPeek:" + item); //Print 8

            // Difference between TryPeek and TryDequeue methods
            // Both method works same, TryDequeue method removes an item from the list and TryPeek does not remove an item from the list.


            int  item2;
            bool isSuccess2 = stack.TryPop(out item2); //isSuccess = True, item = 6     //USing TryPop method

            stack.TryPop(out item2);
            log.Debug("Removeing element from thr stack using tryPop:" + item2);

            int[] array = stack.ToArray();
            foreach (var j in array)
            {
                log.Debug("Converting stack to array:" + j);
            }


            ConcurrentQueue <int> coll = new ConcurrentQueue <int>();

            coll.Enqueue(56);
            coll.Enqueue(57);
            coll.Enqueue(58);

            foreach (var i in coll)
            {
                log.Debug("Printing all the Queue values:" + i);
            }

            int item3;

            coll.TryDequeue(out item3);
            log.Debug("Removing element from the queue:" + item3);


            bool isEmpty = coll.IsEmpty;

            log.Debug("IS the queue Empty?:" + isEmpty);

            ConcurrentDictionary <string, string> dict = new ConcurrentDictionary <string, string>();

            dict.TryAdd("1", "Aditya");
            dict.TryAdd("2", "Sharma");
            dict.TryAdd("2", "N"); //returns false;

            string itemValue1;

            dict.TryGetValue("1", out itemValue1);  //returns true
            log.Debug("Retreving the element using its key:" + itemValue1);

            foreach (var item4 in dict)
            {
                log.Debug("Retreving all the elements of the dic:" + item4.Key + "-" + item4.Value);
            }
            int total = dict.Count();

            log.Debug("Total elements in the dic are:" + total);

            bool firstItemFound = dict.ContainsKey("5");

            log.Debug(firstItemFound); //returns false

            Console.ReadKey();
        }
Ejemplo n.º 17
0
 public int MidiInMessagesCount()
 {
     return(mMidiEventStack.Count());
 }
Ejemplo n.º 18
0
        public static void ProcessJSON2(string projectDirectoryPath, int threads = 1)
        {
            ZemiIO.EnableConcurrentWriting();
            Say("Preparing database-dependent data...");
            Say("Checking SpriteType rows.. ", false);
            SeedSpriteTypeTable();
            Say("OK");

            Say("Mapping sb2 opcodes to sb3 opcodes.. ", false);
            MapSb2ToSb3.Map(false);
            Say("OK");

            Say("Loading sb2 and sb3 opcode caches.. ", false);
            Dictionary <string, OpCode> sb2Opcodes = GetOpcodes(true, false);
            Dictionary <string, OpCode> sb3OpCodes = GetOpcodes(false, true);

            Say("OK");

            Say($"Checking log output files, set to {projectDirectoryPath}.. ", false);
            ZemiIO.CreateFileIfNotexists(projectDirectoryPath, "HandledProjects.txt");
            ZemiIO.CreateFileIfNotexists(projectDirectoryPath, "EmptyProjects.txt");
            ZemiIO.CreateFileIfNotexists(projectDirectoryPath, "UnregisteredProjects.txt");
            ZemiIO.CreateFileIfNotexists(projectDirectoryPath, "MalformedBlocks.txt");
            ZemiIO.CreateDirectoryIfNotExists(projectDirectoryPath, @"remixes\");
            string remixPath = Path.Combine(projectDirectoryPath, @"remixes\");

            ZemiIO.CreateDirectoryIfNotExists(projectDirectoryPath, @"sb1\");
            string sb1Path = Path.Combine(projectDirectoryPath, @"sb1\");

            Say("OK");

            Say($"Enumerating files in {projectDirectoryPath}. This may take a while.. ");
            string[] allFilesInDirectory = Directory.GetFiles(projectDirectoryPath);
            ConcurrentStack <string> allFilesToHandle = new ConcurrentStack <string>(allFilesInDirectory);
            int count_allFilesInDirectory             = allFilesInDirectory.Count();

            allFilesInDirectory = null;
            GC.Collect();
            Say($"Enumeration completed. Found {allFilesToHandle.Count()} files.");
            int totalFilesHandled = 0;

            System.Timers.Timer titleBarUpdateTimer = new System.Timers.Timer();
            titleBarUpdateTimer.Elapsed += (sender, args) => Console.Title = $"{totalFilesHandled}/{count_allFilesInDirectory}";
            titleBarUpdateTimer.Interval = 1000;
            titleBarUpdateTimer.Start();

            Say("Starting parsers... NOW!");
            for (int i = 0; i < threads; i++)
            {
                Say($"Starting parser {i+1} of {threads}.");
                new Thread(() =>
                {
                    using (ApplicationDatabase ctxt = new ApplicationDatabase())
                    {
                        Sb2Parser sb2Parser = new Sb2Parser(sb2Opcodes);
                        Sb3Parser sb3Parser = new Sb3Parser(sb3OpCodes);

                        while (true)
                        {
                            string handling = null;
                            while (handling == null)
                            {
                                allFilesToHandle.TryPop(out handling);
                            }
                            Interlocked.Increment(ref totalFilesHandled);

                            string[] splitProjectFileName = handling.Split('.');
                            if (splitProjectFileName.Length != 3)
                            {
                                continue;                                   //This guarantees we only handle projectid.sb3.json files, and not the handledprojects.txt, for example.
                            }
                            string projectId        = Path.GetFileName(splitProjectFileName[0]);
                            string projectSbVersion = splitProjectFileName[1];
                            int projectIdAsInt      = Int32.Parse(projectId);

                            if (projectSbVersion == "sb")
                            {
                                try
                                {
                                    File.Move(handling, Path.Combine(sb1Path, Path.GetFileName(handling)));
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message, ex.StackTrace);
                                }
                                continue;
                            }

                            if (ctxt.Scripts.Any(o => o.ProjectId == projectIdAsInt))
                            {
                                //Say($"Already handled {projectIdAsInt}");
                                continue;
                            }

                            Project possibleExistingProject = ctxt.Projects.AsNoTracking().Where(o => o.Id == projectIdAsInt).FirstOrDefault();
                            if (possibleExistingProject == null)
                            {
                                //Say($"Unregistered: {projectId}");
                                HandleUnregisteredProject(projectId.ToString(), projectDirectoryPath);
                                continue;
                            }
                            else if (possibleExistingProject.IsRemix)
                            {
                                //Say($"Remix: {projectId}");
                                File.Move(handling, Path.Combine(remixPath, Path.GetFileName(handling)));
                                continue;
                            }

                            string projectJson = File.ReadAllText(handling);
                            if (string.IsNullOrEmpty(projectJson) || string.IsNullOrWhiteSpace(projectJson))
                            {
                                Say($"Empty: {projectId}");
                                HandleEmptyProject(projectId.ToString(), projectDirectoryPath);
                                continue;
                            }
                            else if (projectJson.StartsWith("ScratchV") || projectJson.StartsWith("PK"))
                            {
                                try
                                {
                                    Say($"Uncaught sb1: {Path.GetFileName(handling)}");
                                    File.Move(handling, Path.Combine(sb1Path, Path.GetFileName(handling)));
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message, ex.StackTrace);
                                }
                                continue;
                            }

                            try
                            {
                                switch (projectSbVersion)
                                {
                                case "sb2":
                                    sb2Parser.ParseProject(projectJson, projectId);
                                    break;

                                case "sb3":
                                    sb3Parser.ParseProject(projectJson, Int32.Parse(projectId));
                                    break;

                                default:
                                    Say("Can't parse sb1 formats yet.");
                                    break;
                                }
                                HandleParsedProject(projectId, projectDirectoryPath);
                            }
                            catch (Exception ex)
                            {
                                Say(ex.Message);
                            }
                        }
                    }
                }, 16000000).Start();
            }
            Say("Look at 'em go!");
        }
Ejemplo n.º 19
0
        static void Main(string[] args)
        {
            //trying enumerables - too much overhead, recreating list with each call, not easily updatable
            //trying lists of special class - still not as easy to update as a dictionary
            //trying dictionary
            //moving away from recursion - had stack overflow @ 24000 network
            //still taking ~60s to reach 2500 - trying search for possible matches vs list of all words
            //iterating through all possible 1-levdist from each friend has reduced time dramatically as opposed to
            //  multiple scans against the large dictionary of all terms - checking in to github before breaking anything
            //  (~13s on beast computer, ~30s on slower one)
            //tried backgroundworkers - too slow
            //tried threadpool - not enough control
            //tried a dynamic list of threads and concurrent objects; w/ 4 threads, runs in ~17s on slow computer
            //removed ref'd count (was inaccurate), using dictionary for final numbers, w/ 8 threads ~14s on slow computer

            //Two words are friends if they have a Levenshtein distance (http://en.wikipedia.org/wiki/Levenshtein_distance) of 1.
            //That is, you can add, remove, or substitute exactly one letter in word X to create word Y.
            //A word’s social network consists of all of its friends, plus all of their friends, and all of their friends’ friends, and so on.
            //Write a program to tell us how big the social network for the word “causes” is, using this word list
            //(https://github.com/causes/puzzles/raw/master/word_friends/word.list).

            ConcurrentDictionary <string, bool> wordList;

            if (USE_WEBSITE_LIST)
            {
                wordList = GetWordListFromWeb(WEBSITE_URL); //string - word; bool - whether it's in the network
            }
            else
            {
                wordList = GetWordListFromFile(PATH_TO_WORDLIST);
            }

            ConcurrentStack <string> friends = new ConcurrentStack <string>();

            Stopwatch sw = Stopwatch.StartNew();

            if (wordList.Any()) //make sure we got some words back from the site
            {
                friends.Push(START_WORD);

                int threadCount = THREAD_COUNT;

                List <Thread> threads = new List <Thread>();
                for (int i = 0; i < threadCount; i++)
                {
                    threads.Add(new Thread(delegate()
                    {
                        while (friends.Count() > 0)
                        {
                            string popped = "";
                            if (friends.TryPop(out popped))
                            {
                                FindNextFriends(popped, ref friends, ref wordList);
                            }
                        }
                    }));
                }

                //fire off the threads
                foreach (Thread thread in threads)
                {
                    Console.WriteLine("starting thread");
                    thread.Start();
                }

                Console.WriteLine("waiting for threads to complete");

                //wait for everyone to come home
                foreach (Thread thread in threads)
                {
                    thread.Join();
                    Console.WriteLine("thread complete");
                }
            }
            else
            {
                Console.WriteLine("No words in word list");
            }
            sw.Stop();

            int count = wordList.Count(w => w.Value);

            Console.WriteLine("Found: {0} words in network; took {1}ms", count, sw.Elapsed.TotalMilliseconds);

            //78482 is the magic number
            Console.WriteLine("Testing Dictionary for target numer ({0}): {1}", TEST_FINAL_VALUE, (TEST_FINAL_VALUE == count).ToString());

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }