namespace Yuuna.Contracts.Patterns { using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using Yuuna.Contracts.Semantics; using System.Linq; using System.Diagnostics; public sealed class Pattern : IPattern { public IImmutableList SequentialKeys { get; private set; } private IImmutableList _groups; private readonly ImmutableArray.Builder _keyBuilder; private readonly ImmutableArray.Builder _groupBuilder; internal Pattern() { this._keyBuilder = ImmutableArray.CreateBuilder(); this._groupBuilder = ImmutableArray.CreateBuilder(); } internal void Add(IGroup g) { if (g == null) return; this._keyBuilder.Add(g.Key); this._groupBuilder.Add(g); } internal void Immute() { this.SequentialKeys = this._keyBuilder.ToImmutable(); this._groups = this._groupBuilder.ToImmutable(); } public int Count { get { Debug.Assert(this._groups != null); return this._groups.Count; } } public IImmutableList ToImmutable() { Debug.Assert(this._groups != null); return this._groups; } public bool Equals( IPattern other) { Debug.Assert(this.SequentialKeys != null); return this.SequentialKeys.SequenceEqual(other.SequentialKeys); } } }