Exemplo n.º 1
0
// Write the compact table
void CompactHashtableWriter::dump(SimpleCompactHashtable *cht, const char* table_name) {
  NumberSeq summary;
  allocate_table();
  dump_table(&summary);

  int table_bytes = _stats->bucket_bytes + _stats->hashentry_bytes;
  address base_address = address(MetaspaceShared::shared_rs()->base());
  cht->init(base_address,  _num_entries, _num_buckets,
            _compact_buckets->data(), _compact_entries->data());

  if (PrintSharedSpaces) {
    double avg_cost = 0.0;
    if (_num_entries > 0) {
      avg_cost = double(table_bytes)/double(_num_entries);
    }
    tty->print_cr("Shared %s table stats -------- base: " PTR_FORMAT,
                  table_name, (intptr_t)base_address);
    tty->print_cr("Number of entries       : %9d", _num_entries);
    tty->print_cr("Total bytes used        : %9d", table_bytes);
    tty->print_cr("Average bytes per entry : %9.3f", avg_cost);
    tty->print_cr("Average bucket size     : %9.3f", summary.avg());
    tty->print_cr("Variance of bucket size : %9.3f", summary.variance());
    tty->print_cr("Std. dev. of bucket size: %9.3f", summary.sd());
    tty->print_cr("Empty buckets           : %9d", _num_empty_buckets);
    tty->print_cr("Value_Only buckets      : %9d", _num_value_only_buckets);
    tty->print_cr("Other buckets           : %9d", _num_other_buckets);
  }
}
Exemplo n.º 2
0
void
rehash(tw_hash * hash_t, int pe)
{
	int             old_size;
	int             old_stored;
	int             i;
	tw_event      **old_list;

	old_stored = hash_t->num_stored[pe];
	old_list = hash_t->incoming[pe];
	old_size = hash_t->hash_sizes[pe];

	hash_t->num_stored[pe] = 0;
	hash_t->hash_sizes[pe] = next_prime(hash_t->hash_sizes[pe]);
	hash_t->incoming[pe] = allocate_table(hash_t->hash_sizes[pe]);

	for (i = 0; i < old_size; i++)
	{
		if (old_list[i] != NULL)
		{
			insert(hash_t->incoming[pe], old_list[i], hash_t->hash_sizes[pe]);
			(hash_t->num_stored[pe])++;
		}
	}

	if(old_stored != hash_t->num_stored[pe])
		tw_error(TW_LOC, "Did not rehash properly!");

#if VERIFY_HASH_QUAD
	printf("\nHASH TABLE RESIZED: old size = %d, new size = %d \n\n", old_size,
		   hash_t->hash_sizes[pe]);
#endif
}
Exemplo n.º 3
0
static struct convtable *
use_both_charmaps (struct charmap_t *from_charmap,
		   struct charmap_t *to_charmap)
{
  /* In this case we iterate over all the entries in the from_charmap,
     determine the internal name, and find an appropriate entry in the
     to_charmap (if it exists).  */
  struct convtable *rettbl = allocate_table ();
  void *ptr = NULL;
  const void *key;
  size_t keylen;
  void *data;

  while (iterate_table (&from_charmap->char_table, &ptr, &key, &keylen, &data)
	 >= 0)
    {
      struct charseq *in = (struct charseq *) data;
      struct charseq *out = charmap_find_value (to_charmap, key, keylen);

      if (out != NULL)
	add_bytes (rettbl, in, out);
    }

  return rettbl;
}
Exemplo n.º 4
0
static struct convtable *
use_from_charmap (struct charmap_t *from_charmap, const char *to_code)
{
  /* We iterate over all entries in the from_charmap and for those which
     have a known UCS4 representation we use an iconv() call to determine
     the mapping to the to_code charset.  */
  struct convtable *rettbl;
  iconv_t cd;
  void *ptr = NULL;
  const void *key;
  size_t keylen;
  void *data;

  cd = iconv_open (to_code, "WCHAR_T");
  if (cd == (iconv_t) -1)
    /* We cannot do anything.  */
    return NULL;

  rettbl = allocate_table ();

  while (iterate_table (&from_charmap->char_table, &ptr, &key, &keylen, &data)
	 >= 0)
    {
      struct charseq *in = data;
      struct charseq *newp = convert_charseq (cd, in);
      if (newp != NULL)
        add_bytes (rettbl, in, newp);
    }

  iconv_close (cd);

  return rettbl;
}
Exemplo n.º 5
0
int main () {
	ulong *f1;
	ulong *f2;
	ulong i, j, m; /* M is the number of variables in the boolean function */
	printf ("Enter the number of variables in your boolean function: ");
	scanf ("%lu", &m);
	f1 = allocate_table (m);
	f2 = allocate_table (m);
	ulong n = 1UL << m;
	for ( j = 0; j < 2; j++ ) {
		for ( i = 0; i < n; i++ ) {
			printf ("Enter the [%lu] boolean value of [%lu] boolean table: ", i, j);
			if ( j == 0) {
				scanf ("%lu", &f1[i]);
				if ( f1[i] != 0 && f1[i] != 1 ) {
					printf ("Program accepts only boolean values[0/1]\n");
					i = i - 1;
				}
			}
			else {
				scanf ("%lu", &f2[i]);
				if ( f2[i] != 0 && f2[i] != 1 ) {
					printf ("Program accepts only boolean values[0/1]\n");
					i = i - 1;
				}
			}
		}
	}
	printf ("\nTruth Table\n");
	printf ("f1\tf2\n");
	for ( i = 0; i < n; i++ ) {
		for ( j = 0; j < 2; j++ ) {
			if ( j == 0 )
				printf ("%lu\t", f1[i]);
			else 
				printf ("%lu\n", f2[i]);
		}
	}
	printf ("\nThe hamming distance of the two tables is %lu\n", hamming_distance(f1, f2, m));	
	free(f1);
	free(f2);
	return 0;
}
Exemplo n.º 6
0
void InliningDatabase::add_lookup_entry(LookupKey* outer, LookupKey* inner) {
  if (table_no * 2 >= table_size) {
    if (table == NULL) {
      allocate_table(4 * K);
    } else {
      // Expand table
      InliningDatabaseKey* old_table      = table;
      unsigned int         old_table_size = table_size;
      allocate_table(table_size * 2);
      for (unsigned int index = 0 ; index < old_table_size; index++) {
        if (old_table[index].is_filled())
	  add_lookup_entry(&old_table[index].outer, &old_table[index].inner);
      }
      FreeHeap(old_table);
    }
  }
  assert(table_no * 2 < table_size, "just checking density");
  
  unsigned int index = index_for(outer, inner);

  while (table[index].is_filled()) {
    if (table[index].equal(outer, inner)) return;
    index = next_index(index);
  }

  table[index].outer = *outer;
  if (inner) {
    table[index].inner = *inner;
  }
  table_no++;

  if (TraceInliningDatabase) {
    std->print_cr("InliningDatabase::add_lookup_entry @ %d", index);
    if (inner) {
      inner->print();
      std->print(" ");
    }
    outer->print();
    std->cr();
  }
}
Exemplo n.º 7
0
void           *
tw_hash_create()
{
#ifdef USE_AVL_TREE
  int i;

  g_tw_pe[0]->avl_tree_size = 0;

  g_tw_pe[0]->avl_list = (AvlTree)tw_calloc(TW_LOC, "avl tree", sizeof(struct avlNode), AVL_NODE_COUNT);

  for (i = 0; i < AVL_NODE_COUNT - 1; i++) {
    g_tw_pe[0]->avl_list[i].next = &g_tw_pe[0]->avl_list[i + 1];
  }
  g_tw_pe[0]->avl_list[i].next = NULL;

  g_tw_pe[0]->avl_list_head = &g_tw_pe[0]->avl_list[0];

  return NULL;
#else
	tw_hash        *h;
	unsigned int             pi;

	ncpu = tw_nnodes();
	h = (tw_hash *) tw_calloc(TW_LOC, "tw_hash", sizeof(tw_hash), 1);

	if (!h)
		tw_error(TW_LOC, "Cannot allocate tw_hash.");

	h->num_stored = (int *) tw_calloc(TW_LOC, "tw_hash", sizeof(int) * ncpu, 1);
	h->hash_sizes = (unsigned int *) tw_calloc(TW_LOC, "tw_hash", sizeof(int) * ncpu, 1);
	h->incoming = (tw_event ***) tw_calloc(TW_LOC, "tw_hash", sizeof(tw_event *)* ncpu, 1);

	if(!is_prime(g_tw_hash_size))
		g_tw_hash_size = next_prime(g_tw_hash_size);

	for (pi = 0; pi < ncpu; pi++)
	{
		h->num_stored[pi] = 0;
		h->hash_sizes[pi] = g_tw_hash_size;
		h->incoming[pi] = allocate_table(h->hash_sizes[pi]);
	}

	return (void *) h;
#endif
}
Exemplo n.º 8
0
/* Try to solve in solve_nr_remaining half-moves.
 * @param si slice index
 * @note assigns solve_result the length of solution found and written, i.e.:
 *            previous_move_is_illegal the move just played is illegal
 *            this_move_is_illegal     the move being played is illegal
 *            immobility_on_next_move  the moves just played led to an
 *                                     unintended immobility on the next move
 *            <=n+1 length of shortest solution found (n+1 only if in next
 *                                     branch)
 *            n+2 no solution found in this branch
 *            n+3 no solution found in next branch
 *            (with n denominating solve_nr_remaining)
 */
void exclusive_chess_exclusivity_detector_solve(slice_index si)
{
  TraceFunctionEntry(__func__);
  TraceFunctionParam("%u",si);
  TraceFunctionParamListEnd();

  assert(ply_horizon==maxply);
  ply_horizon = nbply+6;

  exclusive_chess_undecidable_continuations[nbply] = allocate_table();

  detect_exclusivity_and_solve_accordingly(si);

  free_table(exclusive_chess_undecidable_continuations[nbply]);

  TraceFunctionExit(__func__);
  TraceFunctionResultEnd();
}
Exemplo n.º 9
0
static struct convtable *
use_to_charmap (const char *from_code, struct charmap_t *to_charmap)
{
  /* We iterate over all entries in the to_charmap and for those which
     have a known UCS4 representation we use an iconv() call to determine
     the mapping to the from_code charset.  */
  struct convtable *rettbl;
  iconv_t cd;
  void *ptr = NULL;
  const void *key;
  size_t keylen;
  void *data;

  /* Note that the conversion we use here is the reverse direction.  Without
     exhaustive search we cannot figure out which input yields the UCS4
     character we are looking for.  Therefore we determine it the other
     way round.  */
  cd = iconv_open (from_code, "WCHAR_T");
  if (cd == (iconv_t) -1)
    /* We cannot do anything.  */
    return NULL;

  rettbl = allocate_table ();

  while (iterate_table (&to_charmap->char_table, &ptr, &key, &keylen, &data)
	 >= 0)
    {
      struct charseq *out = data;
      struct charseq *newp = convert_charseq (cd, out);
      if (newp != NULL)
        {
          add_bytes (rettbl, newp, out);
          free (newp);
        }
    }

  iconv_close (cd);

  return rettbl;
}
Exemplo n.º 10
0
/* Try to solve in solve_nr_remaining half-moves.
 * @param si slice index
 * @note assigns solve_result the length of solution found and written, i.e.:
 *            previous_move_is_illegal the move just played is illegal
 *            this_move_is_illegal     the move being played is illegal
 *            immobility_on_next_move  the moves just played led to an
 *                                     unintended immobility on the next move
 *            <=n+1 length of shortest solution found (n+1 only if in next
 *                                     branch)
 *            n+2 no solution found in this branch
 *            n+3 no solution found in next branch
 *            (with n denominating solve_nr_remaining)
 */
void exclusive_chess_nested_exclusivity_detector_solve(slice_index si)
{
  TraceFunctionEntry(__func__);
  TraceFunctionParam("%u",si);
  TraceFunctionParamListEnd();

  assert(ply_horizon<maxply);

  if (nbply>ply_horizon)
  {
    TraceText("stopping recursion");
    remember_previous_move_as_undecidable();
    solve_result = previous_move_is_illegal;
  }
  else
  {
    ply const save_ply_horizon = ply_horizon;

    exclusive_chess_undecidable_continuations[nbply] = allocate_table();

    detect_exclusivity_and_solve_accordingly(si);

    TraceValue("%u",nbply);
    TraceValue("%u",exclusive_chess_nr_continuations_reaching_goal[nbply]);
    TraceValue("%u",nr_decidable_continuations_not_reaching_goal[nbply]);
    TraceValue("%u\n",table_length(exclusive_chess_undecidable_continuations[nbply]));

    if (nr_decidable_continuations_not_reaching_goal[nbply]==0
        && exclusive_chess_nr_continuations_reaching_goal[nbply]<=1
        && table_length(exclusive_chess_undecidable_continuations[nbply])+exclusive_chess_nr_continuations_reaching_goal[nbply]>1)
      remember_previous_move_as_undecidable();

    free_table(exclusive_chess_undecidable_continuations[nbply]);

    ply_horizon = save_ply_horizon;
  }

  TraceFunctionExit(__func__);
  TraceFunctionResultEnd();
}
Exemplo n.º 11
0
void test_hash(int prime, int table_size, int elems_count) {
  struct HashTableParams params = GET_HASH_PARAMS(&prime, hash_func, 0, int);
  struct HashTable *table = allocate_table(table_size, params);
  for (int i = 0; i != elems_count; ++i) {
    for (int j = 0; j != i; ++j) {
      int value = j * j;
      assert(table_has(table, &value));
      assert(table_insert(table, &value) == 1);
    }

    for (int j = i; j != elems_count; ++j) {
      int value = j * j;
      assert(!table_has(table, &value));
      assert(!table_remove(table, &value));
    }

    int value = i * i;
    assert(!table_insert(table, &value));
  }

  for (int i = 0; i != elems_count; ++i) {
    for (int j = i; j != elems_count; ++j) {
      int value = j * j;
      assert(table_has(table, &value));
    }

    int value = i * i;
    assert(table_remove(table, &value));
    assert(!table_remove(table, &value));

    for (int j = 0; j != i; ++j) {
      int value = j * j;
      assert(!table_has(table, &value));
    }
  }

  deallocate_table(table);
}
Exemplo n.º 12
0
/* Try to solve in solve_nr_remaining half-moves.
 * @param si slice index
 * @note assigns solve_result the length of solution found and written, i.e.:
 *            previous_move_is_illegal the move just played is illegal
 *            this_move_is_illegal     the move being played is illegal
 *            immobility_on_next_move  the moves just played led to an
 *                                     unintended immobility on the next move
 *            <=n+1 length of shortest solution found (n+1 only if in next
 *                                     branch)
 *            n+2 no solution found in this branch
 *            n+3 no solution found in next branch
 *            (with n denominating solve_nr_remaining)
 */
void threat_solver_solve(slice_index si)
{
  TraceFunctionEntry(__func__);
  TraceFunctionParam("%u",si);
  TraceFunctionParamListEnd();

  threats[nbply] = allocate_table();

  if (!is_in_check(SLICE_STARTER(si)))
  {
    fork_solve_delegate(si);
    threat_lengths[nbply] = solve_result-1;
  }

  pipe_solve_delegate(si);

  free_table(threats[nbply]);
  threat_lengths[nbply] = no_threats_found;
  threats[nbply] = table_nil;

  TraceFunctionExit(__func__);
  TraceFunctionResultEnd();
}
Exemplo n.º 13
0
void select_init()
{
    read_handlers = allocate_table(init, key_from_fd, compare_fd);
    write_handlers = allocate_table(init, key_from_fd, compare_fd);
}
Exemplo n.º 14
0
int main(int argc, char *argv[])
{
    prog = argv[0];
    int ret = EXIT_SUCCESS;
    char cmdline[MAXLINE];                        /* command line */

    // File pointer:
    extern FILE *f_p;
    extern char *filename;
    extern char *startfile;

    extern char *prompt;

    extern int pr7_debug;
    pr7_debug = 0;

    // Getopt declarations:
    extern int optind;
    extern char *optarg;

    int i_flag = 0;
    int e_flag = 0;
    int s_flag = 0;
    extern int verbose; // This was initialized to 0 in cmpsc311.c
    int ch;

    // Getopt switch statement to locate command line options:
    optind = 1;
    while((ch = (getopt(argc, argv, ":hvdies:"))) != -1)
    {
        switch(ch)
        {
            case 'h':
                usage(ret);
                break;
            case 'v':
                verbose++;
                break;
            case 'd':
                pr7_debug++;
                break;
            case 'i':
                i_flag++;
                break;
            case 'e':
                e_flag++;
                break;
            case 's':
                s_flag++;
                startfile = Strdup(optarg);
                break;
            default:
                usage(EXIT_FAILURE);
                break;
        }
    }

    // start the signal handlers:
    install_signal_handler(SIGINT, SIGINT_handler);
    install_signal_handler(SIGCHLD, SIGCHLD_handler);

    // create the process table:
    process_table = allocate_table();

    if(s_flag > 0)
    {
        f_p = fopen(startfile, "r");
        if(f_p == NULL)
        {
            fprintf(stderr, "%s: Cannot open file %s: %s\n", prog, filename, strerror(errno));
            exit(EXIT_FAILURE);
        }
    }
    else                                          // open the default init file, if it exists
    {
        f_p = fopen("pr7.init", "r");
        if(f_p == NULL)
        {
            fprintf(stderr, "%s: Cannot open file %s: %s\n", prog, filename, strerror(errno));
            exit(EXIT_FAILURE);
        }
    }

    // STARTFILE LOOP:
    while(fgets(cmdline, MAXLINE, f_p) != NULL)
    {
    /* issue prompt and read command line */
        if (feof(stdin))                          /* end of file */
            { break; }

        if((ret = eval_line(cmdline, e_flag)) == EXIT_FAILURE)
            fprintf(stderr, "%s: failure in eval_line, on line %s. %s\n", prog, cmdline, strerror(errno));
    }

    fclose(f_p);

    // Now read from either a file or stdin

    // If a file is given, open it here
    if(argv[optind] != NULL)
    {
        // there is a filename given, so read from that only.
        f_p = fopen(argv[optind], "r");
        if(f_p == NULL)
        {
            fprintf(stderr, "%s: Cannot open file %s: %s\n", prog, filename, strerror(errno));
            exit(EXIT_FAILURE);
        }
        filename = argv[optind];
    }

    // Else, no file was given, so just read from stdin.
    else
    {
        f_p = stdin;
        filename = "[stdin]";
    }

    // MAIN PROGRAM LOOP:
    prompt = argv[0];
    if(f_p == stdin && i_flag > 0)
        printf("%s%% ", prompt);

    while(fgets(cmdline, MAXLINE, f_p) != NULL)
    {
    /* issue prompt and read command line */
        if (feof(stdin))                          /* end of file */
            { break; }

        if((ret = eval_line(cmdline, e_flag)) == EXIT_FAILURE)
            fprintf(stderr, "%s: failure in eval_line, on line %s. %s\n", prog, cmdline, strerror(errno));

        if(f_p == stdin && i_flag > 0)
            printf("%s%% ", prompt);
    }

    free(startfile);

    return ret;
}