Exemple #1
0
        //public static IEnumerable<ISorterStage> ReplaceAtIndex(this IEnumerable<ISorterStage> source, int index,
        //    ISorterStage replacement)
        //{
        //    var lst = source.ToList();
        //    lst[index] = replacement;
        //    return lst;
        //}

        public static Tuple <bool, IPermutation> Sort(this ISorterStage stage, IPermutation perm)
        {
            var aRet = new uint[stage.Order];

            for (uint i = 0; i < stage.Order; i++)
            {
                aRet[i] = perm[i];
            }

            var wasUsed = false;

            for (uint i = 0; i < stage.Order; i++)
            {
                var m = stage[i];
                if (m > i)
                {
                    var llv = perm[i];
                    var hlv = perm[m];
                    if (llv > hlv)
                    {
                        aRet[m] = llv;
                        aRet[i] = hlv;
                        wasUsed = true;
                    }
                }
            }

            return(new Tuple <bool, IPermutation>(wasUsed, PermutationEx.MakePermutation(aRet)));
        }
Exemple #2
0
        public static IPermutation ToPermutation(this StageBits2 stageBits)
        {
            var permArray    = 0u.CountUp(stageBits.Order).ToArray();
            var sbScratchPad = stageBits.ToSbScratchPad2();

            //System.Diagnostics.Debug.WriteLine(sbScratchPad.PrintOverlaps());
            //System.Diagnostics.Debug.WriteLine(sbScratchPad.PrintUsed());

            while (!sbScratchPad.IsSpent())
            {
                var dSbSp = sbScratchPad.BestOnDiag();
                dSbSp.IsUsed = true;
                var bSpSp = sbScratchPad.BestOnCol(dSbSp.Col);
                permArray[bSpSp.Col] = bSpSp.Row;
                permArray[bSpSp.Row] = bSpSp.Col;
                sbScratchPad.Mark(bSpSp, stageBits.Order);

                //  System.Diagnostics.Debug.WriteLine(sbScratchPad.PrintUsed());
            }

            return(PermutationEx.MakePermutation(permArray));
        }
Exemple #3
0
 public static IPermutation GetPermutation(this ISortable sortable)
 {
     return(PermutationEx.MakePermutation(
                sortable.GetMap()
                ));
 }