public int GetHashCode(MultiSet <T> obj) { Utils.ThrowException(obj == null ? new ArgumentNullException("obj") : null); int hashCode = 0; foreach (KeyValuePair <T, int> item in obj) { hashCode ^= item.Key.GetHashCode() ^ item.Value.GetHashCode(); } return(hashCode); }
public bool Equals(MultiSet <T> .ReadOnly x, MultiSet <T> .ReadOnly y) { if (x == null && y == null) { return(true); } if (x == null || y == null) { return(false); } return(Equals(x.Inner, y.Inner)); }
public bool Equals(MultiSet <T> x, MultiSet <T> y) { if (x == null && y == null) { return(true); } if (x == null || y == null) { return(false); } return(x.Count == y.Count && MultiSet <T> .Difference(x, y).Count == 0); }
public static double JaccardSimilarity(MultiSet <T> a, MultiSet <T> b) { Utils.ThrowException(a == null ? new ArgumentNullException("a") : null); Utils.ThrowException(b == null ? new ArgumentNullException("b") : null); MultiSet <T> c = Intersection(a, b); double div = (double)(a.Count + b.Count - c.Count); if (div == 0) { return(1); } // *** if both sets are empty, the similarity is 1 return((double)c.Count / div); }
bool IEqualityComparer.Equals(object x, object y) { if (x is MultiSet <T> ) { x = new MultiSet <T> .ReadOnly((MultiSet <T>) x); } if (y is MultiSet <T> ) { y = new MultiSet <T> .ReadOnly((MultiSet <T>) y); } Utils.ThrowException((x != null && !(x is MultiSet <T> .ReadOnly)) ? new ArgumentTypeException("x") : null); Utils.ThrowException((y != null && !(y is MultiSet <T> .ReadOnly)) ? new ArgumentTypeException("y") : null); return(Equals((MultiSet <T> .ReadOnly)x, (MultiSet <T> .ReadOnly)y)); }
public static MultiSet <T> Difference(MultiSet <T> a, MultiSet <T> b) { Utils.ThrowException(a == null ? new ArgumentNullException("a") : null); Utils.ThrowException(b == null ? new ArgumentNullException("b") : null); MultiSet <T> c = new MultiSet <T>(a.Comparer); // *** inherits comparer from a (b is expected to have the same comparer) foreach (KeyValuePair <T, int> item in a) { int bCount = b.GetCount(item.Key); int cCount = item.Value - bCount; if (cCount > 0) { c.mItems.Add(item.Key, cCount); } } return(c); }
public static MultiSet <T> Intersection(MultiSet <T> a, MultiSet <T> b) { Utils.ThrowException(a == null ? new ArgumentNullException("a") : null); Utils.ThrowException(b == null ? new ArgumentNullException("b") : null); MultiSet <T> c = new MultiSet <T>(a.Comparer); // *** inherits comparer from a (b is expected to have the same comparer) if (b.Count < a.Count) { MultiSet <T> tmp; tmp = a; a = b; b = tmp; } foreach (KeyValuePair <T, int> item in a) { int bCount = b.GetCount(item.Key); if (bCount > 0) { c.mItems.Add(item.Key, Math.Min(item.Value, bCount)); } } return(c); }
// *** IContentEquatable<MultiSet<T>> interface implementation *** public bool ContentEquals(MultiSet <T> other) { if (other == null || Count != other.Count) { return(false); } foreach (KeyValuePair <T, int> item in mItems) { int count; if (other.mItems.TryGetValue(item.Key, out count)) { if (count != item.Value) { return(false); } } else { return(false); } } return(true); }
public int GetHashCode(MultiSet <T> .ReadOnly obj) { Utils.ThrowException(obj == null ? new ArgumentNullException("obj") : null); return(GetHashCode(obj.Inner)); }
// *** IContentEquatable<MultiSet<T>.ReadOnly> interface implementation *** public bool ContentEquals(MultiSet <T> .ReadOnly other) { return(other != null && mSet.ContentEquals(other.Inner)); }
public ReadOnly(BinarySerializer reader) { mSet = new MultiSet <T>(reader); // throws ArgumentNullException, serialization-related exceptions }
public ReadOnly(MultiSet <T> set) { Utils.ThrowException(set == null ? new ArgumentNullException("set") : null); mSet = set; }
public static double JaccardSimilarity(MultiSet <T> .ReadOnly a, MultiSet <T> .ReadOnly b) { Utils.ThrowException(a == null ? new ArgumentNullException("a") : null); Utils.ThrowException(b == null ? new ArgumentNullException("b") : null); return(JaccardSimilarity(a.Inner, b.Inner)); }
public static MultiSet <T> Difference(MultiSet <T> .ReadOnly a, MultiSet <T> .ReadOnly b) { Utils.ThrowException(a == null ? new ArgumentNullException("a") : null); Utils.ThrowException(b == null ? new ArgumentNullException("b") : null); return(Difference(a.Inner, b.Inner)); }