Exemplo n.º 1
0
static int test3(void)
{
	struct test_key k;
	struct test_val v;
	struct hashtbl *h;
	h = hashtbl_create(ht_size,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   key_hash, key_equals,
			   NULL, NULL,
			   NULL, NULL);
	CUT_ASSERT_NOT_NULL(h);
	memset(&k, 0, sizeof(k));
	memset(&v, 0, sizeof(v));
	k.k = 3;
	v.v = 300;
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &k, &v));
	CUT_ASSERT_EQUAL(1, hashtbl_count(h));
	CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &k, &v));
	CUT_ASSERT_EQUAL(1, hashtbl_count(h));
	CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &k, &v));
	CUT_ASSERT_EQUAL(1, hashtbl_count(h));
	CUT_ASSERT_EQUAL(&v, hashtbl_lookup(h, &k));
	CUT_ASSERT_EQUAL(300, ((struct test_val *)hashtbl_lookup(h, &k))->v);
	CUT_ASSERT_TRUE(hashtbl_load_factor(h) > 0);
	hashtbl_clear(h);
	CUT_ASSERT_TRUE(hashtbl_load_factor(h) == 0.0);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	hashtbl_delete(h);
	return 0;
}
Exemplo n.º 2
0
//processes native helices if necessary; called by process_input
//returns id for i,j,k helix
int* process_native(int i, int j, int k) {
  int *id = NULL,l;
  char *key,tmp[ARRAYSIZE];

  for (l=1; l < k; l++) {
    sprintf(tmp,"%d %d",i+l,j-l);
    id = hashtbl_get(bp,tmp);
    if (id) {
      for (l-- ; l >= 0; l--) {
	sprintf(tmp,"%d %d",i+l,j+l);
	hashtbl_insert(bp,tmp,id);
      }
      return id;
    }
  }
  //printf("helix %d %d %d doesn't exist in sfold sample\n",i,j,k);
  id = malloc(sizeof(int));
  *id = hashtbl_numkeys(idhash)+1;
  hashtbl_insert(bp,tmp,id);
  sprintf(tmp,"%d",*id);
  key = malloc(sizeof(char)*ARRAYSIZE);
  sprintf(key,"%d %d %d",i,j,k);
  hashtbl_insert(idhash,tmp,key);
  return id;
}
Exemplo n.º 3
0
static int test6(void)
{
	int accumulator = 0;
	struct test_key k1, k2;
	struct test_val v1, v2;
	struct hashtbl *h;

	h = hashtbl_create(ht_size,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   key_hash, key_equals,
			   NULL, NULL,
			   NULL, NULL);

	CUT_ASSERT_NOT_NULL(h);

	memset(&k1, 0, sizeof(k1));
	memset(&k2, 0, sizeof(k2));
	memset(&v1, 0, sizeof(v1));
	memset(&v2, 0, sizeof(v2));

	k1.k = 3;
	v1.v = 300;
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &k1, &v1));
	CUT_ASSERT_EQUAL(1, hashtbl_count(h));
	CUT_ASSERT_EQUAL(&v1, hashtbl_lookup(h, &k1));
	CUT_ASSERT_EQUAL(300, ((struct test_val *)hashtbl_lookup(h, &k1))->v);
	CUT_ASSERT_EQUAL(1, hashtbl_count(h));

	k2.k = 4;
	v2.v = 400;
	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &k2, &v2));
	CUT_ASSERT_EQUAL(2, hashtbl_count(h));
	CUT_ASSERT_EQUAL(&v2, hashtbl_lookup(h, &k2));
	CUT_ASSERT_EQUAL(400, ((struct test_val *)hashtbl_lookup(h, &k2))->v);

	CUT_ASSERT_EQUAL(2, hashtbl_apply(h, test6_apply_fn1, &accumulator));
	CUT_ASSERT_EQUAL(700, accumulator);

	CUT_ASSERT_EQUAL(1, hashtbl_apply(h, test6_apply_fn2, &accumulator));
	CUT_ASSERT_EQUAL(1400, accumulator);

	hashtbl_clear(h);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));

	hashtbl_delete(h);
	return 0;
}
Exemplo n.º 4
0
/* Store user defined numerical formats ie. FORMAT records */
void wbook_store_all_num_formats(struct wbookctx *wbook)
{
  int index = 164;  /* Start from 0xA4 */
  struct htbl *num_formats;
  int key;
  int i;

  num_formats = hashtbl_new(1);

  /* User defined formats */
  for (i = 0; i < wbook->formatcount; i++) {
    int data;
    if (wbook->formats[i]->num_format_str == NULL)
      continue;
    key = fmt_gethash(wbook->formats[i]);
    data = hashtbl_get(num_formats, key);
    if (data >= 0) {
      /* FONT has already been used */
      wbook->formats[i]->num_format = data;
    } else {
      /* Add a new FONT record */
      hashtbl_insert(num_formats, key, index);
      wbook->formats[i]->num_format = index;
      wbook_store_num_format(wbook, wbook->formats[i]->num_format_str, index);
      index++;
    }
  }
  hashtbl_destroy(num_formats);
}
Exemplo n.º 5
0
static int test21(void)
{
	unsigned int i;
	long long int keys[] = { 1LL << 32, 1LL << 33, 1LL << 34 };
	long long int values[] = { 1LL << 35, 1LL << 36, 1LL << 37 };
	struct hashtbl *h;
	h = hashtbl_create(ht_size,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_int64_hash, hashtbl_int64_equals,
			   NULL, NULL,
			   NULL, NULL);
	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	for (i = 0; i < NELEMENTS(keys); i++) {
		long long int x = keys[i];
		CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &keys[i], &values[i]));
		CUT_ASSERT_NOT_NULL(hashtbl_lookup(h, &x));
		CUT_ASSERT_EQUAL(values[i], *(long long *)hashtbl_lookup(h, &x));
	}
	CUT_ASSERT_EQUAL(NELEMENTS(keys), hashtbl_count(h));
	for (i = 0; i < NELEMENTS(keys); i++) {
		long long x = keys[i];
		CUT_ASSERT_EQUAL(0, hashtbl_remove(h, &x));
	}
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	hashtbl_clear(h);
	hashtbl_delete(h);
	return 0;
}
Exemplo n.º 6
0
static int test12(void)
{
	unsigned int i;
	char *keys[]   = { "100", "200", "300" };
	char *values[] = { "100", "200", "300" };
	struct hashtbl *h;

	h = hashtbl_create(ht_size,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_string_hash, hashtbl_string_equals,
			   NULL, NULL,
			   NULL, NULL);

	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));

	for (i = 0; i < NELEMENTS(keys); i++) {
		CUT_ASSERT_EQUAL(0, hashtbl_insert(h, keys[i], values[i]));
		CUT_ASSERT_NOT_NULL(hashtbl_lookup(h, keys[i]));
		CUT_ASSERT_EQUAL(values[i], hashtbl_lookup(h, keys[i]));
		CUT_ASSERT_TRUE(STREQ(keys[i], (char *)hashtbl_lookup(h, keys[i])));
		CUT_ASSERT_TRUE(STREQ(values[i], (char *)hashtbl_lookup(h, keys[i])));
	}

	hashtbl_delete(h);
	return 0;
}
Exemplo n.º 7
0
static int test11(void)
{
	unsigned int i;
	int keys[] = { 100, 200, 300 };
	int values[] = { 1000, 2000, 3000 };
	struct hashtbl *h;
	h = hashtbl_create(ht_size,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_int_hash, hashtbl_int_equals,
			   NULL, NULL,
			   NULL, NULL);
	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	for (i = 0; i < NELEMENTS(keys); i++) {
		int x = keys[i];
		CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &keys[i], &values[i]));
		CUT_ASSERT_NOT_NULL(hashtbl_lookup(h, &x));
		CUT_ASSERT_EQUAL(values[i], *(int *)hashtbl_lookup(h, &x));
	}
	CUT_ASSERT_EQUAL(NELEMENTS(keys), hashtbl_count(h));
	for (i = 0; i < NELEMENTS(keys); i++) {
		int x = keys[i];
		CUT_ASSERT_EQUAL(0, hashtbl_remove(h, &x));
	}
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	hashtbl_clear(h);
	hashtbl_delete(h);
	return 0;
}
Exemplo n.º 8
0
int hashtbl_resize(HASHTBL *hashtbl, hash_size size)
{
	printf("RESIZE");
	HASHTBL newtbl;
	hash_size n;
	struct hashnode_s *node, *nextnode;

	newtbl.size = size;
	newtbl.hashfunc = hashtbl->hashfunc;

	if (!(newtbl.nodes = calloc(size, sizeof(struct hashnode_s*)))) return -1;

	for (n = 0; n<hashtbl->size; ++n) {
		for (node = hashtbl->nodes[n]; node; node = nextnode) {
			nextnode = node->next;
			hashtbl_insert(&newtbl, node->key, node->data);
			hashtbl_remove(hashtbl, node->key);
		}
	}

	free(hashtbl->nodes);
	hashtbl->size = newtbl.size;
	hashtbl->nodes = newtbl.nodes;

	return 0;
}
Exemplo n.º 9
0
//finds helical difference between profile -> origprof
//k1 and k2 are num of helices in profile and origprof
//return difference with triplets
char* find_diff(HASHTBL *hash,char *profile, char *origprof, int *k1, int *k2) {
  int size = INIT_SIZE,b1,b2,xor,i;
  char *diff,*id;

  diff = malloc(sizeof(char)*ARRAYSIZE*size);
  diff[0] = '\0';
  b1 = make_binary(profile,k1);
  b2 = make_binary(origprof,k2);
  xor = b1 ^ b2;
  //printf("b1 is %d, b2 is %d, and xor is %d\n",b1,b2,xor);
  for (i = 0; xor > 0; xor>>=1,i++) {
    if ((xor & 1)==1) {
      hashtbl_insert(hash,table[i],"1");
      id = hashtbl_get(idhash,table[i]);
      
      if (strlen(diff)+strlen(table[i])+strlen(id)+6 > ARRAYSIZE*size)
	diff = resize(&size,strlen(diff)+strlen(table[i])+strlen(id)+6,diff);
      if (strlen(diff)>1)
	strcat(diff,"\\n");
      sprintf(diff,"%s%s: %s",diff,table[i],id);
    }
  }
  //printf("diff is %s between %s and %s\n",diff,origprof,profile);

  return diff;
}
Exemplo n.º 10
0
static int test15(void)
{
	int i;
	struct hashtbl *h;
	
	h = hashtbl_create(ht_size,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_direct_hash, hashtbl_direct_equals,
			   NULL, NULL,
			   NULL, NULL);
	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));

	for (i = 0; i < (1 << TEST15_N); i++) {
		int *k = &test15_bigtable[i];
		test15_bigtable[i] = i;
		CUT_ASSERT_EQUAL(0, hashtbl_insert(h, k, k));
		CUT_ASSERT_NOT_NULL(hashtbl_lookup(h, k));
	}

	/* Iteration order should reflect insertion order. */

	for (i = 0; i < (1 << TEST15_N); i++) {
		int *k = &test15_bigtable[i];
		CUT_ASSERT_EQUAL(0, hashtbl_remove(h, k));
	}

	hashtbl_delete(h);
	return 0;
}
Exemplo n.º 11
0
//inserts key into graph and converts from rep struct to binary notation
long insert_and_binary(char *key,char *profile,int freq) {
  char *blank = " ",*helix,*copy = strdup(key);
  unsigned long sum = 0;
  int i,length = atoi(strtok(copy,blank)),*val;
  HASHTBL *hash = graph[length-1];

  profile[0] = '\0';
  for (i = 0; i < length; i++) {
    helix = strtok(NULL,blank);
    strcat(profile,helix);
    strcat(profile," ");
    sum += *((long*) hashtbl_get(binary,helix));
    //printf("sum is now %d\n",sum);
  }
  //printf("profile is %s\n",profile);
  if (!hash) {
    if (!(hash = hashtbl_create(HASHSIZE,NULL))) {
      fprintf(stderr, "ERROR: hashtbl_create() failed");
      exit(EXIT_FAILURE);
    }
    graph[length-1] = hash;
  }
  val = malloc(sizeof(int));
  *val = freq;
  hashtbl_insert(hash,profile,val);
  //printf("profile %sinserted with length %d and freq %d\n",profile,length,*val);
  return sum;
}
Exemplo n.º 12
0
void check_functions(void) {
  long double *a = malloc(sizeof(long double));
  *a = 46;
  hashtbl_insert(vars, "a", a);

  ASSERT_EQ(evaluate("max(10, 20, 12, 15)"), 20);
  ASSERT_EQ(evaluate("min(11, 21, 15, 25)"), 11);
  ASSERT_EQ(evaluate("sum(1, 2, 3, 4, 5, 6)"), 21);
  ASSERT_EQ(evaluate("avg(3.4, 4e-2, 3.5, a)"), 13.235);
  ASSERT_EQ(evaluate("abs(-34)"), 34);

  *a = evaluate("random()");
  ASSERT_EQ(evaluate("cos(a)**2 + sin(a)**2"), 1.0);
  ASSERT_EQ(evaluate("sin(phi)/cos(phi) - tan(phi)"), 0.0);
  ASSERT_EQ(evaluate("1 + tan(a)**2 - 1/cos(a)**2"), 0.0);

  ASSERT_EQ(evaluate("atan2(a, phi) == atan(a/phi)"), 1);
  ASSERT_EQ(evaluate("acos(cos(phi)) - phi"), 0.0);
  ASSERT_EQ(evaluate("asin(sin(phi/4)) == phi/4"), 1);

  ASSERT_EQ(evaluate("log(exp(3))"), 3);
  ASSERT_EPS(evaluate("log(234 * 4234) - log(234) - log(4234)"), 0.0, 1.0e-5);

  ASSERT_EQ(roundl(evaluate("gamma(16)")), 1307674368000);
}
Exemplo n.º 13
0
static int test8(void)
{
	struct hashtbl *h;
	struct test_key *k = malloc(sizeof(struct test_key));
	struct test_val *v1 = malloc(sizeof(struct test_val));
	struct test_val *v2 = malloc(sizeof(struct test_val));

	h = hashtbl_create(ht_size,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   key_hash, key_equals,
			   free, free,
			   NULL, NULL);

	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_NOT_NULL(k);
	CUT_ASSERT_NOT_NULL(v1);
	CUT_ASSERT_NOT_NULL(v2);

	memset(k, 0, sizeof(*k));
	memset(v1, 0, sizeof(*v1));
	memset(v2, 0, sizeof(*v2));

	k->k = 3;
	v1->v = 300;
	v2->v = 600;

	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	CUT_ASSERT_EQUAL(0, hashtbl_insert(h, k, v1));
	CUT_ASSERT_EQUAL(1, hashtbl_count(h));
	CUT_ASSERT_EQUAL(v1, hashtbl_lookup(h, k));
	CUT_ASSERT_EQUAL(300, ((struct test_val *)hashtbl_lookup(h, k))->v);

	/* Replace value for same key. */
	CUT_ASSERT_EQUAL(0, hashtbl_insert(h, k, v2));
	CUT_ASSERT_EQUAL(1, hashtbl_count(h));
	CUT_ASSERT_EQUAL(v2, hashtbl_lookup(h, k));
	CUT_ASSERT_EQUAL(600, ((struct test_val *)hashtbl_lookup(h, k))->v);

	CUT_ASSERT_EQUAL(0, hashtbl_remove(h, k));
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	hashtbl_clear(h);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	hashtbl_delete(h);
	return 0;
}
Exemplo n.º 14
0
int main() {
	HASHTBL *hashtbl;
	char *spain, *italy;
	char str[] = "GfG";
	printf("%p \n",str);
	printf("str= %s \n",str);
	change(str);

	if (!(hashtbl = hashtbl_create(16, NULL))) {
		fprintf(stderr, "ERROR: hashtbl_create() failed\n");
		exit(EXIT_FAILURE);
	}
	hashtbl_insert(hashtbl, "France", "Paris");
	hashtbl_insert(hashtbl, "England", "London");
	hashtbl_insert(hashtbl, "Sweden", "Stockholm");
	hashtbl_insert(hashtbl, "Germany", "Berlin");
	hashtbl_insert(hashtbl, "Norway", "Oslo");
	hashtbl_insert(hashtbl, "Italy", "Rome");
	hashtbl_insert(hashtbl, "Spain", "Madrid");
	hashtbl_insert(hashtbl, "Estonia", "Tallinn");
	hashtbl_insert(hashtbl, "Netherlands", "Amsterdam");
	hashtbl_insert(hashtbl, "Ireland", "Dublin");


	printf("After insert:\n");
	italy = hashtbl_get(hashtbl, "Italy");
	printf("Italy: %s\n", italy ? italy : "-");
	spain = hashtbl_get(hashtbl, "Spain");
	printf("Spain: %s\n", spain ? spain : "-");

	hashtbl_remove(hashtbl, "Spain");

	printf("After remove:\n");
	spain = hashtbl_get(hashtbl, "Spain");
	printf("Spain: %s\n", spain ? spain : "-");

	hashtbl_resize(hashtbl, 8);

	printf("After resize:\n");
	italy = hashtbl_get(hashtbl, "Italy");
	printf("Italy: %s\n", italy ? italy : "-");

	hashtbl_destroy(hashtbl);

	return 0;
}
Exemplo n.º 15
0
static int test9(void)
{
	int test9_max = 100;
	struct hashtbl *h;
	int i;
	h = hashtbl_create(ht_size,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   key_hash, key_equals,
			   free, free,
			   NULL, NULL);
	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));

	for (i = 0; i < test9_max; i++) {
		struct test_key *k = malloc(sizeof(struct test_key));
		struct test_val *v = malloc(sizeof(struct test_val));
		CUT_ASSERT_NOT_NULL(k);
		CUT_ASSERT_NOT_NULL(v);
		memset(k, 0, sizeof(*k));
		memset(v, 0, sizeof(*v));
		k->k = i;
		v->v = i + test9_max;
		CUT_ASSERT_EQUAL(0, hashtbl_insert(h, k, v));
		CUT_ASSERT_EQUAL(i + 1, (int) hashtbl_count(h));
		CUT_ASSERT_EQUAL(i + test9_max,
				 ((struct test_val *)hashtbl_lookup(h, k))->v);
	}

	hashtbl_apply(h, test9_apply_fn1, &test9_max);

	for (i = 0; i < test9_max; i++) {
		struct test_key k;
		struct test_val *v;
		memset(&k, 0, sizeof(k));
		k.k = i;
		v = hashtbl_lookup(h, &k);
		CUT_ASSERT_NOT_NULL(v);
		CUT_ASSERT_EQUAL(i + test9_max, v->v);
	}

	for (i = 99; i >= 0; i--) {
		struct test_key k;
		struct test_val *v;
		memset(&k, 0, sizeof(k));
		k.k = i;
		v = hashtbl_lookup(h, &k);
		CUT_ASSERT_NOT_NULL(v);
		CUT_ASSERT_EQUAL(v->v - test9_max, i);
	}

	hashtbl_clear(h);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	hashtbl_delete(h);

	return 0;
}
Exemplo n.º 16
0
void check_longer(void) {
  ASSERT_EQ(evaluate("5.23e+3**4**-2 + avg(34>>2, phi < pi, max(phi, pi, 3.2))"), 5.774346129827);
  long double *x = malloc(sizeof(long double));
  hashtbl_insert(vars, "x", x);
  *x = 3.0;
  ASSERT_EQ(evaluate("e**tan(x)/(1+x**2)*sin((1+log(x)**2)**0.5)"), 0.0864000589547301);
  *x = 17.25;
  ASSERT_EPS(evaluate("e**tan(x)/(1+x**2)*sin((1+log(x)**2)**0.5)"), 514696792827.659, 1.0e-3);
}
Exemplo n.º 17
0
int _oph_query_parser_load_query_params(const char *query_string, HASHTBL *hashtbl)
{
  if (!query_string || !hashtbl){
    pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_QUERY_ENGINE_LOG_NULL_INPUT_PARAM);
  	logging(LOG_ERROR, __FILE__, __LINE__,OPH_QUERY_ENGINE_LOG_NULL_INPUT_PARAM);    
    return OPH_QUERY_ENGINE_NULL_PARAM;
  }

  const char *ptr_begin, *ptr_equal, *ptr_end;

  ptr_begin = query_string;
  ptr_equal = strchr(query_string, OPH_QUERY_ENGINE_LANG_VALUE_SEPARATOR);
  ptr_end = strchr(query_string, OPH_QUERY_ENGINE_LANG_PARAM_SEPARATOR);

  char param[OPH_QUERY_ENGINE_LANG_LEN] = {'\0'};
  char value[OPH_QUERY_ENGINE_LANG_LEN] = {'\0'};
  char *real_val = NULL;

  while(ptr_end)
  {
    if(!ptr_begin || !ptr_equal || !ptr_end ){
      pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_QUERY_ENGINE_LOG_QUERY_PARSING_ERROR, query_string);
    	logging(LOG_ERROR, __FILE__, __LINE__, OPH_QUERY_ENGINE_LOG_QUERY_PARSING_ERROR, query_string);   
		  return OPH_QUERY_ENGINE_PARSE_ERROR;
    }

    if(ptr_end < ptr_equal){
      pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_QUERY_ENGINE_LOG_QUERY_PARSING_ERROR, query_string);
    	logging(LOG_ERROR, __FILE__, __LINE__, OPH_QUERY_ENGINE_LOG_QUERY_PARSING_ERROR, query_string);    
		  return OPH_QUERY_ENGINE_PARSE_ERROR;
    }

    strncpy(param,ptr_begin, strlen(ptr_begin) - strlen(ptr_equal));
	  param[strlen(ptr_begin) - strlen(ptr_equal)] = 0;	
   
    strncpy(value,ptr_equal + 1, strlen(ptr_equal + 1) - strlen(ptr_end));
	  value[strlen(ptr_equal + 1) - strlen(ptr_end)] = 0;	

	  real_val = (char*)strndup(value, (strlen(value) + 1)*sizeof(char));
	  if(real_val == NULL)
	  {
      pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_QUERY_ENGINE_LOG_MEMORY_ALLOC_ERROR);
		  logging(LOG_ERROR, __FILE__, __LINE__, OPH_QUERY_ENGINE_LOG_MEMORY_ALLOC_ERROR);
      return OPH_QUERY_ENGINE_MEMORY_ERROR;
    }

    hashtbl_insert(hashtbl, (char *)param, (char *)real_val);
    real_val = NULL;
    ptr_begin = ptr_end + 1;
    ptr_equal = strchr(ptr_end + 1, OPH_QUERY_ENGINE_LANG_VALUE_SEPARATOR);
    ptr_end = strchr(ptr_end + 1, OPH_QUERY_ENGINE_LANG_PARAM_SEPARATOR);
  }

  return OPH_QUERY_ENGINE_SUCCESS;
}
Exemplo n.º 18
0
/* Write all FONT records. */
void wbook_store_all_fonts(struct wbookctx *wbook)
{
  int i;
  struct pkt *font;
  struct htbl *fonts;
  int key;
  int index;

  font = fmt_get_font(wbook->tmp_format);
  for (i = 1; i < 6; i++) {
    bw_append(wbook->biff, font->data, font->len);
  }
  pkt_free(font);

  fonts = hashtbl_new(wbook->formatcount + 1); /* For tmp_format */
  index = 6;  /* First user defined FONT */

  key = fmt_gethash(wbook->tmp_format);
  hashtbl_insert(fonts, key, 0);  /* Index of the default font */

  /* User defined fonts */
  for (i = 0; i < wbook->formatcount; i++) {
    int data;
    key = fmt_gethash(wbook->formats[i]);
    data = hashtbl_get(fonts, key);
    if (data >= 0) {
      /* FONT has already been used */
      wbook->formats[i]->font_index = data;
    } else {
      /* Add a new FONT record */
      hashtbl_insert(fonts, key, index);
      wbook->formats[i]->font_index = index;
      index++;
      font = fmt_get_font(wbook->formats[i]);
      bw_append(wbook->biff, font->data, font->len);
      pkt_free(font);
    }
  }

  hashtbl_destroy(fonts);
}
Exemplo n.º 19
0
static int test19(void)
{
	struct hashtbl *h;
	static int keys[] = { 100, 200, 300, 400, 500, 600 };
	
	h = hashtbl_create(ht_size,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_direct_hash, hashtbl_direct_equals,
			   NULL, NULL,
			   test19_malloc, test19_free);

	CUT_ASSERT_NOT_NULL(h);

	CUT_ASSERT_EQUAL(1, hashtbl_insert(h, &keys[0], &keys[0]));
	CUT_ASSERT_EQUAL(1, hashtbl_insert(h, &keys[1], &keys[1]));
	CUT_ASSERT_EQUAL(1, hashtbl_insert(h, &keys[2], &keys[2]));
	CUT_ASSERT_EQUAL(1, hashtbl_insert(h, &keys[3], &keys[3]));
	CUT_ASSERT_EQUAL(1, hashtbl_insert(h, &keys[4], &keys[4]));
	CUT_ASSERT_EQUAL(1, hashtbl_insert(h, &keys[5], &keys[5]));
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));

	hashtbl_delete(h);
	return 0;
}
Exemplo n.º 20
0
//inserts profile into graph, updates freq if already exists
//saves LCA into linked list with num = binary rep
//s = number of helices in profile
struct hashnode_s* insert_LCA(char *profile,int num,int s,int frq) {
  int size,*val,*data;
  char *key;
  HASHTBL *hash = NULL;
  struct hashnode_s *temp = NULL;
  //KEY *node;

  hash = graph[s-1];
  if (!hash) {
    if (!(hash = hashtbl_create(HASHSIZE,NULL))) {
      fprintf(stderr, "ERROR: hashtbl_create() failed");
      exit(EXIT_FAILURE);
    }
    graph[s-1] = hash;
  }

  val = hashtbl_get(hash,profile);
  if (!val) {
    val = malloc(sizeof(int));
    *val = frq;
    hashtbl_insert(hash,profile,val);
    //printf("inserted %s into graph\n",profile);
    //if LCA isn't an original, save; will have to compare them later
    //below assumes s is double digits or less
    for (size = 1; size < strlen(profile)+3; size++) ;
    key = malloc(sizeof(char)*ARRAYSIZE*size);
    sprintf(key,"%d %s",s,profile);
    if (!hashtbl_get(cluster,key)) {
      //printf("%s as new vertex with bit %d\n",profile,num);print_vertices(fp
      temp = malloc(sizeof(struct hashnode_s));

      data = malloc(sizeof(int));
      *data = num;
      temp->key = profile;
      temp->data = data;

      //for (;temp;temp = temp->next)
      //printf("node is %s with bit %d\n",temp->key,*((int*)temp->data));
    }
    free(key);
  } else if (*val < frq) {
    //printf("profile %s inserted with freq %d, old freq %d\n",profile,frq,*val);
    *val = frq;
  }
  return temp;
}
Exemplo n.º 21
0
//diff contains helix nums that are inserted into a hash
//to be used before edge_label
//add triplet info to diff
char* insert_diff(HASHTBL *temp,char *diff) {
  int size = INIT_SIZE;
  char *blank = " ", *copy,*k,*val;

  if (strlen(diff) == 0) return "";
  copy = malloc(sizeof(char)*ARRAYSIZE*size);
  copy[0] = '\0';
  for (k = strtok(diff,blank); k; k = strtok(NULL,blank)) {
    hashtbl_insert(temp,k,"1");
    //printf("insert_diff: inserting %s into hash\n",k);
    val = hashtbl_get(idhash,k);
    if (strlen(copy)+strlen(k)+strlen(val)+5 > ARRAYSIZE*size)
      copy = resize(&size,strlen(copy)+strlen(k)+strlen(val)+5,copy);	  
    if (strlen(copy)> 0) 
      strcat(copy,"\\n");
    sprintf(copy,"%s%s: %s",copy,k,val);
  }
  //free(diff);
  return copy;
}
Exemplo n.º 22
0
static int test23(void)
{
	int i;
	HASHTBL_INT(h);
	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	for (i = 0; i < 100; i++) {
		int *k = malloc(sizeof(int));
		int *v = malloc(sizeof(int));
		*k = i;
		*v = i;
		CUT_ASSERT_EQUAL(0, hashtbl_insert(h, k, v));
	}
	CUT_ASSERT_EQUAL(100, hashtbl_count(h));
	for (i = 0; i < 100; i++) {
		int k = i;
		CUT_ASSERT_EQUAL(i, *(int *)hashtbl_lookup(h, &k));
	}
	hashtbl_delete(h);
	return 0;
}
Exemplo n.º 23
0
//checks whether edge exists and inserts if not
//ie insert if profile -> origprof doesn't exist yet
void check_insert_edge(FILE *fp,char *profile,char *origprof) {
  int k1=0,k2=0; //*f1,*f2;
  //double ratio;
  char *diff,*copy,*brac;
  HASHTBL *hash = NULL;

  /*
  hash = graph[k1-1];
  f1 = hashtbl_get(hash,profile);
  hash = graph[k2-1];
  f2 = hashtbl_get(hash,origprof);
  ratio = ((double)*f2)/((double)*f1);
  //  printf("ratio for %s and %s is %d/%d = %.2f\n",origprof,profile,*f2,*f1,ratio);
  */

  copy = malloc(sizeof(char)*(strlen(profile)+strlen(origprof)+4));
  //  if (strlen(profile)+strlen(origprof) > (ARRAYSIZE*size-4)) 
  //copy = resize(&size,strlen(profile)+strlen(origprof)+4,copy);
  sprintf(copy,"%s-> %s",profile,origprof);

  if (hashtbl_get(edges,copy)) return;

  hashtbl_insert(edges,copy,"1");
  if (!(hash = hashtbl_create(HASHSIZE,NULL))) {
    fprintf(stderr, "ERROR: hashtbl_create() failed");
    exit(EXIT_FAILURE);
  }
  diff = find_diff(hash,profile,origprof,&k1,&k2);
  brac = edge_label(hash,profile,origprof,k2);
  //color=gray%.0f,100-(ratio*100);
  if (strlen(brac) > 1)
    fprintf(fp,"\"%s\"-> \"%s\" [label=\"  %s\\n%s  \",fontsize=8];\n",profile,origprof,brac,diff);
  //if (VERBOSE)
  //printf("inserting edge %s\n",copy);

  free(copy);
  free(diff);
  free(brac);
  hashtbl_destroy(hash);
}  
Exemplo n.º 24
0
HASHTBL *bundle_hash_init(const char *filename){

  offset_p* offsets;
  unsigned int num_files, i;
  FILE *fh;
  header_offset toff;
  hash_size hash;
  HASHTBL *hashtbl;

  //  printf("hash size: %ld\n", sizeof(hash));

  if ((fh=fopen(filename, "r+b")) == NULL){
    perror("fopen");
    return -1;
  }

  num_files = header_get_head(fh);

  // get offsets
  if ((offsets = header_get_offsets(fh))==NULL){
    fprintf(stderr, "Cannot get offsets ...\n");
    fclose(fh);
    return NULL;
  }

  // ceating hash table
  if(!(hashtbl=hashtbl_create(num_files, NULL))) {
    fprintf(stderr, "ERROR: hashtbl_create() failed\n");
    return NULL;
  }

  for (i=0; i< num_files;i++){
    hash = offsets[i]->hash;//offsets[i]->hash;
    hashtbl_insert(hashtbl, hash, offsets[i]);
  }

  fclose(fh);
  return hashtbl;

}
Exemplo n.º 25
0
static int test22(void)
{
	int i;
	HASHTBL_STRING(h);
	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));
	for (i = 0; i < 100; i++) {
		char buf[64], *k, *v;
		sprintf(buf, "%d", i);
		CUT_ASSERT_NOT_NULL((k = strdup(buf)));
		CUT_ASSERT_NOT_NULL((v = strdup(buf)));
		CUT_ASSERT_EQUAL(0, hashtbl_insert(h, k, v));
	}
	CUT_ASSERT_EQUAL(100, hashtbl_count(h));
	for (i = 0; i < 100; i++) {
		char buf[64];
		sprintf(buf, "%d", i);
		CUT_ASSERT_TRUE(STREQ(buf, (char *)hashtbl_lookup(h, buf)));
	}
	hashtbl_delete(h);
	return 0;
}
Exemplo n.º 26
0
static int test14(void)
{
	unsigned int i;
	char *keys[] = { "100", "200", "300" };
	char *vals[] = { "1000", "2000", "3000" };
	struct hashtbl *h;
	struct hashtbl_iter iter;
	int key_sum = 0;
	int val_sum = 0;
	
	h = hashtbl_create(ht_size,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_string_hash, hashtbl_string_equals,
			   NULL, NULL,
			   NULL, NULL);

	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));

	for (i = 0; i < NELEMENTS(keys); i++) {
		CUT_ASSERT_EQUAL(0, hashtbl_insert(h, keys[i], vals[i]));
		CUT_ASSERT_EQUAL(vals[i], hashtbl_lookup(h, keys[i]));
		CUT_ASSERT_TRUE(STREQ(vals[i], (char *)hashtbl_lookup(h, keys[i])));
	}

	hashtbl_iter_init(h, &iter);
	while (hashtbl_iter_next(h, &iter)) {
		key_sum += atoi(iter.key);
		val_sum += atoi(iter.val);
	}
	CUT_ASSERT_EQUAL(600, key_sum);
	CUT_ASSERT_EQUAL(6000, val_sum);

	hashtbl_clear(h);
	hashtbl_delete(h);
	return 0;
  
}
Exemplo n.º 27
0
//inserts centroids into graph
//finds and prints edges between them
void find_centroid_edges(FILE *fp) {
  int *i,*zero;
  KEY *node;
  HASHTBL *hash = NULL;

  zero = malloc(sizeof(int));
  *zero = 0;
  for (node = hashtbl_getkeys(input); node; node = node->next) {
    i = hashtbl_get(input,node->data);
    if (*i-1 > graphsize)
      *i = graphsize+1;
    //printf("inserting %s into graph[%d]\n",node->data,*i-1);
    if (!(hash = graph[*i-1])) {
      if (!(hash = hashtbl_create(HASHSIZE,NULL))) {
	fprintf(stderr, "ERROR: hashtbl_create() for input failed");
	exit(EXIT_FAILURE);
      }
      graph[*i-1] = hash;
    }
    if (!hashtbl_get(hash,node->data))
      hashtbl_insert(hash,node->data,zero);    
  }
}
Exemplo n.º 28
0
/*
The number of elements in a hash table is not always known when creating the 
table. If the number of elements grows too large, it will seriously reduce the 
performance of most hash table operations. If the number of elements are 
reduced, the hash table will waste memory. That is why we provide a function 
for resizing the table.

Resizing a hash table is not as easy as a realloc(). All hash values must be 
recalculated and each element must be inserted into its new position.

We create a temporary HASHTBL object (newtbl) to be used while building the new 
hashes. This allows us to reuse hashtbl_inset() and hashtbl_remove(), when 
moving the elements to the new table. After that, we can just free the old table
 and copy the elements from newtbl to hashtbl. 
 */
int hashtbl_resize(HASHTBL *hashtbl, hash_size size) {
	HASHTBL newtbl;
	hash_size n;
	hashnode_s *node,*next;

	newtbl.size = size;
	newtbl.hashfunc = hashtbl->hashfunc;

	if(!(newtbl.nodes = calloc(size, sizeof(struct hashnode_s*)))) 
	  return FAILURE;

	for(n = 0; n<hashtbl->size; ++n) 
		for(node = hashtbl->nodes[n]; node; node = next) {
			next  =  node->next;
			hashtbl_insert(&newtbl, node->key, node->data);
			hashtbl_remove(hashtbl, node->key);
		}

	free(hashtbl->nodes);
	hashtbl->size = newtbl.size;
	hashtbl->nodes = newtbl.nodes;

	return SUCCESS;
}
Exemplo n.º 29
0
static int test16(void)
{
	int i, sum = 0;
	struct hashtbl *h;
	static int keys[] = { 100, 200, 300 };
	struct hashtbl_iter iter;

	h = hashtbl_create(ht_size,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_direct_hash, hashtbl_direct_equals,
			   NULL, NULL,
			   NULL, NULL);

	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_EQUAL(0, hashtbl_count(h));

	for (i = 0; i < (int)NELEMENTS(keys); i++) {
		CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &keys[i], NULL));
	}

	CUT_ASSERT_EQUAL(3, hashtbl_count(h));

	hashtbl_iter_init(h, &iter);
	CUT_ASSERT_TRUE(hashtbl_iter_next(h, &iter));
	sum += *(int *)iter.key;
	CUT_ASSERT_TRUE(hashtbl_iter_next(h, &iter));
	sum += *(int *)iter.key;
	CUT_ASSERT_TRUE(hashtbl_iter_next(h, &iter));
	sum += *(int *)iter.key;
	CUT_ASSERT_FALSE(hashtbl_iter_next(h, &iter));
	CUT_ASSERT_EQUAL(600, sum);

	hashtbl_delete(h);
	return 0;
}
Exemplo n.º 30
0
//finds the bracket notation for profile->origprof based on originating profile of size k
//difference between profile and origprof is in hash
//make sure origprof has a bracket rep (should be done in make_bracket_rep)
char* edge_label(HASHTBL *hash,char *profile, char *origprof,int k) {
  int i=0,count = 0,j=0,num=0,*diff,*save,ind=0,m=0;
  char *origbrac,*brac,*blank = "[]",*copy,*val;
  char **array;

  if (!(origbrac = hashtbl_get(bracket,origprof))) {
    fprintf(stderr,"Error: origprof %s has no bracket representation in bracket: edge_label()\n",origprof);
    return "";
  }
  //  if (strlen(profile) == 0) 
  copy = mystrdup(origbrac);
  brac = malloc(strlen(origbrac));
  //printf("finding edge label between %s and %s\n",origbrac,profile);
  array = malloc(sizeof(char*)*k);
  diff = malloc(sizeof(int)*hashtbl_numkeys(hash));
  save = malloc(sizeof(int)*hashtbl_numkeys(hash));
  //put helices of origprof into array; array in chron order
  for (val = strtok(copy,blank); val; val = strtok(NULL,blank)) {
    //printf("val is %s\n",val);
    if (i >= k) fprintf(stderr,"mismatch between k=%d and number of helices %d for %s: edge_label\n",k,i,origprof);
    array[i++] = val;
    //printf("val %s is at %d\n",val,i-1);
  }
  //save index in origprof of all diff helices; in ascending order
  for (count = 0; count < i; count++) {
    if (hashtbl_get(hash,array[count])) {
      //printf("saving %s at %d to index %d\n",array[count],count,j);
      diff[j++] = count;
    }
  }
  copy[0] = '\0';
  brac[0] = '\0';
  count = 0;
  j = -1;
  //i is index for origbrac, j is index for what level we need to match ']'
  //ind is index for level that increases for '[' and decreases for ']'
  //num is number of '[' encountered
  //count is index of different helix being proecessed
  //m is index for array of origprof helices, to print out ones not in diff
  for (i = 0; origbrac[i] != '\0'; i++) {
    //keep track of how many '['s
    if (origbrac[i] == '[') {
      ind++;
      val = mystrdup(array[m]);
      if (diff[count] == num++) {
	count++;
	save[++j] = ind;
	//printf("\nsaving %d to j=%d\n",ind,j);
	strcat(copy,"{");

      }
      else {
	//strcat(brac,"[");
	//strcat(brac,array[m]);
	sprintf(brac,"%s[%s",brac,array[m]);
	strcat(copy,"[");
      }
      strcat(copy,val);
      free(val);
      m++;
    }
    //keep track of which level brackets are at
    else if (origbrac[i] == ']') {
      //printf("\nchecking ind %d against %d at %d\n",ind,save[j]  puts("after check insert edge");,j);
      if (j >= 0 && save[j] == ind--) {
	j--;
	strcat(copy,"}");
      }
      else {
	strcat(brac,"]");
	strcat(copy,"]");
      }
    }
  }
  if (!(hashtbl_get(bracket,profile))) {
    hashtbl_insert(bracket,profile,brac);
    //printf("new brac is %s for (%s ->) %s with copy %s\n",brac,origprof,profile,copy);
  }
  free(array);
  free(diff);
  free(save);
  return copy;
}