HashSetEqualityComparer.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / System / Collections / Generic / HashSetEqualityComparer.cs / 1305376 / HashSetEqualityComparer.cs

                            using System; 
using System.Collections;
using System.Collections.Generic;

namespace System.Collections.Generic { 

    ///  
    /// Equality comparer for hashsets of hashsets 
    /// 
    ///  
    [Serializable()]
    internal class HashSetEqualityComparer : IEqualityComparer> {

        private IEqualityComparer m_comparer; 

        public HashSetEqualityComparer() { 
            m_comparer = EqualityComparer.Default; 
        }
 
        public HashSetEqualityComparer(IEqualityComparer comparer) {
            if (comparer == null) {
                m_comparer = EqualityComparer.Default;
            } 
            else {
                m_comparer = comparer; 
            } 
        }
 
        // using m_comparer to keep equals properties in tact; don't want to choose one of the comparers
        public bool Equals(HashSet x, HashSet y) {
            return HashSet.HashSetEquals(x, y, m_comparer);
        } 

        public int GetHashCode(HashSet obj) { 
            int hashCode = 0; 
            if (obj != null) {
                foreach (T t in obj) { 
                    hashCode = hashCode ^ (m_comparer.GetHashCode(t) & 0x7FFFFFFF);
                }
            } // else returns hashcode of 0 for null hashsets
            return hashCode; 
        }
 
        // Equals method for the comparer itself. 
        public override bool Equals(Object obj){
            HashSetEqualityComparer comparer = obj as HashSetEqualityComparer; 
            if (comparer == null) {
                return false;
            }
            return (this.m_comparer == comparer.m_comparer); 
        }
 
        public override int GetHashCode() { 
            return m_comparer.GetHashCode();
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK