Improve Util.Shuffle.

Reduce algorithmic complexity from O(n^2) to O(n).
This commit is contained in:
RoosterDragon
2016-02-13 22:19:46 +00:00
parent 2debda926a
commit 3a99111a42

View File

@@ -77,16 +77,21 @@ namespace OpenRA.Mods.Common
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)
{
var items = ts.ToList();
while (items.Count > 0)
// Fisher-Yates
var items = ts.ToArray();
for (var i = 0; i < items.Length - 1; i++)
{
var t = items.Random(random);
yield return t;
items.Remove(t);
var j = random.Next(items.Length - i);
var item = items[i + j];
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)