3 |
* |
* |
4 |
* Copyright (c) 2003, Tuomas J. Lukka |
* Copyright (c) 2003, Tuomas J. Lukka |
5 |
* |
* |
6 |
* This file is part of Gzz. |
* This file is part of Libvob. |
7 |
* |
* |
8 |
* Gzz is free software; you can redistribute it and/or modify it under |
* Libvob is free software; you can redistribute it and/or modify it under |
9 |
* the terms of the GNU Lesser General Public License as published by |
* the terms of the GNU Lesser General Public License as published by |
10 |
* the Free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
11 |
* (at your option) any later version. |
* (at your option) any later version. |
12 |
* |
* |
13 |
* Gzz is distributed in the hope that it will be useful, but WITHOUT |
* Libvob is distributed in the hope that it will be useful, but WITHOUT |
14 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
15 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General |
16 |
* Public License for more details. |
* Public License for more details. |
17 |
* |
* |
18 |
* You should have received a copy of the GNU Lesser General |
* You should have received a copy of the GNU Lesser General |
19 |
* Public License along with Gzz; if not, write to the Free |
* Public License along with Libvob; if not, write to the Free |
20 |
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
21 |
* MA 02111-1307 USA |
* MA 02111-1307 USA |
22 |
* |
* |
31 |
|
|
32 |
/** A simple implementation of a priority queue, for background |
/** A simple implementation of a priority queue, for background |
33 |
* jobs. |
* jobs. |
|
* N.B. the current implementation is not the most efficient |
|
|
* possible one, |
|
|
* by far. |
|
34 |
* <p> |
* <p> |
35 |
* This class is not synchronized! |
* This interface and its implementations are not synchronized, |
36 |
|
* analogous to java.util Containers! |
37 |
*/ |
*/ |
38 |
|
public interface PriorityQueue { |
|
public class PriorityQueue { |
|
|
|
|
|
/** Compare two objects using the jobPriority value |
|
|
* as primary and hash code as secondary code. |
|
|
*/ |
|
|
private class Comp implements Comparator { |
|
|
public int compare(Object o1, Object o2) { |
|
|
Float p1 = (Float)jobPriority.get(o1); |
|
|
Float p2 = (Float)jobPriority.get(o2); |
|
|
float f1 = p1.floatValue(); |
|
|
float f2 = p2.floatValue(); |
|
|
if(f1 < f2) return -1; |
|
|
if(f1 > f2) return 1; |
|
|
int i1 = o1.hashCode(); |
|
|
int i2 = o2.hashCode(); |
|
|
if(i1 < i2) return -1; |
|
|
if(i1 > i2) return 1; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
/** Job object to Float (the priority). |
|
|
*/ |
|
|
private Map jobPriority = new HashMap(); |
|
|
|
|
|
/** Set of jobs, to be compared by their priorities. |
|
|
*/ |
|
|
private TreeSet jobs = new TreeSet(new Comp()); |
|
|
|
|
|
|
|
39 |
/** Add a job to run. |
/** Add a job to run. |
40 |
* If the job has already been added, set the priority |
* If the job has already been added, set the priority |
41 |
* to the lower value (more important) of the two. |
* to the lower value (more important) of the two. |
42 |
*/ |
*/ |
43 |
public void add(Object job, float priority) { |
void add(Object job, float priority) ; |
44 |
Float prevprio = (Float)jobPriority.get(job); |
|
45 |
if(prevprio != null) { |
/** Remove a job from the queue. |
46 |
if(priority >= prevprio.intValue()) return; |
*/ |
47 |
// Now, in order not to corrupt the TreeSet, |
void remove(Object job) ; |
|
// we must FIRST remove the element, then change |
|
|
// the priority and then re-add it after the priority |
|
|
// has been changed. |
|
|
jobs.remove(job); |
|
|
} |
|
|
jobPriority.put(job, new Float(priority)); |
|
|
jobs.add(job); |
|
|
} |
|
48 |
|
|
49 |
/** Get the most important (with lowest numeric priority) job and remove |
/** Get the most important (with lowest numeric priority) job and remove |
50 |
* it from this queue. |
* it from this queue. |
51 |
|
* If there are several jobs with the same importance, |
52 |
|
* no guarantee is made of which job is returned. |
53 |
|
* Subclasses *may* make their own guarantees about this. |
54 |
*/ |
*/ |
55 |
public Object getAndRemoveLowest() { |
Object getAndRemoveLowest() ; |
|
if(jobs.isEmpty()) return null; |
|
|
Object j = jobs.first(); |
|
|
jobs.remove(j); |
|
|
jobPriority.remove(j); |
|
|
return j; |
|
|
} |
|
56 |
} |
} |