Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Search / TopDocCollector.cs
1 /* 
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 using System;
19
20 using PriorityQueue = Mono.Lucene.Net.Util.PriorityQueue;
21
22 namespace Mono.Lucene.Net.Search
23 {
24         
25         /// <summary>A {@link HitCollector} implementation that collects the top-scoring
26         /// documents, returning them as a {@link TopDocs}.  This is used by {@link
27         /// IndexSearcher} to implement {@link TopDocs}-based search.
28         /// 
29         /// <p/>This may be extended, overriding the collect method to, e.g.,
30         /// conditionally invoke <code>super()</code> in order to filter which
31         /// documents are collected.
32         /// 
33         /// </summary>
34         /// <deprecated> Please use {@link TopScoreDocCollector}
35         /// instead, which has better performance.
36         /// 
37         /// </deprecated>
38     [Obsolete("Please use TopScoreDocCollector instead, which has better performance.")]
39         public class TopDocCollector:HitCollector
40         {
41                 
42                 private ScoreDoc reusableSD;
43                 
44                 /// <summary>The total number of hits the collector encountered. </summary>
45                 protected internal int totalHits;
46                 
47                 /// <summary>The priority queue which holds the top-scoring documents. </summary>
48                 protected internal PriorityQueue hq;
49                 
50                 /// <summary>Construct to collect a given number of hits.</summary>
51                 /// <param name="numHits">the maximum number of hits to collect
52                 /// </param>
53                 public TopDocCollector(int numHits):this(new HitQueue(numHits, false))
54                 {
55                 }
56                 
57                 /// <deprecated> use TopDocCollector(hq) instead. numHits is not used by this
58                 /// constructor. It will be removed in a future release.
59                 /// </deprecated>
60         [Obsolete("use TopDocCollector(hq) instead. numHits is not used by this constructor. It will be removed in a future release.")]
61                 internal TopDocCollector(int numHits, PriorityQueue hq)
62                 {
63                         this.hq = hq;
64                 }
65                 
66                 /// <summary>Constructor to collect the top-scoring documents by using the given PQ.</summary>
67                 /// <param name="hq">the PQ to use by this instance.
68                 /// </param>
69                 protected internal TopDocCollector(PriorityQueue hq)
70                 {
71                         this.hq = hq;
72                 }
73                 
74                 // javadoc inherited
75                 public override void  Collect(int doc, float score)
76                 {
77                         if (score > 0.0f)
78                         {
79                                 totalHits++;
80                                 if (reusableSD == null)
81                                 {
82                                         reusableSD = new ScoreDoc(doc, score);
83                                 }
84                                 else if (score >= reusableSD.score)
85                                 {
86                                         // reusableSD holds the last "rejected" entry, so, if
87                                         // this new score is not better than that, there's no
88                                         // need to try inserting it
89                                         reusableSD.doc = doc;
90                                         reusableSD.score = score;
91                                 }
92                                 else
93                                 {
94                                         return ;
95                                 }
96                                 reusableSD = (ScoreDoc) hq.InsertWithOverflow(reusableSD);
97                         }
98                 }
99                 
100                 /// <summary>The total number of documents that matched this query. </summary>
101                 public virtual int GetTotalHits()
102                 {
103                         return totalHits;
104                 }
105                 
106                 /// <summary>The top-scoring hits. </summary>
107                 public virtual TopDocs TopDocs()
108                 {
109                         ScoreDoc[] scoreDocs = new ScoreDoc[hq.Size()];
110                         for (int i = hq.Size() - 1; i >= 0; i--)
111                         // put docs in array
112                                 scoreDocs[i] = (ScoreDoc) hq.Pop();
113                         
114                         float maxScore = (totalHits == 0)?System.Single.NegativeInfinity:scoreDocs[0].score;
115                         
116                         return new TopDocs(totalHits, scoreDocs, maxScore);
117                 }
118         }
119 }