Created
June 11, 2015 09:59
-
-
Save asarkar/ecf9de5a339262e1353c to your computer and use it in GitHub Desktop.
Scala Concurrency Demp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FrequencyCalculator extends RecursiveTask<Map<Character, Integer>> { | |
private static final long serialVersionUID = -5328480156308631556L; | |
private static final int NUM_PROCESSORS = Runtime.getRuntime() | |
.availableProcessors(); | |
private final String word; | |
public FrequencyCalculator(String word) { | |
this.word = word; | |
} | |
@Override | |
protected Map<Character, Integer> compute() { | |
Map<Character, Integer> frequencyMap = new HashMap<>(); | |
int len = word.length(); | |
if (len < NUM_PROCESSORS) { | |
for (int i = 0; i < len; i++) { | |
final char ch = word.charAt(i); | |
if (frequencyMap.containsKey(ch)) { | |
frequencyMap.put(ch, frequencyMap.get(ch) + 1); | |
} else { | |
frequencyMap.put(ch, 1); | |
} | |
} | |
return frequencyMap; | |
} | |
FrequencyCalculator fc1 = new FrequencyCalculator(word.substring(0, | |
len / 2)); | |
fc1.fork(); | |
FrequencyCalculator fc2 = new FrequencyCalculator( | |
word.substring(len / 2)); | |
return merge(fc2.compute(), fc1.join()); | |
} | |
Map<Character, Integer> merge(final Map<Character, Integer> m1, | |
final Map<Character, Integer> m2) { | |
final Map<Character, Integer> frequencyMap = new HashMap<>(); | |
TreeSet<Character> t1 = new TreeSet<>(m1.keySet()); | |
final Iterator<Character> k1 = new TreeSet<>(t1).iterator(); | |
final Iterator<Character> k2 = new TreeSet<>(m2.keySet()).iterator(); | |
char c1 = k1.next(); | |
char c2 = k2.next(); | |
label: while (k1.hasNext()) { | |
while (k2.hasNext()) { | |
if (c1 == c2) { | |
frequencyMap.put(c1, m1.get(c1) + m2.get(c2)); | |
c1 = k1.next(); | |
c2 = k2.next(); | |
} else if (c1 > c2) { | |
frequencyMap.put(c2, m2.get(c2)); | |
c2 = k2.next(); | |
} else { | |
for (char c : t1.headSet(c2)) { | |
frequencyMap.put(c, m1.get(c)); | |
c1 = k1.next(); | |
} | |
continue label; | |
} | |
} | |
if (c1 == c2) { | |
frequencyMap.put(c1, m1.get(c1) + m2.get(c2)); | |
} else { | |
frequencyMap.put(c1, m1.get(c1)); | |
frequencyMap.put(c2, m2.get(c2)); | |
} | |
break label; | |
} | |
while (k1.hasNext()) { | |
c1 = k1.next(); | |
frequencyMap.put(c1, m1.get(c1)); | |
} | |
return frequencyMap; | |
} | |
public static void main(String[] args) { | |
FrequencyCalculator fc = new FrequencyCalculator("abcbdbdc"); | |
Map<Character, Integer> m1 = new HashMap<>(); | |
Map<Character, Integer> m2 = new HashMap<>(); | |
m1.put('a', 1); | |
m1.put('b', 2); | |
m1.put('c', 5); | |
m2.put('b', 1); | |
m2.put('d', 3); | |
Map<Character, Integer> m3 = fc.merge(m1, m2); | |
System.out.println(m3); | |
m3 = new ForkJoinPool().invoke(fc); | |
System.out.println(m3); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parFrequency(str: String) = { | |
val freq = ImmutableHashMap[Char, Int]() | |
str.par.aggregate(freq)((_, c) => ImmutableHashMap(c -> 1), _.merged(_) { | |
case ((k, v1), (_, v2)) => (k, v1 + v2) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment