Improve Util.Shuffle.
Reduce algorithmic complexity from O(n^2) to O(n).
This commit is contained in:
@@ -77,16 +77,21 @@ namespace OpenRA.Mods.Common
|
|||||||
return WPos.Lerp(w.Map.CenterOfCell(from), w.Map.CenterOfCell(to), 1, 2);
|
return WPos.Lerp(w.Map.CenterOfCell(from), w.Map.CenterOfCell(to), 1, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* pretty crap */
|
|
||||||
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> ts, MersenneTwister random)
|
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> ts, MersenneTwister random)
|
||||||
{
|
{
|
||||||
var items = ts.ToList();
|
// Fisher-Yates
|
||||||
while (items.Count > 0)
|
var items = ts.ToArray();
|
||||||
|
for (var i = 0; i < items.Length - 1; i++)
|
||||||
{
|
{
|
||||||
var t = items.Random(random);
|
var j = random.Next(items.Length - i);
|
||||||
yield return t;
|
var item = items[i + j];
|
||||||
items.Remove(t);
|
items[i + j] = items[i];
|
||||||
|
items[i] = item;
|
||||||
|
yield return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (items.Length > 0)
|
||||||
|
yield return items[items.Length - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
static IEnumerable<CPos> Neighbours(CPos c, bool allowDiagonal)
|
static IEnumerable<CPos> Neighbours(CPos c, bool allowDiagonal)
|
||||||
|
|||||||
Reference in New Issue
Block a user