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.BytesRef;
27 * Output is a sequence of bytes, for each input term.
29 * @lucene.experimental
32 public final class ByteSequenceOutputs extends Outputs<BytesRef> {
34 private final static BytesRef NO_OUTPUT = new BytesRef();
36 private ByteSequenceOutputs() {
39 public static ByteSequenceOutputs getSingleton() {
40 return new ByteSequenceOutputs();
44 public BytesRef common(BytesRef output1, BytesRef 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.bytes[pos1] != output2.bytes[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 BytesRef(output1.bytes, output1.offset, pos1-output1.offset);
74 public BytesRef subtract(BytesRef output, BytesRef 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 BytesRef(output.bytes, output.offset + inc.length, output.length-inc.length);
91 public BytesRef add(BytesRef prefix, BytesRef 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 BytesRef result = new BytesRef(prefix.length + output.length);
102 System.arraycopy(prefix.bytes, prefix.offset, result.bytes, 0, prefix.length);
103 System.arraycopy(output.bytes, output.offset, result.bytes, prefix.length, output.length);
104 result.length = prefix.length + output.length;
110 public void write(BytesRef prefix, DataOutput out) throws IOException {
111 assert prefix != null;
112 out.writeVInt(prefix.length);
113 out.writeBytes(prefix.bytes, prefix.offset, prefix.length);
117 public BytesRef read(DataInput in) throws IOException {
118 final int len = in.readVInt();
122 final BytesRef output = new BytesRef(len);
123 in.readBytes(output.bytes, 0, len);
130 public BytesRef getNoOutput() {
135 public String outputToString(BytesRef output) {
136 return output.utf8ToString();