add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / util / fst / ByteSequenceOutputs.java
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 import org.apache.lucene.util.BytesRef;
25
26 /**
27  * Output is a sequence of bytes, for each input term.
28  *
29  * @lucene.experimental
30  */
31
32 public final class ByteSequenceOutputs extends Outputs<BytesRef> {
33
34   private final static BytesRef NO_OUTPUT = new BytesRef();
35
36   private ByteSequenceOutputs() {
37   }
38
39   public static ByteSequenceOutputs getSingleton() {
40     return new ByteSequenceOutputs();
41   }
42
43   @Override
44   public BytesRef common(BytesRef output1, BytesRef output2) {
45     assert output1 != null;
46     assert output2 != null;
47
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]) {
53         break;
54       }
55       pos1++;
56       pos2++;
57     }
58
59     if (pos1 == output1.offset) {
60       // no common prefix
61       return NO_OUTPUT;
62     } else if (pos1 == output1.offset + output1.length) {
63       // output1 is a prefix of output2
64       return output1;
65     } else if (pos2 == output2.offset + output2.length) {
66       // output2 is a prefix of output1
67       return output2;
68     } else {
69       return new BytesRef(output1.bytes, output1.offset, pos1-output1.offset);
70     }
71   }
72
73   @Override
74   public BytesRef subtract(BytesRef output, BytesRef inc) {
75     assert output != null;
76     assert inc != null;
77     if (inc == NO_OUTPUT) {
78       // no prefix removed
79       return output;
80     } else if (inc.length == output.length) {
81       // entire output removed
82       return NO_OUTPUT;
83     } else {
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);
87     }
88   }
89
90   @Override
91   public BytesRef add(BytesRef prefix, BytesRef output) {
92     assert prefix != null;
93     assert output != null;
94     if (prefix == NO_OUTPUT) {
95       return output;
96     } else if (output == NO_OUTPUT) {
97       return prefix;
98     } else {
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;
105       return result;
106     }
107   }
108
109   @Override
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);
114   }
115
116   @Override
117   public BytesRef read(DataInput in) throws IOException {
118     final int len = in.readVInt();
119     if (len == 0) {
120       return NO_OUTPUT;
121     } else {
122       final BytesRef output = new BytesRef(len);
123       in.readBytes(output.bytes, 0, len);
124       output.length = len;
125       return output;
126     }
127   }
128
129   @Override
130   public BytesRef getNoOutput() {
131     return NO_OUTPUT;
132   }
133
134   @Override
135   public String outputToString(BytesRef output) {
136     return output.utf8ToString();
137   }
138 }