com.carrotsearch.hppc
Class ObjectArrayDeque<KType>

java.lang.Object
  extended by com.carrotsearch.hppc.ObjectArrayDeque<KType>
All Implemented Interfaces:
ObjectCollection<KType>, ObjectContainer<KType>, ObjectDeque<KType>, java.lang.Cloneable, java.lang.Iterable<ObjectCursor<KType>>

@Generated(date="2011-11-28T23:36:05+0000",
           value="HPPC generated from: ObjectArrayDeque.java")
public class ObjectArrayDeque<KType>
extends java.lang.Object
implements ObjectDeque<KType>, java.lang.Cloneable

An array-backed deque (doubly linked queue) of KTypes. A single array is used to store and manipulate all elements. Reallocations are governed by a ArraySizingStrategy and may be expensive if they move around really large chunks of memory. A brief comparison of the API against the Java Collections framework:

Java Collections ArrayDeque and HPPC ObjectArrayDeque, related methods.
java.util.ArrayDeque ObjectArrayDeque
addFirst addFirst
addLast addLast
removeFirst removeLast
getFirst getFirst
getLast getLast
removeFirstOccurrence, removeLastOccurrence removeFirstOccurrence, removeLastOccurrence
size size
Object[] toArray()KType[] toArray()
iterator cursor over values
other methods inherited from Stack, Queuenot implemented


Field Summary
 KType[] buffer
          Internal array for storing elements.
static int DEFAULT_CAPACITY
          Default capacity if no other capacity is given in the constructor.
 int head
          The index of the element at the head of the deque or an arbitrary number equal to tail if the deque is empty.
protected  ArraySizingStrategy resizer
          Buffer resizing strategy.
 int tail
          The index at which the next element would be added to the tail of the deque.
 
Constructor Summary
ObjectArrayDeque()
          Create with default sizing strategy and initial capacity for storing 5 elements.
ObjectArrayDeque(int initialCapacity)
          Create with default sizing strategy and the given initial capacity.
ObjectArrayDeque(int initialCapacity, ArraySizingStrategy resizer)
          Create with a custom buffer resizing strategy.
ObjectArrayDeque(ObjectContainer<? extends KType> container)
          Creates a new deque from elements of another container, appending them at the end of this deque.
 
Method Summary
 int addFirst(java.lang.Iterable<? extends ObjectCursor<? extends KType>> iterable)
          Inserts all elements from the given iterable to the front of this deque.
 void addFirst(KType... elements)
          Vararg-signature method for adding elements at the front of this deque.
 void addFirst(KType e1)
          Inserts the specified element at the front of this deque.
 int addFirst(ObjectContainer<? extends KType> container)
          Inserts all elements from the given container to the front of this deque.
 int addLast(java.lang.Iterable<? extends ObjectCursor<? extends KType>> iterable)
          Inserts all elements from the given iterable to the end of this deque.
 void addLast(KType... elements)
          Vararg-signature method for adding elements at the end of this deque.
 void addLast(KType e1)
          Inserts the specified element at the end of this deque.
 int addLast(ObjectContainer<? extends KType> container)
          Inserts all elements from the given container to the end of this deque.
 int bufferIndexOf(KType e1)
          Return the index of the first (counting from head) element equal to e1.
 void clear()
          Removes all elements from this collection.
 ObjectArrayDeque<KType> clone()
          Clone this object.
 boolean contains(KType e)
          Lookup a given element in the container.
<T extends ObjectPredicate<? super KType>>
T
descendingForEach(T predicate)
          Applies a predicate to container elements as long, as the predicate returns true.
<T extends ObjectProcedure<? super KType>>
T
descendingForEach(T procedure)
          Applies procedure to all elements of this deque, tail to head.
 java.util.Iterator<ObjectCursor<KType>> descendingIterator()
          Returns a cursor over the values of this deque (in tail to head order).
protected  void ensureBufferSpace(int expectedAdditions)
          Ensures the internal buffer has enough free slots to store expectedAdditions.
 boolean equals(java.lang.Object obj)
          Compares the specified object with this deque for equality.
<T extends ObjectPredicate<? super KType>>
T
forEach(T predicate)
          Applies a predicate to container elements as long, as the predicate returns true.
<T extends ObjectProcedure<? super KType>>
T
forEach(T procedure)
          Applies a procedure to all container elements.
static
<KType> ObjectArrayDeque<KType>
from(KType... elements)
          Create a new deque by pushing a variable number of arguments to the end of it.
static
<KType> ObjectArrayDeque<KType>
from(ObjectArrayDeque<KType> container)
          Create a new deque by pushing a variable number of arguments to the end of it.
 KType getFirst()
          Retrieves, but does not remove, the first element of this deque.
 KType getLast()
          Retrieves, but does not remove, the last element of this deque.
 int hashCode()
          
 boolean isEmpty()
          Shortcut for size() == 0.
 java.util.Iterator<ObjectCursor<KType>> iterator()
          Returns a cursor over the values of this deque (in head to tail order).
 int lastBufferIndexOf(KType e1)
          Return the index of the last (counting from tail) element equal to e1.
static
<KType> ObjectArrayDeque<KType>
newInstance()
          Returns a new object of this class with no need to declare generic type (shortcut instead of using a constructor).
static
<KType> ObjectArrayDeque<KType>
newInstanceWithCapacity(int initialCapacity)
          Returns a new object of this class with no need to declare generic type (shortcut instead of using a constructor).
 void release()
          Release internal buffers of this deque and reallocate the smallest buffer possible.
 int removeAll(ObjectLookupContainer<? extends KType> c)
          Default implementation uses a predicate for removal.
 int removeAll(ObjectPredicate<? super KType> predicate)
          Removes all elements in this collection for which the given predicate returns true.
 int removeAllOccurrences(KType e1)
          Removes all occurrences of e from this collection.
 void removeAtBufferIndex(int index)
          Removes the element at index in the internal {#link buffer} array, returning its value.
 KType removeFirst()
          Retrieves and removes the first element of this deque.
 int removeFirstOccurrence(KType e1)
          Removes the first element that equals e1, returning its deleted position or -1 if the element was not found.
 KType removeLast()
          Retrieves and removes the last element of this deque.
 int removeLastOccurrence(KType e1)
          Removes the last element that equals e1, returning its deleted position or -1 if the element was not found.
 int retainAll(ObjectLookupContainer<? extends KType> c)
          Default implementation uses a predicate for retaining.
 int retainAll(ObjectPredicate<? super KType> predicate)
          Default implementation redirects to ObjectCollection.removeAll(ObjectPredicate) and negates the predicate.
 int size()
          Return the current number of elements in this container.
 java.lang.Object[] toArray()
          Copies all elements from this container to an Object array.
 KType[] toArray(java.lang.Class<? super KType> clazz)
          Default implementation of copying to an array.
 KType[] toArray(KType[] target)
          Copies elements of this deque to an array.
 java.lang.String toString()
          Convert the contents of this container to a human-friendly string.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.carrotsearch.hppc.ObjectCollection
removeAll, retainAll, retainAll
 
Methods inherited from interface com.carrotsearch.hppc.ObjectContainer
toArray
 

Field Detail

DEFAULT_CAPACITY

public static final int DEFAULT_CAPACITY
Default capacity if no other capacity is given in the constructor.

See Also:
Constant Field Values

buffer

public KType[] buffer
Internal array for storing elements. *

The actual value in this field is always an instance of Object[], regardless of the generic type used. The JDK is inconsistent here too -- ArrayList declares internal Object[] buffer, but 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 result in exceptions at runtime. A workaround is to cast directly to Object[] before accessing the buffer's elements.


head

public int head
The index of the element at the head of the deque or an arbitrary number equal to tail if the deque is empty.


tail

public int tail
The index at which the next element would be added to the tail of the deque.


resizer

protected final ArraySizingStrategy resizer
Buffer resizing strategy.

Constructor Detail

ObjectArrayDeque

public ObjectArrayDeque()
Create with default sizing strategy and initial capacity for storing 5 elements.

See Also:
BoundedProportionalArraySizingStrategy

ObjectArrayDeque

public ObjectArrayDeque(int initialCapacity)
Create with default sizing strategy and the given initial capacity.

See Also:
BoundedProportionalArraySizingStrategy

ObjectArrayDeque

public ObjectArrayDeque(int initialCapacity,
                        ArraySizingStrategy resizer)
Create with a custom buffer resizing strategy.


ObjectArrayDeque

public ObjectArrayDeque(ObjectContainer<? extends KType> container)
Creates a new deque from elements of another container, appending them at the end of this deque.

Method Detail

addFirst

public void addFirst(KType e1)
Inserts the specified element at the front of this deque.

Specified by:
addFirst in interface ObjectDeque<KType>
Parameters:
e1 - the element to add

addFirst

public void addFirst(KType... elements)
Vararg-signature method for adding elements at the front of this deque.

This method is handy, but costly if used in tight loops (anonymous array passing)


addFirst

public final int addFirst(ObjectContainer<? extends KType> container)
Inserts all elements from the given container to the front of this deque.

Returns:
Returns the number of elements actually added as a result of this call.

addFirst

public final int addFirst(java.lang.Iterable<? extends ObjectCursor<? extends KType>> iterable)
Inserts all elements from the given iterable to the front of this deque.

Returns:
Returns the number of elements actually added as a result of this call.

addLast

public void addLast(KType e1)
Inserts the specified element at the end of this deque.

Specified by:
addLast in interface ObjectDeque<KType>
Parameters:
e1 - the element to add

addLast

public void addLast(KType... elements)
Vararg-signature method for adding elements at the end of this deque.

This method is handy, but costly if used in tight loops (anonymous array passing)


addLast

public final int addLast(ObjectContainer<? extends KType> container)
Inserts all elements from the given container to the end of this deque.

Returns:
Returns the number of elements actually added as a result of this call.

addLast

public final int addLast(java.lang.Iterable<? extends ObjectCursor<? extends KType>> iterable)
Inserts all elements from the given iterable to the end of this deque.

Returns:
Returns the number of elements actually added as a result of this call.

removeFirst

public KType removeFirst()
Retrieves and removes the first element of this deque.

Specified by:
removeFirst in interface ObjectDeque<KType>
Returns:
the head element of this deque.

removeLast

public KType removeLast()
Retrieves and removes the last element of this deque.

Specified by:
removeLast in interface ObjectDeque<KType>
Returns:
the tail of this deque.

getFirst

public KType getFirst()
Retrieves, but does not remove, the first element of this deque.

Specified by:
getFirst in interface ObjectDeque<KType>
Returns:
the head of this deque.

getLast

public KType getLast()
Retrieves, but does not remove, the last element of this deque.

Specified by:
getLast in interface ObjectDeque<KType>
Returns:
the tail of this deque.

removeFirstOccurrence

public int removeFirstOccurrence(KType e1)
Removes the first element that equals e1, returning its deleted position or -1 if the element was not found.

Specified by:
removeFirstOccurrence in interface ObjectDeque<KType>

bufferIndexOf

public int bufferIndexOf(KType e1)
Return the index of the first (counting from head) element equal to e1. The index points to the buffer array.

Parameters:
e1 - The element to look for.
Returns:
Returns the index of the first element equal to e1 or -1 if not found.

removeLastOccurrence

public int removeLastOccurrence(KType e1)
Removes the last element that equals e1, returning its deleted position or -1 if the element was not found.

Specified by:
removeLastOccurrence in interface ObjectDeque<KType>

lastBufferIndexOf

public int lastBufferIndexOf(KType e1)
Return the index of the last (counting from tail) element equal to e1. The index points to the buffer array.

Parameters:
e1 - The element to look for.
Returns:
Returns the index of the first element equal to e1 or -1 if not found.

removeAllOccurrences

public int removeAllOccurrences(KType e1)
Removes all occurrences of e from this collection.

Specified by:
removeAllOccurrences in interface ObjectCollection<KType>
Parameters:
e1 - Element to be removed from this collection, if present.
Returns:
The number of removed elements as a result of this call.

removeAtBufferIndex

public void removeAtBufferIndex(int index)
Removes the element at index in the internal {#link buffer} array, returning its value.

Parameters:
index - Index of the element to remove. The index must be located between head and tail in modulo buffer arithmetic.

isEmpty

public boolean isEmpty()
Shortcut for size() == 0.

Specified by:
isEmpty in interface ObjectContainer<KType>

size

public int size()
Return the current number of elements in this container. The time for calculating the container's size may take O(n) time, although implementing classes should try to maintain the current size and return in constant time.

Specified by:
size in interface ObjectContainer<KType>

clear

public void clear()
Removes all elements from this collection.

The internal array buffers are not released as a result of this call.

Specified by:
clear in interface ObjectCollection<KType>
See Also:
release()

release

public void release()
Release internal buffers of this deque and reallocate the smallest buffer possible.


ensureBufferSpace

protected final void ensureBufferSpace(int expectedAdditions)
Ensures the internal buffer has enough free slots to store expectedAdditions. Increases internal buffer size if needed.


toArray

public final java.lang.Object[] toArray()
Copies all elements from this container to an Object array. If you need an array of the type identical with this container's generic type, use ObjectContainer.toArray(Class).

Specified by:
toArray in interface ObjectContainer<KType>
See Also:
ObjectContainer.toArray(Class)

toArray

public KType[] toArray(KType[] target)
Copies elements of this deque to an array. The content of the target array is filled from index 0 (head of the queue) to index size() - 1 (tail of the queue).

Parameters:
target - The target array must be large enough to hold all elements.
Returns:
Returns the target argument for chaining.

clone

public ObjectArrayDeque<KType> clone()
Clone this object. The returned clone will reuse the same hash function and array resizing strategy.

Overrides:
clone in class java.lang.Object

iterator

public java.util.Iterator<ObjectCursor<KType>> iterator()
Returns a cursor over the values of this deque (in head to tail order). The iterator is implemented as a cursor and it returns the same cursor instance on every call to Iterator.next() (to avoid boxing of primitive types). To read the current value (or index in the deque's buffer) use the cursor's public fields. An example is shown below.
 for (IntValueCursor c : intDeque)
 {
     System.out.println("buffer index=" 
         + c.index + " value=" + c.value);
 }
 

Specified by:
iterator in interface ObjectContainer<KType>
Specified by:
iterator in interface java.lang.Iterable<ObjectCursor<KType>>

descendingIterator

public java.util.Iterator<ObjectCursor<KType>> descendingIterator()
Returns a cursor over the values of this deque (in tail to head order). The iterator is implemented as a cursor and it returns the same cursor instance on every call to Iterator.next() (to avoid boxing of primitive types). To read the current value (or index in the deque's buffer) use the cursor's public fields. An example is shown below.
 for (Iterator i = intDeque.descendingIterator(); i.hasNext(); )
 {
     final IntCursor c = i.next();
     System.out.println("buffer index=" 
         + c.index + " value=" + c.value);
 }
 

Specified by:
descendingIterator in interface ObjectDeque<KType>
Returns:
An iterator over elements in this deque in tail-to-head order.

forEach

public <T extends ObjectProcedure<? super KType>> T forEach(T procedure)
Applies a procedure to all container elements. Returns the argument (any subclass of ObjectProcedure. This lets the caller to call methods of the argument by chaining the call (even if the argument is an anonymous type) to retrieve computed values, for example (IntContainer):
 int count = container.forEach(new IntProcedure() {
      int count; // this is a field declaration in an anonymous class.
      public void apply(int value) { count++; }}).count;
 

Specified by:
forEach in interface ObjectContainer<KType>

forEach

public <T extends ObjectPredicate<? super KType>> T forEach(T predicate)
Applies a predicate to container elements as long, as the predicate returns true. The iteration is interrupted otherwise.

Specified by:
forEach in interface ObjectContainer<KType>

descendingForEach

public <T extends ObjectProcedure<? super KType>> T descendingForEach(T procedure)
Applies procedure to all elements of this deque, tail to head.

Specified by:
descendingForEach in interface ObjectDeque<KType>

descendingForEach

public <T extends ObjectPredicate<? super KType>> T descendingForEach(T predicate)
Applies a predicate to container elements as long, as the predicate returns true. The iteration is interrupted otherwise.

Specified by:
descendingForEach in interface ObjectDeque<KType>

removeAll

public int removeAll(ObjectPredicate<? super KType> predicate)
Removes all elements in this collection for which the given predicate returns true.

Specified by:
removeAll in interface ObjectCollection<KType>

contains

public boolean contains(KType e)
Lookup a given element in the container. This operation has no speed guarantees (may be linear with respect to the size of this container).

Specified by:
contains in interface ObjectContainer<KType>
Returns:
Returns true if this container has an element equal to e.

hashCode

public int hashCode()

Specified by:
hashCode in interface ObjectDeque<KType>
Overrides:
hashCode in class java.lang.Object
Returns:
A hash code of elements stored in the deque. The hash code is defined identically to List.hashCode() (should be implemented with the same algorithm), replacing forward index loop with a forward iterator loop.

equals

public boolean equals(java.lang.Object obj)
Compares the specified object with this deque for equality. Returns true if and only if the specified object is also a ObjectDeque, and all corresponding pairs of elements acquired from forward iterators are the same. In other words, two indexed containers are defined to be equal if they contain the same elements in the same order of iteration.

Note that, unlike in List, deques may be of different types and still return true from ObjectDeque.equals(java.lang.Object). This may be dangerous if you use different hash functions in two containers, but don't override the default implementation of ObjectDeque.equals(java.lang.Object). It is the programmer's responsibility to enforcing these contracts properly.

Specified by:
equals in interface ObjectDeque<KType>
Overrides:
equals in class java.lang.Object

newInstance

public static <KType> ObjectArrayDeque<KType> newInstance()
Returns a new object of this class with no need to declare generic type (shortcut instead of using a constructor).


newInstanceWithCapacity

public static <KType> ObjectArrayDeque<KType> newInstanceWithCapacity(int initialCapacity)
Returns a new object of this class with no need to declare generic type (shortcut instead of using a constructor).


from

public static <KType> ObjectArrayDeque<KType> from(KType... elements)
Create a new deque by pushing a variable number of arguments to the end of it.


from

public static <KType> ObjectArrayDeque<KType> from(ObjectArrayDeque<KType> container)
Create a new deque by pushing a variable number of arguments to the end of it.


removeAll

public int removeAll(ObjectLookupContainer<? extends KType> c)
Default implementation uses a predicate for removal.

Specified by:
removeAll in interface ObjectCollection<KType>
Returns:
Returns the number of removed elements.

retainAll

public int retainAll(ObjectLookupContainer<? extends KType> c)
Default implementation uses a predicate for retaining.

Specified by:
retainAll in interface ObjectCollection<KType>
Returns:
Returns the number of removed elements.

retainAll

public int retainAll(ObjectPredicate<? super KType> predicate)
Default implementation redirects to ObjectCollection.removeAll(ObjectPredicate) and negates the predicate.

Specified by:
retainAll in interface ObjectCollection<KType>

toArray

public KType[] toArray(java.lang.Class<? super KType> clazz)
Default implementation of copying to an array.

Specified by:
toArray in interface ObjectContainer<KType>

toString

public java.lang.String toString()
Convert the contents of this container to a human-friendly string.

Overrides:
toString in class java.lang.Object


Copyright © 2011 Carrot Search s.c.. All Rights Reserved.