Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Index / MultipleTermPositions.cs
1 /* 
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
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  */
17
18 using System;
19
20 using PriorityQueue = Mono.Lucene.Net.Util.PriorityQueue;
21
22 namespace Mono.Lucene.Net.Index
23 {
24         
25         /// <summary> Allows you to iterate over the {@link TermPositions} for multiple {@link Term}s as
26         /// a single {@link TermPositions}.
27         /// 
28         /// </summary>
29         public class MultipleTermPositions : TermPositions
30         {
31                 
32                 private sealed class TermPositionsQueue:PriorityQueue
33                 {
34                         internal TermPositionsQueue(System.Collections.IList termPositions)
35                         {
36                                 Initialize(termPositions.Count);
37                                 
38                                 System.Collections.IEnumerator i = termPositions.GetEnumerator();
39                                 while (i.MoveNext())
40                                 {
41                                         TermPositions tp = (TermPositions) i.Current;
42                                         if (tp.Next())
43                                                 Put(tp);
44                                 }
45                         }
46                         
47                         internal TermPositions Peek()
48                         {
49                                 return (TermPositions) Top();
50                         }
51                         
52                         public override bool LessThan(System.Object a, System.Object b)
53                         {
54                                 return ((TermPositions) a).Doc() < ((TermPositions) b).Doc();
55                         }
56                 }
57                 
58                 private sealed class IntQueue
59                 {
60                         public IntQueue()
61                         {
62                                 InitBlock();
63                         }
64                         private void  InitBlock()
65                         {
66                                 _array = new int[_arraySize];
67                         }
68                         private int _arraySize = 16;
69                         private int _index = 0;
70                         private int _lastIndex = 0;
71                         private int[] _array;
72                         
73                         internal void  add(int i)
74                         {
75                                 if (_lastIndex == _arraySize)
76                                         growArray();
77                                 
78                                 _array[_lastIndex++] = i;
79                         }
80                         
81                         internal int next()
82                         {
83                                 return _array[_index++];
84                         }
85                         
86                         internal void  sort()
87                         {
88                                 System.Array.Sort(_array, _index, _lastIndex - _index);
89                         }
90                         
91                         internal void  clear()
92                         {
93                                 _index = 0;
94                                 _lastIndex = 0;
95                         }
96                         
97                         internal int size()
98                         {
99                                 return (_lastIndex - _index);
100                         }
101                         
102                         private void  growArray()
103                         {
104                                 int[] newArray = new int[_arraySize * 2];
105                                 Array.Copy(_array, 0, newArray, 0, _arraySize);
106                                 _array = newArray;
107                                 _arraySize *= 2;
108                         }
109                 }
110                 
111                 private int _doc;
112                 private int _freq;
113                 private TermPositionsQueue _termPositionsQueue;
114                 private IntQueue _posList;
115                 
116                 /// <summary> Creates a new <code>MultipleTermPositions</code> instance.
117                 /// 
118                 /// </summary>
119                 /// <exception cref="IOException">
120                 /// </exception>
121                 public MultipleTermPositions(IndexReader indexReader, Term[] terms)
122                 {
123                         System.Collections.IList termPositions = new System.Collections.ArrayList();
124                         
125                         for (int i = 0; i < terms.Length; i++)
126                                 termPositions.Add(indexReader.TermPositions(terms[i]));
127                         
128                         _termPositionsQueue = new TermPositionsQueue(termPositions);
129                         _posList = new IntQueue();
130                 }
131                 
132                 public bool Next()
133                 {
134                         if (_termPositionsQueue.Size() == 0)
135                                 return false;
136                         
137                         _posList.clear();
138                         _doc = _termPositionsQueue.Peek().Doc();
139                         
140                         TermPositions tp;
141                         do 
142                         {
143                                 tp = _termPositionsQueue.Peek();
144                                 
145                                 for (int i = 0; i < tp.Freq(); i++)
146                                         _posList.add(tp.NextPosition());
147                                 
148                                 if (tp.Next())
149                                         _termPositionsQueue.AdjustTop();
150                                 else
151                                 {
152                                         _termPositionsQueue.Pop();
153                                         tp.Close();
154                                 }
155                         }
156                         while (_termPositionsQueue.Size() > 0 && _termPositionsQueue.Peek().Doc() == _doc);
157                         
158                         _posList.sort();
159                         _freq = _posList.size();
160                         
161                         return true;
162                 }
163                 
164                 public int NextPosition()
165                 {
166                         return _posList.next();
167                 }
168                 
169                 public bool SkipTo(int target)
170                 {
171                         while (_termPositionsQueue.Peek() != null && target > _termPositionsQueue.Peek().Doc())
172                         {
173                                 TermPositions tp = (TermPositions) _termPositionsQueue.Pop();
174                                 if (tp.SkipTo(target))
175                                         _termPositionsQueue.Put(tp);
176                                 else
177                                         tp.Close();
178                         }
179                         return Next();
180                 }
181                 
182                 public int Doc()
183                 {
184                         return _doc;
185                 }
186                 
187                 public int Freq()
188                 {
189                         return _freq;
190                 }
191                 
192                 public void  Close()
193                 {
194                         while (_termPositionsQueue.Size() > 0)
195                                 ((TermPositions) _termPositionsQueue.Pop()).Close();
196                 }
197                 
198                 /// <summary> Not implemented.</summary>
199                 /// <throws>  UnsupportedOperationException </throws>
200                 public virtual void  Seek(Term arg0)
201                 {
202                         throw new System.NotSupportedException();
203                 }
204                 
205                 /// <summary> Not implemented.</summary>
206                 /// <throws>  UnsupportedOperationException </throws>
207                 public virtual void  Seek(TermEnum termEnum)
208                 {
209                         throw new System.NotSupportedException();
210                 }
211                 
212                 /// <summary> Not implemented.</summary>
213                 /// <throws>  UnsupportedOperationException </throws>
214                 public virtual int Read(int[] arg0, int[] arg1)
215                 {
216                         throw new System.NotSupportedException();
217                 }
218                 
219                 
220                 /// <summary> Not implemented.</summary>
221                 /// <throws>  UnsupportedOperationException </throws>
222                 public virtual int GetPayloadLength()
223                 {
224                         throw new System.NotSupportedException();
225                 }
226                 
227                 /// <summary> Not implemented.</summary>
228                 /// <throws>  UnsupportedOperationException </throws>
229                 public virtual byte[] GetPayload(byte[] data, int offset)
230                 {
231                         throw new System.NotSupportedException();
232                 }
233                 
234                 /// <summary> </summary>
235                 /// <returns> false
236                 /// </returns>
237                 // TODO: Remove warning after API has been finalized
238                 public virtual bool IsPayloadAvailable()
239                 {
240                         return false;
241                 }
242         }
243 }