1 package org.apache.lucene.util.packed;
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 org.apache.lucene.store.DataInput;
21 import org.apache.lucene.util.RamUsageEstimator;
23 import java.io.IOException;
24 import java.util.Arrays;
27 * Direct wrapping of 8 bit values to a backing array of bytes.
31 class Direct8 extends PackedInts.ReaderImpl
32 implements PackedInts.Mutable {
33 private byte[] values;
34 private static final int BITS_PER_VALUE = 8;
36 public Direct8(int valueCount) {
37 super(valueCount, BITS_PER_VALUE);
38 values = new byte[valueCount];
41 public Direct8(DataInput in, int valueCount)
43 super(valueCount, BITS_PER_VALUE);
44 byte[] values = new byte[valueCount];
45 for(int i=0;i<valueCount;i++) {
46 values[i] = in.readByte();
48 final int mod = valueCount % 8;
50 final int pad = 8-mod;
52 for(int i=0;i<pad;i++) {
61 * Creates an array backed by the given values.
63 * Note: The values are used directly, so changes to the given values will
64 * affect the structure.
65 * @param values used as the internal backing array.
67 public Direct8(byte[] values) {
68 super(values.length, BITS_PER_VALUE);
72 public long get(final int index) {
73 return 0xFFL & values[index];
76 public void set(final int index, final long value) {
77 values[index] = (byte)(value & 0xFF);
80 public long ramBytesUsed() {
81 return RamUsageEstimator.NUM_BYTES_ARRAY_HEADER + values.length;
85 Arrays.fill(values, (byte)0);
89 public Object getArray() {
94 public boolean hasArray() {