From bb084c346908f1395ec5f1df4b0f6c1bb754293f Mon Sep 17 00:00:00 2001 From: Mark Kellogg Date: Mon, 27 Jul 2015 10:15:36 -0700 Subject: [PATCH] Updated FastLinkList to implement IList. --- .../Scripts/Structure/FastLinkedList.cs | 183 +++++++++++++++++- 1 file changed, 179 insertions(+), 4 deletions(-) diff --git a/Unity/Assets/CSG-BSP/Scripts/Structure/FastLinkedList.cs b/Unity/Assets/CSG-BSP/Scripts/Structure/FastLinkedList.cs index 1813abc..e030248 100644 --- a/Unity/Assets/CSG-BSP/Scripts/Structure/FastLinkedList.cs +++ b/Unity/Assets/CSG-BSP/Scripts/Structure/FastLinkedList.cs @@ -1,12 +1,14 @@ using UnityEngine; using System.Collections; +using System.Collections.Generic; namespace CSG { - public class FastLinkedList + public class FastLinkedList : IList { private Node _First; private Node _Last; + private int length; public Node First { @@ -22,6 +24,7 @@ public FastLinkedList() { _First = null; _Last = null; + length = 0; } public Node AddLast(T value) @@ -39,6 +42,8 @@ public Node AddLast(T value) _Last = newNode; } + length++; + return newNode; } @@ -60,12 +65,18 @@ public void AppendIntoList(FastLinkedList list) _Last = list._Last; if(_Last.Previous == null)_Last.Previous = temp; } + + length = list.length + length; } - public void Clear() + public void CopyInto(IList copy) { - _First = null; - _Last = null; + FastLinkedList.Node current = First; + while(current != null) + { + copy.Add(current.Value); + current = current.Next; + } } public class Node @@ -79,5 +90,169 @@ public Node(T value) Value = value; } } + + // IList Members + public void Clear() + { + _First = null; + _Last = null; + length = 0; + } + + public bool Contains(T value) + { + bool inList = false; + + Node current = First; + while(current != null) + { + if(current.Value.Equals(value)) + { + inList = true; + break; + } + current = current.Next; + } + + return inList; + } + + public int IndexOf(T value) + { + Node current = First; + int index = 0; + while(current != null) + { + if(current.Value.Equals(value)) + { + return index; + } + index++; + current = current.Next; + } + + return -1; + } + + public void Insert(int index, T value) + { + throw new System.NotSupportedException(); + } + + public bool IsFixedSize + { + get + { + return false; + } + } + + public bool IsReadOnly + { + get + { + return false; + } + } + + public void RemoveAt(int index) + { + throw new System.NotSupportedException("The method or operation is not implemented."); + } + + public T this[int index] + { + get + { + Node current = First; + int i = 0; + while(current != null) + { + if(i == index) + { + return current.Value; + } + i++; + current = current.Next; + } + + throw new System.IndexOutOfRangeException("Index is out of range."); + } + + set + { + Node current = First; + int i = 0; + while(current != null) + { + if(i == index) + { + current.Value = value; + break; + } + i++; + current = current.Next; + } + } + } + + // ICollection Members + void ICollection.Add(T val) + { + AddLast(val); + } + + bool ICollection.Remove(T val) + { + RemoveAt(IndexOf(val)); + return true; + } + + void ICollection.CopyTo(T[] array, int index) + { + int j = index; + Node current = First; + while(current != null) + { + array[j] = current.Value; + j++; + current = current.Next; + } + } + + public int Count + { + get + { + return length; + } + } + + public bool IsSynchronized + { + get + { + return false; + } + } + + public object SyncRoot + { + get + { + return this; + } + } + + // IEnumerable Members + IEnumerator IEnumerable.GetEnumerator() + { + throw new System.NotSupportedException("The method or operation is not implemented."); + } + + IEnumerator IEnumerable.GetEnumerator() + { + throw new System.NotSupportedException("The method or operation is not implemented."); + } } } \ No newline at end of file