2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 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
38 return enclosingInstance
;
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;
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];
68 return (Scorer
) scorers
[scorers
.Count
- 1];
71 public override int Doc()
76 public override bool Next()
84 more
= Last().Next(); // trigger further scanning
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];
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
)
110 System
.Collections
.IEnumerator i
= scorers
.GetEnumerator();
111 while (more
&& i
.MoveNext())
113 more
= ((Scorer
) i
.Current
).SkipTo(target
);
117 SortScorers(); // re-sort scorers
122 public override float Score()
124 float score
= 0.0f
; // sum scores
125 System
.Collections
.IEnumerator i
= scorers
.GetEnumerator();
128 score
+= ((Scorer
) i
.Current
).Score();
134 private void Init(bool initScorers
)
136 // compute coord factor
137 coord
= GetSimilarity().Coord(scorers
.Count
, scorers
.Count
);
139 more
= scorers
.Count
> 0;
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();
150 SortScorers(); // initial sort of list
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();