pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / util / packed / Direct64.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 32 bit values to a backing array of ints.
28  * @lucene.internal
29  */
30
31 class Direct64 extends PackedInts.ReaderImpl
32         implements PackedInts.Mutable {
33   private long[] values;
34   private static final int BITS_PER_VALUE = 64;
35
36   public Direct64(int valueCount) {
37     super(valueCount, BITS_PER_VALUE);
38     values = new long[valueCount];
39   }
40
41   public Direct64(DataInput in, int valueCount) throws IOException {
42     super(valueCount, BITS_PER_VALUE);
43     long[] values = new long[valueCount];
44     for(int i=0;i<valueCount;i++) {
45       values[i] = in.readLong();
46     }
47
48     this.values = values;
49   }
50
51   /**
52    * Creates an array backed by the given values.
53    * </p><p>
54    * Note: The values are used directly, so changes to the given values will
55    * affect the structure.
56    * @param values   used as the internal backing array.
57    */
58   public Direct64(long[] values) {
59     super(values.length, BITS_PER_VALUE);
60     this.values = values;
61   }
62
63   public long get(final int index) {
64     return values[index];
65   }
66
67   public void set(final int index, final long value) {
68     values[index] = value;
69   }
70
71   public long ramBytesUsed() {
72     return RamUsageEstimator.NUM_BYTES_ARRAY_HEADER +
73             values.length * RamUsageEstimator.NUM_BYTES_LONG;
74   }
75
76   public void clear() {
77     Arrays.fill(values, 0L);
78   }
79
80   @Override
81   public long[] getArray() {
82     return values;
83   }
84
85   @Override
86   public boolean hasArray() {
87     return true;
88   }
89 }