[Quantproject-developers] QuantProject/b1_ADT/Statistics/Combinatorial Combination.cs, NONE, 1.1
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2006-08-21 19:45:08
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Statistics/Combinatorial In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv25350 Added Files: Combination.cs Log Message: Enumerates all combinations of integer values within a given range of integers --- NEW FILE: Combination.cs --- using System; using System.Collections; namespace QuantProject.ADT.Statistics.Combinatorial { /// <summary> /// Enumerates all combinations of integer values within a given /// range of integers /// </summary> public class Combination : IEnumerator { private int[] currentCombinationValues; private int minValue; private int maxValue; private int length; public int Length { get { return this.length; } } /// <summary> /// Enumerates all combinations of integer values within a given /// range of integers /// </summary> /// <param name="minValue">min value for combination elements</param> /// <param name="maxValue">max value for combination elements</param> /// <param name="length">combination length</param> public Combination( int minValue , int maxValue , int length ) { this.checkParameters( minValue , maxValue , length ); this.minValue = minValue; this.maxValue = maxValue; this.length = length; this.currentCombinationValues = new int[ length ]; } private void checkParameters( int minValue , int maxValue , int length ) { if ( minValue >= maxValue ) throw new Exception( "minValue is not less then maxValue!" ); if ( length <= 0 ) throw new Exception( "length is not greater than zero!" ); } #region Current public object Current { get { return this.getCurrent(); } } private int[] getCurrent() { return this.currentCombinationValues; } #endregion public void Reset() { for ( int currentComponentIndex = 0 ; currentComponentIndex < this.length ; currentComponentIndex ++ ) this.currentCombinationValues[ currentComponentIndex ] = this.minValue + currentComponentIndex; } #region MoveNext private int getMaxValueForElement( int elementIndex ) { int maxValueForElement = this.maxValue - ( this.length - 1 - elementIndex ); return maxValueForElement; } private bool hasNotReachedItsMaxValue( int indexToCheck ) { int maxValueToReach = this.getMaxValueForElement( indexToCheck ); return ( this.currentCombinationValues[ indexToCheck ] < maxValueToReach ); } private int getIndexOfTheFirstItemThatHasNotReachedItsMaxValue() { int indexOfTheFirstItemThatHasNotReachedItsMaxValue = -1 ; bool isIndexFound = false; int indexToCheck = this.length - 1; while ( ( !isIndexFound ) && ( indexToCheck >= 0 ) ) { if ( this.hasNotReachedItsMaxValue( indexToCheck ) ) { indexOfTheFirstItemThatHasNotReachedItsMaxValue = indexToCheck; isIndexFound = true; } else indexToCheck -- ; } return indexOfTheFirstItemThatHasNotReachedItsMaxValue; } private void moveNextActually( int indexOfTheFirstItemThatHasNotReachedItsMaxValue ) { this.currentCombinationValues[ indexOfTheFirstItemThatHasNotReachedItsMaxValue ] ++ ; for ( int i = indexOfTheFirstItemThatHasNotReachedItsMaxValue + 1 ; i < this.currentCombinationValues.Length; i++ ) { this.currentCombinationValues[ i ] = this.currentCombinationValues[ i - 1 ] + 1; } } public bool MoveNext() { bool isNotLastCombination = true; int indexOfTheFirstItemThatHasNotReachedItsMaxValue = this.getIndexOfTheFirstItemThatHasNotReachedItsMaxValue(); if ( indexOfTheFirstItemThatHasNotReachedItsMaxValue == -1 ) // no more combinations to be generated: every item has // reached its max value isNotLastCombination = false; else this.moveNextActually( indexOfTheFirstItemThatHasNotReachedItsMaxValue ); return isNotLastCombination; } #endregion public int GetValue( int elementIndex ) { return this.currentCombinationValues[ elementIndex ]; } } } |