Pattern.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 
  2. namespace Yuuna.Contracts.Patterns
  3. {
  4. using System.Collections.Immutable;
  5. using System.Diagnostics.CodeAnalysis;
  6. using Yuuna.Contracts.Semantics;
  7. using System.Linq;
  8. using System.Diagnostics;
  9. public sealed class Pattern : IPattern
  10. {
  11. public IImmutableList<string> SequentialKeys { get; private set; }
  12. private IImmutableList<IGroup> _groups;
  13. private readonly ImmutableArray<string>.Builder _keyBuilder;
  14. private readonly ImmutableArray<IGroup>.Builder _groupBuilder;
  15. internal Pattern()
  16. {
  17. this._keyBuilder = ImmutableArray.CreateBuilder<string>();
  18. this._groupBuilder = ImmutableArray.CreateBuilder<IGroup>();
  19. }
  20. internal void Add(IGroup g)
  21. {
  22. if (g == null)
  23. return;
  24. this._keyBuilder.Add(g.Key);
  25. this._groupBuilder.Add(g);
  26. }
  27. internal void Immute()
  28. {
  29. this.SequentialKeys = this._keyBuilder.ToImmutable();
  30. this._groups = this._groupBuilder.ToImmutable();
  31. }
  32. public int Count
  33. {
  34. get
  35. {
  36. Debug.Assert(this._groups != null);
  37. return this._groups.Count;
  38. }
  39. }
  40. public IImmutableList<IGroup> ToImmutable()
  41. {
  42. Debug.Assert(this._groups != null);
  43. return this._groups;
  44. }
  45. public bool Equals( IPattern other)
  46. {
  47. Debug.Assert(this.SequentialKeys != null);
  48. return this.SequentialKeys.SequenceEqual(other.SequentialKeys);
  49. }
  50. }
  51. }