source: tags/MetisMQIDemo/src/main/java/weka/clusterers/forOPTICSAndDBScan/Utils/PriorityQueue.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 5.0 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    Copyright (C) 2004
19 *    & Matthias Schubert (schubert@dbs.ifi.lmu.de)
20 *    & Zhanna Melnikova-Albrecht (melnikov@cip.ifi.lmu.de)
21 *    & Rainer Holzmann (holzmann@cip.ifi.lmu.de)
22 */
23
24package weka.clusterers.forOPTICSAndDBScan.Utils;
25
26import weka.core.RevisionHandler;
27import weka.core.RevisionUtils;
28
29import java.util.ArrayList;
30
31/**
32 * <p>
33 * PriorityQueue.java <br/>
34 * Authors: Rainer Holzmann, Zhanna Melnikova-Albrecht, Matthias Schubert <br/>
35 * Date: Aug 27, 2004 <br/>
36 * Time: 5:36:35 PM <br/>
37 * $ Revision 1.4 $ <br/>
38 * </p>
39 *
40 * @author Matthias Schubert (schubert@dbs.ifi.lmu.de)
41 * @author Zhanna Melnikova-Albrecht (melnikov@cip.ifi.lmu.de)
42 * @author Rainer Holzmann (holzmann@cip.ifi.lmu.de)
43 * @version $Revision: 1.3 $
44 */
45public class PriorityQueue
46    implements RevisionHandler {
47
48    /**
49     * Used to store the binary heap
50     */
51    private ArrayList queue;
52
53    // *****************************************************************************************************************
54    // constructors
55    // *****************************************************************************************************************
56
57    /**
58     * Creates a new PriorityQueue backed on a binary heap. The queue is
59     * dynamically growing and shrinking and it is descending, that is: the highest
60     * priority is always in the root.
61     */
62    public PriorityQueue() {
63        queue = new ArrayList();
64    }
65
66    // *****************************************************************************************************************
67    // methods
68    // *****************************************************************************************************************
69
70    /**
71     * Adds a new Object to the queue
72     * @param priority The priority associated with the object
73     * @param o
74     */
75    public void add(double priority, Object o) {
76        queue.add(new PriorityQueueElement(priority, o));
77        heapValueUpwards();
78    }
79
80    /**
81     * Returns the priority for the object at the specified index
82     * @param index the index of the object
83     * @return priority
84     */
85    public double getPriority(int index) {
86        return ((PriorityQueueElement) queue.get(index)).getPriority();
87    }
88
89    /**
90     * Restores the heap after inserting a new object
91     */
92    private void heapValueUpwards() {
93        int a = size();
94        int c = a / 2;
95
96        PriorityQueueElement recentlyInsertedElement = (PriorityQueueElement) queue.get(a - 1);
97
98        while (c > 0 && getPriority(c - 1) < recentlyInsertedElement.getPriority()) {
99            queue.set(a - 1, queue.get(c - 1));       //shift parent-node down
100            a = c;                                    //(c <= 0) => no parent-node remains
101            c = a / 2;
102        }
103        queue.set(a - 1, recentlyInsertedElement);
104    }
105
106    /**
107     * Restores the heap after removing the next element
108     */
109    private void heapValueDownwards() {
110        int a = 1;
111        int c = 2 * a;           //descendant
112
113        PriorityQueueElement priorityQueueElement = (PriorityQueueElement) queue.get(a - 1);
114
115        if (c < size() && (getPriority(c) > getPriority(c - 1))) c++;
116
117        while (c <= size() && getPriority(c - 1) > priorityQueueElement.getPriority()) {
118            queue.set(a - 1, queue.get(c - 1));
119            a = c;
120            c = 2 * a;
121            if (c < size() && (getPriority(c) > getPriority(c - 1))) c++;
122        }
123        queue.set(a - 1, priorityQueueElement);
124    }
125
126    /**
127     * Returns the queue's size
128     * @return size
129     */
130    public int size() {
131        return queue.size();
132    }
133
134    /**
135     * Tests, if the queue has some more elements left
136     * @return true, if there are any elements left, else false
137     */
138    public boolean hasNext() {
139        return !(size() == 0);
140    }
141
142    /**
143     * Returns the element with the highest priority
144     * @return next element
145     */
146    public PriorityQueueElement next() {
147        PriorityQueueElement next = (PriorityQueueElement) queue.get(0);
148        queue.set(0, queue.get(size() - 1));
149        queue.remove(size() - 1);
150        if (hasNext()) {
151            heapValueDownwards();
152        }
153        return next;
154    }
155   
156    /**
157     * Returns the revision string.
158     *
159     * @return          the revision
160     */
161    public String getRevision() {
162      return RevisionUtils.extract("$Revision: 1.3 $");
163    }
164}
Note: See TracBrowser for help on using the repository browser.