/* Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using C5; using System; using System.Reflection; using System.Reflection.Emit; using System.Diagnostics; namespace C5 { struct RecConst { public const int HASHFACTOR = 387281; } /// /// A generic record type with two fields. /// /// Equality is defined field by field, using the Equals method /// inherited from System.Object (i.e. using ). /// /// /// This type is similar to , but the latter /// uses to define field equality instead of . /// /// /// /// public struct Rec : IEquatable>, IShowable { /// /// /// public readonly T1 X1; /// /// /// public readonly T2 X2; /// /// /// /// /// [Tested] public Rec(T1 x1, T2 x2) { this.X1 = x1; this.X2 = x2; } /// /// /// /// /// [Tested] public bool Equals(Rec other) { return (X1 == null ? other.X1 == null : X1.Equals(other.X1)) && (X2 == null ? other.X2 == null : X2.Equals(other.X2)) ; } /// /// /// /// /// [Tested] public override bool Equals(object obj) { return obj is Rec ? Equals((Rec)obj) : false; } /// /// /// /// /// /// [Tested] public static bool operator ==(Rec record1, Rec record2) { return record1.Equals(record2); } /// /// /// /// /// /// [Tested] public static bool operator !=(Rec record1, Rec record2) { return !record1.Equals(record2); } /// /// /// /// [Tested] public override int GetHashCode() { //TODO: don't use 0 as hashcode for null, but something else! int hashcode = X1 == null ? 0 : X1.GetHashCode(); hashcode = hashcode * RecConst.HASHFACTOR + (X2 == null ? 0 : X2.GetHashCode()); return hashcode; } /// /// /// /// public override string ToString() { return String.Format("({0}, {1})", X1, X2); } #region IShowable Members /// /// /// /// /// /// /// public bool Show(System.Text.StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) { bool incomplete = true; stringbuilder.Append("("); rest -= 2; try { if (incomplete = !Showing.Show(X1, stringbuilder, ref rest, formatProvider)) return false; stringbuilder.Append(", "); rest -= 2; if (incomplete = !Showing.Show(X2, stringbuilder, ref rest, formatProvider)) return false; } finally { if (incomplete) { stringbuilder.Append("..."); rest -= 3; } stringbuilder.Append(")"); } return true; } #endregion #region IFormattable Members /// /// /// /// /// /// public string ToString(string format, IFormatProvider formatProvider) { return Showing.ShowString(this, format, formatProvider); } #endregion } /// /// /// /// /// /// public struct Rec : IEquatable>, IShowable { /// /// /// public readonly T1 X1; /// /// /// public readonly T2 X2; /// /// /// public readonly T3 X3; /// /// /// /// /// /// [Tested] public Rec(T1 x1, T2 x2, T3 x3) { this.X1 = x1; this.X2 = x2; this.X3 = x3; } /// /// /// /// /// [Tested] public bool Equals(Rec other) { return (X1 == null ? other.X1 == null : X1.Equals(other.X1)) && (X2 == null ? other.X2 == null : X2.Equals(other.X2)) && (X3 == null ? other.X3 == null : X3.Equals(other.X3)) ; } /// /// /// /// /// [Tested] public override bool Equals(object obj) { return obj is Rec ? Equals((Rec)obj) : false; } /// /// /// /// /// /// [Tested] public static bool operator ==(Rec record1, Rec record2) { return record1.Equals(record2); } /// /// /// /// /// /// [Tested] public static bool operator !=(Rec record1, Rec record2) { return !record1.Equals(record2); } /// /// /// /// [Tested] public override int GetHashCode() { //TODO: don't use 0 as hashcode for null, but something else! int hashcode = X1 == null ? 0 : X1.GetHashCode(); hashcode = hashcode * RecConst.HASHFACTOR + (X2 == null ? 0 : X2.GetHashCode()); hashcode = hashcode * RecConst.HASHFACTOR + (X3 == null ? 0 : X3.GetHashCode()); return hashcode; } /// /// /// /// public override string ToString() { return String.Format("({0}, {1}, {2})", X1, X2, X3); } #region IShowable Members /// /// /// /// /// /// /// public bool Show(System.Text.StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) { bool incomplete = true; stringbuilder.Append("("); rest -= 2; try { if (incomplete = !Showing.Show(X1, stringbuilder, ref rest, formatProvider)) return false; stringbuilder.Append(", "); rest -= 2; if (incomplete = !Showing.Show(X2, stringbuilder, ref rest, formatProvider)) return false; stringbuilder.Append(", "); rest -= 2; if (incomplete = !Showing.Show(X3, stringbuilder, ref rest, formatProvider)) return false; } finally { if (incomplete) { stringbuilder.Append("..."); rest -= 3; } stringbuilder.Append(")"); } return true; } #endregion #region IFormattable Members /// /// /// /// /// /// public string ToString(string format, IFormatProvider formatProvider) { return Showing.ShowString(this, format, formatProvider); } #endregion } /// /// /// /// /// /// /// public struct Rec : IEquatable>, IShowable { /// /// /// public readonly T1 X1; /// /// /// public readonly T2 X2; /// /// /// public readonly T3 X3; /// /// /// public readonly T4 X4; /// /// /// /// /// /// /// [Tested] public Rec(T1 x1, T2 x2, T3 x3, T4 x4) { this.X1 = x1; this.X2 = x2; this.X3 = x3; this.X4 = x4; } /// /// /// /// /// [Tested] public bool Equals(Rec other) { return (X1 == null ? other.X1 == null : X1.Equals(other.X1)) && (X2 == null ? other.X2 == null : X2.Equals(other.X2)) && (X3 == null ? other.X3 == null : X3.Equals(other.X3)) && (X4 == null ? other.X4 == null : X4.Equals(other.X4)) ; } /// /// /// /// /// [Tested] public override bool Equals(object obj) { return obj is Rec ? Equals((Rec)obj) : false; } /// /// /// /// /// /// [Tested] public static bool operator ==(Rec record1, Rec record2) { return record1.Equals(record2); } /// /// /// /// /// /// [Tested] public static bool operator !=(Rec record1, Rec record2) { return !record1.Equals(record2); } /// /// /// /// [Tested] public override int GetHashCode() { //TODO: don't use 0 as hashcode for null, but something else! int hashcode = X1 == null ? 0 : X1.GetHashCode(); hashcode = hashcode * RecConst.HASHFACTOR + (X2 == null ? 0 : X2.GetHashCode()); hashcode = hashcode * RecConst.HASHFACTOR + (X3 == null ? 0 : X3.GetHashCode()); hashcode = hashcode * RecConst.HASHFACTOR + (X4 == null ? 0 : X4.GetHashCode()); return hashcode; } /// /// /// /// public override string ToString() { return String.Format("({0}, {1}, {2}, {3})", X1, X2, X3, X4); } #region IShowable Members /// /// /// /// /// /// /// public bool Show(System.Text.StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) { bool incomplete = true; stringbuilder.Append("("); rest -= 2; try { if (incomplete = !Showing.Show(X1, stringbuilder, ref rest, formatProvider)) return false; stringbuilder.Append(", "); rest -= 2; if (incomplete = !Showing.Show(X2, stringbuilder, ref rest, formatProvider)) return false; stringbuilder.Append(", "); rest -= 2; if (incomplete = !Showing.Show(X3, stringbuilder, ref rest, formatProvider)) return false; stringbuilder.Append(", "); rest -= 2; if (incomplete = !Showing.Show(X4, stringbuilder, ref rest, formatProvider)) return false; } finally { if (incomplete) { stringbuilder.Append("..."); rest -= 3; } stringbuilder.Append(")"); } return true; } #endregion #region IFormattable Members /// /// /// /// /// /// public string ToString(string format, IFormatProvider formatProvider) { return Showing.ShowString(this, format, formatProvider); } #endregion } }