add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / util / fst / IntSequenceOutputs.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.IntsRef;
25
26 /**
27  * Output is a sequence of ints, for each input term.
28  *
29  * @lucene.experimental
30  */
31
32 public final class IntSequenceOutputs extends Outputs<IntsRef> {
33
34   private final static IntsRef NO_OUTPUT = new IntsRef();
35
36   private IntSequenceOutputs() {
37   }
38
39   public static IntSequenceOutputs getSingleton() {
40     return new IntSequenceOutputs();
41   }
42
43   @Override
44   public IntsRef common(IntsRef output1, IntsRef 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.ints[pos1] != output2.ints[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 IntsRef(output1.ints, output1.offset, pos1-output1.offset);
70     }
71   }
72
73   @Override
74   public IntsRef subtract(IntsRef output, IntsRef 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 IntsRef(output.ints, output.offset + inc.length, output.length-inc.length);
87     }
88   }
89
90   @Override
91   public IntsRef add(IntsRef prefix, IntsRef 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       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;
105       return result;
106     }
107   }
108
109   @Override
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]);
115     }
116   }
117
118   @Override
119   public IntsRef read(DataInput in) throws IOException {
120     final int len = in.readVInt();
121     if (len == 0) {
122       return NO_OUTPUT;
123     } else {
124       final IntsRef output = new IntsRef(len);
125       for(int idx=0;idx<len;idx++) {
126         output.ints[idx] = in.readVInt();
127       }
128       output.length = len;
129       return output;
130     }
131   }
132
133   @Override
134   public IntsRef getNoOutput() {
135     return NO_OUTPUT;
136   }
137
138   @Override
139   public String outputToString(IntsRef output) {
140     return output.toString();
141   }
142 }