~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/src/java/org/apache/lucene/util/fst/UpToTwoPositiveIntOutputs.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.util.fst;
2
 
 
3
 
/**
4
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
5
 
 * contributor license agreements.  See the NOTICE file distributed with
6
 
 * this work for additional information regarding copyright ownership.
7
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
8
 
 * (the "License"); you may not use this file except in compliance with
9
 
 * 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, software
14
 
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 
 * See the License for the specific language governing permissions and
17
 
 * limitations under the License.
18
 
 */
19
 
 
20
 
import java.io.IOException;
21
 
 
22
 
import org.apache.lucene.store.DataInput;
23
 
import org.apache.lucene.store.DataOutput;
24
 
 
25
 
/**
26
 
 * Holds one or two longs for each input term.  If it's a
27
 
 * single output, Long is returned; else, TwoLongs.  Order
28
 
 * is preseved in the TwoLongs case, ie .first is the first
29
 
 * input/output added to Builder, and .second is the
30
 
 * second.  You cannot store 0 output with this (that's
31
 
 * reserved to mean "no output")!
32
 
 *
33
 
 * NOTE: the resulting FST is not guaranteed to be minimal!
34
 
 * See {@link Builder}.
35
 
 *
36
 
 * @lucene.experimental
37
 
 */
38
 
 
39
 
public final class UpToTwoPositiveIntOutputs extends Outputs<Object> {
40
 
 
41
 
  public final static class TwoLongs {
42
 
    final long first;
43
 
    final long second;
44
 
 
45
 
    public TwoLongs(long first, long second) {
46
 
      this.first = first;
47
 
      this.second = second;
48
 
      assert first >= 0;
49
 
      assert second >= 0;
50
 
    }
51
 
 
52
 
    @Override
53
 
    public String toString() {
54
 
      return "TwoLongs:" + first + "," + second;
55
 
    }
56
 
 
57
 
    @Override
58
 
    public boolean equals(Object _other) {
59
 
      if (_other instanceof TwoLongs) {
60
 
        final TwoLongs other = (TwoLongs) _other;
61
 
        return first == other.first && second == other.second;
62
 
      } else {
63
 
        return false;
64
 
      }
65
 
    }
66
 
 
67
 
    @Override
68
 
    public int hashCode() {
69
 
      return (int) ((first^(first>>>32)) ^ (second^(second>>32)));
70
 
    }
71
 
  }
72
 
  
73
 
  private final static Long NO_OUTPUT = new Long(0);
74
 
 
75
 
  private final boolean doShare;
76
 
 
77
 
  private final static UpToTwoPositiveIntOutputs singletonShare = new UpToTwoPositiveIntOutputs(true);
78
 
  private final static UpToTwoPositiveIntOutputs singletonNoShare = new UpToTwoPositiveIntOutputs(false);
79
 
 
80
 
  private UpToTwoPositiveIntOutputs(boolean doShare) {
81
 
    this.doShare = doShare;
82
 
  }
83
 
 
84
 
  public static UpToTwoPositiveIntOutputs getSingleton(boolean doShare) {
85
 
    return doShare ? singletonShare : singletonNoShare;
86
 
  }
87
 
 
88
 
  public Long get(long v) {
89
 
    if (v == 0) {
90
 
      return NO_OUTPUT;
91
 
    } else {
92
 
      return Long.valueOf(v);
93
 
    }
94
 
  }
95
 
 
96
 
  public TwoLongs get(long first, long second) {
97
 
    return new TwoLongs(first, second);
98
 
  }
99
 
 
100
 
  @Override
101
 
  public Long common(Object _output1, Object _output2) {
102
 
    assert valid(_output1, false);
103
 
    assert valid(_output2, false);
104
 
    final Long output1 = (Long) _output1;
105
 
    final Long output2 = (Long) _output2;
106
 
    if (output1 == NO_OUTPUT || output2 == NO_OUTPUT) {
107
 
      return NO_OUTPUT;
108
 
    } else if (doShare) {
109
 
      assert output1 > 0;
110
 
      assert output2 > 0;
111
 
      return Math.min(output1, output2);
112
 
    } else if (output1.equals(output2)) {
113
 
      return output1;
114
 
    } else {
115
 
      return NO_OUTPUT;
116
 
    }
117
 
  }
118
 
 
119
 
  @Override
120
 
  public Long subtract(Object _output, Object _inc) {
121
 
    assert valid(_output, false);
122
 
    assert valid(_inc, false);
123
 
    final Long output = (Long) _output;
124
 
    final Long inc = (Long) _inc;
125
 
    assert output >= inc;
126
 
 
127
 
    if (inc == NO_OUTPUT) {
128
 
      return output;
129
 
    } else if (output.equals(inc)) {
130
 
      return NO_OUTPUT;
131
 
    } else {
132
 
      return output - inc;
133
 
    }
134
 
  }
135
 
 
136
 
  @Override
137
 
  public Object add(Object _prefix, Object _output) {
138
 
    assert valid(_prefix, false);
139
 
    assert valid(_output, true);
140
 
    final Long prefix = (Long) _prefix;
141
 
    if (_output instanceof Long) {
142
 
      final Long output = (Long) _output;
143
 
      if (prefix == NO_OUTPUT) {
144
 
        return output;
145
 
      } else if (output == NO_OUTPUT) {
146
 
        return prefix;
147
 
      } else {
148
 
        return prefix + output;
149
 
      }
150
 
    } else {
151
 
      final TwoLongs output = (TwoLongs) _output;
152
 
      final long v = prefix;
153
 
      return new TwoLongs(output.first + v, output.second + v);
154
 
    }
155
 
  }
156
 
 
157
 
  @Override
158
 
  public void write(Object _output, DataOutput out) throws IOException {
159
 
    assert valid(_output, true);
160
 
    if (_output instanceof Long) {
161
 
      final Long output = (Long) _output;
162
 
      out.writeVLong(output<<1);
163
 
    } else {
164
 
      final TwoLongs output = (TwoLongs) _output;
165
 
      out.writeVLong((output.first<<1) | 1);
166
 
      out.writeVLong(output.second);
167
 
    }
168
 
  }
169
 
 
170
 
  @Override
171
 
  public Object read(DataInput in) throws IOException {
172
 
    final long code = in.readVLong();
173
 
    if ((code & 1) == 0) {
174
 
      // single long
175
 
      final long v = code >>> 1;
176
 
      if (v == 0) {
177
 
        return NO_OUTPUT;
178
 
      } else {
179
 
        return Long.valueOf(v);
180
 
      }
181
 
    } else {
182
 
      // two longs
183
 
      final long first = code >>> 1;
184
 
      final long second = in.readVLong();
185
 
      return new TwoLongs(first, second);
186
 
    }
187
 
  }
188
 
 
189
 
  private boolean valid(Long o) {
190
 
    assert o != null;
191
 
    assert o instanceof Long;
192
 
    assert o == NO_OUTPUT || o > 0;
193
 
    return true;
194
 
  }
195
 
 
196
 
  // Used only by assert
197
 
  private boolean valid(Object _o, boolean allowDouble) {
198
 
    if (!allowDouble) {
199
 
      assert _o instanceof Long;
200
 
      return valid((Long) _o);
201
 
    } else if (_o instanceof TwoLongs) {
202
 
      return true;
203
 
    } else {
204
 
      return valid((Long) _o);
205
 
    }
206
 
  }
207
 
 
208
 
  @Override
209
 
  public Object getNoOutput() {
210
 
    return NO_OUTPUT;
211
 
  }
212
 
 
213
 
  @Override
214
 
  public String outputToString(Object output) {
215
 
    return output.toString();
216
 
  }
217
 
 
218
 
  @Override
219
 
  public Object merge(Object first, Object second) {
220
 
    assert valid(first, false);
221
 
    assert valid(second, false);
222
 
    return new TwoLongs((Long) first, (Long) second);
223
 
  }
224
 
}