Example #1
0
int XLALHashTblExtract(
  LALHashTbl *ht,
  const void *x,
  void **y
  )
{

  /* Check input */
  XLAL_CHECK( ht != NULL, XLAL_EFAULT );
  XLAL_CHECK( x != NULL && x != DEL, XLAL_EINVAL );
  XLAL_CHECK( y != NULL, XLAL_EFAULT );

  /* Try to find element matching 'x' in hash table, if found remove it from table and return in 'y' */
  if ( ht->data_len > 0 ) {
    int i = HASHIDX( ht, x );
    while ( ht->data[i] != NULL ) {
      *y = ht->data[i];
      if ( *y != DEL && EQUAL( ht, x, *y ) ) {
        ht->data[i] = DEL;
        --ht->n;
        if ( 8*ht->n < ht->data_len ) { /* Resize hash table to preserve minimum 50% occupancy */
          XLAL_CHECK( hashtbl_resize( ht ) == XLAL_SUCCESS, XLAL_EFUNC );
        }
        return XLAL_SUCCESS;
      }
      INCRIDX( ht, i );
    }
  }

  /* No element matches 'x' */
  *y = NULL;
  return XLAL_SUCCESS;

}
Example #2
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;
}
Example #3
0
static int test20(void)
{
	struct hashtbl *h;
	static int keys[] = { 100, 200, 300, 400, 500, 600 };
	
	h = hashtbl_create(4,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_direct_hash, hashtbl_direct_equals,
			   NULL, NULL,
			   test20_malloc, test20_free);

	CUT_ASSERT_NOT_NULL(h);

	CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &keys[0], &keys[0]));
	CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &keys[1], &keys[1]));
	CUT_ASSERT_EQUAL(1, hashtbl_resize(h, 8));
	CUT_ASSERT_EQUAL(2, hashtbl_count(h));

	hashtbl_delete(h);
	return 0;
}
Example #4
0
int XLALHashTblAdd(
  LALHashTbl *ht,
  void *x
  )
{

  /* Check input */
  XLAL_CHECK( ht != NULL, XLAL_EFAULT );
  XLAL_CHECK( x != NULL && x != DEL, XLAL_EINVAL );

  /* Check that no element matching 'x' exists in the hash table */
  {
    const void *y;
    XLAL_CHECK( XLALHashTblFind( ht, x, &y ) == XLAL_SUCCESS, XLAL_EFUNC );
    XLAL_CHECK( y == NULL, XLAL_EFAILED, "Hash table already contains given element" );
  }

  /* Resize hash table to preserve maximum 50% occupancy */
  if ( 2*( ht->q + 1 ) > ht->data_len ) {
    XLAL_CHECK( hashtbl_resize( ht ) == XLAL_SUCCESS, XLAL_EFUNC );
  }

  /* Add 'x' to the hash table */
  int i = HASHIDX( ht, x );
  while ( ht->data[i] != NULL && ht->data[i] != DEL ) {
    INCRIDX( ht, i );
  }
  if ( ht->data[i] == NULL ) {
    ++ht->q;
  }
  ++ht->n;
  ht->data[i] = x;

  return XLAL_SUCCESS;

}
Example #5
0
static int test13(void)
{
	struct hashtbl *h;
	
	h = hashtbl_create(-1,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_direct_hash, hashtbl_direct_equals,
			   NULL, NULL,
			   NULL, NULL);
	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_EQUAL(1, hashtbl_capacity(h));
	hashtbl_delete(h);

	h = hashtbl_create(0,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_direct_hash, hashtbl_direct_equals,
			   NULL, NULL,
			   NULL, NULL);
	CUT_ASSERT_EQUAL(1, hashtbl_capacity(h));
	CUT_ASSERT_NOT_NULL(h);
	hashtbl_delete(h);

	h = hashtbl_create(HASHTBL_MAX_TABLE_SIZE +1,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_direct_hash, hashtbl_direct_equals,
			   NULL, NULL,
			   NULL, NULL);
	CUT_ASSERT_EQUAL(HASHTBL_MAX_TABLE_SIZE, hashtbl_capacity(h));
	CUT_ASSERT_NOT_NULL(h);
	hashtbl_delete(h);

	h = hashtbl_create(127,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_direct_hash, hashtbl_direct_equals,
			   NULL, NULL,
			   NULL, NULL);
	CUT_ASSERT_NOT_NULL(h);
	CUT_ASSERT_EQUAL(128, hashtbl_capacity(h));
	hashtbl_delete(h);

	h = hashtbl_create(128,
			   HASHTBL_MAX_LOAD_FACTOR,
			   1,
			   hashtbl_direct_hash, hashtbl_direct_equals,
			   NULL, NULL,
			   NULL, NULL);
	CUT_ASSERT_NOT_NULL(h);
	hashtbl_resize(h, 128);
	CUT_ASSERT_EQUAL(128, hashtbl_capacity(h));
	hashtbl_resize(h, 0);
	CUT_ASSERT_EQUAL(128, hashtbl_capacity(h));
	hashtbl_resize(h, 99);
	CUT_ASSERT_EQUAL(128, hashtbl_capacity(h));
	hashtbl_resize(h, 128);
	CUT_ASSERT_EQUAL(128, hashtbl_capacity(h));
	hashtbl_resize(h, HASHTBL_MAX_TABLE_SIZE);
	CUT_ASSERT_EQUAL(HASHTBL_MAX_TABLE_SIZE, hashtbl_capacity(h));
	hashtbl_resize(h, 1+HASHTBL_MAX_TABLE_SIZE);
	CUT_ASSERT_EQUAL(HASHTBL_MAX_TABLE_SIZE, hashtbl_capacity(h));
	hashtbl_delete(h);
	
	h = hashtbl_create(ht_size,
			   -1.0f,
			   1,
			   hashtbl_direct_hash, hashtbl_direct_equals,
			   NULL, NULL,
			   NULL, NULL);
	CUT_ASSERT_NOT_NULL(h);
	hashtbl_delete(h);

	h = hashtbl_create(ht_size,
			   1.1f,
			   1,
			   NULL, NULL,
			   NULL, NULL,
			   NULL, NULL);
	CUT_ASSERT_NOT_NULL(h);
	hashtbl_delete(h);

	return 0;
}