Beispiel #1
0
 /// <summary>
 /// Resources cache
 /// </summary>
 /// <param name="source"></param>
 /// <param name="options"></param>
 public AssetCachePartResources(IAsset source, AssetCacheOptions options)
 {
     this.Source   = source ?? throw new ArgumentNullException(nameof(source));
     this.Options  = options ?? throw new ArgumentNullException(nameof(options));
     this.comparer = LineComparer.Default;
     this.cache    = new Cache(comparer);
 }
Beispiel #2
0
        public void GiveTheExpectedResult(int guessPeg1, int guessPeg2, int secretPeg1, int secretPeg2, int expectedCorrect, int expectedWrongPosition)
        {
            var lineComparer = new LineComparer();
            var guess        = new int[] { guessPeg1, guessPeg2 };
            var secret       = new int[] { secretPeg1, secretPeg2 };
            var actualResult = lineComparer.Compare(guess, secret);

            Assert.Equal(expectedCorrect, actualResult.NumberOfCorrectPegs);
            Assert.Equal(expectedWrongPosition, actualResult.NumberOfPegsAtWrongPosition);
        }
Beispiel #3
0
        private static BatchInfo PartitionSorter(BatchInfo batchInfo)
        {
            Console.WriteLine($"[{DateTime.Now}] Start sorting {batchInfo.Index}");

            Stopwatch stopwatch = Stopwatch.StartNew();
            var       comparer  = new LineComparer();

            Array.Sort(batchInfo.Lines, comparer);

            Console.WriteLine($"[{DateTime.Now}] Partition sorted {batchInfo.Index}. Elapsed: {stopwatch.Elapsed}");

            return(batchInfo);
        }
Beispiel #4
0
        public static string Merge(IEnumerable <string> sortedFiles, long cacheSize)
        {
            var files = sortedFiles.ToArray();

            if (files.Length == 1)
            {
                return(files[0]);
            }

            var resultFile = $"./{_mergeFolder}/{Path.GetRandomFileName()}";

            if (File.Exists(resultFile))
            {
                File.Delete(resultFile);
            }

            int fileCount = files.Length;

            var iterators = new BufferedFileIterator[fileCount];

            for (var i = 0; i < fileCount; i++)
            {
                var iterator = new BufferedFileIterator(files[i], cacheSize);
                iterator.MoveNext();
                iterators[i] = iterator;
            }

            var comaprer    = new LineComparer();
            var buffer      = new List <string>();
            var currentSize = 0;

            while (true)
            {
                IEnumerator <string> minIterator = null;
                foreach (var iterator in iterators)
                {
                    if (iterator.Current != null)
                    {
                        if (minIterator == null || comaprer.Compare(minIterator.Current, iterator.Current) > 0)
                        {
                            minIterator = iterator;
                        }
                    }
                }

                if (minIterator == null)
                {
                    break;
                }

                var minString = minIterator.Current;
                buffer.Add(minString);
                currentSize += minString.Length;
                minIterator.MoveNext();


                if (currentSize > cacheSize)
                {
                    File.AppendAllLines(resultFile, buffer);
                    buffer.Clear();
                    currentSize = 0;
                }
            }

            File.AppendAllLines(resultFile, buffer);

            foreach (var i in iterators)
            {
                i.Dispose();
            }

            return(resultFile);
        }
Beispiel #5
0
 public void Sort(LineOptions lineOptions) => LineComparer.Sort(ref this.lines, lineOptions);
Beispiel #6
0
 public Cache(LineComparer comparer)
 {
     this.data = new Dictionary <ILine, LineResourceBytes>(comparer);
 }
Beispiel #7
0
 public Cache(LineComparer comparer)
 {
     this.strings = new Dictionary <ILine, ILine>(comparer);
 }