28
|
1 using UnityEngine;
|
|
2 using System.Collections;
|
|
3 using System.Collections.Generic;
|
|
4 using System;
|
|
5
|
|
6 namespace JungleDB {
|
|
7 public class List<T> : IEnumerable<T> {
|
|
8 private readonly Node<T> head;
|
|
9
|
|
10 public List() {
|
|
11 this.head = new headNode<T>();
|
|
12 }
|
|
13
|
|
14 // T...はC#だとparamsらしい 可変引数型というみたいだ
|
|
15 public List(params T[] attributes) {
|
|
16 List<T> list = new List<T> ();
|
|
17 foreach (T attribute_local in attributes) {
|
|
18 list = list.addLast (attribute_local);
|
|
19 }
|
|
20 }
|
|
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) {
|
|
47 if (count == num) {
|
|
48 return currentNode.getAttribute ();
|
|
49 }
|
|
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 int count = 0;
|
|
59 int len = currentNode.length();
|
|
60 while (len != count) {
|
|
61 yield return (T)currentNode.getAttribute();
|
|
62 currentNode = currentNode.getNext ();
|
|
63 count++;
|
|
64 }
|
|
65 }
|
|
66
|
|
67 IEnumerator IEnumerable.GetEnumerator()
|
|
68 {
|
|
69 return this.GetEnumerator();
|
|
70 }
|
|
71
|
|
72 public IEnumerator<T> GetEnumerator()
|
|
73 {
|
|
74 return iterator ();
|
|
75 }
|
|
76
|
|
77
|
|
78 public List<T> delete(int num) {
|
|
79 Node<T> newNode = head.delete(0, num);
|
|
80 if (newNode == null)
|
|
81 return this;
|
|
82 return new List<T>(newNode);
|
|
83 }
|
|
84
|
|
85 public List<T> replace(int num, T attribute) {
|
|
86 Node<T> newHead = head.replaceNode(0, num, attribute);
|
|
87 if (newHead == null)
|
|
88 return this;
|
|
89 return new List<T>(newHead);
|
|
90 }
|
|
91
|
|
92 public T tail() {
|
|
93 return index(length() - 1);
|
|
94 }
|
|
95
|
|
96 // java code head.
|
|
97 public T headList() {
|
|
98 return index(0);
|
|
99 }
|
|
100
|
|
101 public List<T> deleteLast() {
|
|
102 return delete(head.length() - 1);
|
|
103 }
|
|
104
|
|
105 public List<T> deleteHead() {
|
|
106 return delete(0);
|
|
107 }
|
|
108
|
|
109 public int length() {
|
|
110 return head.length();
|
|
111 }
|
|
112
|
|
113 public List<T> append(List<T> list) {
|
|
114 IEnumerator<T> iterator = list.iterator();
|
|
115 List<T> newList = this;
|
|
116 while (iterator.MoveNext()) {
|
|
117 T attribute = iterator.Current;
|
|
118 newList = newList.addLast(attribute);
|
|
119 }
|
|
120 return newList;
|
|
121 }
|
|
122 }
|
|
123 }
|