Exemple #1
0
/**
 *
 Creates a hashtable, and inserts words into it.
 The table is then printed before we free the memory allocated to it.

 Note: The tablesize of the hashtable is determined by the first command line
 argument if there is one, with a default table tablesize of 113.
 The number of statistical snapshots to print is determined by the second
 command line argument if there is one, with a default number of 10.

 tablesize = the maximum number of positions in the hash table.
 word      = the string to be inserted into the hash table.
 ht        = the hash table using eith double hashing or linear probing.
 snapshots = the number of statistical snapshots to be used.

 @param argc the number of command-line arguments.
 @param argv an array of strings containing the command-line arguments.

 @return EXIT_SUCCESS if the program is successful.

*/
int main(int argc, char **argv) {
  time_t start,end;
  bool_t entire_table = FALSE, double_hashing = FALSE, print_stats = FALSE,
    do_spell_check = FALSE;
  int tablesize = 113, snapshots = 10;
  char word[256];
  char *filename;
  htable ht;

  set_flags(argc, argv, &do_spell_check, &filename, &double_hashing, &entire_table,
	    &print_stats, &snapshots, &tablesize);

  ht = htable_new(tablesize, (double_hashing) ? DOUBLE_H : LINEAR_P);
  start = clock();
  while (getword(word, sizeof word, stdin) != EOF) {
    htable_insert(ht, word);
  }
  end = clock();
  if(do_spell_check) {
    spell_check(ht,filename,(end-start)/(double)CLOCKS_PER_SEC);
  }
  if (entire_table) {
    htable_print_entire_table(ht, stderr);
  }
  if (print_stats) {
    htable_print_stats(ht, stdout, snapshots);
  } else if (!do_spell_check){ /* print words and frequencies */
    htable_print(ht, stdout);
  }
  htable_delete(ht);

  return EXIT_SUCCESS;
}
Exemple #2
0
errcode_t htable_fini(htable_t * self)
{
	if (self->heads != NULL) {
		#ifdef HTABLE_COLLECT_STATS
		htable_print_stats(self);
		#endif

		free(self->heads);
		self->heads = NULL;
	}
	if (self->buckets != NULL) {
		free(self->buckets);
		self->buckets = NULL;
	}
	RETURN_SUCCESSFUL;
}
Exemple #3
0
/**
 * main programme will be excuted.
 * By default, words are read from stdin and
 * added to hashtable before being printed out
 * alongside their frequencies to stdout.
 * @param argc an integer saying how many arguments
 *  there are argc for “argument count”
 * @param argv an array of strings in which the arguments
 *  are stored (argv for “argument vector ”).
 * @return integer 1 to indicate if the programme excuted successfully or not.
 */
int main(int argc, char *argv[]) {
    const char *optstring = "ht:c:deps:";
    char option;
    char word[256];
    int capacity = 113;

    FILE *infile = NULL;
    clock_t start, end;

    hashing_t method = LINEAR_P;

    double fillTime;
    double searchTime;

    int unknownWords = 0;
    int numOfSnapshot = 0;

    htable  h;
    char *fileToBeChecked = NULL;
    int withC = 0;
    int withE = 0;
    int withP = 0;
    int withS = 0;
    /*
     * Begin processing the argument from the command line
     */
    while ((option = getopt(argc, argv, optstring)) != EOF) {
        switch (option) {
            case 't':
                if (atoi(optarg) <= 0){
                    capacity = 113;
                }else {
                    capacity = primegt(atoi(optarg));
                }
                break;
            case 'd':
                method = DOUBLE_H;
                break;
            case 'c':
                withC = 1;
                fileToBeChecked = optarg;
                break;
            case 'e':
                withE = 1;
                break;
            case 's':
                numOfSnapshot = atoi(optarg);
                if (numOfSnapshot <= 0) {
                    numOfSnapshot = 10;
                }
                withS = 1;
                break;
            case 'p':
                withP = 1;
                break;
            case 'h':
                printHelpInfo();
                return EXIT_SUCCESS;
            default:
                printHelpInfo();
                return EXIT_FAILURE;
        }

    }

    h = htable_new(capacity, method);
    start = clock();

    while (getword(word, sizeof word, stdin) != EOF) {
        htable_insert(h, word);
    }

    end = clock();
    fillTime = (end-start)/(double)CLOCKS_PER_SEC;

    /* prints all the details of the table (-e argument)*/
    if (withE == 1) {
        htable_print_entire_table(h, stderr);
    }

    /* Checks the input file against a dictionary (-c <filename> argument)*/
    if (withC == 1) {
        /*open file and check if it is valid*/
        if (NULL == (infile = fopen(fileToBeChecked, "r"))) {
            fprintf(stderr, "Can't open file '%s' using mode r\n",
                    fileToBeChecked);
            htable_free(h);
            return EXIT_FAILURE;
        }
        start = clock();
        /*Get words from input file, and search for them in the dictionary*/
        while (getword(word, sizeof word, infile) != EOF) {
            /*If the word isn't in the dictionary*/
            if (htable_search(h, word) == 0) {
                printf("%s\n", word);
                unknownWords += 1;
            }
        }
        end = clock();
        fclose(infile);
        searchTime = (end-start)/(double)CLOCKS_PER_SEC;
        fprintf(stderr, "Fill time\t:%f\n", fillTime);
        fprintf(stderr, "Search time\t:%f\n", searchTime);
        fprintf(stderr, "Unknown words = %d\n", unknownWords);

        htable_free(h);
        return EXIT_SUCCESS;
    }


    /*Prints table stats (-p -s arguments)*/
    if (withP == 1 && withS == 0) {
        htable_print_stats(h, stdout, 10);
    } else if (withP == 1 && withS == 1) {
        htable_print_stats(h, stdout, numOfSnapshot);
    } else {
        htable_print(h, print_info);
    }

    htable_free(h);

    return EXIT_SUCCESS;
}