cvsimport
[beagle.git] / beagled / Lucene.Net / Index / MultipleTermPositions.cs
blob05e86788d356ef6fac80a6b323149001e7805a70
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.
17 using System;
18 using PriorityQueue = Lucene.Net.Util.PriorityQueue;
20 namespace Lucene.Net.Index
23 /// <summary> Describe class <code>MultipleTermPositions</code> here.
24 ///
25 /// </summary>
26 /// <author> Anders Nielsen
27 /// </author>
28 /// <version> 1.0
29 /// </version>
30 public class MultipleTermPositions : TermPositions
33 private sealed class TermPositionsQueue:PriorityQueue
35 internal TermPositionsQueue(System.Collections.IList termPositions)
37 Initialize(termPositions.Count);
39 System.Collections.IEnumerator i = termPositions.GetEnumerator();
40 while (i.MoveNext())
42 TermPositions tp = (TermPositions) i.Current;
43 if (tp.Next())
44 Put(tp);
48 internal TermPositions Peek()
50 return (TermPositions) Top();
53 public override bool LessThan(System.Object a, System.Object b)
55 return ((TermPositions) a).Doc() < ((TermPositions) b).Doc();
59 private sealed class IntQueue
61 public IntQueue()
63 InitBlock();
65 private void InitBlock()
67 _array = new int[_arraySize];
69 private int _arraySize = 16;
70 private int _index = 0;
71 private int _lastIndex = 0;
72 private int[] _array;
74 internal void Add(int i)
76 if (_lastIndex == _arraySize)
77 GrowArray();
79 _array[_lastIndex++] = i;
82 internal int Next()
84 return _array[_index++];
87 internal void Sort()
89 System.Array.Sort(_array, _index, _lastIndex - _index);
92 internal void Clear()
94 _index = 0;
95 _lastIndex = 0;
98 internal int Size()
100 return (_lastIndex - _index);
103 private void GrowArray()
105 int[] newArray = new int[_arraySize * 2];
106 Array.Copy(_array, 0, newArray, 0, _arraySize);
107 _array = newArray;
108 _arraySize *= 2;
112 private int _doc;
113 private int _freq;
114 private TermPositionsQueue _termPositionsQueue;
115 private IntQueue _posList;
117 /// <summary> Creates a new <code>MultipleTermPositions</code> instance.
118 ///
119 /// </summary>
120 /// <exception cref="IOException">
121 /// </exception>
122 public MultipleTermPositions(IndexReader indexReader, Term[] terms)
124 System.Collections.IList termPositions = new System.Collections.ArrayList();
126 for (int i = 0; i < terms.Length; i++)
127 termPositions.Add(indexReader.TermPositions(terms[i]));
129 _termPositionsQueue = new TermPositionsQueue(termPositions);
130 _posList = new IntQueue();
133 public bool Next()
135 if (_termPositionsQueue.Size() == 0)
136 return false;
138 _posList.Clear();
139 _doc = _termPositionsQueue.Peek().Doc();
141 TermPositions tp;
144 tp = _termPositionsQueue.Peek();
146 for (int i = 0; i < tp.Freq(); i++)
147 _posList.Add(tp.NextPosition());
149 if (tp.Next())
150 _termPositionsQueue.AdjustTop();
151 else
153 _termPositionsQueue.Pop();
154 tp.Close();
157 while (_termPositionsQueue.Size() > 0 && _termPositionsQueue.Peek().Doc() == _doc);
159 _posList.Sort();
160 _freq = _posList.Size();
162 return true;
165 public int NextPosition()
167 return _posList.Next();
170 public bool SkipTo(int target)
172 while (_termPositionsQueue.Peek() != null && target > _termPositionsQueue.Peek().Doc())
174 TermPositions tp = (TermPositions) _termPositionsQueue.Pop();
175 if (tp.SkipTo(target))
176 _termPositionsQueue.Put(tp);
177 else
178 tp.Close();
180 return Next();
183 public int Doc()
185 return _doc;
188 public int Freq()
190 return _freq;
193 public void Close()
195 while (_termPositionsQueue.Size() > 0)
196 ((TermPositions) _termPositionsQueue.Pop()).Close();
199 /// <summary> Not implemented.</summary>
200 /// <throws> UnsupportedOperationException </throws>
201 public virtual void Seek(Term arg0)
203 throw new System.NotSupportedException();
206 /// <summary> Not implemented.</summary>
207 /// <throws> UnsupportedOperationException </throws>
208 public virtual void Seek(TermEnum termEnum)
210 throw new System.NotSupportedException();
213 /// <summary> Not implemented.</summary>
214 /// <throws> UnsupportedOperationException </throws>
215 public virtual int Read(int[] arg0, int[] arg1)
217 throw new System.NotSupportedException();