Procházet zdrojové kódy

ADD.添加更多注释

RegMs If před 4 roky
rodič
revize
acaa1ec330
4 změnil soubory, kde provedl 40 přidání a 39 odebrání
  1. 10 11
      go/parallel/main.go
  2. 8 9
      go/serial/main.go
  3. 14 11
      java/parallel/WordCount.java
  4. 8 8
      java/serial/WordCount.java

+ 10 - 11
go/parallel/main.go

@@ -40,7 +40,7 @@ func Mapper(config Config, filename chan string, shuffle chan Pair) {
 	output := make(chan Pair, chanSize)
 	merge := make(map[string][]int)
 	for name := range filename {
-		text, _ := os.ReadFile(name)
+		text, _ := os.ReadFile(name) // read file content
 		go config.mapFunc(text, output)
 		for pair := range output {
 			if pair.key == "" { // map finish
@@ -50,7 +50,7 @@ func Mapper(config Config, filename chan string, shuffle chan Pair) {
 		}
 	}
 	for key, list := range merge {
-		shuffle <- config.combineFunc(key, list) // to shuffle stage
+		shuffle <- config.combineFunc(key, list) // combine local result, to shuffle stage
 	}
 	shuffle <- Pair{"", 0} // mapper finish
 }
@@ -145,9 +145,8 @@ func (s PairSlice) Swap(i, j int) {
 func (s PairSlice) Less(i, j int) bool {
 	if s[i].value == s[j].value {
 		return s[i].key < s[j].key
-	} else {
-		return s[i].value > s[j].value
 	}
+	return s[i].value > s[j].value
 }
 
 /* User-defined map function */
@@ -158,7 +157,7 @@ func Map(text []byte, output chan Pair) {
 			text[i] += 'a' - 'A' // to lower case
 		}
 		if text[i] >= 'a' && text[i] <= 'z' || word != "" && text[i] == '\'' {
-			word += string(text[i])
+			word += string(text[i]) // extend word
 		} else if word != "" {
 			output <- Pair{word, 1}
 			word = ""
@@ -181,12 +180,12 @@ func Reduce(key string, list []int) Pair {
 
 func main() {
 	config := Config{
-		path:        "../testdata",
-		mapCnt:      4,
-		reduceCnt:   4,
-		mapFunc:     Map,
-		combineFunc: Reduce,
-		reduceFunc:  Reduce}
+		path:        "../testdata", // directory that stores data
+		mapCnt:      4,             // number of mapper
+		reduceCnt:   4,             // number of reducer
+		mapFunc:     Map,           // map function
+		combineFunc: Reduce,        // combine function
+		reduceFunc:  Reduce}        // reduce function
 	answer, err := Run(config)
 	if err != nil {
 		fmt.Println(err)

+ 8 - 9
go/serial/main.go

@@ -28,45 +28,44 @@ func (s PairSlice) Swap(i, j int) {
 func (s PairSlice) Less(i, j int) bool {
 	if s[i].value == s[j].value {
 		return s[i].key < s[j].key
-	} else {
-		return s[i].value > s[j].value
 	}
+	return s[i].value > s[j].value
 }
 
 func main() {
 	const path = "../testdata"
-	dataDir, err := os.ReadDir(path)
+	dataDir, err := os.ReadDir(path) // get directory info
 	if err != nil {
 		fmt.Println("Directory not found!")
 		return
 	}
 
-	dict := make(map[string]int)
+	dict := make(map[string]int) // store count in map
 	for _, file := range dataDir {
 		if file.IsDir() {
 			continue
 		}
-		text, _ := os.ReadFile(filepath.Join(path, file.Name()))
+		text, _ := os.ReadFile(filepath.Join(path, file.Name())) // read file content
 		word := ""
 		for i := range text {
 			if text[i] >= 'A' && text[i] <= 'Z' {
 				text[i] += 'a' - 'A' // to lower case
 			}
 			if text[i] >= 'a' && text[i] <= 'z' || word != "" && text[i] == '\'' {
-				word += string(text[i])
+				word += string(text[i]) // extend word
 			} else if word != "" {
-				dict[word]++
+				dict[word]++ // update map
 				word = ""
 			}
 		}
 		if word != "" {
-			dict[word]++
+			dict[word]++ // update map
 		}
 	}
 
 	answer := make(PairSlice, 0)
 	for word, cnt := range dict {
-		answer = append(answer, Pair{word, cnt})
+		answer = append(answer, Pair{word, cnt}) // get answer from map
 	}
 	sort.Sort(answer)
 	for _, pair := range answer {

+ 14 - 11
java/parallel/WordCount.java

@@ -19,12 +19,13 @@ public class WordCount {
         private final static IntWritable one = new IntWritable(1);
         private Text word = new Text();
 
+        /* map function */
         public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
             String line = value.toString();
             String str = new String("");
             for (int i = 0; i < line.length(); i++) {
                 if (Character.isLetter(line.charAt(i)) || str.length() > 0 && line.charAt(i) == '\'') {
-                    str += Character.toLowerCase(line.charAt(i));
+                    str += Character.toLowerCase(line.charAt(i)); // extend word
                 } else if (str.length() > 0) {
                     word.set(str);
                     context.write(word, one);
@@ -39,22 +40,24 @@ public class WordCount {
     }
 
     public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
-        private HashMap<String, Integer> record = new HashMap<String, Integer>();
+        private HashMap<String, Integer> record = new HashMap<String, Integer>(); // store count in map, used for final sort
         private Text word = new Text();
         private IntWritable result = new IntWritable();
 
+        /* reduce function */
         public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
             int sum = 0;
             for (IntWritable val : values) {
                 sum += val.get();
             }
-            record.put(key.toString(), record.getOrDefault(key.toString(), 0) + sum);
+            record.put(key.toString(), record.getOrDefault(key.toString(), 0) + sum); // update map, don't write context here
         }
 
+        /* cleanup function executed after reducing */
         public void cleanup(Context context) throws IOException, InterruptedException {
-            ArrayList<HashMap.Entry<String, Integer>> list = new ArrayList<HashMap.Entry<String, Integer>>(record.entrySet());
+            ArrayList<HashMap.Entry<String, Integer>> list = new ArrayList<HashMap.Entry<String, Integer>>(record.entrySet()); // get answer from map
             Collections.sort(list, new Comparator<HashMap.Entry<String, Integer>>() {
-                public int compare(HashMap.Entry<String, Integer> o1, HashMap.Entry<String, Integer> o2) {
+                public int compare(HashMap.Entry<String, Integer> o1, HashMap.Entry<String, Integer> o2) { // compare function
                     if (o1.getValue().equals(o2.getValue())) {
                         return o1.getKey().compareTo(o2.getKey());
                     }
@@ -64,7 +67,7 @@ public class WordCount {
             for (HashMap.Entry<String, Integer> itr : list) {
                 word.set(itr.getKey());
                 result.set(itr.getValue());
-                context.write(word, result);
+                context.write(word, result); // write context here
             }
         }
     }
@@ -73,14 +76,14 @@ public class WordCount {
         Configuration conf = new Configuration();
         Job job = Job.getInstance(conf, "word count");
         job.setJarByClass(WordCount.class);
-        job.setMapperClass(TokenizerMapper.class);
-        job.setCombinerClass(IntSumReducer.class);
-        job.setReducerClass(IntSumReducer.class);
+        job.setMapperClass(TokenizerMapper.class); // map class
+        job.setCombinerClass(IntSumReducer.class); // combine class
+        job.setReducerClass(IntSumReducer.class); // reduce class
         job.setOutputKeyClass(Text.class);
         job.setOutputValueClass(IntWritable.class);
-        FileOutputFormat.setOutputPath(job, new Path(args[0]));
+        FileOutputFormat.setOutputPath(job, new Path(args[0])); // output directory
         for (int i = 1; i < args.length; i++) {
-            FileInputFormat.addInputPath(job, new Path(args[i]));
+            FileInputFormat.addInputPath(job, new Path(args[i])); // input files
         }
         System.exit(job.waitForCompletion(true) ? 0 : 1);
     }

+ 8 - 8
java/serial/WordCount.java

@@ -9,31 +9,31 @@ import java.util.Comparator;
 
 public class WordCount {
     public static void main(String[] args) throws Exception {
-        HashMap<String, Integer> record = new HashMap<String, Integer>();
+        HashMap<String, Integer> record = new HashMap<String, Integer>(); // store count in map
         String path = "../testdata";
-        Path dataDir = Paths.get(path);
+        Path dataDir = Paths.get(path); // get directory info
         DirectoryStream<Path> files = Files.newDirectoryStream(dataDir);
         for (Path file : files) {
             if (Files.isDirectory(file)) {
                 continue;
             }
-            String text = new String(Files.readAllBytes(dataDir.resolve(file.getFileName())));
+            String text = new String(Files.readAllBytes(dataDir.resolve(file.getFileName()))); // read file content
             String word = new String("");
             for (int i = 0; i < text.length(); i++) {
                 if (Character.isLetter(text.charAt(i)) || word.length() > 0 && text.charAt(i) == '\'') {
-                    word += Character.toLowerCase(text.charAt(i));
+                    word += Character.toLowerCase(text.charAt(i)); // extend word
                 } else if (word.length() > 0) {
-                    record.put(word, record.getOrDefault(word, 0) + 1);
+                    record.put(word, record.getOrDefault(word, 0) + 1); // update map
                     word = "";
                 }
             }
             if (word.length() > 0) {
-                record.put(word, record.getOrDefault(word, 0) + 1);
+                record.put(word, record.getOrDefault(word, 0) + 1); // update map
             }
         }
-        ArrayList<HashMap.Entry<String, Integer>> list = new ArrayList<HashMap.Entry<String, Integer>>(record.entrySet());
+        ArrayList<HashMap.Entry<String, Integer>> list = new ArrayList<HashMap.Entry<String, Integer>>(record.entrySet()); // get answer from map
         Collections.sort(list, new Comparator<HashMap.Entry<String, Integer>>() {
-            public int compare(HashMap.Entry<String, Integer> o1, HashMap.Entry<String, Integer> o2) {
+            public int compare(HashMap.Entry<String, Integer> o1, HashMap.Entry<String, Integer> o2) { // compare function
                 if (o1.getValue().equals(o2.getValue())) {
                     return o1.getKey().compareTo(o2.getKey());
                 }