Project Description
Software testing, games of chance and statistics all use the concepts of permutations and combinations. These libraries allow programmers to easily calculate the numbers of combinations, permutations and minimal sets for arbitrary lists. The libraries also allow programmers to easily generate combination and permutation lists.


Features
This library provides some simple tools to allow developers to use foreach semantics to loop over a logical collection that will output permutations or combinations of input lists

Permuations are like (list1 x list 2 x list3 ...).

Combininations are derived from a single list with a number of "picks". If you had an Enum "cards" with 52 cards and you wanted to know all the combiniations of two cards, you would create a combination on cards with Pick 2. Or if you had an enum with 5 permisions and you wanted to generate all the combinations permission possible you would call the OneToNPermutations collection to get them all from singles, pairwise and so on to all 5.

Both libraries work on any collection type and have special logic for converting Enum types into arrays internally for convience.

Goodies under the hood
There is a pretty good example here of how to implement multiple foreach semantics to a single collection. After the first one you are required to create a extra "wrapper" class to express IEnumerable in different ways and pair them with IEnumerator partners to loop over the collections.

Examples
Here is a short program that shows the features of the library and basic ways to use it.

using System;
using System.Collections.Generic;
using MS.CodePlex.PermutationTools;

namespace Sample
{
    class Program
    {



        static void Main(string[] args)
        {
            CombinationGeneratorSample();
            PermutationCollectionSample();
        }

        private static void PermutationCollectionSample()
        {
            PermutationCollection pg = new PermutationCollection();
            string[] stringParameters = { "default", "abcdef", "1234567890", "" };
            int[] intParameters = { 1, 3, 5 };

            pg.Add("PrimeNumber", intParameters);
            pg.Add("UserName", stringParameters);

            //automatically add all the members of an enum.
            pg.Add("GenericUriParserOption", typeof(System.GenericUriParserOptions));

            //Remove one of the enum members from an added enum.
            pg.RemoveListMember("GenericUriParserOption", System.GenericUriParserOptions.NoUserInfo);

            Console.WriteLine(pg.NumOfPermutations);

            //display every combination of paramters
            foreach (Dictionary<string, object> parameters in pg)
            {
                int prime = (int)parameters["PrimeNumber"];
                string username = (string)parameters["UserName"];
                System.GenericUriParserOptions option = (GenericUriParserOptions)parameters["GenericUriParserOption"];
                Console.WriteLine("Prime='{0}' UserName='{1}' Option='{2}'", prime, username, option);
            }
        }

        /* output:
        132
        Prime='1' UserName='default' Option='Default'
        Prime='3' UserName='default' Option='Default'
        Prime='5' UserName='default' Option='Default'
        Prime='1' UserName='abcdef' Option='Default'
        Prime='3' UserName='abcdef' Option='Default'
        Prime='5' UserName='abcdef' Option='Default'
        Prime='1' UserName='1234567890' Option='Default'
        Prime='3' UserName='1234567890' Option='Default'
        Prime='5' UserName='1234567890' Option='Default'
        Prime='1' UserName='' Option='Default'
        Prime='3' UserName='' Option='Default'
        Prime='5' UserName='' Option='Default'
        Prime='1' UserName='default' Option='GenericAuthority'
        Prime='3' UserName='default' Option='GenericAuthority'
        Prime='5' UserName='default' Option='GenericAuthority'
        Prime='1' UserName='abcdef' Option='GenericAuthority'
        Prime='3' UserName='abcdef' Option='GenericAuthority'
        Prime='5' UserName='abcdef' Option='GenericAuthority'
        Prime='1' UserName='1234567890' Option='GenericAuthority'
        Prime='3' UserName='1234567890' Option='GenericAuthority'
        Prime='5' UserName='1234567890' Option='GenericAuthority'
        Prime='1' UserName='' Option='GenericAuthority'
        Prime='3' UserName='' Option='GenericAuthority'
        Prime='5' UserName='' Option='GenericAuthority'
        ...
        */


        enum stuff
        {
            A,
            B,
            C,
            D,
            E
        }


        private static void CombinationGeneratorSample()
        {
            //get a list of everyone combinations of 3 items from the list.      
            CombinationGenerator<stuff> cg = new CombinationGenerator<stuff>(typeof(stuff), 3);
            Console.WriteLine(cg.NumOfCombinations);
            foreach (stuff[] oa in cg)
            {
                foreach (stuff o in oa)
                {
                    Console.Write("{0} ", o);
                }
                Console.WriteLine();
            }


            Console.WriteLine("----------------------------------");
            Console.WriteLine(cg.NumOfOneToNCombinations);
            //Get a list of all the combinations of 1, 2..N items from the list where N is the number of items in the list.
            foreach (stuff[] oa in cg.OneToNPermutations)
            {
                foreach (stuff o in oa)
                {
                    Console.Write("{0} ", o);
                }
                Console.WriteLine();
            }
        }
        /* Output:
        10
        A B C
        A B D
        A B E
        A C D
        A C E
        A D E
        B C D
        B C E
        B D E
        C D E
        --------------------------------
        31
        A
        B
        C
        D
        E
        E
        A B
        A C
        A D
        A E
        B C
        B D
        B E
        C D
        C E
        D E
        D E
        A B C
        A B D
        A B E
        A C D
        A C E
        A D E
        B C D
        B C E
        B D E
        C D E
        C D E
        A B C D
        A B C E
        A B D E
        A C D E
        B C D E
        B C D E
        A B C D E
        */
    }
}

Last edited Oct 25, 2008 at 1:43 AM by dustinandrews, version 7