using UnityEngine; using System.Collections; using System.Collections.Generic; using System; namespace JungleDB { public class List : IEnumerable { private readonly Node head; public List() { this.head = new headNode(); } // T...はC#だとparamsらしい 可変引数型というみたいだ public List(params T[] attributes) { List list = new List (); foreach (T attribute_local in attributes) { list = list.addLast (attribute_local); } } private List(Node head) { this.head = head; } public Node getHead() { return head; } public List add(int num, T attribute) { Node newHead = head.add(0, num, attribute); if (newHead == null) return this; return new List(newHead); } public List addLast(T attribute) { Node newHead = head.addLast(attribute); return new List(newHead); } public T index(int num) { int count = 0; Node currentNode = head.getNext(); while (currentNode != null) { if (count == num) { return currentNode.getAttribute (); } currentNode = currentNode.getNext(); count++; } return default(T); } public IEnumerator iterator() { Node currentNode = head.getNext(); int count = 0; int len = currentNode.length(); while (len != count) { yield return (T)currentNode.getAttribute(); currentNode = currentNode.getNext (); count++; } } IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } public IEnumerator GetEnumerator() { return iterator (); } public List delete(int num) { Node newNode = head.delete(0, num); if (newNode == null) return this; return new List(newNode); } public List replace(int num, T attribute) { Node newHead = head.replaceNode(0, num, attribute); if (newHead == null) return this; return new List(newHead); } public T tail() { return index(length() - 1); } // java code head. public T headList() { return index(0); } public List deleteLast() { return delete(head.length() - 1); } public List deleteHead() { return delete(0); } public int length() { return head.length(); } public List append(List list) { IEnumerator iterator = list.iterator(); List newList = this; while (iterator.MoveNext()) { T attribute = iterator.Current; newList = newList.addLast(attribute); } return newList; } } }