pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / util / packed / GrowableWriter.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 /**     
21  * Implements {@link PackedInts.Mutable}, but grows the
22  * bit count of the underlying packed ints on-demand.
23  *
24  * <p>@lucene.internal</p>
25  */
26
27 public class GrowableWriter implements PackedInts.Mutable {
28
29   private long currentMaxValue;
30   private PackedInts.Mutable current;
31   private final boolean roundFixedSize;
32
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());
37   }
38
39   private final int getSize(int bpv) {
40     if (roundFixedSize) {
41       return PackedInts.getNextFixedSize(bpv);
42     } else {
43       return bpv;
44     }
45   }
46
47   public long get(int index) {
48     return current.get(index);
49   }
50
51   public int size() {
52     return current.size();
53   }
54
55   public int getBitsPerValue() {
56     return current.getBitsPerValue();
57   }
58
59   public PackedInts.Mutable getMutable() {
60     return current;
61   }
62
63   // @Override
64   public Object getArray() {
65     return current.getArray();
66   }
67
68   // @Override
69   public boolean hasArray() {
70     return current.hasArray();
71   }
72
73   public void set(int index, long value) {
74     if (value >= currentMaxValue) {
75       int bpv = getBitsPerValue();
76       while(currentMaxValue <= value && currentMaxValue != Long.MAX_VALUE) {
77         bpv++;
78         currentMaxValue *= 2;
79       }
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));
84       }
85       current = next;
86       currentMaxValue = PackedInts.maxValue(current.getBitsPerValue());
87     }
88     current.set(index, value);
89   }
90
91   public void clear() {
92     current.clear();
93   }
94
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++) {
99       next.set(i, get(i));
100     }
101     return next;
102   }
103 }