Example #1
0
        /// <summary>
        /// Fetches prime numbers from existing result files.
        /// </summary>
        /// <param name="indexedResultFiles">All the result files accompanied with their index.</param>
        /// <returns>Container holding information about the loaded prime numbers.</returns>
        private ExistingPrimesLoadingResult fetchPrimes(SortedDictionary <int, string> indexedResultFiles)
        {
            var result = new ExistingPrimesLoadingResult();

            var resultFiles = indexedResultFiles
                              .Select(f => f.Value)
                              .ToList();
            var lastResultFile = resultFiles.LastOrDefault(f => File.ReadLines(f).Any());

            for (int i = 0; i < resultFiles.Count; i++)
            {
                var loadingArgs = new PrimesLoadingFromFile(i, resultFiles.Count);
                OnLoadingPrimesFromFile?.Invoke(this, loadingArgs);

                var subPrimes = File
                                .ReadLines(resultFiles[i])
                                .Select(l => BigInteger.Parse(l));

                //Verify that the file contained any prime numbers.
                if (subPrimes.Any())
                {
                    //Calculate the next number to check if it's a prime number.
                    result.NextNumberToCheck = subPrimes.Last() + 1;
                }
                else
                {
                    //TODO: Test this use case.
                    return(result);
                }

                //Store the loaded primes.
                result = storeSubPrimesInMemory(result, subPrimes, lastResultFile);

                //There's no use in loading any more primes if the memory is full.
                if (result.MemoryLimitReached)
                {
                    return(result);
                }

                //Check if the user cancelled the execution.
                if (userPressedEscape())
                {
                    result.ExecutionWasAborted = true;
                    return(result);
                }
            }

            return(result);
        }
Example #2
0
 /// <summary>
 /// Handles the OnLoadingPrimesFromFile event for the object loading existing prime numbers.
 /// </summary>
 /// <param name="loader">The object loading the existing prime numbers.</param>
 /// <param name="args">The information about the event.</param>
 private void Loader_OnLoadingPrimesFromFile(object loader, PrimesLoadingFromFile args)
 {
     OnLoadingPrimesFromFile?.Invoke(loader, args);
 }