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 IDeque.cs,NONE,1.1 LinkedLi


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/Generics IDeque.cs,NONE,1.1 LinkedList.cs,1.3,1.4
Date: Mon, 24 Feb 2003 00:52:43 -0500

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

Modified Files:
        LinkedList.cs 
Added Files:
        IDeque.cs 
Log Message:


Add the IDeque interface, and make LinkedList implement
IDeque, IQueue, and IStack.


--- NEW FILE ---
/*
 * IDeque.cs - Interface to deque-like structures.
 *
 * 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 interface IDeque<T> : ICollection<T>
{
        void PushFront(T value);
        void PushBack(T value);
        T PopFront();
        T PopBack();
        T PeekFront();
        T PeekEnd();
        T[] ToArray();

}; // interface IDeque<T>

}; // namespace Generics

Index: LinkedList.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Generics/LinkedList.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** LinkedList.cs       24 Feb 2003 05:26:09 -0000      1.3
--- LinkedList.cs       24 Feb 2003 05:52:40 -0000      1.4
***************
*** 28,32 ****
  using System;
  
! public class LinkedList<T> : ICollection<T>, IList<T>, IIterable<T>, 
ICloneable
  {
        // Structure of a list node.
--- 28,33 ----
  using System;
  
! public class LinkedList<T>
!       : IDeque<T>, IQueue<T>, IStack<T>, IList<T>, ICloneable
  {
        // Structure of a list node.
***************
*** 129,132 ****
--- 130,277 ----
                        }
  
+       // Implement the IDeque<T> interface.
+       public virtual void PushFront(T value)
+                       {
+                               Node<T> node = new Node<T>(item);
+                               node.next = first;
+                               if(first != null)
+                               {
+                                       first.prev = node;
+                               }
+                               else
+                               {
+                                       last = node;
+                               }
+                               first = node;
+                               ++count;
+                       }
+       public virtual void PushBack(T value)
+                       {
+                               Node<T> node = new Node<T>(item);
+                               node.prev = last;
+                               if(last != null)
+                               {
+                                       last.next = node;
+                               }
+                               else
+                               {
+                                       first = node;
+                               }
+                               last = node;
+                               ++count;
+                       }
+       public virtual T PopFront()
+                       {
+                               if(first != null)
+                               {
+                                       Node<T> node = first;
+                                       if(node.next != null)
+                                       {
+                                               node.next.prev = null;
+                                       }
+                                       else
+                                       {
+                                               last = null;
+                                       }
+                                       first = node.next;
+                                       --count;
+                                       return node.data;
+                               }
+                               else
+                               {
+                                       throw new InvalidOperationException
+                                               (S._("Invalid_EmptyList"));
+                               }
+                       }
+       public virtual T PopBack()
+                       {
+                               if(last != null)
+                               {
+                                       Node<T> node = last;
+                                       if(node.prev != null)
+                                       {
+                                               node.prev.next = null;
+                                       }
+                                       else
+                                       {
+                                               first = null;
+                                       }
+                                       last = node.prev;
+                                       --count;
+                                       return node.data;
+                               }
+                               else
+                               {
+                                       throw new InvalidOperationException
+                                               (S._("Invalid_EmptyList"));
+                               }
+                       }
+       public virtual T PeekFront()
+                       {
+                               if(first != null)
+                               {
+                                       return first.data;
+                               }
+                               else
+                               {
+                                       throw new InvalidOperationException
+                                               (S._("Invalid_EmptyList"));
+                               }
+                       }
+       public virtual T PeekEnd()
+                       {
+                               if(last != null)
+                               {
+                                       return last.data;
+                               }
+                               else
+                               {
+                                       throw new InvalidOperationException
+                                               (S._("Invalid_EmptyList"));
+                               }
+                       }
+       public virtual T[] ToArray()
+                       {
+                               T[] array = new T [Count];
+                               CopyTo(array, 0);
+                               return array;
+                       }
+ 
+       // Implement the IQueue<T> interface privately.
+       void IQueue<T>.Enqueue(T value)
+                       {
+                               PushBack(value);
+                       }
+       T IQueue<T>.Dequeue()
+                       {
+                               return PopFront();
+                       }
+       T IQueue<T>.Peek()
+                       {
+                               return PeekFront();
+                       }
+       T[] IQueue<T>.ToArray()
+                       {
+                               return ToArray();
+                       }
+ 
+       // Implement the IStack<T> interface privately.
+       void IStack<T>.Push(T value)
+                       {
+                               PushFront(value);
+                       }
+       T IStack<T>.Pop()
+                       {
+                               return PopFront();
+                       }
+       T IStack<T>.Peek()
+                       {
+                               return PeekFront();
+                       }
+       T[] IStack<T>.ToArray()
+                       {
+                               return ToArray();
+                       }
+ 
        // Implement the ICollection<T> interface.
        public virtual void CopyTo(T[] array, int index)
***************
*** 164,168 ****
                        {
                                int index = count;
!                               AddLast(value);
                                return index;
                        }
--- 309,313 ----
                        {
                                int index = count;
!                               PushBack(value);
                                return index;
                        }
***************
*** 271,275 ****
                                if(index == count)
                                {
!                                       AddLast(value);
                                }
                                else
--- 416,420 ----
                                if(index == count)
                                {
!                                       PushBack(value);
                                }
                                else
***************
*** 365,483 ****
                                while(e.MoveNext())
                                {
!                                       clone.AddLast(e.Current);
                                }
                                return clone;
                        }
  
-       // Add a data item to the end of this list.
-       public virtual void AddLast(T data)
-                       {
-                               Node<T> node = new Node<T>(item);
-                               node.prev = last;
-                               if(last != null)
-                               {
-                                       last.next = node;
-                               }
-                               else
-                               {
-                                       first = node;
-                               }
-                               last = node;
-                               ++count;
-                       }
- 
-       // Add a data item to the front of this list.
-       public virtual void AddFirst(T data)
-                       {
-                               Node<T> node = new Node<T>(item);
-                               node.next = first;
-                               if(first != null)
-                               {
-                                       first.prev = node;
-                               }
-                               else
-                               {
-                                       last = node;
-                               }
-                               first = node;
-                               ++count;
-                       }
- 
-       // Remove the last data item in the list.
-       public virtual T RemoveLast()
-                       {
-                               if(last != null)
-                               {
-                                       Node<T> node = last;
-                                       if(node.prev != null)
-                                       {
-                                               node.prev.next = null;
-                                       }
-                                       else
-                                       {
-                                               first = null;
-                                       }
-                                       last = node.prev;
-                                       --count;
-                                       return node.data;
-                               }
-                               else
-                               {
-                                       throw new InvalidOperationException
-                                               (S._("Invalid_EmptyList"));
-                               }
-                       }
- 
-       // Remove the first data item in the list.
-       public virtual T RemoveFirst()
-                       {
-                               if(first != null)
-                               {
-                                       Node<T> node = first;
-                                       if(node.next != null)
-                                       {
-                                               node.next.prev = null;
-                                       }
-                                       else
-                                       {
-                                               last = null;
-                                       }
-                                       first = node.next;
-                                       --count;
-                                       return node.data;
-                               }
-                               else
-                               {
-                                       throw new InvalidOperationException
-                                               (S._("Invalid_EmptyList"));
-                               }
-                       }
- 
-       // Remove a data item from the list (from the front).
-       public T Remove()
-                       {
-                               return RemoveFirst();
-                       }
- 
-       // Wrap a list to make it synchronized.
-       public static LinkedList<T> Synchronized<T>(LinkedList<T> list)
-                       {
-                               if(list == null)
-                               {
-                                       throw new ArgumentNullException("list");
-                               }
-                               return new SynchronizedList<T>(list);
-                       }
- 
-       // Wrap a list to make it read-only.
-       public static LinkedList<T> ReadOnly<T>(LinkedList<T> list)
-                       {
-                               if(list == null)
-                               {
-                                       throw new ArgumentNullException("list");
-                               }
-                               return new ReadOnlyList<T>(list);
-                       }
- 
        // Iterator class for lists.
        private class ListIterator<T> : IListIterator<T>
--- 510,518 ----
                                while(e.MoveNext())
                                {
!                                       clone.PushBack(e.Current);
                                }
                                return clone;
                        }
  
        // Iterator class for lists.
        private class ListIterator<T> : IListIterator<T>
***************
*** 601,913 ****
  
        }; // class ListIterator<T>
- 
-       // Wrapper class for synchronized lists.
-       private sealed class SynchronizedList<T> : IList<T>
-       {
-               // Internal state.
-               private LinkedList<T> list;
- 
-               // Constructor.
-               public SynchronizedList(LinkedList<T> list)
-                               {
-                                       this.list = list;
-                               }
- 
-               // Override the parent's API, wrapping everything in a lock.
-               public override void CopyTo(T[] array, int index)
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               list.CopyTo(array, index);
-                                       }
-                               }
-               public override int Count
-                               {
-                                       get
-                                       {
-                                               lock(SyncRoot)
-                                               {
-                                                       return list.Count;
-                                               }
-                                       }
-                               }
-               public override bool IsSynchronized
-                               {
-                                       get
-                                       {
-                                               return true;
-                                       }
-                               }
-               public override Object SyncRoot
-                               {
-                                       get
-                                       {
-                                               return list.SyncRoot;
-                                       }
-                               }
-               public override int Add(T value)
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               return list.Add(value);
-                                       }
-                               }
-               public override void Clear()
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               list.Clear();
-                                       }
-                               }
-               public override bool Contains(T value)
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               return list.Contains(value);
-                                       }
-                               }
-               public override IListIterator<T> GetIterator()
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               return new 
SynchronizedListIterator
-                                                       (list.GetIterator());
-                                       }
-                               }
-               public override int IndexOf(T value)
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               return list.IndexOf(value);
-                                       }
-                               }
-               public override void Insert(int index, T value)
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               list.Insert(index, value);
-                                       }
-                               }
-               public override void Remove(T value)
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               list.Remove(value);
-                                       }
-                               }
-               public override void RemoveAt(int index)
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               list.RemoveAt(index);
-                                       }
-                               }
-               public override bool IsFixedSize
-                               {
-                                       get
-                                       {
-                                               lock(SyncRoot)
-                                               {
-                                                       return list.IsFixedSize;
-                                               }
-                                       }
-                               }
-               public override bool IsReadOnly
-                               {
-                                       get
-                                       {
-                                               lock(SyncRoot)
-                                               {
-                                                       return list.IsReadOnly;
-                                               }
-                                       }
-                               }
-               public override T this[int index]
-                               {
-                                       get
-                                       {
-                                               lock(SyncRoot)
-                                               {
-                                                       return list[index];
-                                               }
-                                       }
-                                       set
-                                       {
-                                               lock(SyncRoot)
-                                               {
-                                                       list[index] = value;
-                                               }
-                                       }
-                               }
-               public override Object Clone()
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               return new SynchronizedList<T>
-                                                       
((LinkedList<T>)(list.Clone()));
-                                       }
-                               }
-               public override void AddLast(T data)
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               list.AddLast(data);
-                                       }
-                               }
-               public override void AddFirst(T data)
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               list.AddFirst(data);
-                                       }
-                               }
-               public override T RemoveLast()
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               return list.RemoveLast();
-                                       }
-                               }
-               public override T RemoveFirst()
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               return list.RemoveFirst();
-                                       }
-                               }
- 
-       }; // class SynchronizedList<T>
- 
-       // Wrapper class for read-only lists.
-       private sealed class ReadOnlyList<T> : IList<T>
-       {
-               // Internal state.
-               private LinkedList<T> list;
- 
-               // Constructor.
-               public ReadOnlyList(LinkedList<T> list)
-                               {
-                                       this.list = list;
-                               }
- 
-               // Override the parent's API, stubbing non-read methods.
-               public override void CopyTo(T[] array, int index)
-                               {
-                                       list.CopyTo(array, index);
-                               }
-               public override int Count
-                               {
-                                       get
-                                       {
-                                               return list.Count;
-                                       }
-                               }
-               public override bool IsSynchronized
-                               {
-                                       get
-                                       {
-                                               return list.IsSynchronized;
-                                       }
-                               }
-               public override Object SyncRoot
-                               {
-                                       get
-                                       {
-                                               return list.SyncRoot;
-                                       }
-                               }
-               public override int Add(T value)
-                               {
-                                       throw new InvalidOperationException
-                                               (S._("NotSupp_ReadOnly"));
-                               }
-               public override void Clear()
-                               {
-                                       throw new InvalidOperationException
-                                               (S._("NotSupp_ReadOnly"));
-                               }
-               public override bool Contains(T value)
-                               {
-                                       return list.Contains(value);
-                               }
-               public override IListIterator<T> GetIterator()
-                               {
-                                       return new 
ReadOnlyListIterator(list.GetIterator());
-                               }
-               public override int IndexOf(T value)
-                               {
-                                       return list.IndexOf(value);
-                               }
-               public override void Insert(int index, T value)
-                               {
-                                       throw new InvalidOperationException
-                                               (S._("NotSupp_ReadOnly"));
-                               }
-               public override void Remove(T value)
-                               {
-                                       throw new InvalidOperationException
-                                               (S._("NotSupp_ReadOnly"));
-                               }
-               public override void RemoveAt(int index)
-                               {
-                                       throw new InvalidOperationException
-                                               (S._("NotSupp_ReadOnly"));
-                               }
-               public override bool IsFixedSize
-                               {
-                                       get
-                                       {
-                                               return list.IsFixedSize;
-                                       }
-                               }
-               public override bool IsReadOnly
-                               {
-                                       get
-                                       {
-                                               return true;
-                                       }
-                               }
-               public override T this[int index]
-                               {
-                                       get
-                                       {
-                                               return list[index];
-                                       }
-                                       set
-                                       {
-                                               throw new 
InvalidOperationException
-                                                       
(S._("NotSupp_ReadOnly"));
-                                       }
-                               }
-               public override Object Clone()
-                               {
-                                       lock(SyncRoot)
-                                       {
-                                               return new ReadOnlyList<T>
-                                                       
((LinkedList<T>)(list.Clone()));
-                                       }
-                               }
-               public override void AddLast(T data)
-                               {
-                                       throw new InvalidOperationException
-                                               (S._("NotSupp_ReadOnly"));
-                               }
-               public override void AddFirst(T data)
-                               {
-                                       throw new InvalidOperationException
-                                               (S._("NotSupp_ReadOnly"));
-                               }
-               public override T RemoveLast()
-                               {
-                                       throw new InvalidOperationException
-                                               (S._("NotSupp_ReadOnly"));
-                               }
-               public override T RemoveFirst()
-                               {
-                                       throw new InvalidOperationException
-                                               (S._("NotSupp_ReadOnly"));
-                               }
- 
-       }; // class ReadOnlyList<T>
  
  }; // class LinkedList<T>
--- 636,639 ----





reply via email to

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