/*======================================================================== Copyright (C) 2006 by Geir-Tore Lindsve, Torbjørn Meistad and Yngve Raudberget, hereby refered to as "the authors". All rights reserved Permission is hereby granted, without written agreement and without license or royalty fees, to use, reproduce, prepare derivative works, distribute, and display this software and its documentation for NONCOMMERCIAL RESEARCH AND EDUCATIONAL PURPOSES, provided that (1) the above copyright notice and the following two paragraphs appear in all copies of the source code and (2) redistributions, including without limitation binaries, reproduce these notices in the supporting documentation. IN NO EVENT SHALL THE AUTHORS, OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE AUTHORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. ========================================================================*/ using System; using System.Collections.Generic; using System.Collections; using System.Text; using Iesi.Collections; namespace Casper.Algorithm { /// /// The class which stores the information /// of a variable and its domaine values. Used by /// . /// public class LookAheadVarDom { private int varID; private Set domainValues; private Set removedValues; private IEnumerator domainEnumerator; private int? selectedValue; /// /// Initializes a new instance of the class. /// /// The variable ID. /// The domain values. public LookAheadVarDom(int varID, Set domainValues) { removedValues = new HybridSet(); this.varID = varID; this.domainValues = domainValues; domainEnumerator = domainValues.GetEnumerator(); } /// /// Gets og sets the variable ID. /// /// The variable ID. public int VarID { get { return varID; } set { varID = value; } } /// /// Gets or sets the domain values. /// /// The domain values. public Set DomainValues { get { return domainValues; } set { domainValues = value; domainEnumerator = domainValues.GetEnumerator(); } } /// /// Gets the current selected value of the variable /// /// public int? SelectedValue { get { return selectedValue; } } /// /// Gets the next value. The domain value ordering is ascending. /// Jumps over values which is added to the removed set by the /// method. /// /// If set to true, start from the beginning. /// public int? GetNextValue(Boolean startFromBeginning) { if (startFromBeginning) { domainEnumerator.Reset(); } while (domainEnumerator.MoveNext()) { int nextVal = (int)domainEnumerator.Current; selectedValue = nextVal; if (removedValues.Contains(selectedValue)) { continue; } return nextVal; } selectedValue = null; return null; } /// /// Adds a value to a set of removed values. /// The value is not removed from the domain values before /// is run. /// /// The value. public void AddToRemoved(int val) { removedValues.Add(val); } /// /// Updates the domain with removed values in the removed set. /// The removed set is emptied. /// public void UpdateDomainWithRemoved() { domainValues = (Set)domainValues.Minus(removedValues); domainEnumerator = domainValues.GetEnumerator(); removedValues.Clear(); } /// /// Method used by the algorithm to reset the domain during backtracking. /// /// The removed values which should be reset. public void ResetDomain(Set removedVals) { domainValues = (Set) domainValues.Union(removedVals); domainEnumerator = domainValues.GetEnumerator(); removedVals.Clear(); } /// /// Gets the domain size. /// /// The domain size. public int DomainSize() { return domainValues.Count; } } }