//-----------------------------------------------------------------------
// // project="SteelGame"
// assembly="SteelGame"
// solution="Steel"
// company="Opuno">
// Copyright (c) Opuno. All rights reserved.
//
// Andreas Brekken
//
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Opuno.Steel.Utils
{
public static class Permutation
{
public static IEnumerable> WaysToMake(int components, int highestNumber)
{
var distinct = false;
var componentN = components - 1;
var current = new List();
for (int i = 0; i < components; i++)
{
current.Add(1);
}
while (true)
{
yield return current.ToList();
while (componentN > 0 && current[componentN] == (distinct ? current[componentN - 1] : highestNumber))
{
componentN--;
}
if (componentN == 0 && current[0] == highestNumber)
{
yield break;
}
current[componentN] += 1;
for (int i = componentN + 1; i < components; i++)
{
current[i] = 1;
}
componentN = components - 1;
}
}
public class ListEqualityComparer : IEqualityComparer>
{
public static readonly ListEqualityComparer Instance = new ListEqualityComparer();
public bool Equals(List x, List y)
{
if (x.Count != y.Count)
{
return false;
}
if (x == y)
{
return true;
}
for (int i = 0; i < x.Count; i++)
{
if (!x[i].Equals(y[i]))
{
return false;
}
}
return true;
}
public int GetHashCode(List obj)
{
var result = 0;
for (int i = 0; i < obj.Count; i++)
{
result ^= obj[i];
}
return result;
}
}
public static IEnumerable> DisregardOrder(IEnumerable> combinations)
{
return combinations.Select(combination => combination.OrderBy(k => k).ToList()).Distinct(ListEqualityComparer.Instance);
}
}
}