All pastes #791247 Raw Edit

tobz

public text v1 · immutable
·published 2007-11-20 20:19 UTC
rendered paste body
public void FlagChange(StatTypes statType)
		{
			StatCollection oldCollection;
			StatCollection newCollection;
			List<StatChange> flaggedChanges;
				
				do {
					oldCollection = m_statCollection;

					if(!oldCollection.IsChangeFlagged(statType))
					{
						flaggedChanges = oldCollection.Changes;
						flaggedChanges.Add(new StatChange(statType));
					}

					newCollection = new StatCollection(oldCollection.Stats, flaggedChanges);
				} while (Interlocked.CompareExchange(ref m_statCollection, newCollection, oldCollection) != oldCollection);
		}


public class StatCollection
	{
		private Dictionary<StatCollectionType, Dictionary<StatTypes, double>> m_stats;
		private ReadOnlyCollection<StatChange> m_changes;

		public StatCollection(IDictionary<StatCollectionType, IDictionary<StatTypes, double>> stats, IList<StatChange> changes)
		{
			m_stats = stats;
			m_changes = changes;
		}

		public IDictionary<StatCollectionType, IDictionary<StatTypes, double>> Stats
		{
			get
			{
				return new Dictionary<StatCollectionType, Dictionary<StatTypes, double>>(m_stats);
			}
		}

		public IList<StatChange> Changes
		{
			get
			{
				return new ReadOnlyCollection<StatChange>(m_changes);
			}
		}

		/// <summary>
		/// Gets the value for a specific <see cref="StatTypes" /> and a specific <see cref="StatCollectionType" />. 
		/// </summary>
		/// <param name="collectionType">the collection type</param>
		/// <param name="statType">the stat type</param>
		/// <returns>the value for the given collection type/stat type</returns>
		public double GetValue(StatCollectionType collectionType, StatTypes statType)
		{
			return m_stats[collectionType][statType];
		}

		/// <summary>
		/// Checks if a change is flagged in the collection.
		/// </summary>
		/// <param name="statType">the stat type</param>
		/// <returns>true if the stat type is flagged; false otherwise</returns>
		public bool IsChangeFlagged(StatTypes statType)
		{
			return m_changes.Contains(statType);
		}
	}