Skip to content
Snippets Groups Projects
Commit 1e2997cd authored by Rico Possienka's avatar Rico Possienka
Browse files

add list extension to calc median

parent 7178bd69
No related branches found
No related tags found
No related merge requests found
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class EnumerableEx
{
static int[] indices = new int[1024];
//https://github.com/jianminchen/AlgorithmsPractice/blob/master/quickSortAlgorithm.cs
//left and right are inclusive
private static void Quick_Sort_indices<T>(IEnumerable<T> that, int[] indices, int left, int right, Func<T, T, int> comparer)
{
if (left > right || left < 0 || right < 0) return;
int index = partition(that, indices, left, right, comparer);
if (index != -1)
{
Quick_Sort_indices(that, indices, left, index - 1, comparer);
Quick_Sort_indices(that, indices, index + 1, right, comparer);
}
}
private static int partition<T>(IEnumerable<T> that, int[] indices, int left, int right, Func<T, T, int> comparer)
{
if (left > right) return -1;
int end = left;
T pivot = GetElement(that, indices, right); // choose last one to pivot, easy to code
for (int i = left; i < right; i++)
{
if (comparer( GetElement(that, indices,i), pivot) < 0)
{
swap(indices, i, end);
end++;
}
}
swap(indices, end, right);
return end;
}
private static void swap(int[] A, int left, int right)
{
int tmp = A[left];
A[left] = A[right];
A[right] = tmp;
}
private static T GetElement<T>(IEnumerable<T> enumerable, int[] indices, int index)
{
var array = enumerable as T[];
if (array != null)
{
return array[indices[index]];
}
var list = enumerable as List<T>;
if (list != null)
{
return list[indices[index]];
}
int i = 0;
foreach (var item in enumerable)
{
if (i == index) return item;
++i;
}
throw new ArgumentOutOfRangeException("index");
}
private static int SortIndices<T>(IEnumerable<T> that, Func<T, T, int> comparer)
{
int n = Count(that);
if (indices.Length < n)
{
indices = new int[indices.Length + n];
}
for (int i = 0; i != n; ++i)
{
indices[i] = i;
}
Quick_Sort_indices<T>(that, indices, 0, n-1, comparer);
return n;
}
public static int MedianIndex<T>(this IEnumerable<T> that, Func<T, T, int> comparer)
{
int n = SortIndices(that, comparer);
return indices[n / 2];
}
public static int MinIndex<T>(this IEnumerable<T> that, Func<T, T, int> comparer)
{
SortIndices(that, comparer);
return indices[0];
}
public static int MaxIndex<T>(this IEnumerable<T> that, Func<T, T, int> comparer)
{
int n = SortIndices(that, comparer);
return indices[n - 1];
}
private static int Count<T>(IEnumerable<T> enumerable)
{
var array = enumerable as T[];
if (array != null)
{
return array.Length;
}
var list = enumerable as List<T>;
if (list != null)
{
return list.Count;
}
int n = 0;
foreach (var item in enumerable)
{
++n;
}
return n;
}
}
fileFormatVersion: 2
guid: 2c37a18095b8a6b4bbacd9b6f5cc622c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment