int folders_init(void)
{
	int ret;
	const char *resources_dir = config_get(CONFIG_RESOURCES_DIR);
	const char *list_name;

	ULOGD("%s", __func__);

	list_name = "names";
	ret = load_words(resources_dir, list_name, &folders_names);
	if (ret < 0) {
		ULOGE("load_words %s: %s", list_name, strerror(-ret));
		return ret;
	}
	list_name = "adjectives";
	ret = load_words(resources_dir, list_name, &folders_adjectives);
	if (ret < 0) {
		ULOGE("load_words %s: %s", list_name, strerror(-ret));
		goto err;
	}

	return 0;
err:
	folders_cleanup();

	return ret;
}
Exemple #2
0
	ForthView() {
        Forth_test_stdwords(false);

        load_words(&fEnv, &fContext);

        fBM.setConfig(SkBitmap::kARGB_8888_Config, 640, 480);
        fBM.allocPixels();
        fBM.eraseColor(0);
        fContext.init(fBM);

        fEnv.parse(": mycolor ( x. y. -- x. y. ) OVER OVER f< IF #FFFF0000 ELSE #FF0000FF THEN setColor ;");
        fEnv.parse(": view.onClick ( x. y. -- ) mycolor 10. drawCircle ;");
        fOnClickWord = fEnv.findWord("view.onClick");
#if 0
        env.parse(
                  ": draw1 "
                  "10. setStrokeWidth 1 setStyle #FF000000 setColor "
                  "10. 20. 200. 100. drawLine "
                  "0 setStyle #80FF0000 setColor "
                  "50. 50. 250. 150. drawRect "
                  ";");
        env.parse("#FF0000FF drawColor "
                  "draw1 "
                  "150. 120. translate "
                  "draw1 "
                  );
#endif
    }
Exemple #3
0
/*
 * Play wheel of fortune using words loaded from a file.
 */
int main() {
    // Initialize wheel
    initialize_wheel();

    // Select random seed
    srandom(RANDOM_SEED);

    // Load words
    int numwords = 0;
    item_t *list_head = load_words(WORDS_FILE, &numwords);
    if (NULL == list_head) {
        printf("Failed to load words from %s\n", WORDS_FILE);
        return 1;
    }

    // Select a word
    char *word = choose_random_word(list_head, numwords);
    if (NULL == word) {
        printf("Failed to choose a word\n");
        return 1;
    }

    // Play game
    int winner = play_round(word);
    printf("Player %d solved the puzzle!\n", winner);

    // Clean up
    free_words(list_head);
}
Exemple #4
0
int
main(int argc, char **argv) {

	list<set<string> *>   *word_sets = load_words();
	list< vector<double> *> *targets = load_targets();

	train(word_sets,targets,0.5);


	/// get cases and report....

	return(0);
}
Exemple #5
0
int main(int argc, char **argv)
{
    const char *path = "/usr/share/dict/words";

    if (argc > 1)
        path = argv[1];

    std::cout << "Loading words from " << path << '\n';

    test_vector vec;
    std::vector<int> series;
    std::mt19937 rnd;
    test_map m;

    load_words(path, vec);
    assert(vec.size() >= 1000);
    std::cout << "Working with " << vec.size() << " items\n";

    std::sort(vec.begin(), vec.end());
    rnd.seed(5);

    int all_sum = 0;

    for (unsigned int i = 0; i < vec.size(); i++) {
        all_sum += vec[i].second;
        series.push_back(all_sum);
    }

    std::cout << "Inserting...\n";

    for (unsigned int i = 0; i < vec.size(); i++) {
        m.insert(vec[i]);
        range_test(rnd, m, vec, series, i + 1);
        assert(m.sum().sum == series[i]);
    }

    std::cout << "Sum: " << m.sum().sum << '\n';

    std::cout << "Removing...\n";

    for (int i = vec.size() - 1; i >= 0; i--) {
        assert(m.sum().sum == series[i]);
        m.erase(vec[i].first);
        range_test(rnd, m, vec, series, i);
    }

    assert(m.sum().sum == 0);
    return 0;
}
int main() {
    srandom(time(NULL));
    int num_words = 0;
    wordnode *words = load_words("/usr/share/dict/words", &num_words);
    if (num_words == 0) {
        printf("Didn't load any words?!\n");
        return 0;
    }
    printf("Words loaded: %d\n", num_words);
    const char *word = choose_random_word(words, num_words);
    printf("The random word is: %s\n", word);
    one_game(word);
    free_words(words);
    //printf("Evereyone is free!\n");
    return 0;
}
Exemple #7
0
int main(int argc, char **argv)
{
  parse_args(argc,argv);

  if (randseed == -1){
    randseed = 0; //important that all sites use the same rand seed
  }
  srandom(randseed);

  printf ("generating URLS\n");
  gen_urls();
  printf ("loading words\n");
  load_words();
  printf ("building zipfian\n");
  load_zipf();
  printf ("writing docs for site %d\n", siteno);
  gen_docs();

  return 0;
}
int					/* O - Exit status */
main(int  argc,				/* I - Number of command-line arguments */
     char *argv[])			/* I - Command-line arguments */
{
  int		i;			/* Looping var */
  cups_array_t	*array,			/* Test array */
		*dup_array;		/* Duplicate array */
  int		status;			/* Exit status */
  char		*text;			/* Text from array */
  char		word[256];		/* Word from file */
  double	start,			/* Start time */
		end;			/* End time */
  cups_dir_t	*dir;			/* Current directory */
  cups_dentry_t	*dent;			/* Directory entry */
  char		*saved[32];		/* Saved entries */
  void		*data;			/* User data for arrays */


 /*
  * No errors so far...
  */

  status = 0;

 /*
  * cupsArrayNew()
  */

  fputs("cupsArrayNew: ", stdout);

  data  = (void *)"testarray";
  array = cupsArrayNew((cups_array_func_t)strcmp, data);

  if (array)
    puts("PASS");
  else
  {
    puts("FAIL (returned NULL, expected pointer)");
    status ++;
  }

 /*
  * cupsArrayUserData()
  */

  fputs("cupsArrayUserData: ", stdout);
  if (cupsArrayUserData(array) == data)
    puts("PASS");
  else
  {
    printf("FAIL (returned %p instead of %p!)\n", cupsArrayUserData(array),
           data);
    status ++;
  }

 /*
  * cupsArrayAdd()
  */

  fputs("cupsArrayAdd: ", stdout);

  if (!cupsArrayAdd(array, strdup("One Fish")))
  {
    puts("FAIL (\"One Fish\")");
    status ++;
  }
  else
  {
    if (!cupsArrayAdd(array, strdup("Two Fish")))
    {
      puts("FAIL (\"Two Fish\")");
      status ++;
    }
    else
    {
      if (!cupsArrayAdd(array, strdup("Red Fish")))
      {
	puts("FAIL (\"Red Fish\")");
	status ++;
      }
      else
      {
        if (!cupsArrayAdd(array, strdup("Blue Fish")))
	{
	  puts("FAIL (\"Blue Fish\")");
	  status ++;
	}
	else
	  puts("PASS");
      }
    }
  }

 /*
  * cupsArrayCount()
  */

  fputs("cupsArrayCount: ", stdout);
  if (cupsArrayCount(array) == 4)
    puts("PASS");
  else
  {
    printf("FAIL (returned %d, expected 4)\n", cupsArrayCount(array));
    status ++;
  }

 /*
  * cupsArrayFirst()
  */

  fputs("cupsArrayFirst: ", stdout);
  if ((text = (char *)cupsArrayFirst(array)) != NULL &&
      !strcmp(text, "Blue Fish"))
    puts("PASS");
  else
  {
    printf("FAIL (returned \"%s\", expected \"Blue Fish\")\n", text);
    status ++;
  }

 /*
  * cupsArrayNext()
  */

  fputs("cupsArrayNext: ", stdout);
  if ((text = (char *)cupsArrayNext(array)) != NULL &&
      !strcmp(text, "One Fish"))
    puts("PASS");
  else
  {
    printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
    status ++;
  }

 /*
  * cupsArrayLast()
  */

  fputs("cupsArrayLast: ", stdout);
  if ((text = (char *)cupsArrayLast(array)) != NULL &&
      !strcmp(text, "Two Fish"))
    puts("PASS");
  else
  {
    printf("FAIL (returned \"%s\", expected \"Two Fish\")\n", text);
    status ++;
  }

 /*
  * cupsArrayPrev()
  */

  fputs("cupsArrayPrev: ", stdout);
  if ((text = (char *)cupsArrayPrev(array)) != NULL &&
      !strcmp(text, "Red Fish"))
    puts("PASS");
  else
  {
    printf("FAIL (returned \"%s\", expected \"Red Fish\")\n", text);
    status ++;
  }

 /*
  * cupsArrayFind()
  */

  fputs("cupsArrayFind: ", stdout);
  if ((text = (char *)cupsArrayFind(array, (void *)"One Fish")) != NULL &&
      !strcmp(text, "One Fish"))
    puts("PASS");
  else
  {
    printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
    status ++;
  }

 /*
  * cupsArrayCurrent()
  */

  fputs("cupsArrayCurrent: ", stdout);
  if ((text = (char *)cupsArrayCurrent(array)) != NULL &&
      !strcmp(text, "One Fish"))
    puts("PASS");
  else
  {
    printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
    status ++;
  }

 /*
  * cupsArrayDup()
  */

  fputs("cupsArrayDup: ", stdout);
  if ((dup_array = cupsArrayDup(array)) != NULL &&
      cupsArrayCount(dup_array) == 4)
    puts("PASS");
  else
  {
    printf("FAIL (returned %p with %d elements, expected pointer with 4 elements)\n",
           dup_array, cupsArrayCount(dup_array));
    status ++;
  }

 /*
  * cupsArrayRemove()
  */

  fputs("cupsArrayRemove: ", stdout);
  if (cupsArrayRemove(array, (void *)"One Fish") &&
      cupsArrayCount(array) == 3)
    puts("PASS");
  else
  {
    printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n",
           cupsArrayCount(array));
    status ++;
  }

 /*
  * cupsArrayClear()
  */

  fputs("cupsArrayClear: ", stdout);
  cupsArrayClear(array);
  if (cupsArrayCount(array) == 0)
    puts("PASS");
  else
  {
    printf("FAIL (%d elements, expected 0 elements)\n",
           cupsArrayCount(array));
    status ++;
  }

 /*
  * Now load this source file and grab all of the unique words...
  */

  fputs("Load unique words: ", stdout);
  fflush(stdout);

  start = get_seconds();

  if ((dir = cupsDirOpen(".")) == NULL)
  {
    puts("FAIL (cupsDirOpen failed)");
    status ++;
  }
  else
  {
    while ((dent = cupsDirRead(dir)) != NULL)
    {
      i = strlen(dent->filename) - 2;

      if (i > 0 && dent->filename[i] == '.' &&
          (dent->filename[i + 1] == 'c' ||
	   dent->filename[i + 1] == 'h'))
	load_words(dent->filename, array);
    }

    cupsDirClose(dir);

    end = get_seconds();

    printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array),
           end - start, cupsArrayCount(array) / (end - start));
    fflush(stdout);

    for (text = (char *)cupsArrayFirst(array); text;)
    {
     /*
      * Copy this word to the word buffer (safe because we strdup'd from
      * the same buffer in the first place... :)
      */

      strlcpy(word, text, sizeof(word));

     /*
      * Grab the next word and compare...
      */

      if ((text = (char *)cupsArrayNext(array)) == NULL)
	break;

      if (strcmp(word, text) >= 0)
	break;
    }

    if (text)
    {
      printf("FAIL (\"%s\" >= \"%s\"!)\n", word, text);
      status ++;
    }
    else
      puts("PASS");
  }

 /*
  * Test deleting with iteration...
  */

  fputs("Delete While Iterating: ", stdout);

  text = (char *)cupsArrayFirst(array);
  cupsArrayRemove(array, text);
  free(text);

  text = (char *)cupsArrayNext(array);
  if (!text)
  {
    puts("FAIL (cupsArrayNext returned NULL!)");
    status ++;
  }
  else
    puts("PASS");

 /*
  * Test save/restore...
  */

  fputs("cupsArraySave: ", stdout);

  for (i = 0, text = (char *)cupsArrayFirst(array);
       i < 32;
       i ++, text = (char *)cupsArrayNext(array))
  {
    saved[i] = text;

    if (!cupsArraySave(array))
      break;
  }

  if (i < 32)
    printf("FAIL (depth = %d)\n", i);
  else
    puts("PASS");

  fputs("cupsArrayRestore: ", stdout);

  while (i > 0)
  {
    i --;

    text = cupsArrayRestore(array);
    if (text != saved[i])
      break;
  }

  if (i)
    printf("FAIL (depth = %d)\n", i);
  else
    puts("PASS");

 /*
  * Delete the arrays...
  */

  cupsArrayDelete(array);
  cupsArrayDelete(dup_array);

 /*
  * Test the array with string functions...
  */

  fputs("_cupsArrayNewStrings(\" \\t\\nfoo bar\\tboo\\nfar\", ' '): ", stdout);
  array = _cupsArrayNewStrings(" \t\nfoo bar\tboo\nfar", ' ');
  if (!array)
  {
    status = 1;
    puts("FAIL (unable to create array)");
  }
  else if (cupsArrayCount(array) != 4)
  {
    status = 1;
    printf("FAIL (got %d elements, expected 4)\n", cupsArrayCount(array));
  }
  else if (strcmp(text = (char *)cupsArrayFirst(array), "bar"))
  {
    status = 1;
    printf("FAIL (first element \"%s\", expected \"bar\")\n", text);
  }
  else if (strcmp(text = (char *)cupsArrayNext(array), "boo"))
  {
    status = 1;
    printf("FAIL (first element \"%s\", expected \"boo\")\n", text);
  }
  else if (strcmp(text = (char *)cupsArrayNext(array), "far"))
  {
    status = 1;
    printf("FAIL (first element \"%s\", expected \"far\")\n", text);
  }
  else if (strcmp(text = (char *)cupsArrayNext(array), "foo"))
  {
    status = 1;
    printf("FAIL (first element \"%s\", expected \"foo\")\n", text);
  }
  else
    puts("PASS");

  fputs("_cupsArrayAddStrings(array, \"foo2,bar2\", ','): ", stdout);
  _cupsArrayAddStrings(array, "foo2,bar2", ',');

  if (cupsArrayCount(array) != 6)
  {
    status = 1;
    printf("FAIL (got %d elements, expected 6)\n", cupsArrayCount(array));
  }
  else if (strcmp(text = (char *)cupsArrayFirst(array), "bar"))
  {
    status = 1;
    printf("FAIL (first element \"%s\", expected \"bar\")\n", text);
  }
  else if (strcmp(text = (char *)cupsArrayNext(array), "bar2"))
  {
    status = 1;
    printf("FAIL (first element \"%s\", expected \"bar2\")\n", text);
  }
  else if (strcmp(text = (char *)cupsArrayNext(array), "boo"))
  {
    status = 1;
    printf("FAIL (first element \"%s\", expected \"boo\")\n", text);
  }
  else if (strcmp(text = (char *)cupsArrayNext(array), "far"))
  {
    status = 1;
    printf("FAIL (first element \"%s\", expected \"far\")\n", text);
  }
  else if (strcmp(text = (char *)cupsArrayNext(array), "foo"))
  {
    status = 1;
    printf("FAIL (first element \"%s\", expected \"foo\")\n", text);
  }
  else if (strcmp(text = (char *)cupsArrayNext(array), "foo2"))
  {
    status = 1;
    printf("FAIL (first element \"%s\", expected \"foo2\")\n", text);
  }
  else
    puts("PASS");

  cupsArrayDelete(array);

 /*
  * Summarize the results and return...
  */

  if (!status)
    puts("\nALL TESTS PASSED!");
  else
    printf("\n%d TEST(S) FAILED!\n", status);

  return (status);
}
Exemple #9
0
int
main(int argc, char **argv)
{
    char *dictionary = NULL;
    char *contains = NULL;
    struct word phrase, contain;
    struct bitfield *temp_bf;
    UChar *internal, *alphabet;
    struct wordlist *words = NULL, *stack = NULL, *w;
    int maxlen, minlen = 0;
    int maxwords = INT_MAX, maxtotal = INT_MAX;
    int opt, longindex;
    bool just_test = false, include_contains = false, just_words = false, just_anagram_words = false;
    const struct option long_opts[] = {
        {"contain", required_argument, 0, 'c'},
        {"dict", required_argument, 0, 'd'},
        {"length", required_argument, 0, 'l'},
        {"minimum", required_argument, 0, 'm'},
        {"number", required_argument, 0, 'n'},
        {"words", no_argument, 0, 'w'},
        {"help", no_argument, 0, 'h'},
        {"used", required_argument, 0, 'u'},
        {"test", required_argument, 0, 't'},
        {"version", no_argument, 0, 'v'},
        {0, 0, 0, 0}
    };

    memset(&phrase, 0, sizeof(phrase));
    memset(&contain, 0, sizeof(contain));

    /* parse arguments */

    while ((opt = getopt_long (argc, argv, "c:d:hl:m:n:t:u:vwW",
                               long_opts, &longindex)) != -1) {
        switch (opt) {
        case 'c':
            contains = safe_strdup(optarg);
            just_test = false;
            include_contains = true;
            break;
        case 'd':
            dictionary = safe_strdup(optarg);
            break;
        case 'h':
        case '?':
            print_help(argv[0]);
            exit(0);
        case 'l':
            maxwords = atoi(optarg);
            break;
        case 'm':
            minlen = atoi(optarg);
            break;
        case 'n':
            maxtotal = atoi(optarg);
            break;
        case 't':
            contains = safe_strdup(optarg);
            just_test = true;
            break;
        case 'u':
            contains = safe_strdup(optarg);
            include_contains = false;
            break;
        case 'v':
            printf("%s\n", VERSION);
            exit(0);
        case 'w':
            just_words = true;
            break;
        case 'W':
            just_anagram_words = true;
            break;
        default:
            fprintf(stderr, "Unexpected option %c\n", optopt);
            exit(99);
        }
    }

    if (argc - optind != 1) {
        fprintf(stderr, "%s: incorrect number of arguments\n", argv[0]);
        print_help(argv[0]);
        exit(1);
    }

    phrase.utf8_form = safe_strdup(argv[optind]);

    internal = utf8tointernal(phrase.utf8_form);

    maxlen = phrase.length = u_strlen(internal);

    alphabet = make_alphabet(internal);
    if (u_strlen(alphabet) > BITFIELD_MAX_LENGTH) {
        fprintf(stderr, "The phrase contains too many unique letters.\n");
        exit(1);
    }

    phrase.bits = make_bitfield(internal, alphabet);
    free(internal);

    if (contains) {
        contain.utf8_form = contains;
        internal = utf8tointernal(contains);
        contain.length = u_strlen(internal);
        contain.bits = make_bitfield(internal, alphabet);
        if ((contain.bits == NULL) || !bf_contains(phrase.bits, contain.bits)) {
            printf("Phrase '%s' does not contain '%s'\n",
                   phrase.utf8_form, contain.utf8_form);
            exit(1);
        }
        if (include_contains)
            stack = push_wordstack(stack, &contain);
        temp_bf = bf_subtract(phrase.bits, contain.bits);
        free_bitfield(phrase.bits);
        phrase.bits = temp_bf;
        maxlen -= contain.length;
        if (just_test) {
            if (maxlen == 0) {
                printf("%s can be made from %s\n",
                       contain.utf8_form,
                       phrase.utf8_form);
                exit(0);
            } else {
                printf("%s can not be made from %s\n",
                       contain.utf8_form,
                       phrase.utf8_form);
                exit(1);
            }
        }
    }

    if (dictionary == NULL)
        dictionary = safe_strdup(DEFAULT_DICT);

    load_words(&words, alphabet, phrase.bits, maxlen, minlen, dictionary);

    free(alphabet);

    if (just_words) {
        for (w = words; w != NULL; w = w->next) {
            /* printf(" %3d %s\n", w->word->length, w->word->utf8_form); */
            puts(w->word->utf8_form);
        }
    } else if (just_anagram_words) {
        find_anagram_words(words, stack, phrase.bits, maxwords, maxlen, &maxtotal);
    } else {
        find_anagrams(words, stack, phrase.bits, maxwords, maxlen, &maxtotal);
    }

    return 0;
}
int main(int argc, char *argv[])
{
	srand((unsigned)time(NULL));
	unsigned char filter[FILTER_SIZE_MAX];

	if (argc == 1)
		err("usage:\n"
		"  ./prog -hash some_word\n"
		"  ./prog -m_byte 3 -m_bit 12 -hash some_word\n"
		"  ./prog -d_file dict.txt -tr_files test.txt result.txt\n"
		"  ./prog -d_file dict.txt -tr_files test.txt result.txt -b_file data.bin\n"
		"  ./prog -show_options\n"
		);

	FILTER_SIZE = MAX_DB_SIZE - MAX_JS_SIZE;

	init_filter(filter);

	int i;
	for (i = 0; i < argc; i++) {
		if (!strcmp(argv[i], "-m_byte")) {
			MULTIPLIER_BYTE = atoi(argv[i + 1]);
		}

		if (!strcmp(argv[i], "-m_bit")) {
			MULTIPLIER_BIT = atoi(argv[i + 1]);
		}

		if (!strcmp(argv[i], "-max_db")) {
			MAX_DB_SIZE = atoi(argv[i + 1]);
			FILTER_SIZE = MAX_DB_SIZE - MAX_JS_SIZE;
		}

		if (!strcmp(argv[i], "-max_js")) {
			MAX_JS_SIZE = atoi(argv[i + 1]);
			FILTER_SIZE = MAX_DB_SIZE - MAX_JS_SIZE;
		}

		if (!strcmp(argv[i], "-filter_size")) {
			FILTER_SIZE = atoi(argv[i + 1]);
		}

		if (FILTER_SIZE <= 0) {
			err("FILTER_SIZE must be > 0\n");
		}

		if (!strcmp(argv[i], "-hash")) {
			unsigned int byte = hash_byte(argv[i + 1]);
			unsigned int bit = hash_bit(argv[i + 1]);
			printf("%s:%d:%d\n", argv[i + 1], byte, bit);
		}

		// dictionary file
		if (!strcmp(argv[i], "-d_file")) {
			load_words(filter, argv[i + 1]);
		}

		// test file, result file
		if (!strcmp(argv[i], "-tr_files")) {
			testing_words(filter, argv[i + 1], argv[i + 2]);
		}

		// binary file
		if (!strcmp(argv[i], "-b_file")) {
			save2bin_file(filter, argv[i + 1]);
		}

		if (!strcmp(argv[i], "-show_options")) {
			printf("MAX_DB_SIZE     = %d;\n", MAX_DB_SIZE);
			printf("MAX_JS_SIZE     = %d;\n", MAX_JS_SIZE);
			printf("FILTER_SIZE     = %d;\n", FILTER_SIZE);
			printf("MULTIPLIER_BYTE = %d;\n", MULTIPLIER_BYTE);
			printf("MULTIPLIER_BIT  = %d;\n", MULTIPLIER_BIT);
		}
	}

	return 0;
}
Exemple #11
0
int main(int argc, char **argv) {
    int num_threads = 1;
    int max_values_to_add = 10;
    int num_hash_buckets = 13;
    
    int c;
    while ((c = getopt(argc, argv, "t:m:s:h")) != -1) {
        switch(c) {
            case 't':
                num_threads = atoi(optarg);
                if (num_threads < 1 || num_threads > 100) {
                    usage(argv[0]);
                }
                break;
            case 'm':
                max_values_to_add = atoi(optarg);
                if (max_values_to_add < 1) {
                    usage(argv[0]);
                }
                break;
            case 's':
                num_hash_buckets = atoi(optarg);
                if (num_hash_buckets < 1) {
                    usage(argv[0]);
                }
                break;
            case 'h':
            default:
                usage(argv[0]);
                break;
        }
    }

    // see the RNG
    srandom(time(NULL));

    // set up thread arguments
    struct args thread_args;
    thread_args.hash = hashtable_new(num_hash_buckets);
    thread_args.max_values = max_values_to_add;
    thread_args.num_words = 0;
    thread_args.words = NULL;
    thread_args.done_adding = 0;
    pthread_mutex_init(&thread_args.done_adding_mutex, NULL);
    pthread_cond_init(&thread_args.done_adding_condv, NULL);

    // load up words from text file
    load_words("words.txt", &thread_args);

    // here are our threads...
    pthread_t threads[num_threads];
    int i = 0;

    // start up the threads; they'll start adding to the hashtable
    // immediately.
    for (i = 0; i < num_threads; i++) {
        if (0 > pthread_create(&threads[i], NULL, hasher_thread, (void*)&thread_args)) {
            fprintf(stderr, "Error creating thread: %s\n", strerror(errno));
        }
    }

    // do nothing in the main thread while worker
    // threads are adding to hashtable.  
    while (thread_args.done_adding < num_threads) {
        // sleep for half a second
        usleep(500000);
    }

    // threads are done adding - dump the hashtable
    printf("Dump of the hash table (which should be as full as it's gonna get).\n");
    hashtable_print(thread_args.hash);

    // signal worker threads to start removing from hashtable
    pthread_mutex_lock(&thread_args.done_adding_mutex);
    thread_args.done_adding = -1;
    pthread_mutex_unlock(&thread_args.done_adding_mutex);
    pthread_cond_broadcast(&thread_args.done_adding_condv);
    
    // wait for workers to complete
    for (i = 0; i < num_threads; i++) {
        pthread_join(threads[i], NULL);
    }
    
    printf("Dump of the hash table (which should be empty!)\n");
    hashtable_print(thread_args.hash);

    for (i = 0; i < thread_args.num_words; i++) {
        free(thread_args.words[i]);
    }
    free(thread_args.words);
    hashtable_free(thread_args.hash);
    exit(0);
}
Exemple #12
0
int main(int argc, char **argv)
{
    const char *path = "/usr/share/dict/words";

    if (argc > 1)
	path = argv[1];

    std::cout << "Loading words from " << path << '\n';

    test_vector vec;
    test_map m;

    load_words(path, vec, 1000);
    assert(vec.size() == 1000);
    check_absent(m, vec, 0, vec.size());

    std::cout << "Inserting...\n";
    shuffle(vec, 1);

    for (unsigned int i = 0; i < vec.size(); i++) {
	m.insert(vec[i]);
	assert(m.size() == i + 1);
	m.check();
    }

    check_present(m, vec, 0, vec.size());

    std::cout << "Removing some...\n";
    shuffle(vec, 2);

    for (int i = vec.size() - 1; i >= (signed)vec.size() / 2; i--) {
	m.erase(vec[i].first);
	assert(m.size() == (unsigned)i);
	m.check();
    }

    check_present(m, vec, 0, vec.size() / 2);
    check_absent(m, vec, vec.size() / 2, vec.size());

    std::cout << "Adding some...\n";

    for (unsigned int i = vec.size() / 2; i < vec.size(); i++) {
	m.insert(vec[i]);
	assert(m.size() == i + 1);
	m.check();
    }

    check_present(m, vec, 0, vec.size());

    std::cout << "Removing...\n";
    shuffle(vec, 3);

    for (int i = vec.size() - 1; i >= 0; i--) {
	m.erase(vec[i].first);
	assert(m.size() == (unsigned)i);
	m.check();
    }

    check_absent(m, vec, 0, vec.size());

    return 0;
}
Exemple #13
0
int
HashmapCount(int verbose, struct cfg *cfg, char *args[])
{
	struct test t;
	char *mem = NULL;
	int ret = 0, fd = 0, data;
	int useal = atoi(args[0]);
	struct stat st;
	void *mm;

	if (useal) {
		if ((mem = malloc(0xFFFFFF)) == NULL ||
				(t.al = suba_init(mem, 0xFFFFFF, 1, 0)) == NULL) {
			AMSG("");
			ret = -1;
			goto err;
		}
	} else {
		t.al = NULL;
	}
	if ((t.words = hashmap_new(hash_str, (cmp_fn)strcmp, NULL, t.al)) == NULL ||
				(fd = open(args[1], 0)) == -1 ||
				fstat(fd, &st) == -1) {
		AMSG("");
		ret = -1;
		goto err;
	}
	if ((t.src = mm = mmap(NULL, (int)st.st_size,
					PROT_READ | PROT_WRITE,
					MAP_PRIVATE,
					fd, 0)) == NULL) {
		AMSG("");
		ret = -1;
		goto err;
	}

	t.slim = t.src + st.st_size;
	if (load_words(&t) == -1) {
		AMSG("");
		ret = -1;
		goto err;
	}

	if ((data = (int)hashmap_get(t.words, args[2])) == 0 ||
			atoi(args[3]) != data ||
			(data = (int)hashmap_get(t.words, args[4])) == 0 ||
			atoi(args[5]) != data) {
		errno = ERANGE;
		PMNF(errno, ": %d != %d", atoi(args[3]), data);
		ret = -1;
		goto err;
	}

if (verbose)
	print_values(t.words);
err:
	munmap(mm, st.st_size);
	if (fd)
		close(fd);
	hashmap_del(t.words, NULL, NULL, NULL);
	free(mem);

	cfg = NULL;
	return ret;
}