Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / ConjunctionScorer.cs
blob4521bdc4756aed7ccd43285c9c3308956daaff6f
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
16 using System;
17 namespace Lucene.Net.Search
20 /// <summary>Scorer for conjunctions, sets of queries, all of which are required. </summary>
21 sealed class ConjunctionScorer:Scorer
23 private class AnonymousClassComparator : System.Collections.IComparer
25 public AnonymousClassComparator(ConjunctionScorer enclosingInstance)
27 InitBlock(enclosingInstance);
29 private void InitBlock(ConjunctionScorer enclosingInstance)
31 this.enclosingInstance = enclosingInstance;
33 private ConjunctionScorer enclosingInstance;
34 public ConjunctionScorer Enclosing_Instance
36 get
38 return enclosingInstance;
42 // sort the array
43 public int Compare(System.Object o1, System.Object o2)
45 return ((Scorer) o1).Doc() - ((Scorer) o2).Doc();
47 public bool equals(System.Object o1, System.Object o2)
49 return ((Scorer) o1).Doc() == ((Scorer) o2).Doc();
52 private System.Collections.ArrayList scorers = new System.Collections.ArrayList();
53 private bool firstTime = true;
54 private bool more = true;
55 private float coord;
57 public ConjunctionScorer(Similarity similarity):base(similarity)
61 internal void Add(Scorer scorer)
63 scorers.Insert(scorers.Count, scorer);
66 private Scorer First()
68 return (Scorer) scorers[0];
70 private Scorer Last()
72 return (Scorer) scorers[scorers.Count - 1];
75 public override int Doc()
77 return First().Doc();
80 public override bool Next()
82 if (firstTime)
84 Init();
86 else if (more)
88 more = Last().Next(); // trigger further scanning
90 return DoNext();
93 private bool DoNext()
95 while (more && First().Doc() < Last().Doc())
97 // find doc w/ all clauses
98 more = First().SkipTo(Last().Doc()); // skip first upto last
99 System.Object tempObject;
100 tempObject = scorers[0];
101 scorers.RemoveAt(0);
102 scorers.Insert(scorers.Count, tempObject); // move first to last
104 return more; // found a doc with all clauses
107 public override bool SkipTo(int target)
109 System.Collections.IEnumerator i = scorers.GetEnumerator();
110 while (more && i.MoveNext())
112 more = ((Scorer) i.Current).SkipTo(target);
114 if (more)
115 SortScorers(); // re-sort scorers
116 return DoNext();
119 public override float Score()
121 float score = 0.0f; // sum scores
122 System.Collections.IEnumerator i = scorers.GetEnumerator();
123 while (i.MoveNext())
125 score += ((Scorer) i.Current).Score();
127 score *= coord;
128 return score;
131 private void Init()
133 more = scorers.Count > 0;
135 // compute coord factor
136 coord = GetSimilarity().Coord(scorers.Count, scorers.Count);
138 // move each scorer to its first entry
139 System.Collections.IEnumerator i = scorers.GetEnumerator();
140 while (more && i.MoveNext())
142 more = ((Scorer) i.Current).Next();
144 if (more)
145 SortScorers(); // initial sort of list
147 firstTime = false;
150 private void SortScorers()
152 // move scorers to an array
153 Scorer[] array = (Scorer[]) scorers.ToArray(typeof(Scorer));
154 scorers.Clear(); // empty the list
156 // note that this comparator is not consistent with equals!
157 System.Array.Sort(array, new AnonymousClassComparator(this));
159 for (int i = 0; i < array.Length; i++)
161 scorers.Insert(scorers.Count, array[i]); // re-build list, now sorted
165 public override Explanation Explain(int doc)
167 throw new System.NotSupportedException();