Exemplo n.º 1
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        public static IList <IList <string> > Permute(
            IList <string> list,
            ListTransformCallback callback
            )
        {
            IList <IList <string> > result = null;

            if (list != null)
            {
                IList <string> localList = new StringList(list); /* COPY */

                HandlePermuteResult(callback, localList, ref result);

                int   count   = localList.Count;
                int[] indexes = new int[count + 1];
                int   index1  = 1;

                while (index1 < count)
                {
                    if (indexes[index1] < index1)
                    {
                        int    index2    = index1 % 2 * indexes[index1];
                        string temporary = localList[index2];

                        localList[index2] = localList[index1];
                        localList[index1] = temporary;

                        HandlePermuteResult(callback, localList, ref result);

                        indexes[index1]++;
                        index1 = 1;
                    }
                    else
                    {
                        indexes[index1] = 0;
                        index1++;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        private static void HandlePermuteResult(
            ListTransformCallback callback,    /* in */
            IList <string> list,               /* in */
            ref IList <IList <string> > result /* in, out */
            )
        {
            if (list == null)
            {
                return;
            }

            if ((callback == null) || callback(list))
            {
                if (result == null)
                {
                    result = new List <IList <string> >();
                }

                result.Add(new StringList(list)); /* COPY */
            }
        }