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

com.ajjpj.abase.collection.mutable.ArrayStack Maven / Gradle / Ivy

Go to download

a-base is a library of basic (hence the name) classes, most notably immutable collection classes with copy-on-write operations

There is a newer version: 1.0-pre11
Show newest version
package com.ajjpj.abase.collection.mutable;

import com.ajjpj.abase.collection.ACollectionHelper;
import com.ajjpj.abase.collection.immutable.AOption;

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


/**
 * This is a mutable array-based implementation of a LIFO stack. It is intended as a replacement of Java's
 *  java.util.Stack that is broken in many ways.
 *
 * @author arno
 */
public class ArrayStack implements Iterable {
    private T[] data;
    private int size = 0;

    public ArrayStack() {
        this(10);
    }

    @SuppressWarnings("unchecked")
    public ArrayStack(int initialSize) {
        if(initialSize <= 0) {
            throw new IllegalArgumentException("size must be greater than 0");
        }

        data = (T[]) new Object[initialSize];
    }

    /**
     * Adds an element to the top of the stack.
     */
    @SuppressWarnings("unchecked")
    public void push(T el) {
        if(size >= data.length) {
            final T[] oldData = data;
            data = (T[]) new Object[2*oldData.length];
            System.arraycopy(oldData, 0, data, 0, oldData.length);
        }

        data[size] = el;
        size += 1;
    }

    /**
     * Removes and returns the element at the top of the stack, throwing a NoSuchElementException if the
     *  stack is empty.
     */
    public T pop() {
        if(isEmpty()) {
            throw new NoSuchElementException("stack is empty");
        }
        final T result = data[size-1];
        data[size-1] = null; // allow GC of previous element
        size -= 1;
        return result;
    }

    /**
     * Returns the element at the top of the stack without removing it, throwing a NoSuchElementException
     *  if the stack is empty.
     */
    public T peek() {
        if(isEmpty()) {
            throw new NoSuchElementException("stack is empty");
        }
        return data[size-1];
    }

    /**
     * Removes the element at the top of the stack and returns it in an AOption.some(...) if the stack
     *  is non-empty, and returns AOption.none() otherwise.
     */
    public AOption tryPop() {
        if(isEmpty()) {
            return AOption.none();
        }
        return AOption.some(pop());
    }

    /**
     * Returns the element at the top of the stack in an AOption.some(...) without removing it, or returns
     *  AOption.none() if the stack is empty.
     */
    public AOption tryPeek() {
        if(isEmpty()) {
            return AOption.none();
        }
        return AOption.some(data[size-1]);
    }


    public boolean contains(T el) {
        for(int i=0; ipop() order without modifying the stack
     */
    public Iterator iterator() {
        return new Iterator() {
            int idx=size();

            @Override public boolean hasNext() {
                return idx>0;
            }

            @Override public T next() {
                idx -= 1;
                return data[idx];
            }

            @Override public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

    /**
     * iterates through the stack's elements in reverse pop() order, i.e. starting with the element that
     *  was added first end finishing with the most recently added element.
     */
    public Iterator reverseIterator() {
        return new Iterator() {
            int idx = 0;

            @Override
            public boolean hasNext() {
                return idx < size();
            }

            @Override public T next() {
                return data[idx++];
            }

            @Override public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }
    public boolean nonEmpty() {
        return size > 0;
    }

    @Override
    public String toString() {
        return ACollectionHelper.mkString(this, "[", ", ", "]");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy