Skip to content

Commit 5fa4911

Browse files
Caching last hashing result
1 parent f2c938f commit 5fa4911

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package YASL.Hashing;
2+
3+
public class ChgCachedHash<T> implements IHashingGenerator<T> {
4+
private final IHashingGenerator<T> _gen;
5+
6+
public ChgCachedHash(IHashingGenerator<T> gen) {
7+
this._gen = gen;
8+
}
9+
10+
@Override
11+
public IHasher<T> generate(int range, int levels) {
12+
final IHasher<T> gen = _gen.generate(range, levels);
13+
return new IHasher<T>() {
14+
private T _lastRequest = null;
15+
private int[] _lastResult = null;
16+
17+
@Override
18+
public int[] apply(T x) {
19+
if (!x.equals(_lastRequest)) {
20+
_lastRequest = x;
21+
_lastResult = gen.apply(x);
22+
}
23+
return _lastResult;
24+
}
25+
};
26+
}
27+
}

0 commit comments

Comments
 (0)