dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] pnetlib/runtime/System/Collections ComparisonType.


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] pnetlib/runtime/System/Collections ComparisonType.cs, NONE, 1.1 IKeyComparer.cs, NONE, 1.1 KeyComparer.cs, NONE, 1.1
Date: Mon, 03 Nov 2003 23:24:37 +0000

Update of /cvsroot/dotgnu-pnet/pnetlib/runtime/System/Collections
In directory subversions:/tmp/cvs-serv31835/runtime/System/Collections

Added Files:
        ComparisonType.cs IKeyComparer.cs KeyComparer.cs 
Log Message:


Add some new .NET Framework 1.2 collection classes.


--- NEW FILE: IKeyComparer.cs ---
/*
 * IKeyComparer.cs - Implementation of the
 *                      "System.Collections.IKeyComparer" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Collections
{

#if !ECMA_COMPAT

// Framework 1.2
public interface IKeyComparer
{
        // Determine if two objects are equal.
        bool Equals(Object x, Object y);

}; // interface IKeyComparer

#endif // !ECMA_COMPAT

}; // namespace System.Collections

--- NEW FILE: KeyComparer.cs ---
/*
 * KeyComparer.cs - Implementation of the
 *                      "System.Collections.KeyComparer" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Collections
{

#if !ECMA_COMPAT

using System.Globalization;

// Framework 1.2
public sealed class KeyComparer : IKeyComparer
{
        // Internal state.
        private IComparer comparer;
        private IHashCodeProvider hashCodeProvider;
        private static IHashCodeProvider defaultHashCodeProvider =
                        new DefaultHashCodeProvider();

        // Constructors.
        private KeyComparer(IComparer comparer, IHashCodeProvider 
hashCodeProvider)
                        {
                                this.comparer = comparer;
                                this.hashCodeProvider = hashCodeProvider;
                        }

        // Determine if two objects are equal.
        bool IKeyComparer.Equals(Object x, Object y)
                        {
                                if(hashCodeProvider.GetHashCode(x) !=
                                   hashCodeProvider.GetHashCode(y))
                                {
                                        return false;
                                }
                                return (comparer.Compare(x, y) == 0);
                        }

        // Create a new key comparer.
        public static IKeyComparer CreateKeyComparer(ComparisonType 
comparisonType)
                        {
                                switch(comparisonType)
                                {
                                        case ComparisonType.CurrentCulture:
                                        {
                                                return new KeyComparer
                                                        (new 
Comparer(CultureInfo.CurrentCulture),
                                                         
defaultHashCodeProvider);
                                        }

                                        case 
ComparisonType.CurrentCultureIgnoreCase:
                                        {
                                                return new KeyComparer
                                                        (new 
CaseInsensitiveComparer
                                                                
(CultureInfo.CurrentCulture),
                                                         new 
CaseInsensitiveHashCodeProvider
                                                                
(CultureInfo.CurrentCulture));
                                        }

                                        case ComparisonType.InvariantCulture:
                                        {
                                                return new KeyComparer
                                                        
(Comparer.DefaultInvariant,
                                                         
defaultHashCodeProvider);
                                        }

                                        case 
ComparisonType.InvariantCultureIgnoreCase:
                                        {
                                                return new KeyComparer
                                                        
(CaseInsensitiveComparer.DefaultInvariant,
                                                         
CaseInsensitiveHashCodeProvider.DefaultInvariant);
                                        }

                                        default:
                                        {
                                                return new KeyComparer
                                                        (new OrdinalComparer(), 
defaultHashCodeProvider);
                                        }
                                }
                        }
        public static IKeyComparer CreateKeyComparer
                                (CultureInfo culture, bool ignoreCase)
                        {
                                if(culture == null)
                                {
                                        throw new 
ArgumentNullException("culture");
                                }
                                if(!ignoreCase)
                                {
                                        return new KeyComparer
                                                (new Comparer(culture), 
defaultHashCodeProvider);
                                }
                                else
                                {
                                        return new KeyComparer
                                                (new 
CaseInsensitiveComparer(culture),
                                                 new 
CaseInsensitiveHashCodeProvider(culture));
                                }
                        }
        public static IKeyComparer CreateKeyComparer
                                (IComparer comparer, IHashCodeProvider 
hashCodeProvider)
                        {
                                if(comparer == null)
                                {
                                        throw new 
ArgumentNullException("comparer");
                                }
                                if(hashCodeProvider == null)
                                {
                                        throw new 
ArgumentNullException("hashCodeProvider");
                                }
                                return new KeyComparer(comparer, 
hashCodeProvider);
                        }

        // Default hash code provider class.
        private sealed class DefaultHashCodeProvider : IHashCodeProvider
        {
                // Get the hash code for an object.
                public int GetHashCode(Object obj)
                                {
                                        if(obj != null)
                                        {
                                                return obj.GetHashCode();
                                        }
                                        else
                                        {
                                                return 0;
                                        }
                                }

        }; // class DefaultHashCodeProvider

        // Comparison class that uses ordinal values for strings.
        private sealed class OrdinalComparer : IComparer
        {

                // Implement the IComparer interface.
                public int Compare(Object a, Object b)
                                {
                                        IComparable cmp;
                                        if(a != null && b != null)
                                        {
                                                if(a is String && b is String)
                                                {
                                                        return 
String.CompareOrdinal((String)a, (String)b);
                                                }
                                                cmp = (a as IComparable);
                                                if(cmp != null)
                                                {
                                                        return cmp.CompareTo(b);
                                                }
                                                cmp = (b as IComparable);
                                                if(cmp != null)
                                                {
                                                        return 
-(cmp.CompareTo(a));
                                                }
                                                throw new ArgumentException
                                                        
(_("Arg_ABMustBeComparable"));
                                        }
                                        else if(a != null)
                                        {
                                                return 1;
                                        }
                                        else if(b != null)
                                        {
                                                return -1;
                                        }
                                        else
                                        {
                                                return 0;
                                        }
                                }

        }; // class OrdinalComparer

}; // class KeyComparer

#endif // !ECMA_COMPAT

}; // namespace System.Collections

--- NEW FILE: ComparisonType.cs ---
/*
 * ComparisonType.cs - Implementation of the
 *                      "System.Collections.ComparisonType" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Collections
{

#if !ECMA_COMPAT

// Framework 1.2
public enum ComparisonType
{
        CurrentCulture                          = 0,
        CurrentCultureIgnoreCase        = 1,
        InvariantCulture                        = 2,
        InvariantCultureIgnoreCase      = 3,
        Ordinal                                         = 4

}; // enum ComparisonType

#endif // !ECMA_COMPAT

}; // namespace System.Collections





reply via email to

[Prev in Thread] Current Thread [Next in Thread]