I got some code from a thread I found from StackOverflow, but it is throwing some strange errors. I’ve tried all the fixes I could find, but they didn’t work.
IReadOnlyList is from the System.Collections.Generic namespace, but its not working for some reason.
Anyone able to shed some light on this issue? The code in question finds permutations for an array.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public static class permutor {
public static IEnumerable<IEnumerable<T>> GetPermutations<T>(this IEnumerable<T> enumerable)
{
var array = enumerable as T[] ?? enumerable.ToArray();
var factorials = Enumerable.Range(0, array.Length + 1)
.Select(Factorial)
.ToArray();
for (var i = 0L; i < factorials[array.Length]; i++)
{
// Error CS1503 Argument 3: cannot convert from 'long[]' to 'IReadOnlyList<long>'
var sequence = GenerateSequence(i, array.Length - 1, factorials);
// Error CS1503 Argument 3: cannot convert from 'int[]' to 'IReadOnlyList<int>'
yield return GeneratePermutation(array, sequence);
}
}
//Error CS0246 The type or namespace name 'IReadOnlyList<int>' could not be found (are you missing a using directive or an assembly reference?)
private static IEnumerable<T> GeneratePermutation<T>(T[] array, IReadOnlyList<int> sequence)
{
var clone = (T[])array.Clone();
for (int i = 0; i < clone.Length - 1; i++)
{
Swap(ref clone[i], ref clone[i + sequence[i]]);
}
return clone;
}
//Error CS0246 The type or namespace name 'IReadOnlyList<long>' could not be found (are you missing a using directive or an assembly reference?)
private static int[] GenerateSequence(long number, int size, IReadOnlyList<long> factorials)
{
var sequence = new int[size];
for (var j = 0; j < sequence.Length; j++)
{
var facto = factorials[sequence.Length - j];
sequence[j] = (int)(number / facto);
number = (int)(number % facto);
}
return sequence;
}
static void Swap<T>(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}
private static long Factorial(int n)
{
long result = n;
for (int i = 1; i < n; i++)
{
result = result * i;
}
return result;
}
}