Update the thread-local storage patch, to fix #335178
[beagle.git] / beagled / Lucene.Net / Index / MultipleTermPositions.cs
blobdda81659cb123eeec51c70447be9fb9f7108a00f
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.
16 using System;
17 using PriorityQueue = Lucene.Net.Util.PriorityQueue;
18 namespace Lucene.Net.Index
22 /// <summary> Describe class <code>MultipleTermPositions</code> here.
23 ///
24 /// </summary>
25 /// <author> Anders Nielsen
26 /// </author>
27 /// <version> 1.0
28 /// </version>
29 public class MultipleTermPositions : TermPositions
31 private sealed class TermPositionsQueue:PriorityQueue
33 internal TermPositionsQueue(System.Collections.IList termPositions)
35 Initialize(termPositions.Count);
37 System.Collections.IEnumerator i = termPositions.GetEnumerator();
38 while (i.MoveNext())
40 TermPositions tp = (TermPositions) i.Current;
41 if (tp.Next())
42 Put(tp);
46 internal TermPositions Peek()
48 return (TermPositions) Top();
51 public override bool LessThan(System.Object a, System.Object b)
53 return ((TermPositions) a).Doc() < ((TermPositions) b).Doc();
57 private sealed class IntQueue
59 public IntQueue()
61 InitBlock();
63 private void InitBlock()
65 _array = new int[_arraySize];
67 private int _arraySize = 16;
69 private int _index = 0;
70 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;
115 private TermPositionsQueue _termPositionsQueue;
116 private IntQueue _posList;
118 /// <summary> Creates a new <code>MultipleTermPositions</code> instance.
119 ///
120 /// </summary>
121 /// <param name="indexReader">an <code>IndexReader</code> value
122 /// </param>
123 /// <param name="terms">a <code>Term[]</code> value
124 /// </param>
125 /// <exception cref=""> IOException if an error occurs
126 /// </exception>
127 public MultipleTermPositions(IndexReader indexReader, Term[] terms)
129 System.Collections.IList termPositions = new System.Collections.ArrayList();
131 for (int i = 0; i < terms.Length; i++)
132 termPositions.Add(indexReader.TermPositions(terms[i]));
134 _termPositionsQueue = new TermPositionsQueue(termPositions);
135 _posList = new IntQueue();
138 public bool Next()
140 if (_termPositionsQueue.Size() == 0)
141 return false;
143 _posList.Clear();
144 _doc = _termPositionsQueue.Peek().Doc();
146 TermPositions tp;
149 tp = _termPositionsQueue.Peek();
151 for (int i = 0; i < tp.Freq(); i++)
152 _posList.Add(tp.NextPosition());
154 if (tp.Next())
155 _termPositionsQueue.AdjustTop();
156 else
158 _termPositionsQueue.Pop();
159 tp.Close();
162 while (_termPositionsQueue.Size() > 0 && _termPositionsQueue.Peek().Doc() == _doc);
164 _posList.Sort();
165 _freq = _posList.Size();
167 return true;
170 public int NextPosition()
172 return _posList.Next();
175 public bool SkipTo(int target)
177 while (target > _termPositionsQueue.Peek().Doc())
179 TermPositions tp = (TermPositions) _termPositionsQueue.Pop();
181 if (tp.SkipTo(target))
182 _termPositionsQueue.Put(tp);
183 else
184 tp.Close();
187 return Next();
190 public int Doc()
192 return _doc;
195 public int Freq()
197 return _freq;
200 public void Close()
202 while (_termPositionsQueue.Size() > 0)
203 ((TermPositions) _termPositionsQueue.Pop()).Close();
206 /// <summary>Not implemented.</summary>
207 /// <throws> UnsupportedOperationException </throws>
208 public virtual void Seek(Term arg0)
210 throw new System.NotSupportedException();
213 /// <summary>Not implemented.</summary>
214 /// <throws> UnsupportedOperationException </throws>
215 public virtual void Seek(TermEnum termEnum)
217 throw new System.NotSupportedException();
220 /// <summary>Not implemented.</summary>
221 /// <throws> UnsupportedOperationException </throws>
222 public virtual int Read(int[] arg0, int[] arg1)
224 throw new System.NotSupportedException();