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.
21 * Implements {@link PackedInts.Mutable}, but grows the
22 * bit count of the underlying packed ints on-demand.
24 * <p>@lucene.internal</p>
27 public class GrowableWriter implements PackedInts.Mutable {
29 private long currentMaxValue;
30 private PackedInts.Mutable current;
31 private final boolean roundFixedSize;
33 public GrowableWriter(int startBitsPerValue, int valueCount, boolean roundFixedSize) {
34 this.roundFixedSize = roundFixedSize;
35 current = PackedInts.getMutable(valueCount, getSize(startBitsPerValue));
36 currentMaxValue = PackedInts.maxValue(current.getBitsPerValue());
39 private final int getSize(int bpv) {
41 return PackedInts.getNextFixedSize(bpv);
47 public long get(int index) {
48 return current.get(index);
52 return current.size();
55 public int getBitsPerValue() {
56 return current.getBitsPerValue();
59 public PackedInts.Mutable getMutable() {
64 public Object getArray() {
65 return current.getArray();
69 public boolean hasArray() {
70 return current.hasArray();
73 public void set(int index, long value) {
74 if (value >= currentMaxValue) {
75 int bpv = getBitsPerValue();
76 while(currentMaxValue <= value && currentMaxValue != Long.MAX_VALUE) {
80 final int valueCount = size();
81 PackedInts.Mutable next = PackedInts.getMutable(valueCount, getSize(bpv));
82 for(int i=0;i<valueCount;i++) {
83 next.set(i, current.get(i));
86 currentMaxValue = PackedInts.maxValue(current.getBitsPerValue());
88 current.set(index, value);
95 public GrowableWriter resize(int newSize) {
96 GrowableWriter next = new GrowableWriter(getBitsPerValue(), newSize, roundFixedSize);
97 final int limit = Math.min(size(), newSize);
98 for(int i=0;i<limit;i++) {