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

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

[Dotgnu-pnet-commits] CVS: pnetlib/I18N/Common HandlerCollection.cs,NON


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/I18N/Common HandlerCollection.cs,NONE,1.1 Manager.cs,1.5,1.6
Date: Sun, 23 Feb 2003 20:34:14 -0500

Update of /cvsroot/dotgnu-pnet/pnetlib/I18N/Common
In directory subversions:/tmp/cvs-serv31065/I18N/Common

Modified Files:
        Manager.cs 
Added Files:
        HandlerCollection.cs 
Log Message:


Change the representation of the I18N handler table to reduce
the startup code associated with the hash table version.


--- NEW FILE ---
/*
 * HandlerCollection.cs - Implementation of the
 *              "I18N.Common.HandlerCollection" class.
 *
 * Copyright (c) 2003  Southern Storm Software, Pty Ltd
 *
 * 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.
 */

namespace I18N.Common
{

using System;
using System.Collections;

// This class manages a collection of handler names and the
// assemblies that implement them.  Previously we used a hash
// table, but it incurred a large application startup cost.

public sealed class HandlerCollection : IDictionary
{

        // Internal state.
        private String[] names;
        private String[] namespaces;
        private int numHandlers;

        // Estimate of the number of handlers (should be >= the number
        // of lines in the "I18N-handlers.def" file).
        private const int HandlerCountEstimate = 160;

        // Constructor.
        public HandlerCollection()
                        {
                                names = new String [HandlerCountEstimate];
                                namespaces = new String [HandlerCountEstimate];
                                numHandlers = 0;
                        }

        // Find the index of a specific name.
        private int IndexOf(String name)
                        {
                                int posn;
                                for(posn = 0; posn < numHandlers; ++posn)
                                {
                                        if(names[posn] == name)
                                        {
                                                return posn;
                                        }
                                }
                                return -1;
                        }

        // Implement the IDictionary interface.
        public void Add(Object key, Object value)
                        {
                                if(numHandlers >= names.Length)
                                {
                                        String[] newNames = new String 
[names.Length + 32];
                                        String[] newNamespaces = new String 
[names.Length + 32];
                                        Array.Copy(names, 0, newNames, 0, 
names.Length);
                                        Array.Copy(namespaces, 0, 
newNamespaces, 0, names.Length);
                                        names = newNames;
                                        namespaces = newNamespaces;
                                }
                                names[numHandlers] = (String)key;
                                namespaces[numHandlers] = (String)value;
                                ++numHandlers;
                        }
        public void Clear()
                        {
                                numHandlers = 0;
                        }
        public bool Contains(Object key)
                        {
                                return (IndexOf((String)key) != -1);
                        }
        public IDictionaryEnumerator GetEnumerator()
                        {
                                return new HandlerCollectionEnumerator(this);
                        }
        public void Remove(Object key)
                        {
                                // Not used in this implementation.
                                throw new InvalidOperationException();
                        }
        public bool IsFixedSize
                        {
                                get
                                {
                                        return false;
                                }
                        }
        public bool IsReadOnly
                        {
                                get
                                {
                                        return false;
                                }
                        }
        public Object this[Object key]
                        {
                                get
                                {
                                        int index = IndexOf((String)key);
                                        if(index != -1)
                                        {
                                                return namespaces[index];
                                        }
                                        else
                                        {
                                                return null;
                                        }
                                }
                                set
                                {
                                        Add(key, value);
                                }
                        }
        public ICollection Keys
                        {
                                get
                                {
                                        // Not used in this implementation.
                                        throw new InvalidOperationException();
                                }
                        }
        public ICollection Values
                        {
                                get
                                {
                                        // Not used in this implementation.
                                        throw new InvalidOperationException();
                                }
                        }

        // Implement the ICollection interface.
        public void CopyTo(Array array, int index)
                        {
                                // Not used in this implementation.
                                throw new InvalidOperationException();
                        }
        public int Count
                        {
                                get
                                {
                                        return numHandlers;
                                }
                        }
        public bool IsSynchronized
                        {
                                get
                                {
                                        return false;
                                }
                        }
        public Object SyncRoot
                        {
                                get
                                {
                                        return this;
                                }
                        }

        // Implement the IEnumerable interface.
        IEnumerator IEnumerable.GetEnumerator()
                        {
                                return new HandlerCollectionEnumerator(this);
                        }

        // Enumerator class for this collection.
        private sealed class HandlerCollectionEnumerator : IDictionaryEnumerator
        {
                // Internal state.
                private HandlerCollection coll;
                private int posn;

                // Constructor.
                public HandlerCollectionEnumerator(HandlerCollection coll)
                                {
                                        this.coll = coll;
                                        this.posn = -1;
                                }

                // Implement the IEnumerator class.
                public bool MoveNext()
                                {
                                        ++posn;
                                        return (posn < coll.numHandlers);
                                }
                public void Reset()
                                {
                                        posn = -1;
                                }
                public Object Current
                                {
                                        get
                                        {
                                                if(posn >= 0 && posn < 
coll.numHandlers)
                                                {
                                                        return new 
DictionaryEntry
                                                                
(coll.names[posn], coll.namespaces[posn]);
                                                }
                                                throw new 
InvalidOperationException();
                                        }
                                }

                // Implement the IDictionaryEnumerator class.
                public DictionaryEntry Entry
                                {
                                        get
                                        {
                                                if(posn >= 0 && posn < 
coll.numHandlers)
                                                {
                                                        return new 
DictionaryEntry
                                                                
(coll.names[posn], coll.namespaces[posn]);
                                                }
                                                throw new 
InvalidOperationException();
                                        }
                                }
                public Object Key
                                {
                                        get
                                        {
                                                if(posn >= 0 && posn < 
coll.numHandlers)
                                                {
                                                        return coll.names[posn];
                                                }
                                                throw new 
InvalidOperationException();
                                        }
                                }
                public Object Value
                                {
                                        get
                                        {
                                                if(posn >= 0 && posn < 
coll.numHandlers)
                                                {
                                                        return 
coll.namespaces[posn];
                                                }
                                                throw new 
InvalidOperationException();
                                        }
                                }

        }; // class HandlerCollectionEnumerator

}; // class HandlerCollection

}; // namespace I18N.Common

Index: Manager.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/I18N/Common/Manager.cs,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -r1.5 -r1.6
*** Manager.cs  14 Nov 2002 06:29:03 -0000      1.5
--- Manager.cs  24 Feb 2003 01:34:12 -0000      1.6
***************
*** 45,49 ****
  
        // Internal state.
!       private Hashtable handlers;             // List of all handler classes.
        private Hashtable active;               // Currently active handlers.
        private Hashtable assemblies;   // Currently loaded region assemblies.
--- 45,49 ----
  
        // Internal state.
!       private HandlerCollection handlers; // List of all handler classes.
        private Hashtable active;               // Currently active handlers.
        private Hashtable assemblies;   // Currently loaded region assemblies.
***************
*** 52,56 ****
        private Manager()
                        {
!                               handlers = new Hashtable();
                                active = new Hashtable(16);
                                assemblies = new Hashtable(8);
--- 52,56 ----
        private Manager()
                        {
!                               handlers = new HandlerCollection();
                                active = new Hashtable(16);
                                assemblies = new Hashtable(8);
***************
*** 397,404 ****
                                                // attached to the name of the 
handler class.
                                                String name = 
line.Substring(posn + 1);
!                                               if(!handlers.Contains(name))
!                                               {
!                                                       handlers.Add(name, 
line.Substring(0, posn));
!                                               }
                                        }
                                }
--- 397,401 ----
                                                // attached to the name of the 
handler class.
                                                String name = 
line.Substring(posn + 1);
!                                               handlers.Add(name, 
line.Substring(0, posn));
                                        }
                                }
***************
*** 420,427 ****
                                                // attached to the name of the 
handler class.
                                                String name = 
line.Substring(posn + 1);
!                                               if(!handlers.Contains(name))
!                                               {
!                                                       handlers.Add(name, 
line.Substring(0, posn));
!                                               }
                                        }
                                }
--- 417,421 ----
                                                // attached to the name of the 
handler class.
                                                String name = 
line.Substring(posn + 1);
!                                               handlers.Add(name, 
line.Substring(0, posn));
                                        }
                                }





reply via email to

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