0
|
1 using UnityEngine;
|
|
2 using System.Collections;
|
|
3 using System.Collections.Generic;
|
|
4 using System;
|
|
5
|
4
|
6 public class List<T> : IEnumerable<T> {
|
|
7 private readonly Node<T> head;
|
0
|
8
|
|
9 public List() {
|
|
10 this.head = new headNode<T>();
|
|
11 }
|
|
12
|
4
|
13 // T...はC#だとparamsらしい 可変引数型というみたいだ
|
|
14 public List(params T[] attributes) {
|
0
|
15 List<T> list = new List<T> ();
|
4
|
16 foreach (T attribute_local in attributes) {
|
|
17 list = list.addLast (attribute_local);
|
|
18 }
|
|
19 this.head = list.getHead ();
|
|
20 }
|
0
|
21
|
|
22 private List(Node<T> head) {
|
|
23 this.head = head;
|
|
24 }
|
|
25
|
|
26 public Node<T> getHead() {
|
|
27 return head;
|
|
28 }
|
|
29
|
|
30 public List<T> add(int num, T attribute) {
|
|
31 Node<T> newHead = head.add(0, num, attribute);
|
|
32 if (newHead == null)
|
|
33 return this;
|
|
34 return new List<T>(newHead);
|
|
35 }
|
|
36
|
|
37 public List<T> addLast(T attribute) {
|
|
38 Node<T> newHead = head.addLast(attribute);
|
|
39 return new List<T>(newHead);
|
|
40 }
|
|
41
|
|
42
|
|
43 public T index(int num) {
|
|
44 int count = 0;
|
|
45 Node<T> currentNode = head.getNext();
|
|
46 while (currentNode != null) {
|
4
|
47 if (count == num) {
|
|
48 return currentNode.getAttribute ();
|
|
49 }
|
0
|
50 currentNode = currentNode.getNext();
|
|
51 count++;
|
|
52 }
|
|
53 return default(T);
|
|
54 }
|
|
55
|
|
56 public IEnumerator<T> iterator() {
|
|
57 Node<T> currentNode = head.getNext();
|
|
58 while (currentNode.getAttribute() != null) {
|
7
|
59 Debug.Log (currentNode.getAttribute ().ToString());
|
0
|
60 yield return (T)currentNode.getAttribute();
|
7
|
61 currentNode = currentNode.getNext ();
|
0
|
62 }
|
|
63 }
|
4
|
64
|
|
65 IEnumerator IEnumerable.GetEnumerator()
|
|
66 {
|
|
67 // call the generic version of the method
|
|
68 return this.GetEnumerator();
|
|
69 }
|
|
70
|
|
71 public IEnumerator<T> GetEnumerator()
|
|
72 {
|
|
73 return iterator ();
|
|
74 }
|
0
|
75
|
|
76
|
|
77 public List<T> delete(int num) {
|
|
78 Node<T> newNode = head.delete(0, num);
|
|
79 if (newNode == null)
|
|
80 return this;
|
|
81 return new List<T>(newNode);
|
|
82 }
|
|
83
|
|
84 public List<T> replace(int num, T attribute) {
|
|
85 Node<T> newHead = head.replaceNode(0, num, attribute);
|
|
86 if (newHead == null)
|
|
87 return this;
|
|
88 return new List<T>(newHead);
|
|
89 }
|
|
90
|
|
91 public T tail() {
|
|
92 return index(length() - 1);
|
|
93 }
|
|
94
|
|
95 // java code head.
|
|
96 public T headList() {
|
|
97 return index(0);
|
|
98 }
|
|
99
|
|
100 public List<T> deleteLast() {
|
|
101 return delete(head.length() - 1);
|
|
102 }
|
|
103
|
|
104 public List<T> deleteHead() {
|
|
105 return delete(0);
|
|
106 }
|
|
107
|
|
108 public int length() {
|
|
109 return head.length();
|
|
110 }
|
|
111
|
|
112 public string toString() {
|
|
113 //IEnumerator<T> iterator = reverseIterator();
|
7
|
114 // pathString += ">";
|
|
115 return "toString";
|
0
|
116 }
|
|
117
|
|
118 public List<T> append(List<T> list) {
|
|
119 IEnumerator<T> iterator = list.iterator();
|
|
120 List<T> newList = this;
|
|
121 while (iterator.MoveNext()) {
|
|
122 T attribute = iterator.Current;
|
|
123 newList = newList.addLast(attribute);
|
|
124 }
|
|
125 return newList;
|
|
126 }
|
|
127
|
|
128 // public IEnumerator<T> iterator() {
|
|
129 // Node<T> currentNode = head.getNext();
|
|
130 // while (currentNode != null) {
|
|
131 // yield return currentNode;
|
|
132 // currentNode = currentNode.getNext();
|
|
133 // }
|
|
134 // }
|
|
135 }
|