Skip to content
Snippets Groups Projects
Commit 2df17f7f authored by Christian Wulf's avatar Christian Wulf
Browse files

improved performance of the CountingMap

parent b0fb800b
No related branches found
No related tags found
No related merge requests found
#FindBugs User Preferences
#Thu Mar 19 12:32:13 CET 2015
#Thu Mar 19 17:05:30 CET 2015
detector_threshold=3
effort=max
excludefilter0=.fbExcludeFilterFile|true
......
......@@ -112,7 +112,13 @@
<groupId>org.jctools</groupId>
<artifactId>jctools-core</artifactId>
<version>1.0</version>
<!-- <version>1.1-SNAPSHOT</version> -->
<!-- <version>1.1-SNAPSHOT</version> -->
</dependency>
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>hppc</artifactId>
<version>0.6.0</version>
</dependency>
</dependencies>
......
......@@ -15,12 +15,7 @@
*/
package teetime.stage;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import teetime.framework.AbstractConsumerStage;
import teetime.framework.OutputPort;
import teetime.stage.util.CountingMap;
/**
......@@ -36,25 +31,15 @@ import teetime.stage.util.CountingMap;
*/
public final class CountingMapMerger<T> extends AbstractConsumerStage<CountingMap<T>> {
private final CountingMap<T> result = new CountingMap<T>();
private final OutputPort<Map<T, Integer>> port = createOutputPort();
private final CountingMap<T> mergedResult = new CountingMap<T>();
@Override
protected void execute(final CountingMap<T> element) {
Set<Map.Entry<T, Integer>> entries = element.entrySet();
for (Entry<T, Integer> entry : entries) {
result.add(entry.getKey(), entry.getValue());
}
}
@Override
public void onTerminating() throws Exception {
port.send(result);
super.onTerminating();
mergedResult.add(element);
}
public CountingMap<T> getResult() {
return result;
return mergedResult;
}
}
......@@ -15,25 +15,23 @@
*/
package teetime.stage.util;
import java.util.HashMap;
import com.carrotsearch.hppc.ObjectIntMap;
import com.carrotsearch.hppc.ObjectIntOpenHashMap;
import com.carrotsearch.hppc.procedures.ObjectIntProcedure;
/**
* An implementation of HashMap which can be used to count the occurrence of different keys.
* This contains all methods of HashMap, but is enhanced with the {@link #add(T, Integer) add} and {@link #increment(T) increment} methods.
* An implementation of a map which can be used to count the occurrence of different keys.
*
* @since 1.1
*
* @author Nelson Tavares de Sousa
* @author Nelson Tavares de Sousa, Christian Wulf
*
* @param <T>
* Key type to be count
*/
public final class CountingMap<T> extends HashMap<T, Integer> {
public final class CountingMap<T> {
/**
* Generated serialVersionUID
*/
private static final long serialVersionUID = -8036971796701648200L;
private final ObjectIntMap<T> map = new ObjectIntOpenHashMap<T>();
/**
* Increments the value of key by one.
......@@ -42,31 +40,37 @@ public final class CountingMap<T> extends HashMap<T, Integer> {
* The key which sould be incremented
*/
public void increment(final T key) {
Integer count = super.get(key);
if (null != count) {
count++;
super.put(key, count);
} else {
super.put(key, 1);
}
map.addTo(key, 1);
}
/**
* Adds i to the value of key.
*
* @param key
* Key which is used to add i.
* @param i
* Integer value to be added.
* the key which is used to add i.
* @param value
* the value to be added.
*/
public void add(final T key, final Integer i) {
if (super.containsKey(key)) {
Integer j = super.get(key);
j += i;
super.put(key, j);
} else {
super.put(key, i);
}
public void add(final T key, final int value) {
map.addTo(key, value);
}
public void add(final CountingMap<T> otherMap) {
final ObjectIntProcedure<? super T> procedure = new ObjectIntProcedure<T>() {
@Override
public void apply(final T key, final int value) {
map.addTo(key, value);
}
};
otherMap.map.forEach(procedure);
}
public int get(final T key) {
return map.get(key);
}
public int size() {
return map.size();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment