KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > LargeNumberedMap


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 Ondrej Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * 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 import java.util.*;
22
23 /** A java.util.Map-like map with Numberable objects as the keys.
24  * This one is designed for maps close to the size of the universe.
25  * For smaller maps, use SmallNumberedMap.
26  *
27  * @author Ondrej Lhotak
28  */

29
30 public final class LargeNumberedMap {
31     public LargeNumberedMap( ArrayNumberer universe ) {
32         this.universe = universe;
33         int newsize = universe.size();
34         if( newsize < 8 ) newsize = 8;
35         values = new Object JavaDoc[newsize];
36     }
37     public boolean put( Numberable key, Object JavaDoc value ) {
38         int number = key.getNumber();
39         if( number == 0 ) throw new RuntimeException JavaDoc( "oops, forgot to initialize" );
40         if( number >= values.length ) {
41             Object JavaDoc[] oldValues = values;
42             values = new Object JavaDoc[ universe.size()*2+5 ];
43             System.arraycopy(oldValues,0,values,0,oldValues.length);
44         }
45         boolean ret = ( values[number] != value );
46         values[number] = value;
47         return ret;
48     }
49     public Object JavaDoc get( Numberable key ) {
50         int i = key.getNumber();
51         if( i >= values.length ) return null;
52         return values[ i ];
53     }
54
55     /* Private stuff. */
56
57     private Object JavaDoc[] values;
58     private long[] bits;
59     private int size = 0;
60     private ArrayNumberer universe;
61 }
62
Popular Tags