
com.carrotsearch.hppcrt.lists.LongStack Maven / Gradle / Ivy
package com.carrotsearch.hppcrt.lists;
import java.util.*;
import com.carrotsearch.hppcrt.*;
import com.carrotsearch.hppcrt.cursors.*;
import com.carrotsearch.hppcrt.predicates.*;
import com.carrotsearch.hppcrt.procedures.*;
import com.carrotsearch.hppcrt.sorting.*;
/**
* An extension to {@link LongArrayList} adding stack-related utility methods. The top of
* the stack is at the {@link #size()} - 1
buffer position.
* However, this stack is also a LongIndexedContainer, for which index 0 is the top of the stack,
* and index size() -1 is the bottom of the stack.
*
* See {@link ObjectArrayList} class for API similarities and differences against Java
* Collections.
*/
@javax.annotation.Generated(date = "2014-08-19T19:56:32+0200", value = "HPPC-RT generated from: LongStack.java")
public class LongStack extends LongArrayList
{
/**
* Create with default sizing strategy and initial capacity.
*
* @see BoundedProportionalArraySizingStrategy
*/
public LongStack()
{
super();
}
/**
* Create with default sizing strategy and the given initial capacity.
*
* @see BoundedProportionalArraySizingStrategy
*/
public LongStack(final int initialCapacity)
{
super(initialCapacity);
}
/**
* Create with a custom buffer resizing strategy.
*/
public LongStack(final int initialCapacity, final ArraySizingStrategy resizer)
{
super(initialCapacity, resizer);
}
/**
* Create a stack by pushing all elements of another container to it.
*/
public LongStack(final LongContainer container)
{
super(container);
}
/**
* {@inheritDoc}
* @param index : counted from the top of the stack, i.e = 0 if top, bottom is index = size() - 1
*/
@Override
public void insert(final int index, final long e1)
{
super.insert(this.elementsCount - index, e1);
}
/**
* {@inheritDoc}
* @param index : counted from the top of the stack, i.e = 0 if top, bottom is index = size() - 1
*/
@Override
public long get(final int index)
{
return super.get(this.elementsCount - index - 1);
}
/**
* {@inheritDoc}
* @param index : counted from the top of the stack, i.e = 0 if top, bottom is index = size() - 1
*/
@Override
public long set(final int index, final long e1)
{
return super.set(this.elementsCount - index - 1, e1);
}
/**
* {@inheritDoc}
* @param index : counted from the top of the stack, i.e = 0 if top, bottom is index = size() - 1
*/
@Override
public long remove(final int index)
{
return super.remove(this.elementsCount - index - 1);
}
/**
* {@inheritDoc}
* @param index : counted from the top of the stack, i.e = 0 if top, bottom is index = size() - 1
*/
@Override
public void removeRange(final int fromIndex, final int toIndex)
{
final int size = size();
final int startRemoveRange = size - toIndex;
final int endRemoveRange = size - fromIndex;
super.removeRange(startRemoveRange, endRemoveRange);
}
/**
* {@inheritDoc}
* The first occurrence is counted from the top of the stack, going to the bottom.
*/
@Override
public int removeFirstOccurrence(final long e1)
{
return super.removeFirstOccurrence(e1);
}
/**
* {@inheritDoc}
* The last occurrence is counted from the bottom of the stack, going upwards to the top.
*/
@Override
public int removeLastOccurrence(final long e1)
{
return super.removeLastOccurrence(e1);
}
/**
* {@inheritDoc}
* @return counted from the top of the stack, i.e = 0 if top, bottom is index = size() - 1
*/
@Override
public int indexOf(final long e1)
{
//reverse logic
int res = super.lastIndexOf(e1);
if (res != -1)
{
res = elementsCount - res - 1;
}
return res;
}
@Override
public int lastIndexOf(final long e1)
{
//reverse logic
int res = super.indexOf(e1);
if (res != -1)
{
res = elementsCount - res - 1;
}
return res;
}
/**
* {@inheritDoc}
* @return Returns the target argument for chaining, built from top of the stack to bottom.
*/
@Override
public long[] toArray(final long[] target)
{
final int size = elementsCount;
//copy the buffer backwards.
for (int i = 0; i < size; i++)
{
target[i] = buffer[size - i - 1];
}
return target;
}
/**
* Adds one long to the stack.
*/
public void push(final long e1)
{
ensureBufferSpace(1);
buffer[elementsCount++] = e1;
}
/**
* Adds two longs to the stack.
*/
public void push(final long e1, final long e2)
{
ensureBufferSpace(2);
buffer[elementsCount++] = e1;
buffer[elementsCount++] = e2;
}
/**
* Adds three longs to the stack.
*/
public void push(final long e1, final long e2, final long e3)
{
ensureBufferSpace(3);
buffer[elementsCount++] = e1;
buffer[elementsCount++] = e2;
buffer[elementsCount++] = e3;
}
/**
* Adds four longs to the stack.
*/
public void push(final long e1, final long e2, final long e3, final long e4)
{
ensureBufferSpace(4);
buffer[elementsCount++] = e1;
buffer[elementsCount++] = e2;
buffer[elementsCount++] = e3;
buffer[elementsCount++] = e4;
}
/**
* Add a range of array elements to the stack.
*/
public void push(final long[] elements, final int start, final int len)
{
assert start >= 0 && len >= 0;
ensureBufferSpace(len);
System.arraycopy(elements, start, buffer, elementsCount, len);
elementsCount += len;
}
/**
* Vararg-signature method for pushing elements at the top of the stack.
*
This method is handy, but costly if used in tight loops (anonymous
* array passing)
*/
public void push(final long... elements)
{
push(elements, 0, elements.length);
}
/**
* Pushes all elements from another container to the top of the stack.
*/
public int pushAll(final LongContainer container)
{
return addAll(container);
}
/**
* Pushes all elements from another iterable to the top of the stack.
*/
public int pushAll(final Iterable extends LongCursor> iterable)
{
return addAll(iterable);
}
/**
* Discard an arbitrary number of elements from the top of the stack.
*/
public void discard(final int count)
{
assert elementsCount >= count;
elementsCount -= count;
}
/**
* Discard the top element from the stack.
*/
public void discard()
{
assert elementsCount > 0;
elementsCount--;
/* */
}
/**
* Remove the top element from the stack and return it.
*/
public long pop()
{
assert elementsCount > 0;
final long v = buffer[--elementsCount];
/* */
return v;
}
/**
* Peek at the top element on the stack.
*/
public long peek()
{
assert elementsCount > 0;
return buffer[elementsCount - 1];
}
/**
* Returns a new object of this class with no need to declare generic type (shortcut
* instead of using a constructor).
*/
public static LongStack newInstance()
{
return new LongStack();
}
/**
* Returns a new object of this list with no need to declare generic type (shortcut
* instead of using a constructor).
*/
public static LongStack newInstanceWithCapacity(final int initialCapacity)
{
return new LongStack(initialCapacity);
}
/**
* Create a stack by pushing a variable number of arguments to it.
*/
public static LongStack from(final long... elements)
{
final LongStack stack = new LongStack(elements.length);
stack.push(elements);
return stack;
}
/**
* Create a stack by pushing all elements of another container to it.
*/
public static LongStack from(final LongContainer container)
{
return new LongStack(container);
}
/**
* Sort the stack from [beginIndex, endIndex[
* by natural ordering (smaller first, from top to bottom of stack)
* @param beginIndex
* @param endIndex
*/
public void sort(int beginIndex, int endIndex)
{
assert endIndex <= elementsCount;
if (endIndex - beginIndex > 1)
{
//take care of ordering the right range : [startSortingRange, endSortingRange[
final int size = size();
final int startSortingRange = size - endIndex;
final int endSortingRange = size - beginIndex;
LongSort.quicksort(buffer, startSortingRange, endSortingRange);
//reverse [startSortingRange, endSortingRange [
long tmpValue;
final int halfSize = (endSortingRange - startSortingRange) / 2;
for (int i = 0; i < halfSize; i++)
{
tmpValue = this.buffer[i + startSortingRange];
this.buffer[i + startSortingRange] = this.buffer[endSortingRange - i - 1];
this.buffer[endSortingRange - i - 1] = tmpValue;
}
}
}
/**
* Sort the whole stack by natural ordering (smaller first, from top to bottom of stack)
*
* This routine uses Dual-pivot Quicksort, from [Yaroslavskiy 2009]
*
* @param beginIndex
* @param endIndex
*/
public void sort()
{
//sort full
super.sort();
//reverse all elements
final int size = size();
final int halfSize = size / 2;
long tmpValue;
for (int i = 0; i < halfSize; i++)
{
tmpValue = this.buffer[i];
this.buffer[i] = this.buffer[size - i - 1];
this.buffer[size - i - 1] = tmpValue;
}
}
/**
* Sort the stack of long
s from [beginIndex, endIndex[
* where [beginIndex, endIndex[ is counted from the top of the stack, i.e top is = index 0, bottom is endIndex[. That way,
* the smallest elements are at the top of the stack.
* It uses a LongComparator *
* This routine uses Dual-pivot Quicksort, from [Yaroslavskiy 2009] *
*/
@Override
public void sort(
final int beginIndex, final int endIndex,
LongComparator
comp)
{
assert endIndex <= elementsCount;
if (endIndex - beginIndex > 1)
{
//take care of ordering the right range : [startSortingRange, endSortingRange[
final int size = size();
final int startSortingRange = size - endIndex;
final int endSortingRange = size - beginIndex;
LongSort.quicksort(buffer, startSortingRange, endSortingRange, comp);
//reverse [startSortingRange, endSortingRange [
long tmpValue;
final int halfSize = (endSortingRange - startSortingRange) / 2;
for (int i = 0; i < halfSize; i++)
{
tmpValue = this.buffer[i + startSortingRange];
this.buffer[i + startSortingRange] = this.buffer[endSortingRange - i - 1];
this.buffer[endSortingRange - i - 1] = tmpValue;
}
}
}
/**
* Sort by dual-pivot quicksort an entire stack of long
s, the way
* the smallest elements are at the top of the stack.
* It uses a LongComparator *
* This routine uses Dual-pivot Quicksort, from [Yaroslavskiy 2009] *
*/
@Override
public void sort(
LongComparator
comp)
{
//sort full
super.sort(comp);
//reverse all elements
final int size = size();
final int halfSize = size / 2;
long tmpValue;
for (int i = 0; i < halfSize; i++)
{
tmpValue = this.buffer[i];
this.buffer[i] = this.buffer[size - i - 1];
this.buffer[size - i - 1] = tmpValue;
}
}
/**
* {@inheritDoc}
*/
@Override
public LongStack clone()
{
final LongStack cloned = new LongStack(this.size(), this.resizer);
cloned.defaultValue = this.defaultValue;
//in order by construction
cloned.addAll(this);
return cloned;
}
/**
* {@inheritDoc}
*/
@Override
/* */
public boolean equals(final Object obj)
{
if (obj != null)
{
if (obj == this)
return true;
if (obj instanceof LongIndexedContainer)
{
final LongIndexedContainer other = (LongIndexedContainer) obj;
return other.size() == this.size() &&
allIndexesEqual(this, (LongIndexedContainer) other, this.size());
}
}
return false;
}
}