Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 2.46 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Patterns
  7. {
  8.     /// <summary>
  9.     /// Represents a pattern node
  10.     /// </summary>
  11.     class PatternNode
  12.     {
  13.         private Dictionary<Dimension, ItemType> _terminators = new Dictionary<Dimension, ItemType>(DimensionEqualityComparer.Default);
  14.         // You may want to flip this to a dictionary instead - depending on how many items you have.
  15.         private PatternNode[] _nodes;
  16.         private static readonly int _nodesCount;
  17.  
  18.         static PatternNode()
  19.         {
  20.             var max = 0;
  21.             foreach (int val in Enum.GetValues(typeof(ItemType)))
  22.             {
  23.                 max = Math.Max(max, val);
  24.             }
  25.             _nodesCount = max + 1;
  26.         }
  27.  
  28.         /// <summary>
  29.         /// Gets the <see cref="Patterns.PatternNode"/> with the specified item type.
  30.         /// </summary>
  31.         public PatternNode this[ItemType next]
  32.         {
  33.             get
  34.             {
  35.                 if (_nodes == null)
  36.                     return null;
  37.                 return _nodes[(int)next];
  38.             }
  39.         }
  40.  
  41.         /// <summary>
  42.         /// Gets or sets the <see cref="Patterns.ItemType"/> with the specified dimension.
  43.         /// </summary>
  44.         public ItemType this[Dimension dimension]
  45.         {
  46.             get
  47.             {
  48.                 ItemType result;
  49.                 if (!_terminators.TryGetValue(dimension, out result))
  50.                     return ItemType.Nothing;
  51.                 return result;
  52.             }
  53.         }
  54.  
  55.         /// <summary>
  56.         /// Initializes a new instance of the <see cref="PatternNode"/> class.
  57.         /// </summary>
  58.         public PatternNode()
  59.         {
  60.         }
  61.  
  62.         /// <summary>
  63.         /// Adds the specified terminator.
  64.         /// </summary>
  65.         /// <param name="dimension">The dimension.</param>
  66.         /// <param name="next">Type of the item.</param>
  67.         public void Add(Dimension dimension, ItemType itemType)
  68.         {
  69.             _terminators.Add(dimension, itemType);
  70.         }
  71.  
  72.         /// <summary>
  73.         /// Creates or gets a node for the specified item type.
  74.         /// </summary>
  75.         /// <param name="next">The next item type.</param>
  76.         public PatternNode Add(ItemType next)
  77.         {
  78.             // Definitely not thread safe.
  79.             var nodes = _nodes ?? (_nodes = new PatternNode[_nodesCount]);
  80.             return _nodes[(int)next] ?? (_nodes[(int)next] = new PatternNode());
  81.         }
  82.     }
  83. }