2 * Copyright 2004 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 namespace Lucene
.Net
.Util
22 /// <summary>A PriorityQueue maintains a partial ordering of its elements such that the
23 /// least element can always be found in constant time. Put()'s and pop()'s
24 /// require log(size) time.
26 public abstract class PriorityQueue
28 private System
.Object
[] heap
;
32 /// <summary>Determines the ordering of objects in this priority queue. Subclasses
33 /// must define this one method.
35 public abstract bool LessThan(System
.Object a
, System
.Object b
);
37 /// <summary>Subclass constructors must call this. </summary>
38 protected internal void Initialize(int maxSize
)
41 int heapSize
= maxSize
+ 1;
42 heap
= new System
.Object
[heapSize
];
43 this.maxSize
= maxSize
;
46 /// <summary> Adds an Object to a PriorityQueue in log(size) time.
47 /// If one tries to add more objects than maxSize from initialize
48 /// a RuntimeException (ArrayIndexOutOfBound) is thrown.
50 public void Put(System
.Object element
)
57 /// <summary> Adds element to the PriorityQueue in log(size) time if either
58 /// the PriorityQueue is not full, or not lessThan(element, top()).
60 /// <param name="element">
62 /// <returns> true if element is added, false otherwise.
64 public virtual bool Insert(System
.Object element
)
71 else if (size
> 0 && !LessThan(element
, Top()))
81 /// <summary>Returns the least element of the PriorityQueue in constant time. </summary>
82 public System
.Object
Top()
90 /// <summary>Removes and returns the least element of the PriorityQueue in log(size)
93 public System
.Object
Pop()
97 System
.Object result
= heap
[1]; // save first value
98 heap
[1] = heap
[size
]; // move last to first
99 heap
[size
] = null; // permit GC of objects
101 DownHeap(); // adjust heap
108 /// <summary>Should be called when the Object at top changes values. Still log(n)
109 /// worst case, but it's at least twice as fast to <pre>
110 /// { pq.top().change(); pq.adjustTop(); }
111 /// </pre> instead of <pre>
112 /// { o = pq.pop(); o.change(); pq.push(o); }
115 public void AdjustTop()
121 /// <summary>Returns the number of elements currently stored in the PriorityQueue. </summary>
127 /// <summary>Removes all entries from the PriorityQueue. </summary>
130 for (int i
= 0; i
<= size
; i
++)
135 private void UpHeap()
138 System
.Object node
= heap
[i
]; // save bottom node
139 int j
= SupportClass
.Number
.URShift(i
, 1);
140 while (j
> 0 && LessThan(node
, heap
[j
]))
142 heap
[i
] = heap
[j
]; // shift parents down
144 j
= SupportClass
.Number
.URShift(j
, 1);
146 heap
[i
] = node
; // install saved node
149 private void DownHeap()
152 System
.Object node
= heap
[i
]; // save top node
153 int j
= i
<< 1; // find smaller child
155 if (k
<= size
&& LessThan(heap
[k
], heap
[j
]))
159 while (j
<= size
&& LessThan(heap
[j
], node
))
161 heap
[i
] = heap
[j
]; // shift up child
165 if (k
<= size
&& LessThan(heap
[k
], heap
[j
]))
170 heap
[i
] = node
; // install saved node