01: /*
02: * Bytecode Analysis Framework
03: * Copyright (C) 2003,2004 University of Maryland
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: */
19:
20: package edu.umd.cs.findbugs.ba.vna;
21:
22: import java.util.ArrayList;
23: import java.util.Collections;
24:
25: /**
26: * Factory for ValueNumbers.
27: * A single Factory must be used to create all of the ValueNumbers
28: * for a method.
29: *
30: * @author David Hovemeyer
31: * @see ValueNumber
32: */
33: public class ValueNumberFactory {
34: /**
35: * Store all allocated value numbers.
36: */
37: private ArrayList<ValueNumber> allocatedValueList = new ArrayList<ValueNumber>();
38:
39: /**
40: * Create a fresh (unique) value number.
41: */
42: public ValueNumber createFreshValue() {
43: ValueNumber result = new ValueNumber(getNumValuesAllocated());
44: allocatedValueList.add(result);
45: return result;
46: }
47:
48: /**
49: * Return a previously allocated value.
50: */
51: public ValueNumber forNumber(int number) {
52: if (number >= getNumValuesAllocated())
53: throw new IllegalArgumentException("Value " + number
54: + " has not been allocated");
55: return allocatedValueList.get(number);
56: }
57:
58: /**
59: * Get the number of values which have been created.
60: */
61: public int getNumValuesAllocated() {
62: return allocatedValueList.size();
63: }
64:
65: /**
66: * Compact the value numbers produced by this factory.
67: *
68: * @param map array mapping old numbers to new numbers
69: * @param numValuesAllocated the number of values allocated in the new numbering
70: */
71: public void compact(int[] map, int numValuesAllocated) {
72: ArrayList<ValueNumber> oldList = this .allocatedValueList;
73: ArrayList<ValueNumber> newList = new ArrayList<ValueNumber>(
74: Collections.nCopies(numValuesAllocated,
75: (ValueNumber) null));
76:
77: for (ValueNumber value : oldList) {
78: int newNumber = map[value.getNumber()];
79: if (newNumber >= 0) {
80: // Note: because we are simply assigning new numbers to the
81: // old ValueNumber objects, their flags remain valid.
82: value.number = newNumber;
83: newList.set(newNumber, value);
84: }
85: }
86:
87: this .allocatedValueList = newList;
88: }
89:
90: }
91:
92: // vim:ts=4
|