pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / util / packed / Direct16.java
1 package org.apache.lucene.util.packed;
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 org.apache.lucene.store.DataInput;
21 import org.apache.lucene.util.RamUsageEstimator;
22
23 import java.io.IOException;
24 import java.util.Arrays;
25
26 /**
27  * Direct wrapping of 16 bit values to a backing array of shorts.
28  * @lucene.internal
29  */
30
31 class Direct16 extends PackedInts.ReaderImpl
32         implements PackedInts.Mutable {
33   private short[] values;
34   private static final int BITS_PER_VALUE = 16;
35
36   public Direct16(int valueCount) {
37     super(valueCount, BITS_PER_VALUE);
38     values = new short[valueCount];
39   }
40
41   public Direct16(DataInput in, int valueCount) throws IOException {
42     super(valueCount, BITS_PER_VALUE);
43     short[] values = new short[valueCount];
44     for(int i=0;i<valueCount;i++) {
45       values[i] = in.readShort();
46     }
47     final int mod = valueCount % 4;
48     if (mod != 0) {
49       final int pad = 4-mod;
50       // round out long
51       for(int i=0;i<pad;i++) {
52         in.readShort();
53       }
54     }
55
56     this.values = values;
57   }
58
59   /**
60    * Creates an array backed by the given values.
61    * </p><p>
62    * Note: The values are used directly, so changes to the values will
63    * affect the structure.
64    * @param values   used as the internal backing array.
65    */
66   public Direct16(short[] values) {
67     super(values.length, BITS_PER_VALUE);
68     this.values = values;
69   }
70
71   public long get(final int index) {
72     return 0xFFFFL & values[index];
73   }
74
75   public void set(final int index, final long value) {
76     values[index] = (short)(value & 0xFFFF);
77   }
78
79   public long ramBytesUsed() {
80     return RamUsageEstimator.NUM_BYTES_ARRAY_HEADER +
81             values.length * RamUsageEstimator.NUM_BYTES_SHORT;
82   }
83
84   public void clear() {
85     Arrays.fill(values, (short)0);
86   }
87
88   @Override
89   public Object getArray() {
90     return values;
91   }
92
93   @Override
94   public boolean hasArray() {
95     return true;
96   }
97 }