01: /* Soot - a J*va Optimization Framework
02: * Copyright (C) 2002 Ondrej Lhotak
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the
16: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17: * Boston, MA 02111-1307, USA.
18: */
19:
20: package soot.util;
21:
22: import java.util.*;
23:
24: /** A class that numbers objects, so they can be placed in bitsets.
25: *
26: * @author Ondrej Lhotak
27: */
28:
29: public class ArrayNumberer implements IterableNumberer {
30: Numberable[] numberToObj = new Numberable[1024];
31: int lastNumber = 0;
32:
33: public void add(Object oo) {
34: Numberable o = (Numberable) oo;
35: if (o.getNumber() != 0)
36: return;
37: ++lastNumber;
38: if (lastNumber >= numberToObj.length) {
39: Numberable[] newnto = new Numberable[numberToObj.length * 2];
40: System.arraycopy(numberToObj, 0, newnto, 0,
41: numberToObj.length);
42: numberToObj = newnto;
43: }
44: numberToObj[lastNumber] = o;
45: o.setNumber(lastNumber);
46: }
47:
48: public long get(Object oo) {
49: if (oo == null)
50: return 0;
51: Numberable o = (Numberable) oo;
52: int ret = o.getNumber();
53: if (ret == 0)
54: throw new RuntimeException("unnumbered: " + o);
55: return ret;
56: }
57:
58: public Object get(long number) {
59: if (number == 0)
60: return null;
61: Object ret = numberToObj[(int) number];
62: if (ret == null)
63: throw new RuntimeException("no object with number "
64: + number);
65: return ret;
66: }
67:
68: public int size() {
69: return lastNumber;
70: }
71:
72: public Iterator iterator() {
73: return new NumbererIterator();
74: }
75:
76: final class NumbererIterator implements Iterator {
77: int cur = 1;
78:
79: public final boolean hasNext() {
80: return cur < numberToObj.length && numberToObj[cur] != null;
81: }
82:
83: public final Object next() {
84: if (!hasNext())
85: throw new NoSuchElementException();
86: return numberToObj[cur++];
87: }
88:
89: public final void remove() {
90: throw new UnsupportedOperationException();
91: }
92: }
93: }
|