1/*
2 * Javassist, a Java-bytecode translator toolkit.
3 * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License.  Alternatively, the contents of this file may be used under
8 * the terms of the GNU Lesser General Public License Version 2.1 or later.
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 */
15
16package javassist.bytecode;
17
18final class LongVector {
19    static final int ASIZE = 128;
20    static final int ABITS = 7;  // ASIZE = 2^ABITS
21    static final int VSIZE = 8;
22    private ConstInfo[][] objects;
23    private int elements;
24
25    public LongVector() {
26        objects = new ConstInfo[VSIZE][];
27        elements = 0;
28    }
29
30    public LongVector(int initialSize) {
31        int vsize = ((initialSize >> ABITS) & ~(VSIZE - 1)) + VSIZE;
32        objects = new ConstInfo[vsize][];
33        elements = 0;
34    }
35
36    public int size() { return elements; }
37
38    public int capacity() { return objects.length * ASIZE; }
39
40    public ConstInfo elementAt(int i) {
41        if (i < 0 || elements <= i)
42            return null;
43
44        return objects[i >> ABITS][i & (ASIZE - 1)];
45    }
46
47    public void addElement(ConstInfo value) {
48        int nth = elements >> ABITS;
49        int offset = elements & (ASIZE - 1);
50        int len = objects.length;
51        if (nth >= len) {
52            ConstInfo[][] newObj = new ConstInfo[len + VSIZE][];
53            System.arraycopy(objects, 0, newObj, 0, len);
54            objects = newObj;
55        }
56
57        if (objects[nth] == null)
58            objects[nth] = new ConstInfo[ASIZE];
59
60        objects[nth][offset] = value;
61        elements++;
62    }
63}
64