comparison Main/jungle-main/data/list/List.cs @ 28:9588ad364fdd

Last commit before change.
author Kazuma Takeda
date Wed, 18 Jan 2017 19:53:29 +0900
parents 1f99e150f336
children f2ea780b3e80
comparison
equal deleted inserted replaced
27:9ff715ff8e09 28:9588ad364fdd
1 using UnityEngine; 1 using UnityEngine;
2 using System.Collections; 2 using System.Collections;
3 using System.Collections.Generic; 3 using System.Collections.Generic;
4 using System; 4 using System;
5 5
6 public class List<T> : IEnumerable<T> { 6 namespace JungleDB {
7 private readonly Node<T> head; 7 public class List<T> : IEnumerable<T> {
8 private readonly Node<T> head;
8 9
9 public List() { 10 public List() {
10 this.head = new headNode<T>(); 11 this.head = new headNode<T>();
11 } 12 }
12 13
13 // T...はC#だとparamsらしい 可変引数型というみたいだ 14 // T...はC#だとparamsらしい 可変引数型というみたいだ
14 public List(params T[] attributes) { 15 public List(params T[] attributes) {
15 List<T> list = new List<T> (); 16 List<T> list = new List<T> ();
16 foreach (T attribute_local in attributes) { 17 foreach (T attribute_local in attributes) {
17 list = list.addLast (attribute_local); 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;
18 } 121 }
19 } 122 }
20
21 private List(Node<T> head) {
22 this.head = head;
23 }
24
25 public Node<T> getHead() {
26 return head;
27 }
28
29 public List<T> add(int num, T attribute) {
30 Node<T> newHead = head.add(0, num, attribute);
31 if (newHead == null)
32 return this;
33 return new List<T>(newHead);
34 }
35
36 public List<T> addLast(T attribute) {
37 Node<T> newHead = head.addLast(attribute);
38 return new List<T>(newHead);
39 }
40
41
42 public T index(int num) {
43 int count = 0;
44 Node<T> currentNode = head.getNext();
45 while (currentNode != null) {
46 if (count == num) {
47 return currentNode.getAttribute ();
48 }
49 currentNode = currentNode.getNext();
50 count++;
51 }
52 return default(T);
53 }
54
55 public IEnumerator<T> iterator() {
56 Node<T> currentNode = head.getNext();
57 int count = 0;
58 int len = currentNode.length();
59 while (len != count) {
60 yield return (T)currentNode.getAttribute();
61 currentNode = currentNode.getNext ();
62 count++;
63 }
64 }
65
66 IEnumerator IEnumerable.GetEnumerator()
67 {
68 return this.GetEnumerator();
69 }
70
71 public IEnumerator<T> GetEnumerator()
72 {
73 return iterator ();
74 }
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 List<T> append(List<T> list) {
113 IEnumerator<T> iterator = list.iterator();
114 List<T> newList = this;
115 while (iterator.MoveNext()) {
116 T attribute = iterator.Current;
117 newList = newList.addLast(attribute);
118 }
119 return newList;
120 }
121 } 123 }