QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / beagled / Lucene.Net / Search / ConjunctionScorer.cs
blobd655c2378c85de368bf9b9ddfc9a27503b37f914
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.
17 using System;
19 namespace Lucene.Net.Search
22 /// <summary>Scorer for conjunctions, sets of queries, all of which are required. </summary>
23 class ConjunctionScorer : Scorer
25 private class AnonymousClassComparator : System.Collections.IComparer
27 public AnonymousClassComparator(ConjunctionScorer enclosingInstance)
29 InitBlock(enclosingInstance);
31 private void InitBlock(ConjunctionScorer enclosingInstance)
33 this.enclosingInstance = enclosingInstance;
35 private ConjunctionScorer enclosingInstance;
36 public ConjunctionScorer Enclosing_Instance
38 get
40 return enclosingInstance;
44 // sort the array
45 public virtual int Compare(System.Object o1, System.Object o2)
47 return ((Scorer) o1).Doc() - ((Scorer) o2).Doc();
50 private System.Collections.ArrayList scorers = new System.Collections.ArrayList();
51 private bool firstTime = true;
52 private bool more = true;
53 private float coord;
55 public ConjunctionScorer(Similarity similarity):base(similarity)
59 internal void Add(Scorer scorer)
61 scorers.Insert(scorers.Count, scorer);
64 private Scorer First()
66 return (Scorer) scorers[0];
68 private Scorer Last()
70 return (Scorer) scorers[scorers.Count - 1];
73 public override int Doc()
75 return First().Doc();
78 public override bool Next()
80 if (firstTime)
82 Init(true);
84 else if (more)
86 more = Last().Next(); // trigger further scanning
88 return DoNext();
91 private bool DoNext()
93 while (more && First().Doc() < Last().Doc())
95 // find doc w/ all clauses
96 more = First().SkipTo(Last().Doc()); // skip first upto last
97 System.Object tempObject;
98 tempObject = scorers[0];
99 scorers.RemoveAt(0);
100 scorers.Insert(scorers.Count, tempObject); // move first to last
102 return more; // found a doc with all clauses
105 public override bool SkipTo(int target)
107 if (firstTime)
109 Init(false);
112 System.Collections.IEnumerator i = scorers.GetEnumerator();
113 while (more && i.MoveNext())
115 more = ((Scorer) i.Current).SkipTo(target);
118 if (more)
119 SortScorers(); // re-sort scorers
121 return DoNext();
124 public override float Score()
126 float score = 0.0f; // sum scores
127 System.Collections.IEnumerator i = scorers.GetEnumerator();
128 while (i.MoveNext())
130 score += ((Scorer) i.Current).Score();
132 score *= coord;
133 return score;
136 private void Init(bool initScorers)
138 // compute coord factor
139 coord = GetSimilarity().Coord(scorers.Count, scorers.Count);
141 more = scorers.Count > 0;
143 if (initScorers)
145 // move each scorer to its first entry
146 System.Collections.IEnumerator i = scorers.GetEnumerator();
147 while (more && i.MoveNext())
149 more = ((Scorer) i.Current).Next();
151 if (more)
152 SortScorers(); // initial sort of list
155 firstTime = false;
158 private void SortScorers()
160 // move scorers to an array
161 Scorer[] array = (Scorer[]) scorers.ToArray(typeof(Scorer));
162 scorers.Clear(); // empty the list
164 // note that this comparator is not consistent with equals!
165 System.Array.Sort(array, new AnonymousClassComparator(this));
167 for (int i = 0; i < array.Length; i++)
169 scorers.Insert(scorers.Count, array[i]); // re-build list, now sorted
173 public override Explanation Explain(int doc)
175 throw new System.NotSupportedException();