Cambridge SMT System
ProvenanceCountMap.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use these files except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  *
14  * Copyright 2014 - Juan Pino, Aurelien Waite, William Byrne
15  *******************************************************************************/
16 package uk.ac.cam.eng.extraction.hadoop.datatypes;
17 
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;
23 import java.util.Map;
24 import java.util.Set;
25 
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;
30 
36 public class ProvenanceCountMap implements Writable,
37  Map<ByteWritable, IntWritable> {
38 
39  // Probably should fix the integer cache to work in the same way
40  // as the cache in the Integer class
41  private static LRUMap lruIntCache = new LRUMap(100000);
42 
43  private static Map<Byte, ByteWritable> bytesCache = new HashMap<>();
44 
45  private Map<ByteWritable, IntWritable> instance = new HashMap<ByteWritable, IntWritable>();
46 
47  public ProvenanceCountMap() {
48 
49  }
50 
52  instance.putAll(other.instance);
53  }
54 
55  public Map<ByteWritable, IntWritable> getInstance() {
56  return instance;
57  }
58 
59  private static ByteWritable getCached(byte b) {
60  if (bytesCache.containsKey(b)) {
61  return bytesCache.get(b);
62  }
63  ByteWritable result = new ByteWritable(b);
64  bytesCache.put(b, result);
65  return result;
66  }
67 
68  private static IntWritable getCached(int i) {
69  if (lruIntCache.containsKey(i)) {
70  return (IntWritable) lruIntCache.get(i);
71  }
72  IntWritable result = new IntWritable(i);
73  lruIntCache.put(i, result);
74  return result;
75  }
76 
77 
78  public void increment(ProvenanceCountMap newCounts) {
79  for (Entry<ByteWritable, IntWritable> provCount : newCounts.entrySet()) {
80  ByteWritable key = provCount.getKey();
81  if (containsKey(key)) {
82  put(key, getCached(get(key).get() + newCounts.get(key).get()));
83  } else {
84  put(key, newCounts.get(key));
85  }
86  }
87  }
88 
89  public int size() {
90  return instance.size();
91  }
92 
93  public boolean isEmpty() {
94  return instance.isEmpty();
95  }
96 
97  public boolean containsKey(Object key) {
98  return instance.containsKey(key);
99  }
100 
101  public boolean containsValue(Object value) {
102  return instance.containsValue(value);
103  }
104 
105  public IntWritable get(Object key) {
106  return instance.get(key);
107  }
108 
109  public IntWritable put(ByteWritable key, IntWritable value) {
110  return instance.put(key, value);
111  }
112 
113  public IntWritable remove(Object key) {
114  return instance.remove(key);
115  }
116 
117  public void putAll(Map<? extends ByteWritable, ? extends IntWritable> m) {
118  instance.putAll(m);
119  }
120 
121  public void clear() {
122  instance.clear();
123  }
124 
125  public Set<ByteWritable> keySet() {
126  return instance.keySet();
127  }
128 
129  public Collection<IntWritable> values() {
130  return instance.values();
131  }
132 
133  public Set<java.util.Map.Entry<ByteWritable, IntWritable>> entrySet() {
134  return instance.entrySet();
135  }
136 
137  public boolean equals(Object o) {
138  return instance.equals(o);
139  }
140 
141  public int hashCode() {
142  return instance.hashCode();
143  }
144 
145  public String toString() {
146  return instance.toString();
147  }
148 
149  @Override
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);
155  }
156 
157  }
158 
159  @Override
160  public void readFields(DataInput in) throws IOException {
161  instance.clear();
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));
167  }
168 
169  }
170 
171 }
Set< java.util.Map.Entry< ByteWritable, IntWritable > > entrySet()
IntWritable put(ByteWritable key, IntWritable value)
fst::TropicalWeightTpl< F > Map(double)
void putAll(Map<?extends ByteWritable,?extends IntWritable > m)