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

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

[Dotgnu-pnet-commits] CVS: pnetlib/Generics RangeList.cs,NONE,1.1 Array


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/Generics RangeList.cs,NONE,1.1 ArrayList.cs,1.8,1.9 Generics.html,1.3,1.4
Date: Sat, 01 Mar 2003 02:58:35 -0500

Update of /cvsroot/dotgnu-pnet/pnetlib/Generics
In directory subversions:/tmp/cvs-serv15771/Generics

Modified Files:
        ArrayList.cs Generics.html 
Added Files:
        RangeList.cs 
Log Message:


Fix the implementation of ArrayList.IndexOf; add the RangeList class.


--- NEW FILE ---
/*
 * RangeList.cs - Wrap an IList to access a sub-range.
 *
 * 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 Generics
{

using System;

public sealed class RangeList<T> : IList<T>, ICloneable
{
        // Internal state.
        private IList<T> list;
        private int index, count;

        // Constructor.
        public RangeList(IList<T> list, int index, int count)
                        {
                                if(list == null)
                                {
                                        throw new ArgumentNullException("list");
                                }
                                if(index < 0 || index > list.Count)
                                {
                                        throw new ArgumentOutOfRangeException
                                                ("index", 
S._("Arg_InvalidArrayIndex"));
                                }
                                else if(count < 0 || (list.Count - index) < 
count)
                                {
                                        throw new 
ArgumentException(S._("Arg_InvalidArrayRange"));
                                }
                                this.list = list;
                                this.index = index;
                                this.count = count;
                        }

        // Implement the IList<T> interface.
        public int Add(T value)
                        {
                                list.Insert(index + count, value);
                                ++count;
                                return index + count - 1;
                        }
        public void Clear()
                        {
                                if(index == 0 && count == list.Count)
                                {
                                        list.Clear();
                                        count = 0;
                                }
                                else
                                {
                                        int posn = index + count - 1;
                                        while(count > 0)
                                        {
                                                list.RemoveAt(posn--);
                                                --count;
                                        }
                                }
                        }
        public bool Contains(T item)
                        {
                                int posn;
                                if(typeof(T).IsValueType)
                                {
                                        for(posn = 0; posn < count; ++posn)
                                        {
                                                if(item.Equals(list[index + 
posn]))
                                                {
                                                        return true;
                                                }
                                        }
                                        return false;
                                }
                                else
                                {
                                        if(((Object)item) != null)
                                        {
                                                for(posn = 0; posn < count; 
++posn)
                                                {
                                                        
if(item.Equals(list[index + posn]))
                                                        {
                                                                return true;
                                                        }
                                                }
                                                return false;
                                        }
                                        else
                                        {
                                                for(posn = 0; posn < count; 
++posn)
                                                {
                                                        if(((Object)(list[index 
+ posn])) == null)
                                                        {
                                                                return true;
                                                        }
                                                }
                                                return false;
                                        }
                                }
                        }
        public IListIterator<T> GetIterator()
                        {
                                return new RangeListIterator<T>(this);
                        }
        public int IndexOf(T value)
                        {
                                int posn;
                                if(typeof(T).IsValueType)
                                {
                                        for(posn = 0; posn < count; ++posn)
                                        {
                                                if(item.Equals(list[index + 
posn]))
                                                {
                                                        return posn;
                                                }
                                        }
                                        return -1;
                                }
                                else
                                {
                                        if(((Object)item) != null)
                                        {
                                                for(posn = 0; posn < count; 
++posn)
                                                {
                                                        
if(item.Equals(list[index + posn]))
                                                        {
                                                                return posn;
                                                        }
                                                }
                                                return -1;
                                        }
                                        else
                                        {
                                                for(posn = 0; posn < count; 
++posn)
                                                {
                                                        if(((Object)(list[index 
+ posn])) == null)
                                                        {
                                                                return posn;
                                                        }
                                                }
                                                return -1;
                                        }
                                }
                        }
        public void Insert(int index, T value)
                        {
                                if(index < 0 || index > count)
                                {
                                        throw new ArgumentOutOfRangeException
                                                ("index", 
S._("ArgRange_Array"));
                                }
                                list.Insert(this.index + index, value);
                                ++count;
                        }
        public void Remove(T value)
                        {
                                int posn;
                                if(typeof(T).IsValueType)
                                {
                                        for(posn = 0; posn < count; ++posn)
                                        {
                                                if(item.Equals(list[index + 
posn]))
                                                {
                                                        list.RemoveAt(index + 
posn);
                                                        return;
                                                }
                                        }
                                }
                                else
                                {
                                        if(((Object)item) != null)
                                        {
                                                for(posn = 0; posn < count; 
++posn)
                                                {
                                                        
if(item.Equals(list[index + posn]))
                                                        {
                                                                
list.RemoveAt(index + posn);
                                                                return;
                                                        }
                                                }
                                        }
                                        else
                                        {
                                                for(posn = 0; posn < count; 
++posn)
                                                {
                                                        if(((Object)(list[index 
+ posn])) == null)
                                                        {
                                                                
list.RemoveAt(index + posn);
                                                                return;
                                                        }
                                                }
                                        }
                                }
                        }
        public void RemoveAt(int index)
                        {
                                if(index < 0 || index >= count)
                                {
                                        throw new ArgumentOutOfRangeException
                                                ("index", 
S._("ArgRange_Array"));
                                }
                                list.Remove(this.index + index);
                                --count;
                        }
        public bool IsRandomAccess
                        {
                                get
                                {
                                        return list.IsRandomAccess;
                                }
                        }
        public T this[int index]
                        {
                                get
                                {
                                        if(index < 0 || index >= count)
                                        {
                                                throw new 
ArgumentOutOfRangeException
                                                        ("index", 
S._("ArgRange_Array"));
                                        }
                                        return list[this.index + index];
                                }
                                set
                                {
                                        if(index < 0 || index >= count)
                                        {
                                                throw new 
ArgumentOutOfRangeException
                                                        ("index", 
S._("ArgRange_Array"));
                                        }
                                        list[this.index + index] = value;
                                }
                        }

        // Implement the ICollection<T> interface.
        public void CopyTo(T[] array, int arrayIndex)
                        {
                                int posn;
                                for(posn = 0; posn < count; ++posn)
                                {
                                        array[arrayIndex++] = list[index + 
posn];
                                }
                        }
        public int Count
                        {
                                get
                                {
                                        return count;
                                }
                        }
        public bool IsFixedSize
                        {
                                get
                                {
                                        return list.IsFixedSize;
                                }
                        }
        public bool IsReadOnly
                        {
                                get
                                {
                                        return list.IsReadOnly;
                                }
                        }
        public bool IsSynchronized
                        {
                                get
                                {
                                        return list.IsSynchronized;
                                }
                        }
        public Object SyncRoot
                        {
                                get
                                {
                                        return list.SyncRoot;
                                }
                        }

        // Implement the IIterable<T> interface.
        IIterator<T> IIterable<T>.GetIterator()
                        {
                                return new RangeListIterator<T>(this);
                        }

        // Implement the ICloneable interface.
        public Object Clone()
                        {
                                if(list is ICloneable)
                                {
                                        return new RangeList<T>
                                                
((IList<T>)(((ICloneable)list).Clone()),
                                                 index, count);
                                }
                                else
                                {
                                        throw new InvalidOperationException
                                                (S._("Invalid_NotCloneable"));
                                }
                        }

        // Range list iterator class.
        private sealed class RangeListIterator<T> : IListIterator<T>
        {
                // Internal state.
                private RangeList<T> list;
                private int position;
                private int removed;
                private bool reset;

                // Constructor.
                public RangeListIterator(RangeList<T> list)
                                {
                                        this.list = list;
                                        position = -1;
                                        removed = -1;
                                        reset = true;
                                }

                // Implement the IIterator<T> interface.
                public bool MoveNext()
                                {
                                        if(reset)
                                        {
                                                // Start at the beginning of 
the range.
                                                position = 0;
                                                reset = false;
                                        }
                                        else if(removed != -1)
                                        {
                                                // An item was removed, so 
re-visit this position.
                                                position = removed;
                                                removed = -1;
                                        }
                                        else
                                        {
                                                ++position;
                                        }
                                        return (position < list.count);
                                }
                public void Reset()
                                {
                                        reset = true;
                                        position = -1;
                                        removed = -1;
                                }
                public void Remove()
                                {
                                        if(position < 0 || position >= 
list.count || removed != -1)
                                        {
                                                throw new 
InvalidOperationException
                                                        
(S._("Invalid_BadIteratorPosition"));
                                        }
                                        list.RemoveAt(position);
                                        removed = position;
                                }
                T IIterator<T>.Current
                                {
                                        get
                                        {
                                                if(position < 0 || position >= 
list.count ||
                                                   removed != -1)
                                                {
                                                        throw new 
InvalidOperationException
                                                                
(S._("Invalid_BadIteratorPosition"));
                                                }
                                                return list.list[position + 
list.index];
                                        }
                                }

                // Implement the IListIterator<T> interface.
                public bool MovePrev()
                                {
                                        if(reset)
                                        {
                                                // Start at the end of the 
range.
                                                position = list.count - 1;
                                                reset = false;
                                        }
                                        else if(removed != -1)
                                        {
                                                // An item was removed, so move 
to just before it.
                                                position = removed - 1;
                                                removed = -1;
                                        }
                                        else
                                        {
                                                --position;
                                        }
                                        return (position >= 0);
                                }
                public int Position
                                {
                                        get
                                        {
                                                if(position < 0 || position >= 
list.count ||
                                                   removed != -1)
                                                {
                                                        throw new 
InvalidOperationException
                                                                
(S._("Invalid_BadIteratorPosition"));
                                                }
                                                return position;
                                        }
                                }
                public T Current
                                {
                                        get
                                        {
                                                if(position < 0 || position >= 
list.count ||
                                                   removed != -1)
                                                {
                                                        throw new 
InvalidOperationException
                                                                
(S._("Invalid_BadIteratorPosition"));
                                                }
                                                return list.list[position + 
list.index];
                                        }
                                        set
                                        {
                                                if(position < 0 || position >= 
list.Count ||
                                                   removed != -1)
                                                {
                                                        throw new 
InvalidOperationException
                                                                
(S._("Invalid_BadIteratorPosition"));
                                                }
                                                list.list[position + 
list.index] = value;
                                        }
                                }

        }; // class RangeListIterator<T>

}; // class RangeList<T>

}; // namespace Generics

Index: ArrayList.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Generics/ArrayList.cs,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -r1.8 -r1.9
*** ArrayList.cs        25 Feb 2003 04:45:17 -0000      1.8
--- ArrayList.cs        1 Mar 2003 07:58:33 -0000       1.9
***************
*** 162,166 ****
        public int IndexOf(T value)
                        {
!                               return IndexOf(value, 0, Count);
                        }
        public void Insert(int index, T value)
--- 162,202 ----
        public int IndexOf(T value)
                        {
!                               int index;
!                               if(typeof(T).IsValueType)
!                               {
!                                       for(index = 0; index < count; ++index)
!                                       {
!                                               if(item.Equals(store[index]))
!                                               {
!                                                       return index;
!                                               }
!                                       }
!                                       return -1;
!                               }
!                               else
!                               {
!                                       if(((Object)item) != null)
!                                       {
!                                               for(index = 0; index < count; 
++index)
!                                               {
!                                                       
if(item.Equals(store[index]))
!                                                       {
!                                                               return index;
!                                                       }
!                                               }
!                                               return -1;
!                                       }
!                                       else
!                                       {
!                                               for(index = 0; index < count; 
++index)
!                                               {
!                                                       
if(((Object)(store[index])) == null)
!                                                       {
!                                                               return index;
!                                                       }
!                                               }
!                                               return -1;
!                                       }
!                               }
                        }
        public void Insert(int index, T value)

Index: Generics.html
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Generics/Generics.html,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** Generics.html       1 Mar 2003 06:22:29 -0000       1.3
--- Generics.html       1 Mar 2003 07:58:33 -0000       1.4
***************
*** 377,380 ****
--- 377,383 ----
                has been satisfied by the argument.</dd>
  
+ <dt><code>RangeList&lt;T&gt;</code></dt>
+       <dd>Wraps up an <code>IList&lt;T&gt;</code> to access a sub-range.</dd>
+ 
  <dt><code>ReverseIterator&lt;T&gt;</code></dt>
        <dd>Wraps up an <code>IListIterator&lt;T&gt;</code> to reverse





reply via email to

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