view src/samer/silk/SchemeList.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
line wrap: on
line source
package samer.silk;
import java.util.*;
import jscheme.*;

public class SchemeList extends AbstractSequentialList {
	SchemePair	head;

	public SchemeList(SchemePair pair) { head=pair; }

	public ListIterator listIterator(int index) {
		return new Iterator(head,index);
	}

	public int size() {
		int i=0;
		for (SchemePair p=head; !p.isEmpty(); p=(SchemePair)p.getRest()) i++;
		return i;
	}

	class Iterator implements ListIterator {
		SchemePair	current, previous=null;
		int			index=0;

		public Iterator(SchemePair head, int index) { 
			current=head;
			for (int i=0; i<index; i++) next();
		}

		public void add(Object o) { 
			if (previous==null) no();
			current=new jsint.Pair(o,current);
			previous.setRest(current);
		}

		public void remove() { no(); }
//			if (previous==null) no();
//			previous.setRest(current.getRest());
//			current=(SchemePair)current.getRest();
//		}

		public void set(Object o) { previous.setFirst(o); }

		public boolean hasNext() {
			return !current.isEmpty();
		}

		public Object next() {
			Object rc=current.getFirst();
			previous=current;
			current=(SchemePair)current.getRest();
			index++;
			return rc;
		}

		public int nextIndex() { return index; }
		public boolean hasPrevious() { no(); return false; }
		public Object previous() { no(); return null; }
		public int previousIndex() { no(); return 0; }

		private void no() { throw new Error("Unsupported"); }
	}
}