Beispiel #1
0
        public static void Unrank(int n, long r, int[] pi)
        {
            if (n == 1)
            {
                return;
            }
            int s = (int)(r / KevinMath.Factorial(n - 1));

            ArrayUtility.Swap(pi, n - 1, s);
            Unrank(n - 1, r % KevinMath.Factorial(n - 1), pi);
        }
Beispiel #2
0
        public static long Rank(int n, int[] pi, int[] inv)
        {
            if (n == 1)
            {
                return(0);
            }

            int s = pi[n - 1];

            ArrayUtility.Swap(pi, n - 1, inv[n - 1]);
            ArrayUtility.Swap(inv, s, inv[n - 1]);
            return(s * KevinMath.Factorial(n - 1) + Rank(n - 1, pi, inv));
        }
        public void UnrankSingletonString()
        {
            string s = "x";
            int    n = s.Length;
            long   r = KevinMath.LongRandom(0, KevinMath.Factorial(n) - 1, new Random());

            int[] pi = RankUnrank.InitIndexArray(n);
            RankUnrank.Unrank(n, r, pi);

            string ps = GetPermString(s, pi);

            Console.WriteLine(string.Format("r:{0} s:{1}  p:{2}", r, s, ps));
        }
        public void UnrankStringLenThree()
        {
            string s = "xyz";
            int    n = s.Length;

            Console.WriteLine(string.Format("s:{0}", s));
            for (int r = 0; r < KevinMath.Factorial(n); r++)
            {
                int[] pi = RankUnrank.InitIndexArray(n);
                RankUnrank.Unrank(n, r, pi);

                string ps = GetPermString(s, pi);
                Console.WriteLine(string.Format("r:{0} p:{1}", r, ps));

                //int[] inv = RankUnrank.GetInverseArray(pi);
                //long rank = RankUnrank.Rank(n, pi, inv);
                //Assert.AreEqual(r, rank);
            }
        }