| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.util.SparseArray; |
| 22 |
import android.util.SparseIntArray; |
| 23 |
|
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.Collections; |
| 26 |
import java.util.List; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public interface HeavyHitterSketch<T> { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static <V> @NonNull HeavyHitterSketch<V> newDefault() { |
| 45 |
return new HeavyHitterSketchImpl<V>(); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
void setConfig(int inputSize, int capacity); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
void add(@Nullable T newInstance); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
@Nullable |
| 73 |
List<T> getTopHeavyHitters(int k, @Nullable List<T> holder, @Nullable List<Float> freqs); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
@Nullable |
| 81 |
List<T> getCandidates(@Nullable List<T> holder); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
void reset(); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
float getRequiredValidationInputRatio(); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
he |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
es |
| 107 |
|
| 108 |
|
| 109 |
final class HeavyHitterSketchImpl<T> implements HeavyHitterSketch<T> { |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
private final SparseArray<T> mObjects = new SparseArray<>(); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
private final SparseIntArray mFrequencies = new SparseIntArray(); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private int mPassSize; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
private int mTotalSize; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
private int mCapacity; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
private int mNumInputs; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
private boolean mConfigured; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public void setConfig(final int inputSize, final int capacity) { |
| 153 |
if (inputSize < capacity || inputSize <= 1) { |
| 154 |
mConfigured = false; |
| 155 |
throw new IllegalArgumentException(); |
| 156 |
} |
| 157 |
reset(); |
| 158 |
mTotalSize = inputSize; |
| 159 |
mPassSize = inputSize >> 1; |
| 160 |
mCapacity = capacity; |
| 161 |
mConfigured = true; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
@Override |
| 170 |
public void add(@Nullable final T newInstance) { |
| 171 |
if (!mConfigured) { |
| 172 |
throw new IllegalStateException(); |
| 173 |
} |
| 174 |
if (mNumInputs < mPassSize) { |
| 175 |
addToMGSummary(newInstance); |
| 176 |
} else if (mNumInputs < mTotalSize) { |
| 177 |
|
| 178 |
validate(newInstance); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
private void addToMGSummary(@Nullable final T newInstance) { |
| 192 |
final int hashCode = newInstance != null ? newInstance.hashCode() : 0; |
| 193 |
final int index = mObjects.indexOfKey(hashCode); |
| 194 |
|
| 195 |
if (index >= 0) { |
| 196 |
mFrequencies.setValueAt(index, mFrequencies.valueAt(index) + 1); |
| 197 |
} else if (mObjects.size() < mCapacity - 1) { |
| 198 |
mObjects.put(hashCode, newInstance); |
| 199 |
mFrequencies.put(hashCode, 1); |
| 200 |
} else { |
| 201 |
for (int i = mFrequencies.size() - 1; i >= 0; i--) { |
| 202 |
final int val = mFrequencies.valueAt(i) - 1; |
| 203 |
if (val == 0) { |
| 204 |
mObjects.removeAt(i); |
| 205 |
mFrequencies.removeAt(i); |
| 206 |
} else { |
| 207 |
mFrequencies.setValueAt(i, val); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
if (++mNumInputs == mPassSize) { |
| 212 |
|
| 213 |
for (int i = mFrequencies.size() - 1; i >= 0; i--) { |
| 214 |
mFrequencies.setValueAt(i, 0); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
private void validate(@Nullable final T newInstance) { |
| 224 |
final int hashCode = newInstance != null ? newInstance.hashCode() : 0; |
| 225 |
final int index = mObjects.indexOfKey(hashCode); |
| 226 |
if (index >= 0) { |
| 227 |
mFrequencies.setValueAt(index, mFrequencies.valueAt(index) + 1); |
| 228 |
} |
| 229 |
if (++mNumInputs == mTotalSize) { |
| 230 |
final int lower = mPassSize / mCapacity; |
| 231 |
|
| 232 |
for (int i = mFrequencies.size() - 1; i >= 0; i--) { |
| 233 |
final int val = mFrequencies.valueAt(i); |
| 234 |
if (val < lower) { |
| 235 |
mFrequencies.removeAt(i); |
| 236 |
mObjects.removeAt(i); |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
@Override |
| 251 |
@Nullable |
| 252 |
public List<T> getTopHeavyHitters(final int k, final @Nullable List<T> holder, |
| 253 |
final @Nullable List<Float> freqs) { |
| 254 |
if (!mConfigured) { |
| 255 |
throw new IllegalStateException(); |
| 256 |
} |
| 257 |
|
| 258 |
if (k >= mCapacity) { |
| 259 |
throw new IllegalArgumentException(); |
| 260 |
} |
| 261 |
|
| 262 |
if (mNumInputs < mTotalSize) { |
| 263 |
|
| 264 |
throw new IllegalStateException(); |
| 265 |
} |
| 266 |
|
| 267 |
ArrayList<Integer> indexes = null; |
| 268 |
for (int i = mFrequencies.size() - 1; i >= 0; i--) { |
| 269 |
final int val = mFrequencies.valueAt(i); |
| 270 |
if (val > 0) { |
| 271 |
if (indexes == null) { |
| 272 |
indexes = new ArrayList<>(); |
| 273 |
} |
| 274 |
indexes.add(i); |
| 275 |
} |
| 276 |
} |
| 277 |
if (indexes == null) { |
| 278 |
return null; |
| 279 |
} |
| 280 |
|
| 281 |
Collections.sort(indexes, (a, b) -> mFrequencies.valueAt(b) - mFrequencies.valueAt(a)); |
| 282 |
|
| 283 |
final List<T> result = holder != null ? holder : new ArrayList<T>(); |
| 284 |
final int max = Math.min(k == 0 ? (mCapacity - 1) : k, indexes.size()); |
| 285 |
for (int i = 0; i < max; i++) { |
| 286 |
final int index = indexes.get(i); |
| 287 |
final T obj = mObjects.valueAt(index); |
| 288 |
if (obj != null) { |
| 289 |
result.add(obj); |
| 290 |
if (freqs != null) { |
| 291 |
freqs.add((float) mFrequencies.valueAt(index) / mPassSize); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
return result; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
@Nullable |
| 304 |
public List<T> getCandidates(final @Nullable List<T> holder) { |
| 305 |
if (!mConfigured) { |
| 306 |
throw new IllegalStateException(); |
| 307 |
} |
| 308 |
if (mNumInputs < mPassSize) { |
| 309 |
|
| 310 |
return null; |
| 311 |
} |
| 312 |
|
| 313 |
List<T> result = holder != null ? holder : new ArrayList<T>(); |
| 314 |
for (int i = mObjects.size() - 1; i >= 0; i--) { |
| 315 |
final T obj = mObjects.valueAt(i); |
| 316 |
if (obj != null) { |
| 317 |
result.add(obj); |
| 318 |
} |
| 319 |
} |
| 320 |
return result; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
@Override |
| 327 |
public void reset() { |
| 328 |
mNumInputs = 0; |
| 329 |
mObjects.clear(); |
| 330 |
mFrequencies.clear(); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public float getRequiredValidationInputRatio() { |
| 338 |
return 0.5f; |
| 339 |
} |
| 340 |
} |
| 341 |
} |