Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
MemoryTermPositionVector.cs
Go to the documentation of this file.
1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Text;
26 
27 namespace Lucene.Net.Index.Memory
28 {
29  public partial class MemoryIndex
30  {
31  private sealed partial class MemoryIndexReader
32  {
33  private class MemoryTermPositionVector : TermPositionVector
34  {
35  private readonly MemoryIndex _index;
36  private readonly string _fieldName;
37  private readonly KeyValuePair<String,ArrayIntList>[] sortedTerms;
38 
39  public MemoryTermPositionVector(MemoryIndex index, Info info, string fieldName)
40  {
41  _index = index;
42  _fieldName = fieldName;
43  sortedTerms = info.SortedTerms;
44  }
45 
46  public string Field
47  {
48  get { return _fieldName; }
49  }
50 
51  public int Size
52  {
53  get { return sortedTerms.Length; }
54  }
55 
56  public string[] GetTerms()
57  {
58  var terms = new String[sortedTerms.Length];
59  for (int i = sortedTerms.Length; --i >= 0; )
60  {
61  terms[i] = sortedTerms[i].Key;
62  }
63  return terms;
64  }
65 
66  public int[] GetTermFrequencies()
67  {
68  int[] freqs = new int[sortedTerms.Length];
69  for (int i = sortedTerms.Length; --i >= 0; )
70  {
71  freqs[i] = _index.NumPositions(sortedTerms[i].Value);
72  }
73  return freqs;
74  }
75 
76  public int IndexOf(string term)
77  {
78  int i = Array.BinarySearch(sortedTerms, new KeyValuePair<string, ArrayIntList>(term, null), Info.ArrayIntListComparer);
79  return i >= 0 ? i : -1;
80  }
81 
82  public int[] IndexesOf(string[] terms, int start, int len)
83  {
84  int[] indexes = new int[len];
85  for (int i = 0; i < len; i++)
86  {
87  indexes[i] = IndexOf(terms[start++]);
88  }
89  return indexes;
90  }
91 
92  public int[] GetTermPositions(int index)
93  {
94  return sortedTerms[index].Value.ToArray(_index.stride);
95  }
96 
97  public TermVectorOffsetInfo[] GetOffsets(int index)
98  {
99  if (_index.stride == 1) return null; // no offsets stored
100 
101  ArrayIntList positions = sortedTerms[index].Value;
102  int size = positions.Size();
103  TermVectorOffsetInfo[] offsets = new TermVectorOffsetInfo[size / _index.stride];
104 
105  for (int i = 0, j = 1; j < size; i++, j += _index.stride)
106  {
107  int start = positions.Get(j);
108  int end = positions.Get(j + 1);
109  offsets[i] = new TermVectorOffsetInfo(start, end);
110  }
111  return offsets;
112  }
113  }
114  }
115  }
116 }