forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSmallNumberedMap.java
More file actions
179 lines (159 loc) · 4.15 KB
/
SmallNumberedMap.java
File metadata and controls
179 lines (159 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package soot.util;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2002 Ondrej Lhotak
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import java.lang.reflect.Array;
import java.util.Iterator;
/**
* A java.util.Map with Numberable objects as the keys.
*
* @author Ondrej Lhotak
*/
public final class SmallNumberedMap<K extends Numberable, V> implements INumberedMap<K, V> {
private K[] array = newArray(Numberable.class, 8);
private V[] values = newArray(Object.class, 8);
private int size = 0;
public SmallNumberedMap() {
}
@SuppressWarnings("unchecked")
private static <T> T[] newArray(Class<? super T> componentType, int length) {
return (T[]) Array.newInstance(componentType, length);
}
@Override
public boolean put(K key, V value) {
int pos = findPosition(key);
if (array[pos] == key) {
if (values[pos] == value) {
return false;
}
values[pos] = value;
return true;
}
size++;
if (size * 3 > array.length * 2) {
doubleSize();
pos = findPosition(key);
}
array[pos] = key;
values[pos] = value;
return true;
}
@Override
public V get(K key) {
return values[findPosition(key)];
}
@Override
public void remove(K key) {
int pos = findPosition(key);
if (array[pos] == key) {
array[pos] = null;
values[pos] = null;
size--;
}
}
/**
* Returns the number of non-null values in this map.
*/
public int nonNullSize() {
int ret = 0;
for (V element : values) {
if (element != null) {
ret++;
}
}
return ret;
}
@Override
public Iterator<K> keyIterator() {
return new SmallNumberedMapIterator<K>(array);
}
/**
* Returns an iterator over the non-null values.
*/
public Iterator<V> iterator() {
return new SmallNumberedMapIterator<V>(values);
}
private class SmallNumberedMapIterator<C> implements Iterator<C> {
private final C[] data;
private int cur;
SmallNumberedMapIterator(C[] data) {
this.data = data;
this.cur = 0;
seekNext();
}
protected final void seekNext() {
V[] temp = SmallNumberedMap.this.values;
try {
while (temp[cur] == null) {
cur++;
}
} catch (ArrayIndexOutOfBoundsException e) {
cur = -1;
}
}
@Override
public final void remove() {
SmallNumberedMap.this.array[cur - 1] = null;
SmallNumberedMap.this.values[cur - 1] = null;
}
@Override
public final boolean hasNext() {
return cur != -1;
}
@Override
public final C next() {
C ret = data[cur];
cur++;
seekNext();
return ret;
}
}
private int findPosition(K o) {
int number = o.getNumber();
if (number == 0) {
throw new RuntimeException("unnumbered");
}
number = number & (array.length - 1);
while (true) {
K key = array[number];
if (key == o || key == null) {
return number;
}
number = (number + 1) & (array.length - 1);
}
}
private void doubleSize() {
K[] oldArray = array;
V[] oldValues = values;
final int oldLength = oldArray.length;
final int newLength = oldLength * 2;
array = newArray(Numberable.class, newLength);
values = newArray(Object.class, newLength);
for (int i = 0; i < oldLength; i++) {
K element = oldArray[i];
if (element != null) {
int pos = findPosition(element);
array[pos] = element;
values[pos] = oldValues[i];
}
}
}
}