/// <summary> /// Initialize the combinations by settings a copy of the values from the /// </summary> /// <param name="values">List of values to select combinations from.</param> /// <param name="lowerIndex">The size of each combination set to return.</param> /// <param name="type">The type of Combinations set to generate.</param> /// <remarks> /// Copies the array and parameters and then creates a map of booleans that will /// be used by a permutations object to refence the subset. This map is slightly /// different based on whether the type is with or without repetition. /// /// When the type is WithoutRepetition, then a map of upper index elements is /// created with lower index false's. /// E.g. 8 choose 3 generates: /// Map: {1 1 1 1 1 0 0 0} /// Note: For sorting reasons, false denotes inclusion in output. /// /// When the type is WithRepetition, then a map of upper index - 1 + lower index /// elements is created with the falses indicating that the 'current' element should /// be included and the trues meaning to advance the 'current' element by one. /// E.g. 8 choose 3 generates: /// Map: {1 1 1 1 1 1 1 1 0 0 0} (7 trues, 3 falses). /// </remarks> private void Initialize(ICollection <T> values, Int32 lowerIndex, GenerateOption type) { Type = type; LowerIndex = lowerIndex; _values = new List <T>(); _values.AddRange(values); List <Boolean> map = new List <Boolean>(); if (type == GenerateOption.WithoutRepetition) { map.AddRange(_values.Select((t, i) => i < _values.Count - LowerIndex)); } else { for (Int32 i = 0; i < values.Count - 1; ++i) { map.Add(true); } for (Int32 i = 0; i < LowerIndex; ++i) { map.Add(false); } } _permutations = new Permutations <Boolean>(map); }
/// <summary> /// Construct a enumerator with the parent object. /// </summary> /// <param name="source">The source Permutations object.</param> public Enumerator(Permutations <T> source) { _parent = source; _lexicographicalOrders = new Int32[source._lexicographicOrders.Length]; source._lexicographicOrders.CopyTo(_lexicographicalOrders, 0); Reset(); }