Cambridge SMT System
ProvenanceProbMap.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.hadoop.io.DoubleWritable;
27 import org.apache.hadoop.io.IntWritable;
28 import org.apache.hadoop.io.Writable;
29 import org.apache.hadoop.io.WritableUtils;
30 
39 public class ProvenanceProbMap implements Writable,
40  Map<IntWritable, DoubleWritable> {
41 
42  static final ProvenanceProbMap EMPTY = new ProvenanceProbMap() {
43  public DoubleWritable put(IntWritable key, DoubleWritable value) {
44  throw new UnsupportedOperationException();
45  };
46  };
47 
48  private Map<IntWritable, DoubleWritable> instance = new HashMap<>();
49 
50  public ProvenanceProbMap() {
51 
52  }
53 
55  for (Entry<IntWritable, DoubleWritable> entry : value.entrySet()) {
56  put(entry.getKey(), entry.getValue());
57  }
58  }
59 
60  public int size() {
61  return instance.size();
62  }
63 
64  public boolean isEmpty() {
65  return instance.isEmpty();
66  }
67 
68  public boolean containsKey(Object key) {
69  return instance.containsKey(key);
70  }
71 
72  public boolean containsValue(Object value) {
73  return instance.containsValue(value);
74  }
75 
76  public DoubleWritable get(Object key) {
77  return instance.get(key);
78  }
79 
80  public DoubleWritable put(IntWritable key, DoubleWritable value) {
81  return instance.put(key, value);
82  }
83 
87  public DoubleWritable put(int key, double value) {
88  IntWritable keyObject = IntWritableCache.createIntWritable(key);
89  if (instance.containsKey(keyObject) && instance.get(keyObject) != null) {
90  instance.get(keyObject).set(value);
91  return instance.get(keyObject);
92  } else {
93  return instance.put(keyObject, new DoubleWritable(value));
94  }
95 
96  }
97 
98  public DoubleWritable remove(Object key) {
99  return instance.remove(key);
100  }
101 
102  public void putAll(Map<? extends IntWritable, ? extends DoubleWritable> m) {
103  instance.putAll(m);
104  }
105 
106  public void clear() {
107  instance.clear();
108  }
109 
110  public Set<IntWritable> keySet() {
111  return instance.keySet();
112  }
113 
114  public Collection<DoubleWritable> values() {
115  return instance.values();
116  }
117 
118  public Set<java.util.Map.Entry<IntWritable, DoubleWritable>> entrySet() {
119  return instance.entrySet();
120  }
121 
122  public boolean equals(Object o) {
123  return instance.equals(o);
124  }
125 
126  public int hashCode() {
127  return instance.hashCode();
128  }
129 
130  public String toString() {
131  return instance.toString();
132  }
133 
134  public void merge(ProvenanceProbMap other) {
135  int expectedSize = size() + other.size();
136  putAll(other);
137  if (expectedSize != size()) {
138  throw new RuntimeException("Two features with the same id: " + this
139  + " " + other);
140  }
141  }
142 
143  @Override
144  public void write(DataOutput out) throws IOException {
145  WritableUtils.writeVInt(out, instance.size());
146  for (Entry<IntWritable, DoubleWritable> entry : instance.entrySet()) {
147  WritableUtils.writeVInt(out, entry.getKey().get());
148  entry.getValue().write(out);
149  }
150  }
151 
152  @Override
153  public void readFields(DataInput in) throws IOException {
154  instance.clear();
155  int length = WritableUtils.readVInt(in);
156  for (int i = 0; i < length; ++i) {
157  int key = WritableUtils.readVInt(in);
158  double value = in.readDouble();
159  put(key, value);
160  }
161 
162  }
163 
164 }
DoubleWritable put(IntWritable key, DoubleWritable value)
fst::TropicalWeightTpl< F > Map(double)
Set< java.util.Map.Entry< IntWritable, DoubleWritable > > entrySet()
void putAll(Map<?extends IntWritable,?extends DoubleWritable > m)