Exemple #1
0
        private void Sort(object o)
        {
            Sorter s = o as Sorter;

            if (s == null)
            {
                return;
            }

            if (s.Direction == ListSortDirection.Ascending)
            {
                Sorters.Add(s);
            }
            else if (s.Direction == ListSortDirection.Descending)
            {
                var current_sorter = FindSorter(s.Name);
                current_sorter.Direction = ListSortDirection.Descending;
            }
            else
            {
                var current_sorter = FindSorter(s.Name);
                Sorters.Remove(current_sorter);
            }

            aggregate_comparer.AddComparer(new TextComparer());
            aggregate_comparer.Refresh();

            // Here the view should be refreshed
            // - Start a timer, 100 ms countdown
            // -- Tick should trigger a refresh
        }
Exemple #2
0
        /// <summary>
        /// Based on the selected radiobutton, call the appropriate unmanaged sort.
        /// </summary>
        private void ExecuteSort()
        {
            switch (sortType)
            {
            case SortSelection.StdSort:
            {
                stdSortTime = timeTaken = Sorters.native_sort((int)SortSelection.StdSort, fillArray, dimX * dimY, concurrency, comparatorSize);
            }
            break;

            case SortSelection.PplParallelSort:
            {
                pplParallelSortTime = timeTaken = Sorters.native_sort((int)SortSelection.PplParallelSort, fillArray, dimX * dimY, concurrency, comparatorSize);
            }
            break;

            case SortSelection.PplParallelBufferedSort:
            {
                pplParallelBufferedSortTime = timeTaken = Sorters.native_sort((int)SortSelection.PplParallelBufferedSort, fillArray, dimX * dimY, concurrency, comparatorSize);
            }
            break;

            case SortSelection.PplParallelRadixSort:
            {
                pplParallelRadixSortTime = timeTaken = Sorters.native_sort((int)SortSelection.PplParallelRadixSort, fillArray, dimX * dimY, concurrency, comparatorSize);
            }
            break;
            }
            Thread.Sleep(1000); //Without explicit long sleep, there seems to be an rendering update issue.
            done = true;
        }
Exemple #3
0
        public AbstractSorter <T> CreateInstance(Sorters sorter)
        {
            var generic      = typeof(T);
            var chosenSorter = GetSorterToCreate(sorter).MakeGenericType(generic);

            return((AbstractSorter <T>)Activator.CreateInstance(chosenSorter));
        }
Exemple #4
0
 public void Sort(IEnumerable <Record> records)
 {
     if (records == null)
     {
         throw new ArgumentNullException(nameof(records));
     }
     Sorters.ToList().ForEach(p => p.PerformSort(records));
 }
Exemple #5
0
        public void TestQuickSort3()
        {
            int[] a = { 0, 1 };
            a.PrintToConsole();

            Sorters.MyQuickSort(a);
            a.PrintToConsole();
        }
Exemple #6
0
        public void TestQuickSort2()
        {
            int[] a = { 8, 1, 4, 9, 0, 3, 5, 2, 7, 6 };
            a.PrintToConsole();

            Sorters.MyQuickSort(a);
            a.PrintToConsole();
        }
        // SelectCommand
        public bool CanSelect(object parameter)
        {
            if (Sorters == null)
            {
                return(false);
            }

            return(Sorters.Any(item => item.IsChecked == true));
        }
Exemple #8
0
        public void TestQuickSort()
        {
            int[] a      = { 4, 6, 3, 9, 5 };
            int[] backup = new int[a.Length];
            Array.Copy(a, backup, a.Length);

            Sorters.MyQuickSort(a);
            a.PrintToConsole();
            backup.PrintToConsole();
        }
Exemple #9
0
        public void MergeSort_ShouldSucceed()
        {
            // Arrange
            var testArray = new[] { 50, 20, -1, 5, -10, 31 };
            var expected  = new[] { -10, -1, 5, 20, 31, 50 };

            // Act
            Sorters.MergeSort(testArray, 0, testArray.Length);

            // Assert
            Assert.IsTrue(testArray.SequenceEqual(expected));
        }
Exemple #10
0
        private Type GetSorterToCreate(Sorters sorter)
        {
            foreach (var entry in _sorters)
            {
                if (entry.Key.Contains(sorter.ToString().ToLower()))
                {
                    return(_sorters[entry.Key]);
                }
            }

            return(null);
        }
Exemple #11
0
    public frmDgvSorting()
    {
        InitializeComponent();
        DataTable tbl = GetTestData();

        this._sorters = new Sorters(tbl.Columns);      // Make a sorter from my table
        this._dvw     = new DataView(tbl);             // Make a data view from my data table
        // Do the initial sort of my data view - assigning a value to Sort triggers an immediate sort
        this._dvw.Sort = this._sorters.ToString();
        // Bind my DataGridView to my DataView as a source.
        this.dgv.DataSource = this._dvw;                 // assume .dgv is the name of my DataGridView.
    }
Exemple #12
0
        public void Add(T element)
        {
            if (element == null)
            {
                throw new ArgumentNullException();
            }

            if (this.Count == this.Capacity)
            {
                this.Resize();
            }

            this.innerCollection[this.count++] = element;
            //Array.Sort(this.innerCollection, 0, count);
            Sorters.InsertionSort(this.innerCollection, count);
        }
Exemple #13
0
        public void AddAll(ICollection <T> collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException();
            }

            if (this.Count + collection.Count > this.Capacity)
            {
                this.MultiResize(collection.Count);
            }

            foreach (T element in collection)
            {
                this.innerCollection[this.count++] = element;
            }

            //Array.Sort(this.innerCollection, 0, this.Count, this.comparison);
            Sorters.InsertionSort(this.innerCollection, this.Count, this.comparison);
        }
Exemple #14
0
        private void btnSort_Click(object sender, EventArgs e)
        {
            Sorters    sort = new Sorters();
            List <int> list = lbNotSorted.Items.ToListOfType <int>();
            List <int> ret = new List <int>();
            DateTime   start = DateTime.Now, stop = DateTime.Now;

            start = DateTime.Now;
            switch (comboAlgorithms.SelectedItem.ToString())
            {
            case "Quick Sort":
            {
                ret = sort.QuickSort(list, 0, list.Count - 1).ToListOfType <int>();
                break;
            }

            case "Merge Sort":
            {
                ret = sort.MergeSort(list, 0, list.Count - 1).ToListOfType <int>();
                break;
            }

            case "Heap Sort":
            {
                ret = sort.HeapSort(list).ToListOfType <int>();
                break;
            }

            case "Bubble Sort":
            {
                ret = sort.BubbleSort(list).ToListOfType <int>();
                break;
            }
            }
            stop = DateTime.Now;
            lbSorted.Items.Clear();
            lbSorted.Items.AddRange(ret.Cast <object>().ToArray());
            lblElapsed.Text = String.Format("{0} ticks", (stop - start).Ticks);
        }
Exemple #15
0
 private void QuickChecked(object sender, RoutedEventArgs e)
 {
     _currentSorter = Sorters.QUICK;
 }
Exemple #16
0
 private void ShellChecked(object sender, RoutedEventArgs e)
 {
     _currentSorter = Sorters.SHELL;
 }
Exemple #17
0
 private void InsertionChecked(object sender, RoutedEventArgs e)
 {
     _currentSorter = Sorters.INSERTION;
 }
Exemple #18
0
 private void BubbleChecked(object sender, RoutedEventArgs e)
 {
     _currentSorter = Sorters.BUBBLE;
 }
Exemple #19
0
        public async Task <List <PenPack> > Sort(List <PenPallet> penPalletInfo, int penPackSize)
        {
            var tasks = new List <Task <List <PenPack> > >();
            //моя идея такая. Мы будем кормить сортировщикам максимально большие паллеты. Поиск будет простым проходом, т.е. не очень эффективный.
            var listCopy       = penPalletInfo.Select(t => t).OrderByDescending(t => t.PensColorCodes.Count).ToList();
            var listCopyLocker = new object();

            var totalPens = 0;

            while (listCopy.Count > 0)
            {
                PenPallet penPallet;
                lock (listCopyLocker)
                {
                    penPallet = listCopy.FirstOrDefault(t => t.PensColorCodes.Count <= MaxCapacity - CurrentCapacity);
                    listCopy.Remove(penPallet);
                }

                if (penPallet == null)
                {
                    await Task.Delay(5);

                    continue;
                }
                while (Sorters.All(t => t.IsBusy))
                {
                    //wait for sorter
                    await Task.Delay(5);
                }

                var sorter = Sorters.First(t => !t.IsBusy);
                lock (_capacityLocker)
                {
                    totalPens       += penPallet.PensColorCodes.Count;
                    CurrentCapacity += penPallet.PensColorCodes.Count;
                }

                if (CurrentCapacity > MaxCapacity)
                {
                    Logger.Warn(
                        $"Table capacity exceeded limit ({MaxCapacity}). Capacity: {CurrentCapacity}");
                }

                var sortTask = sorter.Sort(penPallet, penPackSize).ContinueWith(t =>
                {
                    lock (_capacityLocker)
                    {
                        CurrentCapacity -= penPallet.PensColorCodes.Count;
                    }
                    return(t.Result);
                });
                tasks.Add(sortTask);
            }

            Logger.Info($"Total pens sorted on sorting table #{InstanceName}: {totalPens}");
            return(await Task.WhenAll(tasks).ContinueWith(sortTask =>
            {
                //собираем корзинки сортировщиков после того, как отсортировали все карандаши и дособираем из них пачки.
                //Здесь, для "честности" алгоритма правлиьно было бы отдать карандаши сортировщику.
                var bucketPallet = new PenPallet {
                    PensColorCodes = new List <int>()
                };
                foreach (var sorter in Sorters)
                {
                    foreach (var kvp in sorter.PenColorsBuckets)
                    {
                        //собираем псевдо-паллету с количеством карандашей, равным остатку в корзинке сортировщика
                        bucketPallet.PensColorCodes.AddRange(Enumerable.Range(0, kvp.Value).Select(t => kvp.Key));
                    }
                    sorter.PenColorsBuckets.Clear();
                }
                ;

                var bucketPacks = Sorters.First().Sort(bucketPallet, penPackSize).Result;

                var penPacks = sortTask.Result.SelectMany(t => t)
                               .Union(bucketPacks)
                               .ToList();
                return penPacks;
            }));
        }
Exemple #20
0
        public void Test3(int[] initialArray, int[] expectedArray)
        {
            Sorters <int> .SelectSort(initialArray);

            Assert.That(initialArray, Is.EqualTo(expectedArray));
        }
Exemple #21
0
        public void Test4(string[] initialArray, string[] expectedArray)
        {
            Sorters <string> .SelectSort(initialArray);

            Assert.That(initialArray, Is.EqualTo(expectedArray));
        }
Exemple #22
0
        /// <summary>
        /// Resets the drawing area according to the Data Type selected
        /// This internally may call into native std sort
        /// Note: The pixels are gray-scaled for better/clearer effects
        /// </summary>
        private void ResetFill()
        {
            int width     = dimX;
            int height    = dimY;
            int rawStride = (width * pf.BitsPerPixel + 7) / 8;

            fillArray = new byte[rawStride * height];

            switch (dataType)
            {
            case DataTypeSelection.Random:
            {
                //Fill the drawing area with random pixels
                Random rnd = new Random(100);
                for (int i = 0, index = 0; i < dimX; ++i)
                {
                    for (int j = 0; j < dimY; ++j)
                    {
                        byte r = (byte)rnd.Next(256);
                        fillArray[index++] = r;
                        fillArray[index++] = r;
                        fillArray[index++] = r;
                        fillArray[index++] = 255;
                    }
                }
            }
            break;

            case DataTypeSelection.PreSorted:
            {
                //Fill the drawing area with presorted pixels
                Random rnd = new Random(100);
                for (int i = 0, index = 0; i < dimX; ++i)
                {
                    for (int j = 0; j < dimY; ++j)
                    {
                        byte r = (byte)rnd.Next(256);
                        fillArray[index++] = r;
                        fillArray[index++] = r;
                        fillArray[index++] = r;
                        fillArray[index++] = 255;
                    }
                }
                Sorters.native_sort((int)SortSelection.StdSort, fillArray, dimX * dimY, 1, 0);
            }
            break;

            case DataTypeSelection.NearlySorted:
            {
                //Fill the drawing area with nearly sorted (99% sorted) pixels
                Random rnd = new Random(100);
                for (int i = 0, index = 0; i < dimX; ++i)
                {
                    for (int j = 0; j < dimY; ++j)
                    {
                        byte r = (byte)rnd.Next(256);
                        fillArray[index++] = r;
                        fillArray[index++] = r;
                        fillArray[index++] = r;
                        fillArray[index++] = 255;
                    }
                }
                Sorters.native_sort((int)SortSelection.StdSort, fillArray, dimX * dimY, 1, 0);

                for (int i = 0; i < (fillArray.Length * 0.01); ++i)
                {
                    int rndElem1 = 4 + rnd.Next(fillArray.Length - 9);
                    rndElem1 -= rndElem1 % 4;
                    int rndElem2 = 4 + rnd.Next(fillArray.Length - 9);
                    rndElem2 -= rndElem2 % 4;

                    for (int j = 0; j < 4; ++j)
                    {
                        byte temp = fillArray[rndElem1 + j];
                        fillArray[rndElem1 + j] = fillArray[rndElem2 + j];
                        fillArray[rndElem2 + j] = temp;
                    }
                }
            }
            break;

            case DataTypeSelection.Sawtooth:
            {
                //Fill the drawing area with pixels representing a sawtooth
                int    toothSize = fillArray.Length / (concurrency * 4);
                byte[] tempArray = new byte[toothSize * 4];

                Random rnd = new Random(4);
                for (int i = 0, index = 0; i < toothSize; ++i)
                {
                    byte r = (byte)rnd.Next(256);
                    tempArray[index++] = r;
                    tempArray[index++] = r;
                    tempArray[index++] = r;
                    tempArray[index++] = 255;
                }
                Sorters.native_sort((int)SortSelection.StdSort, tempArray, (dimX * dimY) / concurrency, 1, 0);

                for (int i = 0; i < concurrency; ++i)
                {
                    Array.Copy(tempArray, 0, fillArray, i * tempArray.Length, tempArray.Length);
                }
            }
            break;
            }
        }