Skip to content

Commit

Permalink
Replace LinkedList with ArrayDeque
Browse files Browse the repository at this point in the history
  • Loading branch information
zsxwing committed Apr 19, 2014
1 parent 0fc6e2c commit a2604a8
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions rxjava-core/src/main/java/rx/operators/OperatorSkipLast.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package rx.operators;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;

import rx.Observable.Operator;
import rx.Subscriber;
Expand All @@ -38,10 +38,13 @@ public OperatorSkipLast(int count) {
@Override
public Subscriber<? super T> call(final Subscriber<? super T> subscriber) {
return new Subscriber<T>(subscriber) {

private final NotificationLite<T> on = NotificationLite.instance();

/**
* Store the last count elements until now.
*/
private final Deque<T> deque = new LinkedList<T>();
private final Deque<Object> deque = new ArrayDeque<Object>();

@Override
public void onCompleted() {
Expand All @@ -62,14 +65,10 @@ public void onNext(T value) {
subscriber.onNext(value);
return;
}
deque.offerLast(value);
if (deque.size() > count) {
// Now deque has count + 1 elements, so the first
// element in the deque definitely does not belong
// to the last count elements of the source
// sequence. We can emit it now.
subscriber.onNext(deque.removeFirst());
if (deque.size() == count) {
subscriber.onNext(on.getValue(deque.removeFirst()));
}
deque.offerLast(on.next(value));
}

};
Expand Down

0 comments on commit a2604a8

Please sign in to comment.