/// <summary> /// Sorts lights "closest" to the specified geometry that will affect it the most, only if the number of lights /// contained in the state exceeds the set maximum light count. /// </summary> /// <param name="renderable">Renderable object that the lights can be sorted against based on distance</param> public void SortLights(IRenderable renderable) { //If we have less than the max lights, then no reason to sort if (_lightCount > _maxLights) { Spatial spatial = renderable as Spatial; if (spatial == null) { return; } _comparer.SetSpatial(spatial); if (_tempLights == null || _tempLights.Length < _lights.Length) { _tempLights = (Light[])_lights.Clone(); } else { Array.Copy(_lights, _tempLights, _lightCount); } SortHelper.MergeSort <Light>(_tempLights, _lights, 0, _lightCount, 0, _comparer); } //However if we're sorting lights, most likely a light has changed, so refresh the shader either way. _refreshShader = true; }
public void MergeSort_ParticularSort() { int[] array = new int[] { 10, 4, 2, 6, 3, 9, 1 }; int[] expected = new int[] { 10, 4, 2, 3, 6, 9, 1 }; SortHelper.MergeSort(array, 2, array.Length - 2); Assert.AreEqual(expected, array); }
public void MergeSort_SimpleSort() { int[] array = new int[] { 10, 4, 2, 6, 3, 9, 1 }; int[] expected = new int[] { 1, 2, 3, 4, 6, 9, 10 }; SortHelper.MergeSort(array, 0, array.Length - 1); Assert.AreEqual(expected, array); }
public void UpdateOrders() { fOrderedColumns.Clear(); if (fColumns.Count > 0) { foreach (var column in fColumns) { fOrderedColumns.Add(column); } SortHelper.MergeSort(fOrderedColumns, CompareItems); } }
/// <summary> /// Sample which takes an array as an input and applies MergeSort Sorting algorithm to sort it. /// </summary> private void MergeSortSample() { var randomArr = Helper.RandomArray(10); Console.WriteLine("Merge Sort Implementation..."); Console.WriteLine($"Input: {string.Join(',', randomArr)}"); var sortedResult = SortHelper.MergeSort(randomArr); Console.WriteLine($"Sorted Result: {string.Join(',', sortedResult)}"); }
/*protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e) * { #if DEFAULT_HEADER * * e.DrawDefault = true; * #else * * using (var sf = new StringFormat()) * { * Graphics gfx = e.Graphics; * Rectangle rt = e.Bounds; * #if !__MonoCS__ * VisualStyleElement element = VisualStyleElement.Header.Item.Normal; * if ((e.State & ListViewItemStates.Hot) == ListViewItemStates.Hot) * element = VisualStyleElement.Header.Item.Hot; * if ((e.State & ListViewItemStates.Selected) == ListViewItemStates.Selected) * element = VisualStyleElement.Header.Item.Pressed; * * var visualStyleRenderer = new VisualStyleRenderer(element); * visualStyleRenderer.DrawBackground(gfx, rt); #else * e.DrawBackground(); #endif * * switch (e.Header.TextAlign) * { * case HorizontalAlignment.Left: * sf.Alignment = StringAlignment.Near; * break; * * case HorizontalAlignment.Right: * sf.Alignment = StringAlignment.Far; * break; * * case HorizontalAlignment.Center: * sf.Alignment = StringAlignment.Center; * break; * } * * sf.LineAlignment = StringAlignment.Center; * sf.Trimming = StringTrimming.EllipsisCharacter; * sf.FormatFlags = StringFormatFlags.NoWrap; * * int w = TextRenderer.MeasureText(" ", Font).Width; * rt.Inflate(-(w / 5), 0); * * gfx.DrawString(e.Header.Text, Font, Brushes.Black, rt, sf); * * string arrow = ""; * switch (GetColumnSortOrder(e.ColumnIndex)) { * case SortOrder.Ascending: * arrow = "▲"; * break; * case SortOrder.Descending: * arrow = "▼"; * break; * } * * if (arrow != "") { * using (var fnt = new Font(Font.FontFamily, Font.SizeInPoints * 0.6f, FontStyle.Regular)) { * float aw = gfx.MeasureString(arrow, fnt).Width; * float x = rt.Left + (rt.Width - aw) / 2.0f; * gfx.TextRenderingHint = TextRenderingHint.AntiAlias; * gfx.DrawString(arrow, fnt, Brushes.Black, x, rt.Top); * } * } * } * #endif * * base.OnDrawColumnHeader(e); * }*/ /*protected override void OnCellFormatting(GridCellFormatEventArgs e) * { * if (e.Row == fItems.IndexOf((GKListItem)SelectedItem)) { * e.BackgroundColor = SystemColors.Highlight; * e.ForegroundColor = Colors.White; * } else { * var item = e.Item as GKListItem; * if (item != null) { * if (item.BackColor != Colors.Transparent) { * e.BackgroundColor = item.BackColor; * e.ForegroundColor = Colors.Black; * } else { * e.BackgroundColor = Colors.White; * e.ForegroundColor = Colors.Black; * } * } * } * * base.OnCellFormatting(e); * }*/ private void SortContents() { if (fSorting) { if (fListMan != null) { fListMan.SortContents(fSortColumn, fSortOrder == BSDTypes.SortOrder.Ascending); } else { SortHelper.MergeSort(fItems, CompareItems); } } }
// In Eto not exists /*protected override void OnColumnWidthChanged(ColumnWidthChangedEventArgs e) * { * if (fListMan != null && fUpdateCount == 0) { * fListMan.ChangeColumnWidth(e.ColumnIndex, Columns[e.ColumnIndex].Width); * } * * base.OnColumnWidthChanged(e); * }*/ private void SortContents(bool restoreSelected) { if (fSorting) { if (fListMan != null) { fListMan.SortContents(fSortColumn, fSortOrder == BSDSortOrder.Ascending); } else { SortHelper.MergeSort(fItems, CompareItems); } } }
/// <summary> /// Sorts all renderables in the bucket. /// </summary> public void Sort() { //Where the magic happens if (_currentListCount > 1) { //Clone if null or length is less than our current list if (_tempList == null || _tempList.Length < _currentList.Length) { _tempList = (IRenderable[])_currentList.Clone(); } else { Array.Copy(_currentList, _tempList, _currentListCount); } //Merge sort SortHelper.MergeSort <IRenderable>(_tempList, _currentList, 0, _currentListCount, 0, _comparer); } }
private int FindMedian(int[] array, int startIndex, int endIndex) { int size = endIndex - startIndex + 1; int[] subset = new int[size]; int j = 0; for (int i = startIndex; i <= endIndex; i++) { subset[j] = array[i]; j++; } subset = SortHelper.MergeSort(subset); if (size % 2 == 0) { return(subset[size / 2] + subset[(size / 2) - 1]); } else { return(subset[size / 2]); } }