Mercurial > hg > graal-jvmci-8
comparison graal/Compiler/src/com/sun/c1x/util/ArrayMap.java @ 2507:9ec15d6914ca
Pull over of compiler from maxine repository.
author | Thomas Wuerthinger <thomas@wuerthinger.net> |
---|---|
date | Wed, 27 Apr 2011 11:43:22 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2506:4a3bf8a5bf41 | 2507:9ec15d6914ca |
---|---|
1 /* | |
2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.sun.c1x.util; | |
24 | |
25 /** | |
26 * The {@code ArrayMap} class implements an efficient one-level map which is implemented | |
27 * as an array. Note that because of the one-level array inside, this data structure performs best | |
28 * when the range of integer keys is small and densely used. Note that the implementation can | |
29 * handle arbitrary intervals, including negative numbers, up to intervals of size 2^31 - 1. | |
30 * | |
31 * @author Ben L. Titzer | |
32 */ | |
33 public class ArrayMap<T> { | |
34 | |
35 private static final int INITIAL_SIZE = 5; // how big the initial array should be | |
36 private static final int EXTRA = 2; // how far on the left or right of a new element to grow | |
37 | |
38 Object[] map; | |
39 int low; | |
40 | |
41 /** | |
42 * Constructs a new {@code ArrayMap} with no initial assumptions. | |
43 */ | |
44 public ArrayMap() { | |
45 } | |
46 | |
47 /** | |
48 * Constructs a new {@code ArrayMap} that initially covers the specified interval. | |
49 * Note that this map will automatically expand if necessary later. | |
50 * @param low the low index, inclusive | |
51 * @param high the high index, exclusive | |
52 */ | |
53 public ArrayMap(int low, int high) { | |
54 this.low = low; | |
55 this.map = new Object[high - low + 1]; | |
56 } | |
57 | |
58 /** | |
59 * Puts a new value in the map at the specified index. | |
60 * @param i the index at which to store the value | |
61 * @param value the value to store at the specified index | |
62 */ | |
63 public void put(int i, T value) { | |
64 int index = i - low; | |
65 if (map == null) { | |
66 // no map yet | |
67 map = new Object[INITIAL_SIZE]; | |
68 low = index - 2; | |
69 map[INITIAL_SIZE / 2] = value; | |
70 } else if (index < 0) { | |
71 // grow backwards | |
72 growBackward(i, value); | |
73 } else if (index >= map.length) { | |
74 // grow forwards | |
75 growForward(i, value); | |
76 } else { | |
77 // no growth necessary | |
78 map[index] = value; | |
79 } | |
80 } | |
81 | |
82 /** | |
83 * Gets the value at the specified index in the map. | |
84 * @param i the index | |
85 * @return the value at the specified index; {@code null} if there is no value at the specified index, | |
86 * or if the index is out of the currently stored range | |
87 */ | |
88 public T get(int i) { | |
89 int index = i - low; | |
90 if (map == null || index < 0 || index >= map.length) { | |
91 return null; | |
92 } | |
93 Class<T> type = null; | |
94 return Util.uncheckedCast(type, map[index]); | |
95 } | |
96 | |
97 public int length() { | |
98 return map.length; | |
99 } | |
100 | |
101 private void growBackward(int i, T value) { | |
102 int nlow = i - EXTRA; | |
103 Object[] nmap = new Object[low - nlow + map.length]; | |
104 System.arraycopy(map, 0, nmap, low - nlow, map.length); | |
105 map = nmap; | |
106 low = nlow; | |
107 map[i - low] = value; | |
108 } | |
109 | |
110 private void growForward(int i, T value) { | |
111 int nlen = i - low + 1 + EXTRA; | |
112 Object[] nmap = new Object[nlen]; | |
113 System.arraycopy(map, 0, nmap, 0, map.length); | |
114 map = nmap; | |
115 map[i - low] = value; | |
116 } | |
117 } |