/// <summary> /// Return the combination of p with this literal pattern /// </summary> /// <param name="p">the pattern to combine with</param> /// <returns>the combined pattern</returns> public Pattern Combine(Pattern p) { if (p is WildcardPattern) return this; else if (p is EmptyPattern) return p; else if (p is LiteralPattern) { LiteralPattern lp = p as LiteralPattern; return (((IComparable)MinValue).CompareTo(lp.Value) < 0 && ((IComparable)MaxValue).CompareTo(lp.Value) > 0) ? (Pattern)p : (Pattern)new EmptyPattern(); } else if (p is ListPattern) { ListPattern lp = p as ListPattern, lpNew = new ListPattern(); List<object> lilp = new List<object>(); for (int i = 0; i < lp.Values.Length; i++) { if (((IComparable)MinValue).CompareTo(lp.Values[i]) < 0 && ((IComparable)MaxValue).CompareTo(lp.Values[i]) > 0) lilp.Add(lp.Values[i]); } return (lilp.Count > 0) ? new ListPattern(lilp.ToArray()) : (Pattern)new EmptyPattern(); } else if (p is RangePattern) { RangePattern rp = p as RangePattern; if ((((IComparable)rp.MinValue).CompareTo(MaxValue) > 0) || (((IComparable)MinValue).CompareTo(rp.MaxValue) > 0)) return new EmptyPattern(); else { object oMin = (((IComparable)rp.MinValue).CompareTo(MinValue) > 0) ? rp.MinValue : MinValue; object oMax = (((IComparable)rp.MaxValue).CompareTo(MaxValue) < 0) ? rp.MaxValue : MaxValue; return new RangePattern(oMin, oMax); } } else return new EmptyPattern(); }
/// <summary> /// Merges two ListPatterns into a single listpattern /// </summary> /// <param name="lp1">The first list pattern to merge</param> /// <param name="lp2">The second list pattern to merge</param> public ListPattern(ListPattern lp1, ListPattern lp2) { sorted = lp1.sorted && lp2.sorted; int c1 = lp1.Values.Length, c2 = lp2.Values.Length, c = c1 + c2; alo = new object[c]; if (sorted) { int i1 = 0, i2 = 0; for (int i = 0; i < c; i++) { if (i1 >= c1) alo[i] = lp2.Values[i2++]; else if (i2 >= c2 || ((IComparable)lp1.Values[i1]).CompareTo(lp2.Values[i2]) < 0) alo[i] = lp1.Values[i1++]; else alo[i] = lp2.Values[i2++]; } } else { for (int i = 0; i < c1; i++) alo[i] = lp1.Values[i]; for (int i = 0; i < c2; i++) alo[lp1.Values.Length + i] = lp2.Values[i]; } }