Britbot
ExpIterator.cs
Go to the documentation of this file.
00001 #region #Usings
00002 
00003 using System;
00004 using System.Linq;
00005 
00006 #endregion
00007 
00008 namespace Britbot
00009 {
00015     internal class ExpIterator
00016     {
00017         #region Fields & Properies
00018 
00022         public int[] Dimensions { get; private set; }
00023 
00027         public int[] Values { get; set; }
00028 
00029         #endregion
00030 
00031         #region Constructors & Initializers
00032 
00038         public ExpIterator(int[] dims)
00039         {
00040             //set the given dimensions
00041 
00042             this.Dimensions = dims;
00043 
00044             //check if dimensions are legal (meaning strictly positive)
00045             if (Dimensions.Any(dim => dim <= 0))
00046             {
00047                 throw new InvalidIteratorDimensionException("Dimensions must be strictly positive");
00048             }
00049 
00050             //initiate count at zero
00051             this.Values = new int[dims.Length];
00052             for (int i = 0; i < this.Values.Length; i++)
00053             {
00054                 Values[i] = 0;
00055             }
00056         }
00057 
00058         #endregion
00059 
00064         public bool IsZero()
00065         {
00066             //going over the list searching for nonzero
00067             foreach (int value in this.Values)
00068             {
00069                 if (value != 0)
00070                     return false;
00071             }
00072 
00073             //if here it means that all the entries are zero
00074             return true;
00075         }
00076 
00083         public bool NextIteration()
00084         {
00085             //going over the vector entries
00086             for (int i = 0; i < this.Values.Length; i++)
00087             {
00088                 //if we don't need to go over the top of the dimension just add
00089 
00090                 if (this.Values[i] < this.Dimensions[i] - 1)
00091                 {
00092                     this.Values[i]++;
00093                     break;
00094                 }
00095                 this.Values[i] = 0;
00096             }
00097 
00098             //check if we are back at zero
00099             return !this.IsZero();
00100         }
00101 
00106         public override string ToString()
00107         {
00108             return "Dimensions: " + Dimensions + "\n" + "MultiIndex: " + Values;
00109         }
00110     }
00111 }