/// <summary> /// Term order. Comparision is pairwise using the term ordering of <see cref="HashAlgorithms.CompareValues" />. /// </summary> /// <param name="obj"></param> /// <returns>-1 if less than, 0 if equal, 1 if greater than</returns> /// <exception cref="System.ArgumentException">Thrown if <paramref name="obj"/> is not of the same type as this triple.</exception> /// <seealso cref="HashAlgorithms.CompareValues" /> public int CompareTo(object obj) { if ((object)obj == null) { return(1); } if (!(obj is Triple <T, S, R>)) { throw new ArgumentException("Incomparable argument"); } Triple <T, S, R> other = (Triple <T, S, R>)obj; int f1 = HashAlgorithms.CompareValues(this.first, other.first); if (f1 != 0) { return(f1); } int f2 = HashAlgorithms.CompareValues(this.second, other.second); if (f2 != 0) { return(f2); } int f3 = HashAlgorithms.CompareValues(this.third, other.third); //^ assert f3 == 0 ==> Object.Equals(this, other); return(f3); }
public int CompareTo(object obj) { Maplet other = obj as Maplet; if ((object)other == null) { return(1); } int f1 = HashAlgorithms.CompareValues(this.d.First, other.d.First); if (f1 == 1 || f1 == -1) { return(f1); } return(0); }
/// <summary> /// Term order. Comparision is based on type and recursively on fields. /// </summary> /// <param name="obj"></param> /// <returns>-1 if less than, 0 if equal, 1 if greater than</returns> /// <exception cref="System.ArgumentException">Thrown if <paramref name="obj"/> is nonnull but is not an <c>CompoundValue</c>.</exception> public override int CompareTo(object /*?*/ obj) //^ requires obj != null ==> obj is IComparable; //^ ensures result == 0 <==> Object.Equals(this, obj); //^ ensures result == -1 || result == 0 || result == 1; { // Case 1: other obj is null if (obj == null) { return(1); // nonnull is bigger than null } CompoundValue other = obj as CompoundValue; if ((object)other == null) { throw new ArgumentException(MessageStrings.LocalizedFormat(MessageStrings.CompoundValueRequired, obj.GetType().ToString())); } // Case 2: types aren't the same, do type comparison in dictionary order Type t1 = this.GetType(); Type t2 = obj.GetType(); if (t1 != t2) { return(t1.ToString().CompareTo(t2.ToString())); } // Case 3: types are the same, look for first field that is not equal IEnumerator <IComparable> fields1 = this.FieldValues().GetEnumerator(); IEnumerator <IComparable> fields2 = other.FieldValues().GetEnumerator(); while (true) { bool hasNext1 = fields1.MoveNext(); bool hasNext2 = fields2.MoveNext(); if (hasNext1 & !hasNext2) { return(1); // longer comes after shorter } if (!hasNext1 & hasNext2) { return(-1); // shorter comes before longer } if (!hasNext1) { return(0); // items are equal } IComparable c1 = fields1.Current as IComparable; IComparable c2 = fields2.Current as IComparable; if (fields1.Current != null && c1 == null) { throw new ArgumentException(MessageStrings.LocalizedFormat(MessageStrings.ComparableTypeRequired, fields1.Current.GetType().ToString())); } if (fields2.Current != null && c2 == null) { throw new ArgumentException(MessageStrings.LocalizedFormat(MessageStrings.ComparableTypeRequired, fields2.Current.GetType().ToString())); } int fieldCompare = HashAlgorithms.CompareValues(c1, c2); if (fieldCompare != 0) { return(fieldCompare); } } }