-
Notifications
You must be signed in to change notification settings - Fork 1
/
SparseCharArray.java
172 lines (144 loc) · 4.18 KB
/
SparseCharArray.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.nolanlawson.relatedness.util;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
/**
* Basic replacement for either a Map<Character,X>. Low-memory, because it uses
* arrays internally instead of hash maps.
*
* Assumes your key class can be cast to int - otherwise, it'll fail on runtime.
*
* @author nolan
*
* @param <T>
*/
public class SparseCharArray<T> extends AbstractMap<Character, T> {
private SparseCharArray() {
}
/**
* Creates a new SparseCharArray from the given map.
* @param inputMap
* @return
*/
@SuppressWarnings("unchecked")
public static <T> SparseCharArray<T> fromMap(Map<Character, T> inputMap) {
if (inputMap.isEmpty()) {
return (SparseCharArray<T>) EMPTY;
} else if (inputMap.size() == 1) {
return new SingletonSparseCharArray<T>(inputMap.keySet().iterator()
.next(), inputMap.values().iterator().next());
}
MultiSparseCharArray<T> result = new MultiSparseCharArray<T>();
result.startIdx = Ordering.natural().min(inputMap.keySet());
int maxIdx = Ordering.natural().max(inputMap.keySet());
result.data = new Object[(maxIdx - result.startIdx) + 1];
result.entries = new int[inputMap.size()];
int i = 0;
for (Entry<Character, T> entry : inputMap.entrySet()) {
int keyIdx = ((int) entry.getKey().charValue()) - result.startIdx;
result.data[keyIdx] = entry.getValue();
result.entries[i++] = keyIdx;
}
return result;
}
@Override
public Set<Entry<Character, T>> entrySet() {
throw new UnsupportedOperationException(); // implemented in subclasses
}
/**
* Unchanging empty sparse char array.
*/
@SuppressWarnings("rawtypes")
private static final MultiSparseCharArray<?> EMPTY = new MultiSparseCharArray();
static {
EMPTY.startIdx = 0;
EMPTY.data = new Object[0];
EMPTY.entries = new int[0];
}
/**
* SparseCharArray with >1 entries.
* @author nolan
*
* @param <T>
*/
private static class MultiSparseCharArray<T> extends SparseCharArray<T> {
private int startIdx;
private Object[] data;
private int[] entries;
@SuppressWarnings("unchecked")
@Override
public T get(Object key) {
int idx = ((int)(Character) key) - startIdx;
if (idx < 0 || idx >= data.length) {
return null;
}
return (T) data[idx];
}
@SuppressWarnings("unchecked")
@Override
public Set<Entry<Character, T>> entrySet() {
Set<Entry<Character, T>> result = Sets.newHashSet();
for (int i = 0; i < entries.length; i++) {
int entry = entries[i];
result.add(new SCAEntry<T>(Character
.valueOf(((char) (entry + startIdx))), (T) data[entry]));
}
return result;
}
}
/**
* SparseCharArray with only 1 entry. Incredibly, this takes up less space than Collections.singletonMap(),
* because singletonMap relies on an inner singletonSet.
* @author nolan
*
* @param <T>
*/
private static class SingletonSparseCharArray<T> extends SparseCharArray<T> {
private int singleKey;
private T singleValue;
SingletonSparseCharArray(int singleKey, T singleValue) {
this.singleKey = singleKey;
this.singleValue = singleValue;
}
@Override
public Set<Entry<Character, T>> entrySet() {
return Collections.<Entry<Character, T>> singleton(new SCAEntry<T>(
Character.valueOf((char) singleKey), singleValue));
}
@Override
public T get(Object key) {
if (key instanceof Character && ((Character) key).charValue() == singleKey) {
return singleValue;
}
return null;
}
}
/**
* Basic Map.Entry object, nothing fancy.
* @author nolan
*
* @param <T>
*/
private static class SCAEntry<T> implements Entry<Character, T> {
private Character key;
private T value;
private SCAEntry(Character key, T value) {
this.key = key;
this.value = value;
}
public Character getKey() {
return key;
}
public T getValue() {
return value;
}
public T setValue(T value) {
T old = this.value;
this.value = value;
return old;
}
}
}