arraydeque.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 780 行 · 第 1/2 页
JAVA
780 行
int mask = elements.length - 1;
int i = (tail - 1) & mask;
Object x;
while ( (x = elements[i]) != null) {
if (e.equals(x)) {
delete(i);
return true;
}
i = (i - 1) & mask;
}
return false;
}
// *** Queue methods ***
/**
* Inserts the specified element to the end of this deque.
*
* <p>This method is equivalent to {@link #offerLast}.
*
* @param e the element to insert
* @return <tt>true</tt> (as per the spec for {@link Queue#offer})
* @throws NullPointerException if <tt>e</tt> is null
*/
public boolean offer(Object e) {
return offerLast(e);
}
/**
* Inserts the specified element to the end of this deque.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @param e the element to insert
* @return <tt>true</tt> (as per the spec for {@link Collection#add})
* @throws NullPointerException if <tt>e</tt> is null
*/
public boolean add(Object e) {
addLast(e);
return true;
}
/**
* Retrieves and removes the head of the queue represented by
* this deque, or <tt>null</tt> if this deque is empty. In other words,
* retrieves and removes the first element of this deque, or <tt>null</tt>
* if this deque is empty.
*
* <p>This method is equivalent to {@link #pollFirst}.
*
* @return the first element of this deque, or <tt>null</tt> if
* this deque is empty
*/
public Object poll() {
return pollFirst();
}
/**
* Retrieves and removes the head of the queue represented by this deque.
* This method differs from the <tt>poll</tt> method in that it throws an
* exception if this deque is empty.
*
* <p>This method is equivalent to {@link #removeFirst}.
*
* @return the head of the queue represented by this deque
* @throws NoSuchElementException if this deque is empty
*/
public Object remove() {
return removeFirst();
}
/**
* Retrieves, but does not remove, the head of the queue represented by
* this deque, returning <tt>null</tt> if this deque is empty.
*
* <p>This method is equivalent to {@link #peekFirst}
*
* @return the head of the queue represented by this deque, or
* <tt>null</tt> if this deque is empty
*/
public Object peek() {
return peekFirst();
}
/**
* Retrieves, but does not remove, the head of the queue represented by
* this deque. This method differs from the <tt>peek</tt> method only in
* that it throws an exception if this deque is empty.
*
* <p>This method is equivalent to {@link #getFirst}
*
* @return the head of the queue represented by this deque
* @throws NoSuchElementException if this deque is empty
*/
public Object element() {
return getFirst();
}
// *** Stack methods ***
/**
* Pushes an element onto the stack represented by this deque. In other
* words, inserts the element to the front this deque.
*
* <p>This method is equivalent to {@link #addFirst}.
*
* @param e the element to push
* @throws NullPointerException if <tt>e</tt> is null
*/
public void push(Object e) {
addFirst(e);
}
/**
* Pops an element from the stack represented by this deque. In other
* words, removes and returns the the first element of this deque.
*
* <p>This method is equivalent to {@link #removeFirst()}.
*
* @return the element at the front of this deque (which is the top
* of the stack represented by this deque)
* @throws NoSuchElementException if this deque is empty
*/
public Object pop() {
return removeFirst();
}
/**
* Remove the element at the specified position in the elements array,
* adjusting head, tail, and size as necessary. This can result in
* motion of elements backwards or forwards in the array.
*
* <p>This method is called delete rather than remove to emphasize the
* that that its semantics differ from those of List.remove(int).
*
* @return true if elements moved backwards
*/
private boolean delete(int i) {
// Case 1: Deque doesn't wrap
// Case 2: Deque does wrap and removed element is in the head portion
if ((head < tail || tail == 0) || i >= head) {
System.arraycopy(elements, head, elements, head + 1, i - head);
elements[head] = null;
head = (head + 1) & (elements.length - 1);
return false;
}
// Case 3: Deque wraps and removed element is in the tail portion
tail--;
System.arraycopy(elements, i + 1, elements, i, tail - i);
elements[tail] = null;
return true;
}
// *** Collection Methods ***
/**
* Returns the number of elements in this deque.
*
* @return the number of elements in this deque
*/
public int size() {
return (tail - head) & (elements.length - 1);
}
/**
* Returns <tt>true</tt> if this collection contains no elements.<p>
*
* @return <tt>true</tt> if this collection contains no elements.
*/
public boolean isEmpty() {
return head == tail;
}
/**
* Returns an iterator over the elements in this deque. The elements
* will be ordered from first (head) to last (tail). This is the same
* order that elements would be dequeued (via successive calls to
* {@link #remove} or popped (via successive calls to {@link #pop}).
*
* @return an <tt>Iterator</tt> over the elements in this deque
*/
public Iterator iterator() {
return new DeqIterator();
}
private class DeqIterator implements Iterator {
/**
* Index of element to be returned by subsequent call to next.
*/
private int cursor = head;
/**
* Tail recorded at construction (also in remove), to stop
* iterator and also to check for comodification.
*/
private int fence = tail;
/**
* Index of element returned by most recent call to next.
* Reset to -1 if element is deleted by a call to remove.
*/
private int lastRet = -1;
public boolean hasNext() {
return cursor != fence;
}
public Object next() {
Object result;
if (cursor == fence)
throw new NoSuchElementException();
// This check doesn't catch all possible comodifications,
// but does catch the ones that corrupt traversal
if (tail != fence || (result = elements[cursor]) == null)
throw new ConcurrentModificationException();
lastRet = cursor;
cursor = (cursor + 1) & (elements.length - 1);
return result;
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
if (delete(lastRet))
cursor--;
lastRet = -1;
fence = tail;
}
}
/**
* Returns <tt>true</tt> if this deque contains the specified
* element. More formally, returns <tt>true</tt> if and only if this
* deque contains at least one element <tt>e</tt> such that
* <tt>e.equals(o)</tt>.
*
* @param o object to be checked for containment in this deque
* @return <tt>true</tt> if this deque contains the specified element
*/
public boolean contains(Object o) {
if (o == null)
return false;
int mask = elements.length - 1;
int i = head;
Object x;
while ( (x = elements[i]) != null) {
if (o.equals(x))
return true;
i = (i + 1) & mask;
}
return false;
}
/**
* Removes a single instance of the specified element from this deque.
* This method is equivalent to {@link #removeFirstOccurrence}.
*
* @param e element to be removed from this deque, if present
* @return <tt>true</tt> if this deque contained the specified element
*/
public boolean remove(Object e) {
return removeFirstOccurrence(e);
}
/**
* Removes all of the elements from this deque.
*/
public void clear() {
int h = head;
int t = tail;
if (h != t) { // clear all cells
head = tail = 0;
int i = h;
int mask = elements.length - 1;
do {
elements[i] = null;
i = (i + 1) & mask;
} while(i != t);
}
}
/**
* Returns an array containing all of the elements in this list
* in the correct order.
*
* @return an array containing all of the elements in this list
* in the correct order
*/
public Object[] toArray() {
return copyElements(new Object[size()]);
}
/**
* Returns an array containing all of the elements in this deque in the
* correct order; the runtime type of the returned array is that of the
* specified array. If the deque fits in the specified array, it is
* returned therein. Otherwise, a new array is allocated with the runtime
* type of the specified array and the size of this deque.
*
* <p>If the deque fits in the specified array with room to spare (i.e.,
* the array has more elements than the deque), the element in the array
* immediately following the end of the collection is set to <tt>null</tt>.
*
* @param a the array into which the elements of the deque are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose
* @return an array containing the elements of the deque
* @throws ArrayStoreException if the runtime type of a is not a supertype
* of the runtime type of every element in this deque
*/
public Object[] toArray(Object[] a) {
int size = size();
if (a.length < size)
a = (Object[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
copyElements(a);
if (a.length > size)
a[size] = null;
return a;
}
// *** Object methods ***
/**
* Returns a copy of this deque.
*
* @return a copy of this deque
*/
public Object clone() {
try {
ArrayDeque result = (ArrayDeque) super.clone();
// These two lines are currently faster than cloning the array:
result.elements = (Object[]) new Object[elements.length];
System.arraycopy(elements, 0, result.elements, 0, elements.length);
return result;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
/**
* Appease the serialization gods.
*/
private static final long serialVersionUID = 2340985798034038923L;
/**
* Serialize this deque.
*
* @serialData The current size (<tt>int</tt>) of the deque,
* followed by all of its elements (each an object reference) in
* first-to-last order.
*/
private void writeObject(ObjectOutputStream s) throws IOException {
s.defaultWriteObject();
// Write out size
int size = size();
s.writeInt(size);
// Write out elements in order.
int i = head;
int mask = elements.length - 1;
for (int j = 0; j < size; j++) {
s.writeObject(elements[i]);
i = (i + 1) & mask;
}
}
/**
* Deserialize this deque.
*/
private void readObject(ObjectInputStream s)
throws IOException, ClassNotFoundException {
s.defaultReadObject();
// Read in size and allocate array
int size = s.readInt();
allocateElements(size);
head = 0;
tail = size;
// Read in all elements in the proper order.
for (int i = 0; i < size; i++)
elements[i] = (Object)s.readObject();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?