//--------------------------------------------------------------------- /// <summary> /// Initializes a new instance for a site on a landscape. /// </summary> /// <param name="landscape"> /// The landscape where the site is located. /// </param> /// <param name="locationAndIndex"> /// The location of the site, and the index of its data for site /// variables. /// </param> internal protected Site(ILandscape landscape, LocationAndIndex locationAndIndex) { Debug.Assert(landscape.IsValid(locationAndIndex.Location)); this.landscape = landscape; this.locationAndIndex = locationAndIndex; }
//--------------------------------------------------------------------- /// <summary> /// Initializes a new instance for a site on a landscape. /// </summary> /// <param name="landscape"> /// The landscape where the site is located. /// </param> /// <param name="location"> /// The location of the site. /// </param> /// <param name="dataIndex"> /// The index of the site's data for site variables. /// </param> internal protected Site(ILandscape landscape, Location location, uint dataIndex) { Debug.Assert(landscape.IsValid(location)); this.landscape = landscape; this.locationAndIndex = new LocationAndIndex(location, dataIndex); }
//--------------------------------------------------------------------- internal ActiveSiteEnumerator(ILandscape landscape, ActiveSiteMap activeSiteMap) { locationAndIndex = new LocationAndIndex(); mapEtor = activeSiteMap.GetEnumerator(); mapEtor.UseForCurrentEntry(locationAndIndex); if (landscape.ActiveSiteCount > 0) { // Get location of first active site so we have a valid // location for mutable active site ctor. mapEtor.MoveNext(); currentSite = new MutableActiveSite(landscape, locationAndIndex); mapEtor.Reset(); } }
//--------------------------------------------------------------------- /// <summary> /// Gets the next active site in row-major order. /// </summary> /// <param name="site"> /// Current site's location and data index. /// </param> /// <returns> /// true if the next active site's location and data index have been /// assigned to the site parameter. false if there are no more active /// sites. /// </returns> public bool GetNextActive(ref LocationAndIndex site) { Location nextLoc = RowMajor.Next(site.Location, columns); while (nextLoc.Row <= rows) { uint index = indexes[nextLoc.Row - 1, nextLoc.Column - 1]; if (index != InactiveSiteDataIndex) { site.Location = nextLoc; site.Index = index; return(true); } nextLoc = RowMajor.Next(nextLoc, columns); } return(false); }
//--------------------------------------------------------------------- /// <summary> /// Gets the next active site in row-major order. /// </summary> /// <param name="site"> /// Current site's location and data index. /// </param> /// <returns> /// true if the next active site's location and data index have been /// assigned to the site parameter. false if there are no more active /// sites. /// </returns> public bool GetNextActive(ref LocationAndIndex site) { Location nextLoc = RowMajor.Next(site.Location, columns); while (nextLoc.Row <= rows) { uint index = indexes[nextLoc.Row-1, nextLoc.Column-1]; if (index != InactiveSiteDataIndex) { site.Location = nextLoc; site.Index = index; return true; } nextLoc = RowMajor.Next(nextLoc, columns); } return false; }
//--------------------------------------------------------------------- private void Initialize(IInputGrid<bool> activeSites) { this.firstActive = null; this.firstInactive = null; this.rows = activeSites.Rows; this.columns = activeSites.Columns; this.indexes = new uint[this.rows, this.columns]; this.count = 0; for (uint row = 0; row < this.rows; ++row) { for (uint column = 0; column < this.columns; ++column) { if (activeSites.ReadValue()) { this.count++; this.indexes[row, column] = this.count; if (this.firstActive == null) this.firstActive = new LocationAndIndex(new Location(row+1, column+1), this.count); } else { if (this.firstInactive == null) this.firstInactive = new LocationAndIndex(new Location(row+1, column+1), InactiveSiteDataIndex); } } // for each column } // for each row if (logger.IsDebugEnabled) { LogDebug("Active Site Map"); LogDebug(""); LogDebug("Input Grid: {0}", activeSites.Dimensions); LogDebug(""); if (firstActive == null) LogDebug("First Active: null"); else LogDebug("First Active: {0} {1}", firstActive.Location, firstActive.Index); if (firstInactive == null) LogDebug("First Inactive: null"); else LogDebug("First Inactive: {0} {1}", firstInactive.Location, firstInactive.Index); LogDebug(""); StringBuilder line = new StringBuilder(8 * (int) this.columns); line.Append("Column:"); for (int column = 1; column <= this.columns; column++) line.Append('\t').Append(column); LogDebug(line.ToString()); LogDebug("Row"); for (int row = 0; row < this.rows; row++) { line.Remove(0, line.Length); line.Append(row + 1); for (int column = 0; column < this.columns; column++) line.Append('\t').Append(indexes[row, column]); LogDebug(line.ToString()); } } }
public void EnumeratorWithEntryVar() { Assert.AreEqual(activeSites.Count, map.Count); LocationAndIndex myEntry = new LocationAndIndex(); ActiveSiteMapEnumerator mapEtor = map.GetEnumerator(); System.Type type = typeof(ActiveSiteMapEnumerator); MethodInfo useForCurrentEntryMethodInfo = type.GetMethod("UseForCurrentEntry", BindingFlags.Instance | BindingFlags.NonPublic); useForCurrentEntryMethodInfo.Invoke(mapEtor, new object[]{myEntry}); int index = 0; while (mapEtor.MoveNext()) { index++; Assert.IsTrue(index <= map.Count); Assert.AreSame(myEntry, mapEtor.Current); Assert.AreEqual(index, myEntry.Index); Assert.AreEqual(activeSites[index-1], myEntry.Location); } Assert.AreEqual(map.Count, index); }
//--------------------------------------------------------------------- /// <summary> /// Initializes a new instance for a site on a landscape. /// </summary> /// <param name="landscape"> /// The landscape where the site is located. /// </param> /// <param name="location"> /// The location of the site. /// </param> /// <param name="dataIndex"> /// The index of the site's data for site variables. /// </param> internal protected Site(ILandscape landscape, Location location, uint dataIndex) { Debug.Assert( landscape.IsValid(location) ); this.landscape = landscape; this.locationAndIndex = new LocationAndIndex(location, dataIndex); }
//--------------------------------------------------------------------- /// <summary> /// Initializes a new instance for a site on a landscape. /// </summary> /// <param name="landscape"> /// The landscape where the site is located. /// </param> /// <param name="locationAndIndex"> /// The location of the site, and the index of its data for site /// variables. /// </param> internal protected Site(ILandscape landscape, LocationAndIndex locationAndIndex) { Debug.Assert( landscape.IsValid(locationAndIndex.Location) ); this.landscape = landscape; this.locationAndIndex = locationAndIndex; }
public void EnumeratorWithEntryVar() { Assert.AreEqual(activeSites.Count, map.Count); LocationAndIndex myEntry = new LocationAndIndex(); ActiveSiteMapEnumerator mapEtor = map.GetEnumerator(); mapEtor.UseForCurrentEntry(myEntry); int index = 0; while (mapEtor.MoveNext()) { Assert.IsTrue(index < map.Count); Assert.AreSame(myEntry, mapEtor.Current); Assert.AreEqual(index, myEntry.Index); Assert.AreEqual(activeSites[index], myEntry.Location); index++; } Assert.AreEqual(map.Count, index); }
//--------------------------------------------------------------------- internal ActiveSite(ILandscape landscape, LocationAndIndex locationAndIndex) : base(landscape, locationAndIndex) { }
//--------------------------------------------------------------------- private void Initialize(IInputGrid <bool> activeSites) { this.firstActive = null; this.firstInactive = null; this.rows = activeSites.Rows; this.columns = activeSites.Columns; this.indexes = new uint[this.rows, this.columns]; this.count = 0; for (uint row = 0; row < this.rows; ++row) { for (uint column = 0; column < this.columns; ++column) { if (activeSites.ReadValue()) { this.count++; this.indexes[row, column] = this.count; if (this.firstActive == null) { this.firstActive = new LocationAndIndex(new Location(row + 1, column + 1), this.count); } } else { if (this.firstInactive == null) { this.firstInactive = new LocationAndIndex(new Location(row + 1, column + 1), InactiveSiteDataIndex); } } } // for each column } // for each row if (logger.IsDebugEnabled) { LogDebug("Active Site Map"); LogDebug(""); LogDebug("Input Grid: {0}", activeSites.Dimensions); LogDebug(""); if (firstActive == null) { LogDebug("First Active: null"); } else { LogDebug("First Active: {0} {1}", firstActive.Location, firstActive.Index); } if (firstInactive == null) { LogDebug("First Inactive: null"); } else { LogDebug("First Inactive: {0} {1}", firstInactive.Location, firstInactive.Index); } LogDebug(""); StringBuilder line = new StringBuilder(8 * (int)this.columns); line.Append("Column:"); for (int column = 1; column <= this.columns; column++) { line.Append('\t').Append(column); } LogDebug(line.ToString()); LogDebug("Row"); for (int row = 0; row < this.rows; row++) { line.Remove(0, line.Length); line.Append(row + 1); for (int column = 0; column < this.columns; column++) { line.Append('\t').Append(indexes[row, column]); } LogDebug(line.ToString()); } } }