1 package org.apache.lucene.util.fst;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
22 import org.apache.lucene.store.DataInput;
23 import org.apache.lucene.store.DataOutput;
24 import org.apache.lucene.util.IntsRef;
27 * Output is a sequence of ints, for each input term.
29 * @lucene.experimental
32 public final class IntSequenceOutputs extends Outputs<IntsRef> {
34 private final static IntsRef NO_OUTPUT = new IntsRef();
36 private IntSequenceOutputs() {
39 public static IntSequenceOutputs getSingleton() {
40 return new IntSequenceOutputs();
44 public IntsRef common(IntsRef output1, IntsRef output2) {
45 assert output1 != null;
46 assert output2 != null;
48 int pos1 = output1.offset;
49 int pos2 = output2.offset;
50 int stopAt1 = pos1 + Math.min(output1.length, output2.length);
51 while(pos1 < stopAt1) {
52 if (output1.ints[pos1] != output2.ints[pos2]) {
59 if (pos1 == output1.offset) {
62 } else if (pos1 == output1.offset + output1.length) {
63 // output1 is a prefix of output2
65 } else if (pos2 == output2.offset + output2.length) {
66 // output2 is a prefix of output1
69 return new IntsRef(output1.ints, output1.offset, pos1-output1.offset);
74 public IntsRef subtract(IntsRef output, IntsRef inc) {
75 assert output != null;
77 if (inc == NO_OUTPUT) {
80 } else if (inc.length == output.length) {
81 // entire output removed
84 assert inc.length < output.length: "inc.length=" + inc.length + " vs output.length=" + output.length;
85 assert inc.length > 0;
86 return new IntsRef(output.ints, output.offset + inc.length, output.length-inc.length);
91 public IntsRef add(IntsRef prefix, IntsRef output) {
92 assert prefix != null;
93 assert output != null;
94 if (prefix == NO_OUTPUT) {
96 } else if (output == NO_OUTPUT) {
99 assert prefix.length > 0;
100 assert output.length > 0;
101 IntsRef result = new IntsRef(prefix.length + output.length);
102 System.arraycopy(prefix.ints, prefix.offset, result.ints, 0, prefix.length);
103 System.arraycopy(output.ints, output.offset, result.ints, prefix.length, output.length);
104 result.length = prefix.length + output.length;
110 public void write(IntsRef prefix, DataOutput out) throws IOException {
111 assert prefix != null;
112 out.writeVInt(prefix.length);
113 for(int idx=0;idx<prefix.length;idx++) {
114 out.writeVInt(prefix.ints[prefix.offset+idx]);
119 public IntsRef read(DataInput in) throws IOException {
120 final int len = in.readVInt();
124 final IntsRef output = new IntsRef(len);
125 for(int idx=0;idx<len;idx++) {
126 output.ints[idx] = in.readVInt();
134 public IntsRef getNoOutput() {
139 public String outputToString(IntsRef output) {
140 return output.toString();