3 namespace Lucene
.Net
.Util
5 /* ====================================================================
6 * The Apache Software License, Version 1.1
8 * Copyright (c) 2001 The Apache Software Foundation. All rights
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
23 * 3. The end-user documentation included with the redistribution,
24 * if any, must include the following acknowledgment:
25 * "This product includes software developed by the
26 * Apache Software Foundation (http://www.apache.org/)."
27 * Alternately, this acknowledgment may appear in the software itself,
28 * if and wherever such third-party acknowledgments normally appear.
30 * 4. The names "Apache" and "Apache Software Foundation" and
31 * "Apache Lucene" must not be used to endorse or promote products
32 * derived from this software without prior written permission. For
33 * written permission, please contact apache@apache.org.
35 * 5. Products derived from this software may not be called "Apache",
36 * "Apache Lucene", nor may "Apache" appear in their name, without
37 * prior written permission of the Apache Software Foundation.
39 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * ====================================================================
53 * This software consists of voluntary contributions made by many
54 * individuals on behalf of the Apache Software Foundation. For more
55 * information on the Apache Software Foundation, please see
56 * <http://www.apache.org/>.
60 /// A PriorityQueue maintains a partial ordering of its elements such that the
61 /// least element can always be found in constant time. Put()'s and Pop()'s
62 /// require Log(size) time.
64 public abstract class PriorityQueue
66 private Object
[] heap
;
71 /// Determines the ordering of objects in this priority queue. Subclasses
72 /// must define this one method.
74 /// <param name="a"></param>
75 /// <param name="b"></param>
76 /// <returns></returns>
77 protected abstract bool LessThan(Object a
, Object b
);
80 /// Subclass constructors must call this.
82 /// <param name="max_size"></param>
83 protected void Initialize(int max_size
)
86 int heap_size
= max_size
+ 1;
87 heap
= new Object
[heap_size
];
88 this.maxSize
= max_size
;
92 /// Adds an Object to a PriorityQueue in Log(size) time.
94 /// <param name="element"></param>
95 public void Put(Object element
)
103 /// Adds element to the PriorityQueue in log(size) time if either
104 /// the PriorityQueue is not full, or not lessThan(element, top()).
106 /// <param name="element">element</param>
107 /// <returns>true if element is added, false otherwise.</returns>
108 public bool Insert(Object element
)
115 else if(size
> 0 && !LessThan(element
, Top()))
126 /// Returns the least element of the PriorityQueue in constant time.
128 /// <returns></returns>
138 /// Removes and returns the least element of the PriorityQueue in Log(size)
141 /// <returns></returns>
146 Object result
= heap
[1]; // save first value
147 heap
[1] = heap
[size
]; // move last to first
148 heap
[size
] = null; // permit GC of objects
150 DownHeap(); // adjust heap
158 /// Should be called when the Object at top changes values. Still Log(n)
159 /// worst case, but it's at least twice as fast to <pre>
160 /// { pq.Top().Change(); pq.AdjustTop(); }
161 /// </pre> instead of <pre>
162 /// { o = pq.Pop(); o.Change(); pq.Push(o); }
165 public void AdjustTop()
171 /// Returns the number of elements currently stored in the PriorityQueue.
173 /// <returns></returns>
180 /// Removes all entries from the PriorityQueue.
184 for (int i
= 0; i
<= size
; i
++)
189 private void UpHeap()
192 Object node
= heap
[i
]; // save bottom node
193 int j
= (int)(((uint)i
) >> 1);
194 while (j
> 0 && LessThan(node
, heap
[j
]))
196 heap
[i
] = heap
[j
]; // shift parents down
198 j
= (int)(((uint)j
) >> 1);
200 heap
[i
] = node
; // install saved node
203 private void DownHeap()
206 Object node
= heap
[i
]; // save top node
207 int j
= i
<< 1; // find smaller child
209 if (k
<= size
&& LessThan(heap
[k
], heap
[j
]))
213 while (j
<= size
&& LessThan(heap
[j
], node
))
215 heap
[i
] = heap
[j
]; // shift up child
219 if (k
<= size
&& LessThan(heap
[k
], heap
[j
]))
224 heap
[i
] = node
; // install saved node