Skip to content

Commit

Permalink
Updated FastLinkList to implement IList.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkkellogg committed Jul 27, 2015
1 parent fbd2761 commit bb084c3
Showing 1 changed file with 179 additions and 4 deletions.
183 changes: 179 additions & 4 deletions Unity/Assets/CSG-BSP/Scripts/Structure/FastLinkedList.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace CSG
{
public class FastLinkedList<T>
public class FastLinkedList<T> : IList<T>
{
private Node _First;
private Node _Last;
private int length;

public Node First
{
Expand All @@ -22,6 +24,7 @@ public FastLinkedList()
{
_First = null;
_Last = null;
length = 0;
}

public Node AddLast(T value)
Expand All @@ -39,6 +42,8 @@ public Node AddLast(T value)
_Last = newNode;
}

length++;

return newNode;
}

Expand All @@ -60,12 +65,18 @@ public void AppendIntoList(FastLinkedList<T> list)
_Last = list._Last;
if(_Last.Previous == null)_Last.Previous = temp;
}

length = list.length + length;
}

public void Clear()
public void CopyInto(IList<T> copy)
{
_First = null;
_Last = null;
FastLinkedList<T>.Node current = First;
while(current != null)
{
copy.Add(current.Value);
current = current.Next;
}
}

public class Node
Expand All @@ -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<T>.Add(T val)
{
AddLast(val);
}

bool ICollection<T>.Remove(T val)
{
RemoveAt(IndexOf(val));
return true;
}

void ICollection<T>.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<T> IEnumerable<T>.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.");
}
}
}

0 comments on commit bb084c3

Please sign in to comment.