16 package uk.ac.cam.eng.extraction.hadoop.datatypes;
18 import java.io.DataInput;
19 import java.io.DataOutput;
20 import java.io.IOException;
21 import java.util.Collection;
22 import java.util.HashMap;
26 import org.apache.commons.collections.map.LRUMap;
27 import org.apache.hadoop.io.ByteWritable;
28 import org.apache.hadoop.io.IntWritable;
29 import org.apache.hadoop.io.Writable;
37 Map<ByteWritable, IntWritable> {
41 private static LRUMap lruIntCache =
new LRUMap(100000);
43 private static Map<Byte, ByteWritable> bytesCache =
new HashMap<>();
45 private Map<ByteWritable, IntWritable> instance =
new HashMap<ByteWritable, IntWritable>();
52 instance.putAll(other.instance);
59 private static ByteWritable getCached(byte b) {
60 if (bytesCache.containsKey(b)) {
61 return bytesCache.get(b);
63 ByteWritable result =
new ByteWritable(b);
64 bytesCache.put(b, result);
68 private static IntWritable getCached(
int i) {
69 if (lruIntCache.containsKey(i)) {
70 return (IntWritable) lruIntCache.get(i);
72 IntWritable result =
new IntWritable(i);
73 lruIntCache.put(i, result);
79 for (Entry<ByteWritable, IntWritable> provCount : newCounts.
entrySet()) {
80 ByteWritable key = provCount.getKey();
82 put(key, getCached(
get(key).
get() + newCounts.
get(key).get()));
84 put(key, newCounts.
get(key));
90 return instance.size();
94 return instance.isEmpty();
98 return instance.containsKey(key);
102 return instance.containsValue(value);
105 public IntWritable
get(Object key) {
106 return instance.get(key);
109 public IntWritable
put(ByteWritable key, IntWritable value) {
110 return instance.put(key, value);
113 public IntWritable
remove(Object key) {
114 return instance.remove(key);
117 public void putAll(Map<? extends ByteWritable, ? extends IntWritable> m) {
126 return instance.keySet();
129 public Collection<IntWritable>
values() {
130 return instance.values();
133 public Set<java.util.Map.Entry<ByteWritable, IntWritable>>
entrySet() {
134 return instance.entrySet();
138 return instance.equals(o);
142 return instance.hashCode();
146 return instance.toString();
150 public void write(DataOutput out)
throws IOException {
151 out.writeByte(instance.size());
152 for (Entry<ByteWritable, IntWritable> entry : instance.entrySet()) {
153 entry.getKey().write(out);
154 entry.getValue().write(out);
162 byte length = in.readByte();
163 for (
int i = 0; i < length; ++i) {
164 byte key = in.readByte();
165 int value = in.readInt();
166 instance.put(getCached(key), getCached(value));
fst::TropicalWeightTpl< F > Map(double)