Beispiel #1
0
void test_htable_erase() {
    htable *ht = htable_create();

    htable_insert(ht, "one", "1st value");
    htable_insert(ht, "two", "2nd value");

    htable_erase(ht, "one");

    assert(0 == htable_count(ht, "one")
        && "Bucket with key one should not exist.");

    htable_destroy(ht);
}
Beispiel #2
0
/**
 *
 Creates a hashtable, and inserts words into it.
 The table is then printed before we free the memory allocated to it.

 Note: The tablesize of the hashtable is determined by the first command line
 argument if there is one, with a default table tablesize of 113.
 The number of statistical snapshots to print is determined by the second
 command line argument if there is one, with a default number of 10.

 tablesize = the maximum number of positions in the hash table.
 word      = the string to be inserted into the hash table.
 ht        = the hash table using eith double hashing or linear probing.
 snapshots = the number of statistical snapshots to be used.

 @param argc the number of command-line arguments.
 @param argv an array of strings containing the command-line arguments.

 @return EXIT_SUCCESS if the program is successful.

*/
int main(int argc, char **argv) {
  time_t start,end;
  bool_t entire_table = FALSE, double_hashing = FALSE, print_stats = FALSE,
    do_spell_check = FALSE;
  int tablesize = 113, snapshots = 10;
  char word[256];
  char *filename;
  htable ht;

  set_flags(argc, argv, &do_spell_check, &filename, &double_hashing, &entire_table,
	    &print_stats, &snapshots, &tablesize);

  ht = htable_new(tablesize, (double_hashing) ? DOUBLE_H : LINEAR_P);
  start = clock();
  while (getword(word, sizeof word, stdin) != EOF) {
    htable_insert(ht, word);
  }
  end = clock();
  if(do_spell_check) {
    spell_check(ht,filename,(end-start)/(double)CLOCKS_PER_SEC);
  }
  if (entire_table) {
    htable_print_entire_table(ht, stderr);
  }
  if (print_stats) {
    htable_print_stats(ht, stdout, snapshots);
  } else if (!do_spell_check){ /* print words and frequencies */
    htable_print(ht, stdout);
  }
  htable_delete(ht);

  return EXIT_SUCCESS;
}
Beispiel #3
0
static void
search_gui_flush_queue_data(search_t *search, GtkTreeModel *model,
	struct result_data *rd)
{
	GtkTreeIter *parent_iter;
	record_t *rc;

	rc = rd->record;
	record_check(rc);

	if (rc->sha1) {
		struct result_data *parent;

		parent = find_parent(search, rd);
		parent_iter = parent ? &parent->iter : NULL;
		if (parent) {
			record_check(parent->record);
			parent->children++;
			search_gui_data_changed(model, parent);
		} else {
			htable_insert(search->parents, rd, rd);
		}
	} else {
		parent_iter = NULL;
	}

	gtk_tree_store_append(GTK_TREE_STORE(model), &rd->iter, parent_iter);
	search_gui_set_data(model, rd);
}
Beispiel #4
0
//k and v both are in stack
//k td
//v mvalue.data
int
insert_kv_mem(struct rbtree *rbt, struct htable *ds, uchar * k, int klen, 
              int type, uchar * v, int vlen, int hijack, packet_type *lowerdomain)
{
    uchar *val = NULL;
    struct mvalue *mv = NULL, tmp = {0};
    int ret = -1;
    struct rbnode *pn = NULL;
    struct ttlnode tn = { 0 }, *tmp_tn = NULL;
    int idx;
    if (vlen < 0 || vlen > MAX_RECORD_SIZE)
        return -1;
    hashval_t *hash = &(lowerdomain->hash[0]);
    idx = get_pre_mem_hash(k, klen, hash);
    val = malloc(vlen);
    if (val == NULL)
        return -1;
    memcpy(val, v, vlen);
    mv = (struct mvalue *) v;
    ret = htable_insert(ds + idx, k, klen, type, val, 1, &tmp, hash);    //mem, replace
    if (ret >= HTABLE_INSERT_RET_NEVER_EXPIRE) {
        free(val);
    }
    if (rbt) {
        if (ret == HTABLE_INSERT_RET_REPLACE) {
            pthread_spin_lock(&rbt->lock);
            tn.dlen = klen;
            //tmp get old data
            tn.exp = tmp.ttl;
            tn.type = type;
            tn.lowerdomain = NULL;
            tn.data = k;
            pn = find_node(rbt, &tn);
            //if update, we had delete tn in rbt
            //else update tn in rbt
            if (pn != NULL) {
                tmp_tn = delete_node(rbt, pn);
                if (tmp_tn) {
                    free(tmp_tn->lowerdomain);
                    free(tmp_tn);
                }
            }
            pthread_spin_unlock(&rbt->lock);
        }
    }
    if (mv->ttl == (MAX_TTL + 1)) {       //never expired
        return 0;
    }
    if (rbt == NULL) {
        return 0;
    }
    //data exists in htable, delete it in ttl tree, then insert
    pthread_spin_lock(&rbt->lock);
    if (type != NS && ret == HTABLE_INSERT_RET_REPLACE)
        ret = insert_into_ttltree(rbt, k, klen, type, mv->ttl, lowerdomain); //ttl expired tree
    else
        ret = insert_into_ttltree(rbt, k, klen, type, tmp.ttl, lowerdomain); //ttl expired tree
    pthread_spin_unlock(&rbt->lock);
    return 0;
}
Beispiel #5
0
/**
 * Locate record for query hits for specified MUID.
 *
 * @returns located record, or NULL if not found.
 */
static dqhit_t *
dh_locate(const struct guid *muid)
{
	bool found = FALSE;
	const void *key;
	void *value;

	if (NULL == by_muid_old)
		return NULL;		/* DH layer shutdown occurred already */

	/*
	 * Look in the old table first.  If we find something there, move it
	 * to the new table to keep te record "alive" since we still get hits
	 * for this query.
	 */

	found = htable_lookup_extended(by_muid_old, muid, &key, &value);

	if (found) {
		htable_remove(by_muid_old, key);
		g_assert(!htable_contains(by_muid, key));
		htable_insert(by_muid, key, value);
		return value;
	}

	return htable_lookup(by_muid, muid);
}
Beispiel #6
0
/**
 * Record a new leak of `size' bytes allocated at `file', line `line'.
 */
void
leak_add(leak_set_t *ls, size_t size, const char *file, int line)
{
	char key[1024];
	struct leak_record *lr;
	bool found;
	void *v;

	leak_set_check(ls);
	g_assert(file);
	g_assert(line >= 0);

	concat_strings(key, sizeof key,
		file, ":", uint64_to_string(line), (void *) 0);
	found = htable_lookup_extended(ls->places, key, NULL, &v);

	if (found) {
		lr = v;
		lr->size += size;
		lr->count++;
	} else {
		XPMALLOC(lr);
		lr->size = size;
		lr->count = 1;
		htable_insert(ls->places, xstrdup(key), lr);
	}
}
Beispiel #7
0
/**
 * Add header line to the `headers' hash for specified field name.
 * A private copy of the `field' name and of the `text' data is made.
 */
static void
add_header(header_t *o, const char *field, const char *text)
{
	htable_t *ht;
	str_t *v;

	header_check(o);

	ht = header_get_table(o);
	v = htable_lookup(ht, field);
	if (v) {
		/*
		 * Header already exists, according to RFC2616 we need to append
		 * the value, comma-separated.
		 */

		STR_CAT(v, ", ");
		str_cat(v, text);

	} else {
		char *key;

		/*
		 * Create a new header entry in the hash table.
		 */

		key = h_strdup(field);
		v = str_new_from(text);
		htable_insert(ht, key, v);
	}
}
Beispiel #8
0
/**
 * Adds the given node to the gui.
 */
void
nodes_gui_add_node(gnet_node_info_t *info)
{
	static const struct node_data zero_data;
	struct node_data *data;
	gnet_node_flags_t flags;

    g_return_if_fail(info);
	g_return_if_fail(!htable_contains(nodes_handles, info->node_id));

	WALLOC(data);
	*data = zero_data;

	data->node_id = nid_ref(info->node_id);
	data->user_agent = info->vendor ? atom_str_get(info->vendor) : NULL;
	data->country = info->country;
	data->host_size = w_concat_strings(&data->host,
						host_addr_port_to_string(info->addr, info->port),
						(void *) 0);
	str_bprintf(data->version, sizeof data->version, "%u.%u",
		info->proto_major, info->proto_minor);

	guc_node_fill_flags(data->node_id, &flags);
	nodes_gui_update_node_flags(data, &flags);

	htable_insert(nodes_handles, data->node_id, data);

    gtk_list_store_append(nodes_model, &data->iter);
    gtk_list_store_set(nodes_model, &data->iter, 0, data, (-1));

}
Beispiel #9
0
static void
run_test(HTable* htable)
{
	int i, j;

	/* fill table */
	for(i = 1; i <= N; i++)
	{
		for(j = 1; j <= N; j++)
		{
			bool isNewNode;
			ExpressionTableNodeData new_node_data;
			sprintf(new_node_data.expression, "%d + %d", i, j);
			new_node_data.value = (i + j);
			htable_insert(htable, (HTableNode*)&new_node_data, &isNewNode);
			assert(isNewNode);
		}
	}

	assert(htable_nitems(htable) == (N*N));

	/* check hash table is filled right */
	for(i = 1; i <= N; i++)
	{
		for(j = 1; j <= N; j++)
		{
			ExpressionTableNode found_node;
			ExpressionTableNodeData query;
			sprintf(query.expression, "%d + %d", i, j);
			found_node = (ExpressionTableNode)htable_find(htable, (HTableNode*)&query);
			assert(found_node != NULL);
			assert(found_node->value == (i + j));
		}
	}

	/* try to delete a non-existing node */
	{
		bool result;
		ExpressionTableNodeData query;
		sprintf(query.expression, "ololo trololo");
		result = htable_delete(htable, (HTableNode*)&query);
		assert(result == false);
	}

	/* clean table */
	for(i = 1; i <= N; i++)
	{
		for(j = 1; j <= N; j++)
		{
			bool result;
			ExpressionTableNodeData query;
			sprintf(query.expression, "%d + %d", i, j);
			result = htable_delete(htable, (HTableNode*)&query);
			assert(result == true);
		}
	}

	assert(htable_nitems(htable) == 0);
}
Beispiel #10
0
void test_htable_probing() {
    htable *ht = htable_create();
    size_t capacity = htable_capacity(ht);

    assert(_hash_func("one", capacity) == _hash_func("seven", capacity)
        && "Make sure that the keys gives the same key");

    htable_insert(ht, "one", "1st value");
    htable_insert(ht, "seven", "7th value");

    assert(0 == strcmp("1st value", htable_get(ht, "one"))
        && "First value should be equal to 1st value");

    assert(0 == strcmp("7th value", htable_get(ht, "seven"))
        && "Seventh value should be equal to 7th value");

    htable_destroy(ht);
}
Beispiel #11
0
void
main_phrases (void)
{
        phrase_t *p1 = phrase_create ("ala");
        phrase_t *p2 = phrase_create ("ela");
        entry_t *entry;

        htable_t *table = htable_create_p (4);

        htable_insert (table, p1, p1);
        htable_insert (table, p2, p2);

        entry = htable_lookup (table, p2);

        phrase_print ((phrase_t *) entry->value);
        printf ("\n");

        htable_destroy (table, destroy_entry);
}
Beispiel #12
0
int main(void) {
    htable h = htable_new(18143);
    char word[256];
    while (getword(word, sizeof word, stdin) != EOF) {
        htable_insert(h, word);
    }
    htable_print(h, stdout);
    htable_free(h);
    return EXIT_SUCCESS;
}
Beispiel #13
0
void test_htable_size() {
    htable *ht = htable_create();

    assert(0 == htable_size(ht)
        && "Count should be 0 upon creation.");

    htable_insert(ht, "one", "1st value");
    htable_insert(ht, "two", "2nd value");
    htable_insert(ht, "three", "3rd value");

    assert(3 == htable_size(ht)
        && "Count should be 3 after three insertions.");

    htable_clear(ht);

    assert(0 == htable_size(ht)
        && "Count should be 0 after clear.");

    htable_destroy(ht);
}
Beispiel #14
0
int test_add_element_and_find_it(){
  hashtable* myhtable;
  hsize size=101;
  myhtable=htable_create(size,NULL);
  htable_insert(myhtable,"Hugo","Yvaon");
  assert(!strcmp("Yvaon",htable_get(myhtable,"Hugo")));
  htable_destroy(myhtable);
  myhtable=NULL;  

  return 1;
}
Beispiel #15
0
void
fi_gui_source_show(struct download *d)
{
	GtkTreeIter *iter;

	g_return_if_fail(store_sources);
	g_return_if_fail(!htable_contains(fi_sources, d));

	WALLOC(iter);
	htable_insert(fi_sources, d, iter);

	list_store_append_pointer(store_sources, iter, 0, d);
}
Beispiel #16
0
/**
 * Create new record for query hits for specified MUID.
 * New record is registered in the current table.
 */
static dqhit_t *
dh_create(const struct guid *muid)
{
	dqhit_t *dh;
	const struct guid *key;

	WALLOC0(dh);
	key = atom_guid_get(muid);

	htable_insert(by_muid, key, dh);

	return dh;
}
Beispiel #17
0
/**
 * Hash table iterator to invert the "uri -> node" mapping to "node -> uri".
 *
 * Since there are many URIs that can be associated to a given node, the
 * values are actually lists of URIs.
 */
static void
xfmt_invert_uri_kv(const void *key, void *value, void *data)
{
    struct xfmt_invert_ctx *ictx = data;
    const char *uri = key;
    const xnode_t *xn = value;
    GSList *sl;

    g_assert(xn != NULL);

    sl = htable_lookup(ictx->node2uri, xn);
    sl = gm_slist_prepend_const(sl, uri);
    htable_insert(ictx->node2uri, xn, sl);
}
Beispiel #18
0
int main(void) {
 char line[MAXLINE];
 node *hashtable;

 hashtable = (node *)malloc(HASHSIZE * sizeof(node));

 htable_init(hashtable);

 while((fgets(line, MAXLINE, stdin)) != NULL)
  htable_insert(hashtable, line);

 htable_display(hashtable);

 return 0;
}
Beispiel #19
0
/**
 * Record a waiting event.
 *
 * @param key		waiting key
 * @param cb		callback to invoke on wakeup
 * @param arg		additional callback argument
 *
 * @return the registered event, whose reference must be kept if it is meant
 * to be cancelled.
 */
wq_event_t *
wq_sleep(const void *key, wq_callback_t cb, void *arg)
{
	wq_event_t *we;
	hash_list_t *hl;

	we = wq_event_alloc(key, cb, arg);
	hl = htable_lookup(waitqueue, key);
	if (NULL == hl) {
		hl = hash_list_new(pointer_hash, NULL);
		htable_insert(waitqueue, key, hl);
	}
	hash_list_append(hl, we);		/* FIFO layout */

	return we;
}
Beispiel #20
0
void test_htable_get() {
    htable *ht = htable_create();

    htable_insert(ht, "two", "2nd value");

    assert(NULL == htable_get(ht, "one")
        && "First value should be NULL");

    assert(NULL != htable_get(ht, "two")
        && "Second value should not equal NULL");

    assert(0 == strcmp("2nd value", htable_get(ht, "two"))
        && "Second value should be equal to 2nd value");

    htable_destroy(ht);
}
int main(int argc, char **argv) {
    int size;
    htable h;
    char word[256];
    if (argc <=1 ){
        size = 25013;
        fprintf(stderr, "No arguments given, set htable size to %d\n", size);
    } else {
        size = atoi(argv[1]);
    }
    h = htable_new(size);
    while (getword(word, sizeof word, stdin) != EOF) {
        htable_insert(h, word);
    }
    htable_print(h, stdout);
    htable_free(h);
    return EXIT_SUCCESS;
}
Beispiel #22
0
static void
add_word (htable_t *words, word_t *word, phrase_t *phrase, int project_id,
          int location_id, int destroy_phrase)
{
        entry_t *entry;
        list_t *node = xmalloc (sizeof (list_t));
        node->next = NULL;
        node->phrase = phrase;
        node->project_id = project_id;
        node->location_id = location_id;
        node->destroy_phrase = destroy_phrase;

        entry = htable_insert (words, word, node);
        if (entry->value != node){
                node->next = entry->value;
                entry->value = node;
        }
}
Beispiel #23
0
void *
st_th(void *arg)
{
    int i, idx;
    uchar key[50] = { 0 };
    int klen;
    uchar *val = NULL;
    int pre = 0;
//     struct hentry *he = NULL;
    uchar *oval;
    struct htable *ht;
    struct st_hlp *sh = (struct st_hlp *) arg;
    hashval_t hash;
    idx = sh->idx;
    ht = sh->ht;
    for (i = idx * NUMX; i < (idx + 1) * NUMX; i++) {
        hash = 0;
        sprintf((char *)key, "%dkey", i);
        val = malloc(50);
        sprintf((char *)val, "%dval", i);
        //printf("%d,%s,%s\n",idx,key,val);
        klen = strlen((const char *)key) + 1;
        pre = get_pre_mem_hash(key, klen, &hash);
        htable_insert(ht + pre, key, klen, A, val, 0, NULL, &hash);
    }
    if (idx == (THREADX - 1))
        idx = -1;
    sleep(2);
    for (i = (idx + 1) * NUMX; i < (idx + 2) * NUMX; i++) {
        hash = 0;
        sprintf((char *)key, "%dkey", i);
        klen = strlen((const char *)key) + 1;
        pre = get_pre_mem_hash(key, klen, &hash);
        oval = htable_delete(ht + pre, key, klen, A, hash);
        if (oval == NULL) {
            printf("error in test %s,%d,%d\n", key, idx, i);
        }
        else
            free(oval);
    }
    sleep(5);
    return NULL;
}
Beispiel #24
0
static void
hit_word (htable_t *words, htable_t *hits, word_t *word)
{
        entry_t *entry = htable_lookup (words, word);
        list_t *node = (entry) ? (list_t *) entry->value : NULL;

        while (node){
                entry_t *entry;
                hit_t *hit = xmalloc (sizeof (hit_t));
                hit->query_hits = word->occurences;
                hit->storage_hits = word_occurences (word, node->phrase);

                entry = htable_insert (hits, node, hit);
                if (entry->value != hit)
                        merge_and_destroy_hit ((hit_t *)entry->value, hit);

                node = node->next;
        }
}
Beispiel #25
0
void
sip_calls_clear_soft()
{
        // Create again the callid hash table
        htable_destroy(calls.callids);
        calls.callids = htable_create(calls.limit);

        // Repopulate list applying current filter
        calls.list = vector_copy_if(sip_calls_vector(), filter_check_call);
        calls.active = vector_copy_if(sip_active_calls_vector(), filter_check_call);

        // Repopulate callids based on filtered list
        sip_call_t *call;
        vector_iter_t it = vector_iterator(calls.list);

        while ((call = vector_iterator_next(&it)))
        {
                htable_insert(calls.callids, call->callid, call);
        }
}
Beispiel #26
0
//k and v both are in stack
//k td
//v mvalue.data
int insert_kv_mem(struct rbtree *rbt,struct htable *ds,uchar *k,uchar *v,int vlen)
{
 uchar *val = NULL;
 struct mvalue *mv = NULL,tmp;
 int ret = -1;
 struct rbnode *pn = NULL;
 struct ttlnode tn = {0};
 if(vlen < 0 || vlen > MAX_RECORD_SIZE)
	return -1;
 hashval_t hash = nocase_char_hash_function(k);
 hash = get_pre_mem_hash(k);
 val = malloc(vlen);
 if(val == NULL)
	return -1;
 memcpy(val,v,vlen);
 mv = (struct mvalue*)v;
 ret = htable_insert(ds + hash,k,val,1,&tmp); //mem, replace
 if(ret == 2)
	free(val);
 if(mv->ttl == (MAX_TTL + 1))//never expired
	 return 0;
 if(rbt == NULL)
	 return 0;
 //data exists in htable, delete it in ttl tree, then insert
 pthread_mutex_lock(&rbt->lock);
 if(ret != 0)
	{
	 tn.dlen = strlen(k) + 1;
	 //tmp get old data
	 tn.exp = tmp.ttl;
	 tn.data = k;
	 pn = find_node(rbt,&tn);
	 //if update, we had delete tn in rbt
	 //else update tn in rbt
	 if(pn != NULL)
		 delete_node(rbt,pn);
	}
 ret = insert_into_ttltree(rbt,k,mv->ttl);//ttl expired tree
 pthread_mutex_unlock(&rbt->lock);
 return 0;
}
Beispiel #27
0
/**
 * Count a word that has been seen.
 */
static void
search_stats_tally(const word_vec_t *vec)
{
    struct term_counts *val;

    if (vec->word[1] == '\0' || vec->word[2] == '\0')
        return;

    val = htable_lookup(stat_hash, vec->word);

    if (val) {
        val->period_cnt++;
    } else {
        const char *key;

        WALLOC0(val);
        val->period_cnt = vec->amount;
        key = wcopy(vec->word, 1 + strlen(vec->word));
        htable_insert(stat_hash, key, val);
    }
}
Beispiel #28
0
/**
 * Record a new leak of `size' bytes allocated from stack trace.
 */
void
leak_stack_add(leak_set_t *ls, size_t size, const struct stackatom *sa)
{
	struct leak_record *lr;
	bool found;
	void *v;

	leak_set_check(ls);

	found = htable_lookup_extended(ls->stacks, sa, NULL, &v);

	if (found) {
		lr = v;
		lr->size += size;
		lr->count++;
	} else {
		XPMALLOC(lr);
		lr->size = size;
		lr->count = 1;
		htable_insert(ls->stacks, sa, lr);
	}
}
Beispiel #29
0
/**
 * Add a new upload stats row to the model.
 *
 * Add the information within the ul_stats structure
 * to the GtkTreeModel and another row the the
 * upload statistics pane.
 *
 * @param us A ul_stats structure with new upload stats to add.
 *
 */
void
upload_stats_gui_add(struct ul_stats *us)
{
	struct upload_data *data;
    GtkListStore *store;

	g_assert(us != NULL);

	upload_stats_gui_init_intern(TRUE);
	store = GTK_LIST_STORE(gtk_tree_view_get_model(upload_stats_treeview));
	g_return_if_fail(store);
	g_return_if_fail(!htable_contains(ht_uploads, us));

	WALLOC(data);
	data->us = us;

	data->filename = atom_str_get(us->filename);
	htable_insert(ht_uploads, data->us, data);

	gtk_list_store_append(store, &data->iter);
    gtk_list_store_set(store, &data->iter, 0, data, (-1));
}
Beispiel #30
0
int htable_rehash( struct hash_table* table, int new_size )
{
	// Allocate the new table..
	void **newTable = (void**)malloc(sizeof(void**) * new_size);
	if ( newTable == NULL ) return -1;		// Out of memory.


	void **oldTable = table->table;
	int oldSize     = table->size;
	int i;

	for ( i = 0; i < new_size; i++) newTable[i] = NULL;

	table->size = new_size;
	table->total = 0;
	table->table = newTable;


	for ( i = 0; i < oldSize; i++ )
		if ( oldTable[i] != NULL ) htable_insert( table, oldTable[i] );

	free( oldTable );
	return 0;
}