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 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
38 return enclosingInstance
;
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;
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];
72 return (Scorer
) scorers
[scorers
.Count
- 1];
75 public override int Doc()
80 public override bool Next()
88 more
= Last().Next(); // trigger further scanning
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];
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
);
115 SortScorers(); // re-sort scorers
119 public override float Score()
121 float score
= 0.0f
; // sum scores
122 System
.Collections
.IEnumerator i
= scorers
.GetEnumerator();
125 score
+= ((Scorer
) i
.Current
).Score();
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();
145 SortScorers(); // initial sort of list
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();