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();
|
8
|
58 while (currentNode != null) {
|
0
|
59 yield return (T)currentNode.getAttribute();
|
7
|
60 currentNode = currentNode.getNext ();
|
0
|
61 }
|
|
62 }
|
4
|
63
|
|
64 IEnumerator IEnumerable.GetEnumerator()
|
|
65 {
|
|
66 // call the generic version of the method
|
|
67 return this.GetEnumerator();
|
|
68 }
|
|
69
|
|
70 public IEnumerator<T> GetEnumerator()
|
|
71 {
|
|
72 return iterator ();
|
|
73 }
|
0
|
74
|
|
75
|
|
76 public List<T> delete(int num) {
|
|
77 Node<T> newNode = head.delete(0, num);
|
|
78 if (newNode == null)
|
|
79 return this;
|
|
80 return new List<T>(newNode);
|
|
81 }
|
|
82
|
|
83 public List<T> replace(int num, T attribute) {
|
|
84 Node<T> newHead = head.replaceNode(0, num, attribute);
|
|
85 if (newHead == null)
|
|
86 return this;
|
|
87 return new List<T>(newHead);
|
|
88 }
|
|
89
|
|
90 public T tail() {
|
|
91 return index(length() - 1);
|
|
92 }
|
|
93
|
|
94 // java code head.
|
|
95 public T headList() {
|
|
96 return index(0);
|
|
97 }
|
|
98
|
|
99 public List<T> deleteLast() {
|
|
100 return delete(head.length() - 1);
|
|
101 }
|
|
102
|
|
103 public List<T> deleteHead() {
|
|
104 return delete(0);
|
|
105 }
|
|
106
|
|
107 public int length() {
|
|
108 return head.length();
|
|
109 }
|
|
110
|
|
111 public List<T> append(List<T> list) {
|
|
112 IEnumerator<T> iterator = list.iterator();
|
|
113 List<T> newList = this;
|
|
114 while (iterator.MoveNext()) {
|
|
115 T attribute = iterator.Current;
|
|
116 newList = newList.addLast(attribute);
|
|
117 }
|
|
118 return newList;
|
|
119 }
|
|
120 }
|