start of new file
[IRC.git] / Robust / src / Benchmarks / MapReduce / Tag / MapReduceBase.java
1 public class MapReduceBase {
2
3     public static void map(String key, String value, OutputCollector output) {
4         int n = value.length();
5         for (int i = 0; i < n; ) {
6             // Skip past leading whitespace
7             while ((i < n) && isspace(value.charAt(i))) {
8                 ++i;
9             }
10
11             // Find word end
12             int start = i;
13             while ((i < n) && !isspace(value.charAt(i))) {
14                 i++;
15             }
16
17             if (start < i) {
18                 output.emit(value.subString(start, i), "1");
19                 //System.printString(value.subString(start,i) + "\n");
20             }
21         }
22     }
23
24     public static void reduce(String key, Vector values, OutputCollector output) {
25         // Iterate over all entries with the
26         // // same key and add the values
27         int value = 0;
28         for(int i = 0; i < values.size(); ++i) {
29             value += Integer.parseInt((String)values.elementAt(i));
30         }
31
32         // Emit sum for input->key()
33         output.emit(key, String.valueOf(value));
34     }
35
36     static boolean isspace(char c) {
37         if((c == ' ') || 
38                 (c == ',') ||
39                 (c == '.') ||
40                 (c == '!') ||
41                 (c == '?') ||
42                 (c == '"') ||
43                 (c == '(') ||
44                 (c == ')') ||
45                 (c == '[') ||
46                 (c == ']') ||
47                 (c == '{') ||
48                 (c == '}') ||
49                 (c == '\n')) {
50             return true;
51         }
52         return false;
53     }
54 }
55