Exemplo n.º 1
0
 /// <summary>
 /// Construct a enumerator with the parent object.
 /// </summary>
 /// <param name="source">The source Permutations object.</param>
 public Enumerator(Permutations <T> source)
 {
     _         = source ?? throw new ArgumentNullException(nameof(source));
     _myParent = source;
     _myLexicographicalOrders = new int[source._myLexicographicOrders.Length];
     _myValues = new List <T>(source._myValues.Count);
     source._myLexicographicOrders.CopyTo(_myLexicographicalOrders, 0);
     _myPosition = Position.BeforeFirst;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Create a combination set from the provided list of values.
        /// The upper index is calculated as values.Count, the lower index is specified.
        /// </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>
        public Combinations(IEnumerable <T> values, int lowerIndex, GenerateOption type)
        {
            _ = values ?? throw new ArgumentNullException(nameof(values));

            // Copy the array and parameters and then create a map of booleans that will
            // be used by a permutations object to reference 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).

            Type       = type;
            LowerIndex = lowerIndex;
            _myValues  = values.ToList();
            List <bool> myMap;

            if (type == GenerateOption.WithoutRepetition)
            {
                myMap = new List <bool>(_myValues.Count);
                myMap.AddRange(_myValues.Select((t, i) => i < _myValues.Count - LowerIndex));
            }
            else
            {
                myMap = new List <bool>(_myValues.Count + LowerIndex - 1);
                for (var i = 0; i < _myValues.Count - 1; ++i)
                {
                    myMap.Add(true);
                }
                for (var i = 0; i < LowerIndex; ++i)
                {
                    myMap.Add(false);
                }
            }

            _myPermutations = new Permutations <bool>(myMap);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create a variation set from the indicated list of values.
        /// The upper index is calculated as values.Count, the lower index is specified.
        /// </summary>
        /// <param name="values">List of values to select variations from.</param>
        /// <param name="lowerIndex">The size of each variation set to return.</param>
        /// <param name="type">Type indicates whether to use repetition in set generation.</param>
        public Variations(IEnumerable <T> values, int lowerIndex, GenerateOption type)
        {
            Type       = type;
            LowerIndex = lowerIndex;
            _myValues  = values.ToList();

            if (type != GenerateOption.WithoutRepetition)
            {
                return;
            }

            var myMap = new List <int>(_myValues.Count);
            var index = 0;

            for (var i = 0; i < _myValues.Count; ++i)
            {
                myMap.Add(i >= _myValues.Count - LowerIndex ? index++ : int.MaxValue);
            }

            _myPermutations = new Permutations <int>(myMap);
        }