2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 using PriorityQueue = Mono.Lucene.Net.Util.PriorityQueue;
22 namespace Mono.Lucene.Net.Index
25 /// <summary> Allows you to iterate over the {@link TermPositions} for multiple {@link Term}s as
26 /// a single {@link TermPositions}.
29 public class MultipleTermPositions : TermPositions
32 private sealed class TermPositionsQueue:PriorityQueue
34 internal TermPositionsQueue(System.Collections.IList termPositions)
36 Initialize(termPositions.Count);
38 System.Collections.IEnumerator i = termPositions.GetEnumerator();
41 TermPositions tp = (TermPositions) i.Current;
47 internal TermPositions Peek()
49 return (TermPositions) Top();
52 public override bool LessThan(System.Object a, System.Object b)
54 return ((TermPositions) a).Doc() < ((TermPositions) b).Doc();
58 private sealed class IntQueue
64 private void InitBlock()
66 _array = new int[_arraySize];
68 private int _arraySize = 16;
69 private int _index = 0;
70 private int _lastIndex = 0;
73 internal void add(int i)
75 if (_lastIndex == _arraySize)
78 _array[_lastIndex++] = i;
83 return _array[_index++];
88 System.Array.Sort(_array, _index, _lastIndex - _index);
99 return (_lastIndex - _index);
102 private void growArray()
104 int[] newArray = new int[_arraySize * 2];
105 Array.Copy(_array, 0, newArray, 0, _arraySize);
113 private TermPositionsQueue _termPositionsQueue;
114 private IntQueue _posList;
116 /// <summary> Creates a new <code>MultipleTermPositions</code> instance.
119 /// <exception cref="IOException">
121 public MultipleTermPositions(IndexReader indexReader, Term[] terms)
123 System.Collections.IList termPositions = new System.Collections.ArrayList();
125 for (int i = 0; i < terms.Length; i++)
126 termPositions.Add(indexReader.TermPositions(terms[i]));
128 _termPositionsQueue = new TermPositionsQueue(termPositions);
129 _posList = new IntQueue();
134 if (_termPositionsQueue.Size() == 0)
138 _doc = _termPositionsQueue.Peek().Doc();
143 tp = _termPositionsQueue.Peek();
145 for (int i = 0; i < tp.Freq(); i++)
146 _posList.add(tp.NextPosition());
149 _termPositionsQueue.AdjustTop();
152 _termPositionsQueue.Pop();
156 while (_termPositionsQueue.Size() > 0 && _termPositionsQueue.Peek().Doc() == _doc);
159 _freq = _posList.size();
164 public int NextPosition()
166 return _posList.next();
169 public bool SkipTo(int target)
171 while (_termPositionsQueue.Peek() != null && target > _termPositionsQueue.Peek().Doc())
173 TermPositions tp = (TermPositions) _termPositionsQueue.Pop();
174 if (tp.SkipTo(target))
175 _termPositionsQueue.Put(tp);
194 while (_termPositionsQueue.Size() > 0)
195 ((TermPositions) _termPositionsQueue.Pop()).Close();
198 /// <summary> Not implemented.</summary>
199 /// <throws> UnsupportedOperationException </throws>
200 public virtual void Seek(Term arg0)
202 throw new System.NotSupportedException();
205 /// <summary> Not implemented.</summary>
206 /// <throws> UnsupportedOperationException </throws>
207 public virtual void Seek(TermEnum termEnum)
209 throw new System.NotSupportedException();
212 /// <summary> Not implemented.</summary>
213 /// <throws> UnsupportedOperationException </throws>
214 public virtual int Read(int[] arg0, int[] arg1)
216 throw new System.NotSupportedException();
220 /// <summary> Not implemented.</summary>
221 /// <throws> UnsupportedOperationException </throws>
222 public virtual int GetPayloadLength()
224 throw new System.NotSupportedException();
227 /// <summary> Not implemented.</summary>
228 /// <throws> UnsupportedOperationException </throws>
229 public virtual byte[] GetPayload(byte[] data, int offset)
231 throw new System.NotSupportedException();
234 /// <summary> </summary>
237 // TODO: Remove warning after API has been finalized
238 public virtual bool IsPayloadAvailable()