// Path: src/main/java/com/github/davidmoten/rtree/internal/util/BoundedPriorityQueue.java
// public final class BoundedPriorityQueue<T> {
// 
//     private final PriorityQueue<T> queue; /* backing data structure */
//     private final Comparator<? super T> comparator;
//     private final int maxSize;
// 
//     /**
//      * Constructs a {@link BoundedPriorityQueue} with the specified
//      * {@code maxSize} and {@code comparator}.
//      *
//      * @param maxSize
//      *            - The maximum size the queue can reach, must be a positive
//      *            integer.
//      * @param comparator
//      *            - The comparator to be used to compare the elements in the
//      *            queue, must be non-null.
//      */
//     public BoundedPriorityQueue(final int maxSize, final Comparator<? super T> comparator) {
//         Preconditions.checkArgument(maxSize > 0, "maxSize must be > 0");
//         Preconditions.checkNotNull(comparator, "comparator cannot be null");
//         this.queue = new PriorityQueue<T>(reverse(comparator));
//         this.comparator = comparator;
//         this.maxSize = maxSize;
//     }
// 
//     private static <T> Comparator<T> reverse(final Comparator<T> comparator) {
//         return (o1, o2) -> comparator.compare(o2, o1);
//     }
// 
//     public static <T> BoundedPriorityQueue<T> create(final int maxSize,
//             final Comparator<? super T> comparator) {
//         return new BoundedPriorityQueue<T>(maxSize, comparator);
//     }
// 
//     /**
//      * Adds an element to the queue. If the queue contains {@code maxSize}
//      * elements, {@code e} will be compared to the lowest element in the queue
//      * using {@code comparator}. If {@code e} is greater than or equal to the
//      * lowest element, that element will be removed and {@code e} will be added
//      * instead. Otherwise, the queue will not be modified and {@code e} will not
//      * be added.
//      *
//      * @param t
//      *            - Element to be added, must be non-null.
//      */
//     public void add(final T t) {
//         if (t == null) {
//             throw new NullPointerException("cannot add null to the queue");
//         }
//         if (queue.size() >= maxSize) {
//             final T maxElement = queue.peek();
//             if (comparator.compare(maxElement, t) < 1) {
//                 return;
//             } else {
//                 queue.poll();
//             }
//         }
//         queue.add(t);
//     }
// 
//     /**
//      * @return Returns a view of the queue as a
//      *         {@link Collections#unmodifiableList(java.util.List)}
//      *         unmodifiableList sorted in reverse order.
//      */
//     public List<T> asList() {
//         return Collections.unmodifiableList(new ArrayList<>(queue));
//     }
// 
//     public List<T> asOrderedList() {
//         List<T> list = new ArrayList<>(queue);
//         list.sort(comparator);
//         return list;
//     }
// 
// }


import java.util.Comparator;
import java.util.List;
import com.github.davidmoten.rtree.internal.util.BoundedPriorityQueue;
import rx.Observable.Operator;
import rx.Subscriber;
package com.github.davidmoten.rtree.internal.operators;




public final class OperatorBoundedPriorityQueue<T> implements Operator<T, T> {

    private final int maximumSize;
    private final Comparator<? super T> comparator;

    public OperatorBoundedPriorityQueue(int maximumSize, Comparator<? super T> comparator) {
        this.maximumSize = maximumSize;
        this.comparator = comparator;
    }

    @Override
    public Subscriber<? super T> call(final Subscriber<? super T> child) {
// Path: src/main/java/com/github/davidmoten/rtree/internal/util/BoundedPriorityQueue.java
// public final class BoundedPriorityQueue<T> {
// 
//     private final PriorityQueue<T> queue; /* backing data structure */
//     private final Comparator<? super T> comparator;
//     private final int maxSize;
// 
//     /**
//      * Constructs a {@link BoundedPriorityQueue} with the specified
//      * {@code maxSize} and {@code comparator}.
//      *
//      * @param maxSize
//      *            - The maximum size the queue can reach, must be a positive
//      *            integer.
//      * @param comparator
//      *            - The comparator to be used to compare the elements in the
//      *            queue, must be non-null.
//      */
//     public BoundedPriorityQueue(final int maxSize, final Comparator<? super T> comparator) {
//         Preconditions.checkArgument(maxSize > 0, "maxSize must be > 0");
//         Preconditions.checkNotNull(comparator, "comparator cannot be null");
//         this.queue = new PriorityQueue<T>(reverse(comparator));
//         this.comparator = comparator;
//         this.maxSize = maxSize;
//     }
// 
//     private static <T> Comparator<T> reverse(final Comparator<T> comparator) {
//         return (o1, o2) -> comparator.compare(o2, o1);
//     }
// 
//     public static <T> BoundedPriorityQueue<T> create(final int maxSize,
//             final Comparator<? super T> comparator) {
//         return new BoundedPriorityQueue<T>(maxSize, comparator);
//     }
// 
//     /**
//      * Adds an element to the queue. If the queue contains {@code maxSize}
//      * elements, {@code e} will be compared to the lowest element in the queue
//      * using {@code comparator}. If {@code e} is greater than or equal to the
//      * lowest element, that element will be removed and {@code e} will be added
//      * instead. Otherwise, the queue will not be modified and {@code e} will not
//      * be added.
//      *
//      * @param t
//      *            - Element to be added, must be non-null.
//      */
//     public void add(final T t) {
//         if (t == null) {
//             throw new NullPointerException("cannot add null to the queue");
//         }
//         if (queue.size() >= maxSize) {
//             final T maxElement = queue.peek();
//             if (comparator.compare(maxElement, t) < 1) {
//                 return;
//             } else {
//                 queue.poll();
//             }
//         }
//         queue.add(t);
//     }
// 
//     /**
//      * @return Returns a view of the queue as a
//      *         {@link Collections#unmodifiableList(java.util.List)}
//      *         unmodifiableList sorted in reverse order.
//      */
//     public List<T> asList() {
//         return Collections.unmodifiableList(new ArrayList<>(queue));
//     }
// 
//     public List<T> asOrderedList() {
//         List<T> list = new ArrayList<>(queue);
//         list.sort(comparator);
//         return list;
//     }
// 
// }


// Path: src/main/java/com/github/davidmoten/rtree/internal/operators/OperatorBoundedPriorityQueue.java
import java.util.Comparator;
import java.util.List;
import com.github.davidmoten.rtree.internal.util.BoundedPriorityQueue;
import rx.Observable.Operator;
import rx.Subscriber;

package com.github.davidmoten.rtree.internal.operators;




public final class OperatorBoundedPriorityQueue<T> implements Operator<T, T> {

    private final int maximumSize;
    private final Comparator<? super T> comparator;

    public OperatorBoundedPriorityQueue(int maximumSize, Comparator<? super T> comparator) {
        this.maximumSize = maximumSize;
        this.comparator = comparator;
    }

    @Override
    public Subscriber<? super T> call(final Subscriber<? super T> child) {
        final BoundedPriorityQueue<T> q = new BoundedPriorityQueue<T>(maximumSize, comparator);