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.
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
40 return enclosingInstance
;
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;
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];
70 return (Scorer
) scorers
[scorers
.Count
- 1];
73 public override int Doc()
78 public override bool Next()
86 more
= Last().Next(); // trigger further scanning
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];
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
)
112 System
.Collections
.IEnumerator i
= scorers
.GetEnumerator();
113 while (more
&& i
.MoveNext())
115 more
= ((Scorer
) i
.Current
).SkipTo(target
);
119 SortScorers(); // re-sort scorers
124 public override float Score()
126 float score
= 0.0f
; // sum scores
127 System
.Collections
.IEnumerator i
= scorers
.GetEnumerator();
130 score
+= ((Scorer
) i
.Current
).Score();
136 private void Init(bool initScorers
)
138 // compute coord factor
139 coord
= GetSimilarity().Coord(scorers
.Count
, scorers
.Count
);
141 more
= scorers
.Count
> 0;
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();
152 SortScorers(); // initial sort of list
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();