void masterWakeupUser() { // === sort the final reduced file === char *tmp = calloc(0, sizeof(char)); printf("@@ reduce tasks count: %d\n", reduceTasksCount); tmp[0] = (reduceTasksCount) + '0'; printf("@@ tmp: %s\n", tmp); char *finalMergedFileName = getFilename("reduced_", tmp); printf("@@ %s:\n", finalMergedFileName); int numberOfWords = getNumberOfWordsInFile(finalMergedFileName); char *sortedMergeFile = sortWords(finalMergedFileName, numberOfWords); free(tmp); // =================================== // === combine the counts in final reduced file === analyzeWordsCount(sortedMergeFile); // ================================================ // === All MapReduce tasks finish, Master inform Parent to wake-up === close(masterToParentPipe[0]); // avoid master read from output to parent char *buf = "wake-up user!"; printf("master [PID: %d] invoke --> %s\n", getpid(), buf); write(masterToParentPipe[1], buf, strlen(buf)); close(masterToParentPipe[1]); // avoid master further write output to parent // =================================================================== }
int main(int argc, char **argv) { gchar *file_contents = openFile("huckfinn.txt"); gchar **split_file = split(file_contents); GHashTable *map = makeHistogram(split_file); GList *sorted = sortWords(map); printSortedMap(sorted,map); return 0; }
//hash_lookup void hashLookup(HT hashTable, ElemType *input, int (*hashFun)(ElemType *words, int size)){ ElemType inputTemp[keySize]; strcpy(inputTemp, input); //reorder the user input sortWords(inputTemp); //calcuate hash value based on the user input int hValue = (*hashFun)(inputTemp, hashTable->tableSize); //print all the words with calculated hash value printWords(hashTable, hValue); }//end of hashLookup
//hash_lookup NODE* hashLookup(HT hashTable, ElemType *input, int (*hashFun)(ElemType *words, int size)){ ElemType inputTemp[keySize]; strcpy(inputTemp, input); //reorder the user input sortWords(inputTemp); //calcuate hash value based on the user input int hValue = (*hashFun)(inputTemp, hashTable->tableSize); Node *p = &(hashTable->theList[hValue]); while (strcmp(p->r->nameVal.name, input) != 0) { p = p->next; } return p->r; }//end of hashLookup
//hash_insert void tableInsert(HT hashTable, ElemType *words, int (*hashFun)(ElemType *word, int size)){ ElemType keyTemp[keySize]; strcpy(keyTemp, words); //re-order the words from dictionary sortWords(keyTemp); //calculate hash value int hValue = hashFun(keyTemp, hashTable->tableSize); /* if (strcmp(words, "ably") == 0 || strcmp(words, "boas") == 0) { */ /* printf("%s %d\n", words, hValue); */ /* } */ //add words to bucket addFront(hashTable, hValue, words); }// end of tableInsert
/****************************************************************************** 原 型:int AddOneWord (char* Word); 功 能:在字典中增加一个单词 输入参数: Word 单词字符串,调用者保证Word指针不为空,指向的是合法单词 输出参数: 无 返回值: -1 失败(单词在字典中已存在等情况) 0 成功 ********************************************************************************/ int AddOneWord (char* Word) { WordNode *it = gWordHead; WordNode *newNode = NULL; unsigned int wordLen = strlen(Word); if ( Word == NULL ) return -1; while ( it ) { if ( strcmp(it->word, Word) == 0 ) return -1; it = it->next; } newNode = (WordNode *) malloc ( sizeof(WordNode) ); newNode->word = (char *) malloc ( sizeof(char) * wordLen + 1 ); if ( newNode == NULL || newNode->word == NULL ) return -1; strncpy( newNode->word, Word, wordLen ); newNode->word[wordLen] = '\0'; newNode->next = NULL; if ( gWordTail ) { gWordTail->next = newNode; gWordTail = newNode; } else { gWordHead = newNode; gWordTail = newNode; } gWordNum++; sortWords(); return 0; }
/** * The main function of strings.c * Calls getWord repeatidly, stores the words, calls sortWords(), exits. */ int main(int argc, char* argv[]){ char str_list[MAX_STR_COUNT][MAX_STR_LENGTH + 1]; int i = 0; int word_index = 0; int getWordResult = 0; /* * Call getWord repeatidly, retreving every word from stdin * If EOF_BEFORE_WORD is returned, don't count the whitespace read in and break * If EOF_AFTER_WORD is returned, count the word and break */ for(word_index = 0; word_index < MAX_STR_COUNT; word_index++){ getWordResult = getWord(str_list[word_index], MAX_STR_LENGTH); if(getWordResult == EOF_BEFORE_WORD){ word_index--; break; } if(getWordResult == EOF_AFTER_WORD){ break; } } // Output the current word array (unsorted) printf("Inputted Words (unsorted):\n"); for(i = 0; i < word_index + 1; i++){ printWord(str_list[i], strlen(str_list[i])); } printf("\n"); // Sort the array of parsed words sortWords(str_list, word_index + 1); // Output the array of sorted words printf("Inputted Words (sorted):\n"); for(i = 0; i < word_index + 1; i++){ printWord(str_list[i], strlen(str_list[i])); } printf("\n"); return EXIT_SUCCESS; }
int main() { FILE * infile; char line[256]; int numThreads; int i; int numMemes; int j; Sentence memes[32]; char * token; int numPhrases; Sentence phrases[32]; bool foundMeme; bool reject; int idx; // Open the input file infile = fopen("meme.in", "r"); // Read the number of comment threads fgets(line, sizeof(line), infile); sscanf(line, "%d", &numThreads); // Loop through the comment threads for (i=0; i < numThreads; i++) { // Output the header printf("Comment thread #%d:\n", i+1); // Get the number of memes fgets(line, sizeof(line), infile); sscanf(line, "%d", &numMemes); // Read in and split the memes for (j=0; j < numMemes; j++) { // Read in the line fgets(line, sizeof(line), infile); if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0'; // Tokenize and store each word in the meme memes[j].numWords = 0; strcpy(memes[j].line, line); token = strtok(line, " "); while (token != NULL) { // Store this word and increment the count strcpy(memes[j].words[memes[j].numWords], token); capitalize(memes[j].words[memes[j].numWords]); memes[j].used[memes[j].numWords] = false; memes[j].numWords++; // Try to get the next word token = strtok(NULL, " "); } } // Get the number of phrases fgets(line, sizeof(line), infile); sscanf(line, "%d", &numPhrases); // Read in and split the phrases for (j=0; j < numPhrases; j++) { // Read in the line fgets(line, sizeof(line), infile); if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0'; // Tokenize and store each word in the meme phrases[j].numWords = 0; strcpy(phrases[j].line, line); token = strtok(line, " "); while (token != NULL) { // Store this word and increment the count strcpy(phrases[j].words[phrases[j].numWords], token); capitalize(phrases[j].words[phrases[j].numWords]); phrases[j].used[phrases[j].numWords] = false; phrases[j].numWords++; // Try to get the next word token = strtok(NULL, " "); } } // Sort the words within the meme for (j=0; j < numMemes; j++) sortWords(&memes[j]); for (j=0; j < numPhrases; j++) sortWords(&phrases[j]); // Loop through the phrases foundMeme = false; for (j=0; j < numMemes; j++) { // Check this meme against the phrases reject = false; idx = 0; while ( (reject == false) && (idx < numPhrases) ) { // See if this phrase matches the idx-th meme if (check(phrases[idx], memes[j]) == true) { // It's a meme! Warning! reject = true; } else { // Try the next phrase idx++; } } // If the meme was found, output it and note that we found one if (reject == true) { // Output the meme printf(" %s\n", memes[j].line); // Mark that we found at least one meme foundMeme = true; } } // If we found no memes, output accordingly if (foundMeme == false) printf(" No memes detected! Let intellectual discourse continue!\n"); // Leave a blank line after each set printf("\n"); } // Close the input file fclose(infile); }