예제 #1
0
const char * string_set_add(const char * source_string, String_set * ss)
{
	char * str;
	size_t len;
	unsigned int p;
	
	assert(source_string != NULL, "STRING_SET: Can't insert a null string");

	p = find_place(source_string, ss);
	if (ss->table[p] != NULL) return ss->table[p];
	
	len = strlen(source_string);
#ifdef DEBUG
	/* Store the String_set structure address for debug verifications */
	len = ((len+1)&~(sizeof(ss)-1)) + 2*sizeof(ss);
	str = (char *) xalloc(len);
	*(String_set **)&str[len-sizeof(ss)] = ss;
#else
	str = (char *) xalloc(len+1);
#endif
	strcpy(str, source_string);
	ss->table[p] = str;
	ss->count++;
	
	/* We just added it to the table.
	   If the table got too big, we grow it.
	   Too big is defined as being more than 3/4 full */
	if ((4 * ss->count) > (3 * ss->size)) grow_table(ss);
	
	return str;
}
예제 #2
0
const char * string_set_lookup(const char * source_string, String_set * ss)
{
	int p;
	
	p = find_place(source_string, ss);
	return ss->table[p];
}
예제 #3
0
파일: solve.c 프로젝트: OoCharly/Fillit
int		place(int current)
{
	int	x;
	int	y;

	x = 0;
	y = 0;
	if (current == g_tetros.tot)
		return (1);
	while (x < (g_grid.len - 1) && y < (g_grid.len - 1))
	{
		if (!find_place(&x, &y, current))
		{
			return (0);
		}
		place_piece(current, x, y);
		if (place(current + 1))
			return (1);
		else
		{
			erase_piece(current, x, y);
			x = (++x == g_grid.len) ? 0 : x;
			y += (x == 0) ? 1 : 0;
		}
	}
	return (0);
}
예제 #4
0
파일: mds_net.c 프로젝트: GKosiba/core
void mds_add_copy(struct mds_net* net, struct mds* m, mds_id e,
    struct mds_copy c)
{
  struct mds_copies* cs;
  int t;
  int p;
  mds_id i;
  t = mds_type(e);
  i = mds_index(e);
  cs = mds_get_copies(net, e);
  if (cs) {
    p = find_place(cs, c.p);
    cs = realloc(cs, sizeof(struct mds_copies) +
        (cs->n) * sizeof(struct mds_copy));
/* insert sorted by moving greater items up by one */
    memmove(&cs->c[p + 1], &cs->c[p], (cs->n - p) * sizeof(struct mds_copy));
    cs->c[p] = c;
    ++cs->n;
    net->data[t][i] = cs;
  } else {
    cs = mds_make_copies(1);
    cs->c[0] = c;
    mds_set_copies(net, m, e, cs);
  }
}
예제 #5
0
int string_id_lookup(const char *source_string, String_id *ss)
{
	unsigned int p;

	p = find_place(source_string, ss);
	return (ss->table[p].str == NULL) ? -1 : ss->table[p].id;
}
예제 #6
0
int string_id_add(const char *source_string, String_id *ss)
{
	char * str;
	size_t len;
	unsigned int p;

	assert(source_string != NULL, "STRING_SET: Can't insert a null string");

	p = find_place(source_string, ss);
	if (ss->table[p].str != NULL) return ss->table[p].id;

	len = strlen(source_string);
	str = (char *) malloc(len+1);
	strcpy(str, source_string);
	ss->table[p].str = str;
	ss->table[p].id = ss->count;
	ss->count++;

	int keep_id = ss->table[p].id;

	/* We just added it to the table.  If the table got too big,
	 * we grow it.  Too big is defined as being more than 3/8 full.
	 * There's a huge boost from keeping this sparse. */
	if ((8 * ss->count) > (3 * ss->size)) grow_table(ss);

	return keep_id;
}
예제 #7
0
파일: hash.c 프로젝트: auduchinok/homework
int get_instr_num(Hash_Table *t, char *name)
{
	int key = find_place(t, name);

	if (t->table[key].name == NULL)
	{
		return -1;
	}

	return t->table[key].instr_num;
}
예제 #8
0
static int
resolve_conflict(struct da *da, int parent, unsigned char c)
{
  int left;
  struct resolve_stat rs;
  /* collect */
  collect_child(da, &rs, parent, c);
  /* find */
  left = find_place(da, &rs);
  /*printf("left=%d\n", left);*/
  /* move */
  move_children(da, &rs, parent, left);
  return left + c;
}
예제 #9
0
void grow_table(String_set *ss) {
    String_set old;
    int i, p;
    
    old = *ss;
    ss->size = next_prime_up(2 * old.size);  /* at least double the size */
    ss->table = (wchar_t **) xalloc(ss->size * sizeof(wchar_t *));
    ss->count = 0;
    for (i=0; i<ss->size; i++) ss->table[i] = NULL;
    for (i=0; i<old.size; i++) {
	if (old.table[i] != NULL) {
	    p = find_place(old.table[i], ss);
	    ss->table[p] = old.table[i];
	    ss->count++;
	}
    }
    /*wprintf_s("growing from %d to %d\n", old.size, ss->size);*/
    fflush(stdout);
    xfree((wchar_t *) old.table, old.size * sizeof(wchar_t *));
}
예제 #10
0
파일: hash.c 프로젝트: auduchinok/homework
void hash_add(Hash_Table *t, Label to_add)
{
	int size = t->size;
	Label *table = t->table;

	int key = find_place(t, to_add.name);

	if (table[key].name == NULL)
	{
		table[key] = to_add;
	}
	else
	{
		free(to_add.name);
	}
	
	if (t->elements >= 0.8 * size)
	{
		enlarge_table(t);
	}
}
예제 #11
0
wchar_t * string_set_add(wchar_t * source_string, String_set * ss) {
    wchar_t * str;
    int len, p;
    
    assert(source_string != NULL, L"STRING_SET: Can't insert a null string");

    p = find_place(source_string, ss);
    if (ss->table[p] != NULL) return ss->table[p];
    
    len = wcslen(source_string);
    str = (wchar_t *) xalloc(sizeof(wchar_t)*(len+1));
    wcscpy(str, source_string);
    ss->table[p] = str;
    ss->count++;
    
    /* We just added it to the table.
       If the table got too big, we grow it.
       Too big is defined as being more than 3/4 full */
    if ((4 * ss->count) > (3 * ss->size)) grow_table(ss);
    
    return str;
}
예제 #12
0
파일: string-set.c 프로젝트: dyne/AutOrg
static void grow_table(String_set *ss)
{
	String_set old;
	unsigned int i, p;
	
	old = *ss;
	ss->size = next_prime_up(2 * old.size);  /* at least double the size */
	ss->table = (char **) xalloc(ss->size * sizeof(char *));
	memset(ss->table, 0, ss->size*sizeof(char *));
	ss->count = 0;
	for (i=0; i<old.size; i++)
	{
		if (old.table[i] != NULL)
		{
			p = find_place(old.table[i], ss);
			ss->table[p] = old.table[i];
			ss->count++;
		}
	}
	/* printf("growing from %d to %d\n", old.size, ss->size); */
	/* fflush(stdout); */
	xfree((char *) old.table, old.size * sizeof(char *));
}
예제 #13
0
static void grow_table(String_id *ss)
{
	String_id old;
	size_t i;
	unsigned int p;

	old = *ss;
	ss->size = next_prime_up(3 * old.size);  /* at least triple the size */
	ss->table = (ss_id *)malloc(ss->size * sizeof(ss_id));
	memset(ss->table, 0, ss->size*sizeof(ss_id));
	ss->count = 0;
	for (i=0; i<old.size; i++)
	{
		if (old.table[i].str != NULL)
		{
			p = find_place(old.table[i].str, ss);
			ss->table[p] = old.table[i];
			ss->count++;
		}
	}
	/* printf("growing from %d to %d\n", old.size, ss->size); */
	/* fflush(stdout); */
	free(old.table);
}