Guest User

Untitled

a guest
Jan 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. package org.uwaterloo.cs.deltaconway;
  2.  
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5.  
  6. public class DeltaList<E> implements Collection<E> {
  7.  
  8.     Node sentinel;
  9.     Node tail;
  10.     int size;
  11.  
  12.     private class Node {
  13.         E e;
  14.         Node next;
  15.     }
  16.  
  17.     public class DeltaIterator implements Iterator<E> {
  18.         Node current, last, secondLast;
  19.         boolean canRemove;
  20.  
  21.         public DeltaIterator(Node sentinel) {
  22.             last = sentinel;
  23.             current = sentinel.next;
  24.             canRemove = false;
  25.         }
  26.  
  27.         @Override
  28.         public boolean hasNext() {
  29.             return current.next != null;
  30.         }
  31.  
  32.         @Override
  33.         public E next() {
  34.             canRemove = true;
  35.             E e = current.e;
  36.             secondLast = last;
  37.             last = current;
  38.             current = current.next;
  39.             return e;
  40.         }
  41.  
  42.         @Override
  43.         public void remove() {
  44.             if (!canRemove) {
  45.                 throw new IllegalStateException();
  46.             }
  47.             secondLast.next = current;
  48.             last = secondLast;
  49.             canRemove = false;
  50.         }
  51.  
  52.         private DeltaIterator createIteratorPlusOne() {
  53.             return new DeltaIterator(current);
  54.         }
  55.     }
  56.  
  57.     public DeltaList() {
  58.         sentinel = new Node();
  59.         tail = sentinel;
  60.         size = 0;
  61.     }
  62.  
  63.     @Override
  64.     public boolean add(E e) {
  65.         Node node = new Node();
  66.         node.e = e;
  67.         tail.next = node;
  68.         tail = node;
  69.         size++;
  70.         return true;
  71.     }
  72.  
  73.     @Override
  74.     public boolean addAll(Collection<? extends E> c) {
  75.         throw new UnsupportedOperationException();
  76.     }
  77.  
  78.     @Override
  79.     public void clear() {
  80.         throw new UnsupportedOperationException();
  81.     }
  82.  
  83.     @Override
  84.     public boolean contains(Object o) {
  85.         E e;
  86.         Iterator<E> it = iterator();
  87.         while (it.hasNext()) {
  88.             e = it.next();
  89.             if (o == null ? e == null : o.equals(e)) {
  90.                 return true;
  91.             }
  92.         }
  93.         return false;
  94.     }
  95.  
  96.     @Override
  97.     public boolean containsAll(Collection<?> c) {
  98.         Iterator<?> it;
  99.         it = c.iterator();
  100.         while (it.hasNext()) {
  101.             if (!contains(it.next()))
  102.                 return false;
  103.         }
  104.         return true;
  105.     }
  106.  
  107.     @Override
  108.     public boolean isEmpty() {
  109.         return sentinel.next == null;
  110.     }
  111.  
  112.     @Override
  113.     public Iterator<E> iterator() {
  114.         return deltaIterator();
  115.     }
  116.  
  117.     /**
  118.      * Creates a delta iterator.
  119.      *
  120.      * @return A delta iterator.
  121.      */
  122.     public DeltaIterator deltaIterator() {
  123.         return new DeltaIterator(sentinel);
  124.     }
  125.  
  126.     /**
  127.      * Creates a delta iterator from an existing delta iterator by advancing the
  128.      * existing iterator. The existing iterator is not actually modified and can
  129.      * be used to traverse the list. Once the existing iterator is used again,
  130.      * the new iterator cannot be used.
  131.      *
  132.      * @param it
  133.      *            The existing iterator.
  134.      * @return A new iterator.
  135.      */
  136.     public DeltaIterator deltaIteratorPlusOne(DeltaIterator it) {
  137.         return it.createIteratorPlusOne();
  138.     }
  139.  
  140.     @Override
  141.     public boolean remove(Object o) {
  142.         throw new UnsupportedOperationException();
  143.     }
  144.  
  145.     @Override
  146.     public boolean removeAll(Collection<?> c) {
  147.         throw new UnsupportedOperationException();
  148.     }
  149.  
  150.     @Override
  151.     public boolean retainAll(Collection<?> c) {
  152.         throw new UnsupportedOperationException();
  153.     }
  154.  
  155.     @Override
  156.     public int size() {
  157.         return size;
  158.     }
  159.  
  160.     @Override
  161.     public Object[] toArray() {
  162.         int i = 0;
  163.         Object[] array = new Object[size];
  164.         Iterator<E> it = iterator();
  165.         while (it.hasNext()) {
  166.             array[i++] = it.next();
  167.         }
  168.         return array;
  169.     }
  170.  
  171.     @Override
  172.     public <T> T[] toArray(T[] a) {
  173.         // TODO
  174.         throw new UnsupportedOperationException();
  175.     }
  176.  
  177. }
Add Comment
Please, Sign In to add comment