public static Collectible Table(Collectible source, SortBy by) { const int threshold = 100000; ConcurrentQueue <Task> tasks = new ConcurrentQueue <Task>(); void watch() { Task task; while (tasks.TryDequeue(out task)) { task.Wait(); } } void recurse(long low, long high) { if (high - low > threshold) { tasks.Enqueue(Task.Run(() => sort(low, high))); } else { sort(low, high); } } void sort(long low, long high) { long i = low, j = high; dynamic pivot = by.Extract(source, (low + high) / 2); while (i < j) { while (by.IsLessThan(by.Extract(source, i), pivot)) { i++; } while (by.IsLessThan(pivot, by.Extract(source, j))) { j--; } if (i <= j) { by.Swap(source, i++, j--); } } if (low < j) { recurse(low, j); } if (i < high) { recurse(i, high); } } sort(0, source.Count - 1); watch(); source.Flags.IsSorted = true; return(source); }
public void Swap(Collectible source, long left, long right) { by.Swap(source, left, right); }