/
RandomAccessMap.java
150 lines (118 loc) · 2.84 KB
/
RandomAccessMap.java
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
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.badlogic.gdx.utils.ObjectIntMap;
public class RandomAccessMap<K, V> implements Map<K, V>, RandomAccessWithKey<K, V> {
ArrayList<V> items = new ArrayList<V>();
ArrayList<K> keys = new ArrayList<K>();
ObjectIntMap<Object> positions = new ObjectIntMap<Object>();
private final int NOT_PRESENT = -1;
@Override
public int size() {
return items.size();
}
@Override
public boolean isEmpty() {
return items.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return positions.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return items.contains(value);
}
@Override
public V get(Object key) {
int position = positions.get(key, NOT_PRESENT);
if (position == NOT_PRESENT)
return null;
return items.get(position);
}
@Override
public V put(K key, V value) {
int position = positions.get(key, NOT_PRESENT);
if (position == NOT_PRESENT) {
items.add(value);
keys.add(key);
positions.put(key, items.size() - 1);
return null;
}
return items.set(position, value);
}
@Override
public V remove(Object key) {
int position = positions.get(key, NOT_PRESENT);
if (position == NOT_PRESENT)
return null;
int lastPosition = items.size() - 1;
V lastItem = items.get(lastPosition);
positions.remove(key, NOT_PRESENT);
V removedItem = items.remove(lastPosition);
K lastKey = keys.remove(lastPosition);
if (position != lastPosition) {
items.set(position, lastItem);
keys.set(position, lastKey);
positions.put(lastKey, position);
}
return removedItem;
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
for (java.util.Map.Entry<? extends K, ? extends V> entry : m.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
public V get(int index) {
return items.get(index);
}
@Override
public K getKey(int index) {
return keys.get(index);
}
@Override
public void clear() {
items.clear();
positions.clear();
keys.clear();
}
@Override
public Set<K> keySet() {
return new HashSet<K>(keys);
}
@Override
public Collection<V> values() {
return new ArrayList<V>(items);
}
@Override
public Set<java.util.Map.Entry<K, V>> entrySet() {
Set<java.util.Map.Entry<K, V>> result = new HashSet<Map.Entry<K, V>>();
for (int i = 0; i < size(); i++) {
result.add(new Entry(keys.get(i), items.get(i)));
}
return result;
}
class Entry implements Map.Entry<K, V> {
K key;
V value;
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
throw new UnsupportedOperationException("not implemented");
}
}
}