All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin.reflect.jvm.internal.pcollections.ConsPStack Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * Copyright 2010-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kotlin.reflect.jvm.internal.pcollections;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * A simple persistent stack of non-null values.
 * 

* This implementation is thread-safe, although its iterators may not be. */ final class ConsPStack implements Iterable { private static final ConsPStack EMPTY = new ConsPStack(); @SuppressWarnings("unchecked") public static ConsPStack empty() { return (ConsPStack) EMPTY; } final E first; final ConsPStack rest; private final int size; private ConsPStack() { // EMPTY constructor size = 0; first = null; rest = null; } private ConsPStack(E first, ConsPStack rest) { this.first = first; this.rest = rest; this.size = 1 + rest.size; } public E get(int index) { if (index < 0 || index > size) throw new IndexOutOfBoundsException(); try { return iterator(index).next(); } catch (NoSuchElementException e) { throw new IndexOutOfBoundsException("Index: " + index); } } @Override public Iterator iterator() { return iterator(0); } public int size() { return size; } private Iterator iterator(int index) { return new Itr(subList(index)); } private static class Itr implements Iterator { private ConsPStack next; public Itr(ConsPStack first) { this.next = first; } @Override public boolean hasNext() { return next.size > 0; } @Override public E next() { E e = next.first; next = next.rest; return e; } @Override public void remove() { throw new UnsupportedOperationException(); } } public ConsPStack plus(E e) { return new ConsPStack(e, this); } private ConsPStack minus(Object e) { if (size == 0) return this; if (first.equals(e)) // found it return rest; // don't recurse (only remove one) // otherwise keep looking: ConsPStack newRest = rest.minus(e); if (newRest == rest) return this; return new ConsPStack(first, newRest); } public ConsPStack minus(int i) { return minus(get(i)); } private ConsPStack subList(int start) { if (start < 0 || start > size) throw new IndexOutOfBoundsException(); if (start == 0) return this; return rest.subList(start - 1); } }