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
BaseCharFilter.cs
Go to the documentation of this file.
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 using Lucene.Net.Support;
20 using Lucene.Net.Util;
21 
22 namespace Lucene.Net.Analysis
23 {
24 
25  /// <summary>
26  /// * Base utility class for implementing a <see cref="CharFilter" />.
27  /// * You subclass this, and then record mappings by calling
28  /// * <see cref="AddOffCorrectMap" />, and then invoke the correct
29  /// * method to correct an offset.
30  /// </summary>
31  public abstract class BaseCharFilter : CharFilter
32  {
33 
34  private int[] offsets;
35  private int[] diffs;
36  private int size = 0;
37 
38  protected BaseCharFilter(CharStream @in) : base(@in)
39  {
40  }
41 
42  /* Retrieve the corrected offset. */
43  //@Override
44  protected internal override int Correct(int currentOff)
45  {
46  if (offsets == null || currentOff < offsets[0])
47  {
48  return currentOff;
49  }
50 
51  int hi = size - 1;
52  if (currentOff >= offsets[hi])
53  return currentOff + diffs[hi];
54 
55  int lo = 0;
56  int mid = -1;
57 
58  while (hi >= lo)
59  {
60  mid = Number.URShift(lo + hi, 1);
61  if (currentOff < offsets[mid])
62  hi = mid - 1;
63  else if (currentOff > offsets[mid])
64  lo = mid + 1;
65  else
66  return currentOff + diffs[mid];
67  }
68 
69  if (currentOff < offsets[mid])
70  return mid == 0 ? currentOff : currentOff + diffs[mid - 1];
71  return currentOff + diffs[mid];
72  }
73 
74  protected int LastCumulativeDiff
75  {
76  get
77  {
78  return offsets == null ? 0 : diffs[size - 1];
79  }
80  }
81 
82  [Obsolete("Use LastCumulativeDiff property instead")]
83  protected int GetLastCumulativeDiff()
84  {
85  return LastCumulativeDiff;
86  }
87 
88  protected void AddOffCorrectMap(int off, int cumulativeDiff)
89  {
90  if (offsets == null)
91  {
92  offsets = new int[64];
93  diffs = new int[64];
94  }
95  else if (size == offsets.Length)
96  {
97  offsets = ArrayUtil.Grow(offsets);
98  diffs = ArrayUtil.Grow(diffs);
99  }
100 
101  offsets[size] = off;
102  diffs[size++] = cumulativeDiff;
103  }
104  }
105 }