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

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

[Dotgnu-pnet-commits] pnetlib/System/Collections IKeyedCollection.cs, NO


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] pnetlib/System/Collections IKeyedCollection.cs, NONE, 1.1 KeyedCollection.cs, NONE, 1.1 KeyedCollectionBase.cs, NONE, 1.1 KeyedCollectionNotificationDetails.cs, NONE, 1.1
Date: Mon, 03 Nov 2003 23:24:35 +0000

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

Added Files:
        IKeyedCollection.cs KeyedCollection.cs KeyedCollectionBase.cs 
        KeyedCollectionNotificationDetails.cs 
Log Message:


Add some new .NET Framework 1.2 collection classes.


--- NEW FILE: KeyedCollectionNotificationDetails.cs ---
/*
 * KeyedCollectionNotificationDetails.cs - Implementation of
 *              "System.Collections.KeyedCollectionNotificationDetails".
 *
 * 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 struct KeyedCollectionNotificationDetails
{
        // Internal state.
        private bool accessedByKey;
        private int index;
        private Object key;
        private Object newValue;
        private Object oldValue;

        // Get or set this object's properties.
        public bool AccessedByKey
                        {
                                get
                                {
                                        return accessedByKey;
                                }
                                set
                                {
                                        accessedByKey = value;
                                }
                        }
        public int Index
                        {
                                get
                                {
                                        return index;
                                }
                                set
                                {
                                        index = value;
                                }
                        }
        public Object Key
                        {
                                get
                                {
                                        return key;
                                }
                                set
                                {
                                        key = value;
                                }
                        }
        public Object NewValue
                        {
                                get
                                {
                                        return newValue;
                                }
                                set
                                {
                                        newValue = value;
                                }
                        }
        public Object OldValue
                        {
                                get
                                {
                                        return oldValue;
                                }
                                set
                                {
                                        oldValue = value;
                                }
                        }

}; // struct KeyedCollectionNotificationDetails

#endif // !ECMA_COMPAT

}; // namespace System.Collections

--- NEW FILE: IKeyedCollection.cs ---
/*
 * IKeyedCollection.cs - Implementation of
 *              "System.Collections.IKeyedCollection".
 *
 * 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 IKeyedCollection
{
        // Add an item to this keyed collection.
        int Add(Object value, Object key);

        // Determine if this collection contains a particular key.
        bool ContainsKey(Object key);

        // Get the value associated with a particular key.
        Object GetValue(Object key);

        // Get the index associated with a particular key.
        int IndexOfKey(Object key);

        // Insert a new keyed value into this collection.
        void Insert(int index, Object value, Object key);
        void InsertAfter(Object afterKey, Object value, Object key);
        void InsertBefore(Object beforeKey, Object value, Object key);

        // Remove the item with a particular key.
        void RemoveByKey(Object key);

        // Set a particular key to a value.
        void SetValue(Object value, Object key);

}; // interface IKeyedCollection

#endif // !ECMA_COMPAT

}; // namespace System.Collections

--- NEW FILE: KeyedCollectionBase.cs ---
/*
 * KeyedCollectionBase.cs - Implementation of
 *              "System.Collections.KeyedCollectionBase".
 *
 * 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 abstract class KeyedCollectionBase
        : ICollection, IEnumerable, IKeyedCollection, IList
{
        // Internal state.
        internal IKeyComparer comparer;
        internal ArrayList values;
        internal ArrayList keys;

        // Constructors.
        protected KeyedCollectionBase(IKeyComparer comparer, int capacity)
                        {
                                if(comparer == null)
                                {
                                        throw new 
ArgumentNullException("comparer");
                                }
                                this.comparer = comparer;
                                this.values = new ArrayList(capacity);
                                this.keys = new ArrayList(capacity);
                        }
        protected KeyedCollectionBase()
                        : 
this(KeyComparer.CreateKeyComparer(ComparisonType.Ordinal), 16) {}
        protected KeyedCollectionBase(IKeyComparer comparer) : this(comparer, 
16) {}
        protected KeyedCollectionBase(ComparisonType comparisonType)
                        : this(KeyComparer.CreateKeyComparer(comparisonType), 
16) {}
        protected KeyedCollectionBase(ComparisonType comparisonType, int 
capacity)
                        : this(KeyComparer.CreateKeyComparer(comparisonType), 
capacity) {}

        // Find an item with a specific key.
        private int FindKey(Object key)
                        {
                                if(key == null)
                                {
                                        throw new ArgumentNullException("key");
                                }
                                int index;
                                for(index = 0; index < keys.Count; ++index)
                                {
                                        if(comparer.Equals(key, keys[index]))
                                        {
                                                return index;
                                        }
                                }
                                return -1;
                        }

        // Implement the ICollection interface.
        void ICollection.CopyTo(Array array, int index)
                        {
                                values.CopyTo(array, index);
                        }
        public virtual int Count
                        {
                                get
                                {
                                        return values.Count;
                                }
                        }
        bool ICollection.IsSynchronized
                        {
                                get
                                {
                                        return false;
                                }
                        }
        Object ICollection.SyncRoot
                        {
                                get
                                {
                                        return this;
                                }
                        }

        // Implement the IEnumerable interface.
        public virtual IEnumerator GetEnumerator()
                        {
                                return values.GetEnumerator();
                        }

        // Implement the IKeyedCollection interface.
        int IKeyedCollection.Add(Object value, Object key)
                        {
                                OnValidate(value, key);
                                if(FindKey(key) != -1)
                                {
                                        throw new 
ArgumentException(S._("Arg_ExistingEntry"));
                                }
                                KeyedCollectionNotificationDetails details;
                                int index = values.Count;
                                details.AccessedByKey = true;
                                details.Key = key;
                                details.NewValue = value;
                                OnInsert(details);
                                try
                                {
                                        try
                                        {
                                                keys.Add(key);
                                        }
                                        catch(Exception)
                                        {
                                                values.RemoveAt(index);
                                                throw;
                                        }
                                }
                                finally
                                {
                                        OnInsertComplete(details);
                                }
                                return index;
                        }
        bool IKeyedCollection.ContainsKey(Object key)
                        {
                                return (FindKey(key) != -1);
                        }
        Object IKeyedCollection.GetValue(Object key)
                        {
                                int index = FindKey(key);
                                if(index != -1)
                                {
                                        return values[index];
                                }
                                else
                                {
                                        return null;
                                }
                        }
        int IKeyedCollection.IndexOfKey(Object key)
                        {
                                return FindKey(key);
                        }
        void IKeyedCollection.Insert(int index, Object value, Object key)
                        {
                                if(index < 0 || index > values.Count)
                                {
                                        throw new ArgumentOutOfRangeException
                                                ("index", 
S._("ArgRange_Array"));
                                }
                                OnValidate(value, key);
                                int existing = FindKey(key);
                                if(existing != -1)
                                {
                                        throw new 
ArgumentException(S._("Arg_ExistingEntry"));
                                }
                                KeyedCollectionNotificationDetails details;
                                details.AccessedByKey = true;
                                details.Key = key;
                                details.NewValue = value;
                                OnInsert(details);
                                try
                                {
                                        values.Insert(index, value);
                                        try
                                        {
                                                keys.Insert(index, key);
                                        }
                                        catch(Exception)
                                        {
                                                values.RemoveAt(index);
                                        }
                                }
                                finally
                                {
                                        OnInsertComplete(details);
                                }
                        }
        void IKeyedCollection.InsertAfter
                                (Object afterKey, Object value, Object key)
                        {
                                int index = FindKey(key);
                                ((IKeyedCollection)this).Insert(index + 1, 
value, key);
                        }
        void IKeyedCollection.InsertBefore
                                (Object beforeKey, Object value, Object key)
                        {
                                int index = FindKey(key);
                                if(index == -1)
                                {
                                        index = values.Count;
                                }
                                ((IKeyedCollection)this).Insert(index, value, 
key);
                        }
        void IKeyedCollection.RemoveByKey(Object key)
                        {
                                int index = FindKey(key);
                                KeyedCollectionNotificationDetails details;
                                details.AccessedByKey = true;
                                details.Key = key;
                                OnRemove(details);
                                try
                                {
                                        if(index != -1)
                                        {
                                                Object value = values[index];
                                                values.RemoveAt(index);
                                                try
                                                {
                                                        keys.RemoveAt(index);
                                                }
                                                catch(Exception)
                                                {
                                                        values.Insert(index, 
value);
                                                        throw;
                                                }
                                        }
                                }
                                finally
                                {
                                        OnRemoveComplete(details);
                                }
                        }
        void IKeyedCollection.SetValue(Object value, Object key)
                        {
                                OnValidate(value, key);
                                KeyedCollectionNotificationDetails details;
                                int index = FindKey(key);
                                details.AccessedByKey = true;
                                details.Key = key;
                                details.NewValue = value;
                                if(index != -1)
                                {
                                        details.OldValue = values[index];
                                }
                                OnSet(details);
                                try
                                {
                                        if(index != -1)
                                        {
                                                values[index] = value;
                                        }
                                        else
                                        {
                                                index = values.Add(value);
                                                try
                                                {
                                                        keys.Add(key);
                                                }
                                                catch(Exception)
                                                {
                                                        values.RemoveAt(index);
                                                        throw;
                                                }
                                        }
                                }
                                finally
                                {
                                        OnSetComplete(details);
                                }
                        }

        // Implement the IList interface.
        int IList.Add(Object value)
                        {
                                OnValidate(value);
                                KeyedCollectionNotificationDetails details;
                                int index = values.Count;
                                details.AccessedByKey = true;
                                details.Key = GenerateKeyForValue(value);
                                details.NewValue = value;
                                OnInsert(details);
                                values.Add(value);
                                try
                                {
                                        try
                                        {
                                                keys.Add(details.Key);
                                        }
                                        catch(Exception)
                                        {
                                                values.RemoveAt(index);
                                                throw;
                                        }
                                }
                                finally
                                {
                                        OnInsertComplete(details);
                                }
                                return index;
                        }
        public virtual void Clear()
                        {
                                try
                                {
                                        OnClear();
                                        values.Clear();
                                        keys.Clear();
                                }
                                finally
                                {
                                        OnClearComplete();
                                }
                        }
        bool IList.Contains(Object value)
                        {
                                return values.Contains(value);
                        }
        int IList.IndexOf(Object value)
                        {
                                return values.IndexOf(value);
                        }
        void IList.Insert(int index, Object value)
                        {
                                ((IKeyedCollection)this).Insert
                                        (index, value, 
GenerateKeyForValue(value));
                        }
        void IList.Remove(Object value)
                        {
                                int index = values.IndexOf(value);
                                if(index != -1)
                                {
                                        KeyedCollectionNotificationDetails 
details;
                                        details.AccessedByKey = false;
                                        details.Index = index;
                                        OnRemove(details);
                                        try
                                        {
                                                values.RemoveAt(index);
                                                try
                                                {
                                                        keys.RemoveAt(index);
                                                }
                                                catch(Exception)
                                                {
                                                        values.Insert(index, 
value);
                                                        throw;
                                                }
                                        }
                                        finally
                                        {
                                                OnRemoveComplete(details);
                                        }
                                }
                        }
        public virtual void RemoveAt(int index)
                        {
                                KeyedCollectionNotificationDetails details;
                                details.AccessedByKey = false;
                                details.Index = index;
                                OnRemove(details);
                                try
                                {
                                        Object value = values[index];
                                        values.RemoveAt(index);
                                        try
                                        {
                                                keys.RemoveAt(index);
                                        }
                                        catch(Exception)
                                        {
                                                values.Insert(index, value);
                                                throw;
                                        }
                                }
                                finally
                                {
                                        OnRemoveComplete(details);
                                }
                        }
        bool IList.IsFixedSize
                        {
                                get
                                {
                                        return false;
                                }
                        }
        bool IList.IsReadOnly
                        {
                                get
                                {
                                        return false;
                                }
                        }
        Object IList.this[int index]
                        {
                                get
                                {
                                        KeyedCollectionNotificationDetails 
details;
                                        details.AccessedByKey = false;
                                        details.Index = index;
                                        OnGet(details);
                                        return values[index];
                                }
                                set
                                {
                                        OnValidate(value);
                                        KeyedCollectionNotificationDetails 
details;
                                        details.AccessedByKey = false;
                                        details.Index = index;
                                        details.NewValue = value;
                                        details.OldValue = values[index];
                                        OnSet(details);
                                        try
                                        {
                                                values[index] = value;
                                        }
                                        finally
                                        {
                                                OnSetComplete(details);
                                        }
                                }
                        }

        // Generate a key for a specific value.
        protected virtual Object GenerateKeyForValue(Object value)
                        {
                                return value;
                        }

        // Notify subclasses of various interesting properties.
        protected virtual void OnClear() {}
        protected virtual void OnClearComplete() {}
        protected virtual void OnGet(KeyedCollectionNotificationDetails 
details) {}
        protected virtual void OnInsert
                        (KeyedCollectionNotificationDetails details) {}
        protected virtual void OnInsertComplete
                        (KeyedCollectionNotificationDetails details) {}
        protected virtual void OnRemove
                        (KeyedCollectionNotificationDetails details) {}
        protected virtual void OnRemoveComplete
                        (KeyedCollectionNotificationDetails details) {}
        protected virtual void OnSet
                        (KeyedCollectionNotificationDetails details) {}
        protected virtual void OnSetComplete
                        (KeyedCollectionNotificationDetails details) {}
        protected virtual void OnValidate(Object value) {}
        protected virtual void OnValidate(Object value, Object key) {}

        // Get this object as a keyed collection.
        protected IKeyedCollection List
                        {
                                get
                                {
                                        return this;
                                }
                        }
        protected IKeyedCollection InnerList
                        {
                                get
                                {
                                        return this;
                                }
                        }

}; // class KeyedCollectionBase

#endif // !ECMA_COMPAT

}; // namespace System.Collections

--- NEW FILE: KeyedCollection.cs ---
/*
 * KeyedCollection.cs - Implementation of "System.Collections.KeyedCollection".
 *
 * 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 class KeyedCollection : KeyedCollectionBase, ICloneable
{
        // Constructors.
        public KeyedCollection(IKeyComparer comparer, int capacity)
                        : base(comparer, capacity) {}
        public KeyedCollection() : base() {}
        public KeyedCollection(IKeyComparer comparer) : base(comparer) {}
        public KeyedCollection(ComparisonType comparisonType)
                        : base(comparisonType) {}
        public KeyedCollection(ComparisonType comparisonType, int capacity)
                        : base(comparisonType, capacity) {}

        // Add an item to this collection.
        public virtual int Add(Object value)
                        {
                                return ((IList)this).Add(value);
                        }
        public virtual int Add(Object value, String key)
                        {
                                return ((IKeyedCollection)this).Add(value, key);
                        }

        // Add a range of items to this collection.
        public virtual void AddRange(ICollection c)
                        {
                                if(c != null)
                                {
                                        foreach(Object value in c)
                                        {
                                                Add(value);
                                        }
                                }
                        }
        public virtual void AddRange(IDictionary c)
                        {
                                if(c != null)
                                {
                                        IDictionaryEnumerator e = 
c.GetEnumerator();
                                        while(e.MoveNext())
                                        {
                                                
((IKeyedCollection)this).Add(e.Value, e.Key);
                                        }
                                }
                        }

        // Clone this object.
        public virtual Object Clone()
                        {
                                KeyedCollection kc = 
(KeyedCollection)(MemberwiseClone());
                                kc.values = (ArrayList)(values.Clone());
                                kc.keys = (ArrayList)(keys.Clone());
                                return kc;
                        }

        // Determine if this collection contains a specific value.
        public virtual bool Contains(Object value)
                        {
                                return ((IList)this).Contains(value);
                        }

        // Determine if this collection contains a specific key.
        public virtual bool ContainsKey(Object key)
                        {
                                return 
((IKeyedCollection)this).ContainsKey(key);
                        }

        // Copy the elements of this collection into an array.
        public virtual void CopyTo(Array array)
                        {
                                ((ICollection)this).CopyTo(array, 0);
                        }
        public virtual void CopyTo(Array array, int index)
                        {
                                ((ICollection)this).CopyTo(array, index);
                        }

        // Get or set an item from this collection by key.
        public virtual Object this[String key]
                        {
                                get
                                {
                                        return 
((IKeyedCollection)this).GetValue(key);
                                }
                                set
                                {
                                        
((IKeyedCollection)this).SetValue(value, key);
                                }
                        }

        // Get or set an item from this collection by index.
        public virtual Object this[int index]
                        {
                                get
                                {
                                        return ((IList)this)[index];
                                }
                                set
                                {
                                        ((IList)this)[index] = value;
                                }
                        }

        // Get the index of a specific value in this collection.
        public virtual int IndexOf(Object value)
                        {
                                return ((IList)this).IndexOf(value);
                        }

        // Get the index of a specific key in this collection.
        public virtual int IndexOfKey(String key)
                        {
                                return ((IKeyedCollection)this).IndexOfKey(key);
                        }

        // Insert an object into this collection.
        public virtual void Insert(int index, Object value)
                        {
                                ((IList)this).Insert(index, value);
                        }
        public virtual void Insert(int index, Object value, String key)
                        {
                                ((IKeyedCollection)this).Insert(index, value, 
key);
                        }

        // Insert an object into this collection before or after a given key.
        public virtual void InsertBefore
                                (String beforeKey, Object value, String key)
                        {
                                ((IKeyedCollection)this).InsertBefore
                                                (beforeKey, value, key);
                        }
        public virtual void InsertAfter
                                (String afterKey, Object value, String key)
                        {
                                ((IKeyedCollection)this).InsertAfter
                                                (afterKey, value, key);
                        }

        // Remove a particular value from this collection.
        public virtual void Remove(Object value)
                        {
                                ((IList)this).Remove(value);
                        }

        // Remove a particular key from this collection.
        public virtual void RemoveByKey(String key)
                        {
                                ((IKeyedCollection)this).RemoveByKey(key);
                        }

        // Convert this collection into an array.
        public virtual Object[] ToArray()
                        {
                                return values.ToArray();
                        }
        public virtual Array ToArray(Type type)
                        {
                                return values.ToArray(type);
                        }

}; // class KeyedCollection

#endif // !ECMA_COMPAT

}; // namespace System.Collections





reply via email to

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