Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
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 double-linked list of KTypes. This is an hybrid of {@link ObjectDeque} and {@link ObjectIndexedContainer},
* with the ability to push or remove values from either head or tail, in constant time.
* The drawback is the double-linked list ones, i.e insert(index), remove(index) are O(N)
* since they must traverse the collection to reach the index.
* In addition, elements could by pushed or removed from the middle of the list without moving
* big amounts of memory, contrary to {@link ObjectArrayList}s. Like {@link ObjectDeque}s, the double-linked list
* can also be iterated in reverse.
* Plus, the Iterator or reversed-iterator supports powerful methods to modify/delete/set the neighbors,
* in replacement of the error-prone java.util.Iterator ones.
* A compact representation is used to store and manipulate
* all elements, without creating Objects for links. Reallocations are governed by a {@link ArraySizingStrategy}
* and may be expensive if they move around really large chunks of memory.
*
* Important note: DO NOT USE java.util.Iterator methods ! They are here only for enhanced-loop syntax. Use
* the specialized methods of {@link ValueIterator} or {@link DescendingValueIterator} instead !
*
* @author Vincent Sonnier
*/
@javax.annotation.Generated(date = "2014-10-25T20:54:04+0200", value = "HPPC-RT generated from: ObjectLinkedList.java")
public class ObjectLinkedList
extends AbstractObjectCollection implements ObjectIndexedContainer, ObjectDeque, Cloneable
{
/**
* Default capacity if no other capacity is given in the constructor.
*/
public final static int DEFAULT_CAPACITY = 16;
/**
* Internal array for storing the list. The array may be larger than the current size
* ({@link #size()}).
*
*
Important!
* The actual value in this field is always an instance of Object[],
* regardless of the generic type used. The JDK is inconsistent here too:
* {@link ArrayList} declares internal Object[] buffer, but
* {@link ArrayDeque} declares an array of generic type objects like we do. The
* tradeoff is probably minimal, but you should be aware of additional casts generated
* by javac when buffer is directly accessed; these casts
* may also result in exceptions at runtime. A workaround is to cast directly to
* Object[] before accessing the buffer's elements, as shown
* in the following code snippet.
*
*
* Object[] buf = list.buffer;
* for (int i = 2; i < size() + 2; i++) {
* doSomething(buf[i]);
* }
*
*
* Direct list iteration: iterate buffer[i] for i in [2; size()+2[, but beware, it is out of order w.r.t the real list order !
*
*/
public KType[] buffer;
/**
* Represent the before / after nodes for each element of the buffer,
* such as buffer[i] nodes are beforeAfterPointers[i] where
* the 32 highest bits represent the unsigned before index in buffer : (beforeAfterPointers[i] & 0xFFFFFFFF00000000) >> 32
* the 32 lowest bits represent the unsigned after index in buffer : (beforeAfterPointers[i] & 0x00000000FFFFFFFF)
*/
protected long[] beforeAfterPointers;
/**
* Respective positions of head and tail elements of the list,
* as a position in buffer, such as
* after head is the actual first element of the list,
* before tail is the actual last element of the list.
* Technically, head and tail have hardcoded positions of 0 and 1,
* and occupy buffer[0] and buffer[1] virtually.
* So real buffer elements starts at index 2.
*/
protected static final int HEAD_POSITION = 0;
protected static final int TAIL_POSITION = ObjectLinkedList.HEAD_POSITION + 1;
/**
* Current number of elements stored in {@link #buffer}.
* Beware, the real number of elements is elementsCount - 2
*/
protected int elementsCount = 2;
/**
* Buffer resizing strategy.
*/
protected final ArraySizingStrategy resizer;
/**
* internal pool of ValueIterator (must be created in constructor)
*/
protected final IteratorPool, ValueIterator> valueIteratorPool;
/**
* internal pool of DescendingValueIterator (must be created in constructor)
*/
protected final IteratorPool, DescendingValueIterator> descendingValueIteratorPool;
/**
* Create with default sizing strategy and initial capacity for storing
* {@value #DEFAULT_CAPACITY} elements.
*
* @see BoundedProportionalArraySizingStrategy
*/
public ObjectLinkedList()
{
this(ObjectLinkedList.DEFAULT_CAPACITY);
}
/**
* Create with default sizing strategy and the given initial capacity.
*
* @see BoundedProportionalArraySizingStrategy
*/
public ObjectLinkedList(final int initialCapacity)
{
this(initialCapacity, new BoundedProportionalArraySizingStrategy());
}
/**
* Create with a custom buffer resizing strategy.
*/
public ObjectLinkedList(final int initialCapacity, final ArraySizingStrategy resizer)
{
assert initialCapacity >= 0 : "initialCapacity must be >= 0: " + initialCapacity;
assert resizer != null;
this.resizer = resizer;
final int internalSize = resizer.round(initialCapacity);
//allocate internal buffer
this.buffer = Internals.newArray(internalSize + 2);
this.beforeAfterPointers = new long[internalSize + 2];
//initialize
this.elementsCount = 2;
//initialize head and tail: initially, they are linked to each other.
this.beforeAfterPointers[ObjectLinkedList.HEAD_POSITION] = (((long) ObjectLinkedList.HEAD_POSITION << 32) | ObjectLinkedList.TAIL_POSITION);
this.beforeAfterPointers[ObjectLinkedList.TAIL_POSITION] = (((long) ObjectLinkedList.HEAD_POSITION << 32) | ObjectLinkedList.TAIL_POSITION);
this.valueIteratorPool = new IteratorPool, ValueIterator>(
new ObjectFactory() {
@Override
public ValueIterator create()
{
return new ValueIterator();
}
@Override
public void initialize(final ValueIterator obj)
{
obj.internalIndex = -1;
obj.cursor.index = -1;
obj.cursor.value = ObjectLinkedList.this.defaultValue;
obj.buffer = ObjectLinkedList.this.buffer;
obj.pointers = ObjectLinkedList.this.beforeAfterPointers;
obj.internalPos = ObjectLinkedList.HEAD_POSITION;
}
@Override
public void reset(final ValueIterator obj) {
// for GC sake
obj.buffer = null;
obj.pointers = null;
}
});
this.descendingValueIteratorPool = new IteratorPool, DescendingValueIterator>(
new ObjectFactory() {
@Override
public DescendingValueIterator create()
{
return new DescendingValueIterator();
}
@Override
public void initialize(final DescendingValueIterator obj)
{
obj.internalIndex = ObjectLinkedList.this.size();
obj.cursor.index = ObjectLinkedList.this.size();
obj.cursor.value = ObjectLinkedList.this.defaultValue;
obj.buffer = ObjectLinkedList.this.buffer;
obj.pointers = ObjectLinkedList.this.beforeAfterPointers;
obj.internalPos = ObjectLinkedList.TAIL_POSITION;
}
@Override
public void reset(final DescendingValueIterator obj) {
// for GC sake
obj.buffer = null;
obj.pointers = null;
}
});
}
/**
* Creates a new list from elements of another container.
*/
public ObjectLinkedList(final ObjectContainer extends KType> container)
{
this(container.size());
addAll(container);
}
/**
* {@inheritDoc}
*/
@Override
public void add(final KType e1)
{
addLast(e1);
}
/**
* Appends two elements at the end of the list. To add more than two elements,
* use add (vararg-version) or access the buffer directly (tight
* loop).
*/
public void add(final KType e1, final KType e2)
{
ensureBufferSpace(2);
insertAfterPosNoCheck(e1, ((int) (this.beforeAfterPointers[ObjectLinkedList.TAIL_POSITION] >> 32)));;
insertAfterPosNoCheck(e2, ((int) (this.beforeAfterPointers[ObjectLinkedList.TAIL_POSITION] >> 32)));;
}
public void addLast(final KType... elements)
{
addLast(elements, 0, elements.length);
}
/**
* Add all elements from a range of given array to the list.
*/
public void addLast(final KType[] elements, final int start, final int length)
{
assert length + start <= elements.length : "Length is smaller than required";
ensureBufferSpace(length);
final long[] beforeAfterPointers = this.beforeAfterPointers;
for (int i = 0; i < length; i++)
{
insertAfterPosNoCheck(elements[start + i], ((int) (beforeAfterPointers[ObjectLinkedList.TAIL_POSITION] >> 32)));
}
}
/**
* Add all elements from a range of given array to the list, equivalent to addLast()
*/
public void add(final KType[] elements, final int start, final int length)
{
addLast(elements, start, length);
}
/**
* Vararg-signature method for adding elements at the end of the list.
*
This method is handy, but costly if used in tight loops (anonymous
* array passing)
*/
public void add(final KType... elements)
{
addLast(elements, 0, elements.length);
}
/**
* Adds all elements from another container, equivalent to addLast()
*/
public int addAll(final ObjectContainer extends KType> container)
{
return addLast(container);
}
/**
* Adds all elements from another iterable, equivalent to addLast()
*/
public int addAll(final Iterable extends ObjectCursor extends KType>> iterable)
{
return addLast(iterable);
}
/**
* {@inheritDoc}
*/
@Override
public void insert(final int index, final KType e1)
{
assert (index >= 0 && index <= size()) : "Index " + index + " out of bounds [" + 0 + ", " + size() + "].";
ensureBufferSpace(1);
if (index == 0)
{
insertAfterPosNoCheck(e1, ObjectLinkedList.HEAD_POSITION);
}
else
{
insertAfterPosNoCheck(e1, gotoIndex(index - 1));
}
}
/**
* {@inheritDoc}
*/
@Override
public KType get(final int index)
{
assert (index >= 0 && index < size()) : "Index " + index + " out of bounds [" + 0 + ", " + size() + ").";
//get object at pos currentPos in buffer
return this.buffer[gotoIndex(index)];
}
/**
* {@inheritDoc}
*/
@Override
public KType set(final int index, final KType e1)
{
assert (index >= 0 && index < size()) : "Index " + index + " out of bounds [" + 0 + ", " + size() + ").";
//get object at pos currentPos in buffer
final int pos = gotoIndex(index);
//keep the previous value
final KType elem = this.buffer[pos];
//new value
this.buffer[pos] = e1;
return elem;
}
/**
* {@inheritDoc}
*/
@Override
public KType remove(final int index)
{
assert (index >= 0 && index < size()) : "Index " + index + " out of bounds [" + 0 + ", " + size() + ").";
//get object at pos currentPos in buffer
final int currentPos = gotoIndex(index);
final KType elem = this.buffer[currentPos];
//remove
removeAtPosNoCheck(currentPos);
return elem;
}
/**
* {@inheritDoc}
*/
@Override
public void removeRange(final int fromIndex, final int toIndex)
{
assert (fromIndex >= 0 && fromIndex <= size()) : "Index " + fromIndex + " out of bounds [" + 0 + ", " + size() + ").";
assert (toIndex >= 0 && toIndex <= size()) : "Index " + toIndex + " out of bounds [" + 0 + ", " + size() + "].";
assert fromIndex <= toIndex : "fromIndex must be <= toIndex: "
+ fromIndex + ", " + toIndex;
//goto pos
int currentPos = gotoIndex(fromIndex);
//start removing size elements...
final int size = toIndex - fromIndex;
int count = 0;
while (count < size)
{
currentPos = removeAtPosNoCheck(currentPos);
count++;
}
}
/**
* {@inheritDoc}
*/
@Override
public int removeFirstOccurrence(final KType e1)
{
final long[] pointers = this.beforeAfterPointers;
final KType[] buffer = this.buffer;
int currentPos = ((int) (pointers[ObjectLinkedList.HEAD_POSITION] & 0x00000000FFFFFFFFL));
int count = 0;
while (currentPos != ObjectLinkedList.TAIL_POSITION)
{
if (((e1) == null ? (buffer[currentPos]) == null : (e1).equals((buffer[currentPos]))))
{
removeAtPosNoCheck(currentPos);
return count;
}
//increment
currentPos = ((int) (pointers[currentPos] & 0x00000000FFFFFFFFL));
count++;
}
return -1;
}
/**
* {@inheritDoc}
*/
@Override
public int removeLastOccurrence(final KType e1)
{
final long[] pointers = this.beforeAfterPointers;
final KType[] buffer = this.buffer;
final int size = size();
int currentPos = ((int) (pointers[ObjectLinkedList.TAIL_POSITION] >> 32));
int count = 0;
while (currentPos != ObjectLinkedList.HEAD_POSITION)
{
if (((e1) == null ? (buffer[currentPos]) == null : (e1).equals((buffer[currentPos]))))
{
removeAtPosNoCheck(currentPos);
return size - count - 1;
}
currentPos = ((int) (pointers[currentPos] >> 32));
count++;
}
return -1;
}
/**
* {@inheritDoc}
*/
@Override
public int removeAllOccurrences(final KType e1)
{
final KType[] buffer = this.buffer;
int deleted = 0;
//real elements starts in postion 2.
int pos = 2;
//directly iterate the buffer, so out of order.
while (pos < this.elementsCount)
{
if (((e1) == null ? (buffer[pos]) == null : (e1).equals((buffer[pos]))))
{
//each time a pos is removed, pos is patched with the last element,
//so continue to test the same position
removeAtPosNoCheck(pos);
deleted++;
}
else
{
pos++;
}
} //end while
return deleted;
}
/**
* {@inheritDoc}
*/
@Override
public boolean contains(final KType e1)
{
return indexOf(e1) >= 0;
}
/**
* {@inheritDoc}
*/
@Override
public int indexOf(final KType e1)
{
final long[] pointers = this.beforeAfterPointers;
final KType[] buffer = this.buffer;
int currentPos = ((int) (pointers[ObjectLinkedList.HEAD_POSITION] & 0x00000000FFFFFFFFL));
int count = 0;
while (currentPos != ObjectLinkedList.TAIL_POSITION)
{
if (((e1) == null ? (buffer[currentPos]) == null : (e1).equals((buffer[currentPos]))))
{
return count;
}
currentPos = ((int) (pointers[currentPos] & 0x00000000FFFFFFFFL));
count++;
}
return -1;
}
/**
* {@inheritDoc}
*/
@Override
public int lastIndexOf(final KType e1)
{
final long[] pointers = this.beforeAfterPointers;
final KType[] buffer = this.buffer;
int currentPos = ((int) (pointers[ObjectLinkedList.TAIL_POSITION] >> 32));
int count = 0;
while (currentPos != ObjectLinkedList.HEAD_POSITION)
{
if (((e1) == null ? (buffer[currentPos]) == null : (e1).equals((buffer[currentPos]))))
{
return size() - count - 1;
}
currentPos = ((int) (pointers[currentPos] >> 32));
count++;
}
return -1;
}
/**
* Increases the capacity of this instance, if necessary, to ensure
* that it can hold at least the number of elements specified by
* the minimum capacity argument.
*/
public void ensureCapacity(final int minCapacity)
{
if (minCapacity > this.buffer.length) {
ensureBufferSpace(minCapacity - size());
}
}
/**
* Ensures the internal buffer has enough free slots to store
* expectedAdditions. Increases internal buffer size if needed.
* @return true if a reallocation occurs
*/
protected boolean ensureBufferSpace(final int expectedAdditions)
{
final int bufferLen = (this.buffer == null ? 0 : this.buffer.length);
if (this.elementsCount > bufferLen - expectedAdditions)
{
final int newSize = this.resizer.grow(bufferLen, this.elementsCount, expectedAdditions);
assert newSize >= this.elementsCount + expectedAdditions : "Resizer failed to" +
" return sensible new size: " + newSize + " <= "
+ (this.elementsCount + expectedAdditions);
//the first 2 slots are head/tail placeholders
final KType[] newBuffer = Internals.newArray(newSize + 2);
final long[] newPointers = new long[newSize + 2];
if (bufferLen > 0)
{
System.arraycopy(this.buffer, 0, newBuffer, 0, this.buffer.length);
System.arraycopy(this.beforeAfterPointers, 0, newPointers, 0, this.beforeAfterPointers.length);
}
this.buffer = newBuffer;
this.beforeAfterPointers = newPointers;
return true;
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public int size()
{
return this.elementsCount - 2;
}
/**
* {@inheritDoc}
*/
@Override
public int capacity() {
return this.buffer.length - 2;
}
/**
* Sets the number of stored elements to zero.
*/
@Override
public void clear()
{
ObjectArrays.blankArray(this.buffer, 0, this.elementsCount);
//the first two are placeholders
this.elementsCount = 2;
//rebuild head/tail
this.beforeAfterPointers[ObjectLinkedList.HEAD_POSITION] = (((long) ObjectLinkedList.HEAD_POSITION << 32) | ObjectLinkedList.TAIL_POSITION);
this.beforeAfterPointers[ObjectLinkedList.TAIL_POSITION] = (((long) ObjectLinkedList.HEAD_POSITION << 32) | ObjectLinkedList.TAIL_POSITION);
}
/**
* {@inheritDoc}
*/
@Override
public KType[] toArray(final KType[] target)
{
//Return in-order
final long[] pointers = this.beforeAfterPointers;
final KType[] buffer = this.buffer;
int index = 0;
int currentPos = ((int) (pointers[ObjectLinkedList.HEAD_POSITION] & 0x00000000FFFFFFFFL));
while (currentPos != ObjectLinkedList.TAIL_POSITION)
{
target[index] = buffer[currentPos];
//increment both
currentPos = ((int) (pointers[currentPos] & 0x00000000FFFFFFFFL));
index++;
}
return target;
}
/**
* Clone this object. The returned clone will use the same resizing strategy.
* It also realizes a trim-to- this.size() in the process.
*/
@Override
public ObjectLinkedList clone()
{
/* */
@SuppressWarnings("unchecked")
/* */
final ObjectLinkedList cloned = new ObjectLinkedList(this.size(), this.resizer);
cloned.addAll(this);
cloned.defaultValue = this.defaultValue;
return cloned;
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode()
{
final long[] pointers = this.beforeAfterPointers;
final KType[] buffer = this.buffer;
int h = 1;
int currentPos = ((int) (pointers[ObjectLinkedList.HEAD_POSITION] & 0x00000000FFFFFFFFL));
while (currentPos != ObjectLinkedList.TAIL_POSITION)
{
h = 31 * h + Internals.rehash(buffer[currentPos]);
//increment
currentPos = ((int) (pointers[currentPos] & 0x00000000FFFFFFFFL));
}
return h;
}
/**
* {@inheritDoc}
*/
@Override
/* */
@SuppressWarnings("unchecked")
/* */
public boolean equals(final Object obj)
{
if (obj != null)
{
if (obj == this) {
return true;
}
final long[] pointers = this.beforeAfterPointers;
final KType[] buffer = this.buffer;
if (obj instanceof ObjectLinkedList>)
{
final ObjectLinkedList> other = (ObjectLinkedList>) obj;
if (other.size() != this.size())
{
return false;
}
final long[] pointersOther = other.beforeAfterPointers;
final KType[] bufferOther = (KType[]) other.buffer;
//compare index/index
int currentPos = ((int) (pointers[ObjectLinkedList.HEAD_POSITION] & 0x00000000FFFFFFFFL));
int currentPosOther = ((int) (pointersOther[ObjectLinkedList.HEAD_POSITION] & 0x00000000FFFFFFFFL));
while (currentPos != ObjectLinkedList.TAIL_POSITION && currentPosOther != ObjectLinkedList.TAIL_POSITION)
{
if (!((buffer[currentPos]) == null ? (bufferOther[currentPosOther]) == null : (buffer[currentPos]).equals((bufferOther[currentPosOther]))))
{
return false;
}
//increment both
currentPos = ((int) (pointers[currentPos] & 0x00000000FFFFFFFFL));
currentPosOther = ((int) (pointersOther[currentPosOther] & 0x00000000FFFFFFFFL));
}
return true;
}
else if (obj instanceof ObjectDeque>)
{
final ObjectDeque