001: package it.unimi.dsi.fastutil;
002:
003: /*
004: * fastutil: Fast & compact type-specific collections for Java
005: *
006: * Copyright (C) 2003-2008 Sebastiano Vigna
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: *
022: */
023:
024: import java.util.Comparator;
025: import java.util.NoSuchElementException;
026:
027: /** A class providing static methods and objects that do useful things with indirect priority queues.
028: *
029: * @see IndirectPriorityQueue
030: */
031:
032: public class IndirectPriorityQueues {
033:
034: private IndirectPriorityQueues() {
035: }
036:
037: /** An immutable class representing the empty indirect priority queue.
038: *
039: * <P>This class may be useful to implement your own in case you subclass
040: * {@link IndirectPriorityQueue}.
041: */
042:
043: @SuppressWarnings("unchecked")
044: public static class EmptyIndirectPriorityQueue extends
045: AbstractIndirectPriorityQueue {
046:
047: protected EmptyIndirectPriorityQueue() {
048: }
049:
050: public void enqueue(final int i) {
051: throw new UnsupportedOperationException();
052: }
053:
054: public int dequeue() {
055: throw new NoSuchElementException();
056: }
057:
058: public boolean isEmpty() {
059: return true;
060: }
061:
062: public int size() {
063: return 0;
064: }
065:
066: public void clear() {
067: }
068:
069: public int first() {
070: throw new NoSuchElementException();
071: }
072:
073: public int last() {
074: throw new NoSuchElementException();
075: }
076:
077: public void changed() {
078: throw new NoSuchElementException();
079: }
080:
081: public void allChanged() {
082: }
083:
084: public Comparator comparator() {
085: return null;
086: }
087:
088: public void changed(final int i) {
089: throw new IllegalArgumentException("Index " + i
090: + " is not in the queue");
091: }
092:
093: public void remove(final int i) {
094: throw new IllegalArgumentException("Index " + i
095: + " is not in the queue");
096: }
097:
098: public int front(int[] a) {
099: return 0;
100: }
101:
102: }
103:
104: /** An empty indirect priority queue (immutable).
105: */
106:
107: public final static EmptyIndirectPriorityQueue EMPTY_QUEUE = new EmptyIndirectPriorityQueue();
108:
109: /** A synchronized wrapper class for indirect priority queues. */
110:
111: public static class SynchronizedIndirectPriorityQueue<K> implements
112: IndirectPriorityQueue<K> {
113:
114: public static final long serialVersionUID = -7046029254386353129L;
115:
116: final protected IndirectPriorityQueue<K> q;
117: final protected Object sync;
118:
119: protected SynchronizedIndirectPriorityQueue(
120: final IndirectPriorityQueue<K> q, final Object sync) {
121: this .q = q;
122: this .sync = sync;
123: }
124:
125: protected SynchronizedIndirectPriorityQueue(
126: final IndirectPriorityQueue<K> q) {
127: this .q = q;
128: this .sync = this ;
129: }
130:
131: public void enqueue(int x) {
132: synchronized (sync) {
133: q.enqueue(x);
134: }
135: }
136:
137: public int dequeue() {
138: synchronized (sync) {
139: return q.dequeue();
140: }
141: }
142:
143: public int first() {
144: synchronized (sync) {
145: return q.first();
146: }
147: }
148:
149: public int last() {
150: synchronized (sync) {
151: return q.last();
152: }
153: }
154:
155: public boolean isEmpty() {
156: synchronized (sync) {
157: return q.isEmpty();
158: }
159: }
160:
161: public int size() {
162: synchronized (sync) {
163: return q.size();
164: }
165: }
166:
167: public void clear() {
168: synchronized (sync) {
169: q.clear();
170: }
171: }
172:
173: public void changed() {
174: synchronized (sync) {
175: q.changed();
176: }
177: }
178:
179: public void allChanged() {
180: synchronized (sync) {
181: q.allChanged();
182: }
183: }
184:
185: public void changed(int i) {
186: synchronized (sync) {
187: q.changed(i);
188: }
189: }
190:
191: public void remove(int i) {
192: synchronized (sync) {
193: q.remove(i);
194: }
195: }
196:
197: public Comparator<? super K> comparator() {
198: synchronized (sync) {
199: return q.comparator();
200: }
201: }
202:
203: public int front(int[] a) {
204: return q.front(a);
205: }
206: }
207:
208: /** Returns a synchronized type-specific indirect priority queue backed by the specified type-specific indirect priority queue.
209: *
210: * @param q the indirect priority queue to be wrapped in a synchronized indirect priority queue.
211: * @return a synchronized view of the specified indirect priority queue.
212: */
213: public static <K> IndirectPriorityQueue<K> synchronize(
214: final IndirectPriorityQueue<K> q) {
215: return new SynchronizedIndirectPriorityQueue<K>(q);
216: }
217:
218: /** Returns a synchronized type-specific indirect priority queue backed by the specified type-specific indirect priority queue, using an assigned object to synchronize.
219: *
220: * @param q the indirect priority queue to be wrapped in a synchronized indirect priority queue.
221: * @param sync an object that will be used to synchronize the access to the indirect priority queue.
222: * @return a synchronized view of the specified indirect priority queue.
223: */
224:
225: public static <K> IndirectPriorityQueue<K> synchronize(
226: final IndirectPriorityQueue<K> q, final Object sync) {
227: return new SynchronizedIndirectPriorityQueue<K>(q, sync);
228: }
229:
230: }
|