2006-09-10 Francisco Javier F. Serrador <serrador@openshine.com>
[beagle.git] / beagled / Lucene.Net / Search / ConjunctionScorer.cs
blobdf26613ac730ddb385c76cb9b546a2360ed6854f
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 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 virtual int Compare(System.Object o1, System.Object o2)
45 return ((Scorer) o1).Doc() - ((Scorer) o2).Doc();
48 private System.Collections.ArrayList scorers = new System.Collections.ArrayList();
49 private bool firstTime = true;
50 private bool more = true;
51 private float coord;
53 public ConjunctionScorer(Similarity similarity):base(similarity)
57 internal void Add(Scorer scorer)
59 scorers.Insert(scorers.Count, scorer);
62 private Scorer First()
64 return (Scorer) scorers[0];
66 private Scorer Last()
68 return (Scorer) scorers[scorers.Count - 1];
71 public override int Doc()
73 return First().Doc();
76 public override bool Next()
78 if (firstTime)
80 Init(true);
82 else if (more)
84 more = Last().Next(); // trigger further scanning
86 return DoNext();
89 private bool DoNext()
91 while (more && First().Doc() < Last().Doc())
93 // find doc w/ all clauses
94 more = First().SkipTo(Last().Doc()); // skip first upto last
95 System.Object tempObject;
96 tempObject = scorers[0];
97 scorers.RemoveAt(0);
98 scorers.Insert(scorers.Count, tempObject); // move first to last
100 return more; // found a doc with all clauses
103 public override bool SkipTo(int target)
105 if (firstTime)
107 Init(false);
110 System.Collections.IEnumerator i = scorers.GetEnumerator();
111 while (more && i.MoveNext())
113 more = ((Scorer) i.Current).SkipTo(target);
116 if (more)
117 SortScorers(); // re-sort scorers
119 return DoNext();
122 public override float Score()
124 float score = 0.0f; // sum scores
125 System.Collections.IEnumerator i = scorers.GetEnumerator();
126 while (i.MoveNext())
128 score += ((Scorer) i.Current).Score();
130 score *= coord;
131 return score;
134 private void Init(bool initScorers)
136 // compute coord factor
137 coord = GetSimilarity().Coord(scorers.Count, scorers.Count);
139 more = scorers.Count > 0;
141 if (initScorers)
143 // move each scorer to its first entry
144 System.Collections.IEnumerator i = scorers.GetEnumerator();
145 while (more && i.MoveNext())
147 more = ((Scorer) i.Current).Next();
149 if (more)
150 SortScorers(); // initial sort of list
153 firstTime = false;
156 private void SortScorers()
158 // move scorers to an array
159 Scorer[] array = (Scorer[]) scorers.ToArray(typeof(Scorer));
160 scorers.Clear(); // empty the list
162 // note that this comparator is not consistent with equals!
163 System.Array.Sort(array, new AnonymousClassComparator(this));
165 for (int i = 0; i < array.Length; i++)
167 scorers.Insert(scorers.Count, array[i]); // re-build list, now sorted
171 public override Explanation Explain(int doc)
173 throw new System.NotSupportedException();