All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.unimi.dsi.fastutil.PriorityQueues Maven / Gradle / Ivy

Go to download

fastutil extends the Java Collections Framework by providing type-specific maps, sets, lists, and queues with a small memory footprint and fast operations; it provides also big (64-bit) arrays, sets, and lists, sorting algorithms, fast, practical I/O classes for binary and text files, and facilities for memory mapping large files. This jar (fastutil-core.jar) contains data structures based on integers, longs, doubles, and objects, only; fastutil.jar contains all classes. If you have both jars in your dependencies, this jar should be excluded.

There is a newer version: 8.5.15
Show newest version
/*
 * Copyright (C) 2003-2022 Sebastiano Vigna
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.unimi.dsi.fastutil;

import java.io.Serializable;
import java.util.Comparator;
import java.util.NoSuchElementException;

/** A class providing static methods and objects that do useful things with priority queues.
 *
 * @see it.unimi.dsi.fastutil.PriorityQueue
 */

public class PriorityQueues {

	private PriorityQueues() {}

	/** An immutable class representing the empty priority queue.
	 *
	 * 

This class may be useful to implement your own in case you subclass * {@link PriorityQueue}. */ @SuppressWarnings("rawtypes") public static class EmptyPriorityQueue implements PriorityQueue, Serializable { private static final long serialVersionUID = 0L; protected EmptyPriorityQueue() {} @Override public void enqueue(final Object o) { throw new UnsupportedOperationException(); } @Override public Object dequeue() { throw new NoSuchElementException(); } @Override public boolean isEmpty() { return true; } @Override public int size() { return 0; } @Override public void clear() {} @Override public Object first() { throw new NoSuchElementException(); } @Override public Object last() { throw new NoSuchElementException(); } @Override public void changed() { throw new NoSuchElementException(); } @Override public Comparator comparator() { return null; } @Override public Object clone() { return EMPTY_QUEUE; } @Override public int hashCode() { return 0; } @Override public boolean equals(final Object o) { return o instanceof PriorityQueue && ((PriorityQueue)o).isEmpty(); } private Object readResolve() { return EMPTY_QUEUE; } } /** An empty indirect priority queue (immutable). */ public static final EmptyPriorityQueue EMPTY_QUEUE = new EmptyPriorityQueue(); /** Returns an empty queue (immutable). It is serializable and cloneable. * *

This method provides a typesafe access to {@link #EMPTY_QUEUE}. * @param the class of the objects in the queue. * @return an empty queue (immutable). */ @SuppressWarnings("unchecked") public static PriorityQueue emptyQueue() { return EMPTY_QUEUE; } /** A synchronized wrapper class for priority queues. */ public static class SynchronizedPriorityQueue implements PriorityQueue, Serializable { public static final long serialVersionUID = -7046029254386353129L; protected final PriorityQueue q; protected final Object sync; protected SynchronizedPriorityQueue(final PriorityQueue q, final Object sync) { this.q = q; this.sync = sync; } protected SynchronizedPriorityQueue(final PriorityQueue q) { this.q = q; this.sync = this; } @Override public void enqueue(final K x) { synchronized(sync) { q.enqueue(x); } } @Override public K dequeue() { synchronized(sync) { return q.dequeue(); } } @Override public K first() { synchronized(sync) { return q.first(); } } @Override public K last() { synchronized(sync) { return q.last(); } } @Override public boolean isEmpty() { synchronized(sync) { return q.isEmpty(); } } @Override public int size() { synchronized(sync) { return q.size(); } } @Override public void clear() { synchronized(sync) { q.clear(); } } @Override public void changed() { synchronized(sync) { q.changed(); } } @Override public Comparator comparator() { synchronized(sync) { return q.comparator(); } } @Override public String toString() { synchronized(sync) { return q.toString(); } } @Override public int hashCode() { synchronized(sync) { return q.hashCode(); } } @Override public boolean equals(final Object o) { if (o == this) return true; synchronized(sync) { return q.equals(o); } } private void writeObject(final java.io.ObjectOutputStream s) throws java.io.IOException { synchronized(sync) { s.defaultWriteObject(); } } } /** Returns a synchronized priority queue backed by the specified priority queue. * * @param the class of the objects in the queue. * @param q the priority queue to be wrapped in a synchronized priority queue. * @return a synchronized view of the specified priority queue. */ public static PriorityQueue synchronize(final PriorityQueue q) { return new SynchronizedPriorityQueue<>(q); } /** Returns a synchronized priority queue backed by the specified priority queue, using an assigned object to synchronize. * * @param the class of the objects in the queue. * @param q the priority queue to be wrapped in a synchronized priority queue. * @param sync an object that will be used to synchronize the access to the priority queue. * @return a synchronized view of the specified priority queue. */ public static PriorityQueue synchronize(final PriorityQueue q, final Object sync) { return new SynchronizedPriorityQueue<>(q, sync); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy