Created
July 18, 2014 10:09
-
-
Save kuanyingchou/4ecfd50a669f5edbf852 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class TestYield : MonoBehaviour | |
{ | |
public void Start() { | |
TestAdd(); | |
TestOperator(); | |
} | |
public IntList GetIntList() | |
{ | |
//return new IntList(); | |
return new IntListWithYield(); | |
//return new IntListExpandPrint(); | |
} | |
public void TestAdd() | |
{ | |
IntList list = GetIntList(); | |
list.Add(3); | |
list.Add(5); | |
list.Add(7); | |
list.Print(); | |
} | |
public void TestAdd2() | |
{ | |
IntList list = GetIntList(); | |
for (int i = 0; i < 100; i++) | |
{ | |
list.Add(i); | |
} | |
list.Print(); | |
} | |
public void TestOperator() | |
{ | |
IntList list = GetIntList(); | |
list.Add(3); | |
list.Add(5); | |
list.Add(7); | |
list.Print(); | |
list[0] = 1; | |
list.Print(); | |
} | |
public class IntList : IEnumerable { | |
static readonly int INITIAL_SLOTS = 10; | |
protected int[] values = new int[INITIAL_SLOTS]; | |
protected int next = 0; | |
public void Add(int value) | |
{ | |
values[next++] = value; | |
if (next >= values.Length) { | |
Expand(); | |
} | |
} | |
public int Get(int index) | |
{ | |
if (index >= next || index < 0) throw new System.ArgumentException(); | |
return values[index]; | |
} | |
public void Set(int index, int value) { | |
if (index > next || index < 0) throw new System.ArgumentException(); | |
values[index] = value; | |
} | |
public int this[int index] | |
{ | |
get { return Get(index); } | |
set { Set(index, value); } | |
} | |
private void Expand() | |
{ | |
int[] newValues = new int[values.Length * 2]; | |
for (int i = 0; i < values.Length; i++) | |
{ | |
newValues[i] = values[i]; | |
} | |
values = newValues; | |
} | |
public int Count { get { return next; } } | |
public virtual IEnumerator GetEnumerator() | |
{ | |
return new IntListEnum(this); | |
} | |
public virtual void Print() | |
{ | |
foreach (var i in this) | |
{ | |
Debug.Log(i); | |
} | |
} | |
public class IntListEnum : IEnumerator | |
{ | |
int next = -1; | |
IntList list; | |
public IntListEnum(IntList list) | |
{ | |
this.list = list; | |
} | |
object IEnumerator.Current { | |
get { | |
try | |
{ | |
return list.values[next]; | |
} | |
catch (System.ArgumentOutOfRangeException) | |
{ | |
return new System.InvalidOperationException(); | |
} | |
} | |
} | |
public bool MoveNext() | |
{ | |
next++; | |
if (next >= list.Count) | |
{ | |
return false; | |
} | |
else | |
{ | |
return true; | |
} | |
} | |
public void Reset() | |
{ | |
next = -1; | |
} | |
} | |
} | |
public class IntListWithYield : IntList | |
{ | |
public override IEnumerator GetEnumerator() | |
{ | |
yield return StartCoroutine(GetEnumeratorImpl()); | |
} | |
private object StartCoroutine(IEnumerator e) | |
{ | |
while (e.MoveNext()) | |
return e.Current; | |
return null; | |
} | |
private IEnumerator GetEnumeratorImpl() | |
{ | |
for (int i = 0; i < next; i++) | |
{ | |
yield return values[i]; | |
} | |
} | |
} | |
public class IntListExpandPrint : IntList | |
{ | |
public override void Print() | |
{ | |
IEnumerator enumerator = GetEnumerator(); | |
while (enumerator.MoveNext()) | |
{ | |
object curr = enumerator.Current; | |
Debug.Log(curr); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment