コード例 #1
0
ファイル: hashtable.c プロジェクト: FOSSEE-Internship/scilab
/*--------------------------------------------------------------------------*/
void * /* returns value associated with key */
hashtable_remove(struct hashtable *h, void *k)
{
    /* TODO: consider compacting the table when the load factor drops enough,
     *       or provide a 'compact' method. */

    struct entry *e;
    struct entry **pE;
    void *v;
    unsigned int hashvalue, index_;

    hashvalue = hashtable_hash(h, k);
    index_ = indexFor(h->tablelength, hashtable_hash(h, k));
    pE = &(h->table[index_]);
    e = *pE;
    while (NULL != e)
    {
        /* Check hash value to short circuit heavier comparison */
        if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
        {
            *pE = e->next;
            h->entrycount--;
            v = e->v;
            freekey(e->k);
            FREE(e);
            return v;
        }
        pE = &(e->next);
        e = e->next;
    }
    return NULL;
}
コード例 #2
0
ファイル: CHashTable.c プロジェクト: functionGHW/CHashTable
/*Search the hashtable and return the value with specific key;
 *Note: this function just return a memory block without any
 *      info about it's type and size;
 *      In order to avoid problems, I suggest that
 *          one hashtable only use one kind of data type for key,
 *          and one kind of data type for val;
 *      You can add more info to the names:
 *              Hashtable tab_str_str = new_hashtable(size);
 *              Hashtable tab_str_int = new_hashtable(size);
 */
BYTE* hashtable_getval(const Hashtable* table, const BYTE* key, size_t key_size)
{
    if (table == NULL || key == NULL || key_size < 1)
    {
        return NULL;
    }
    size_t hashcode = hashtable_hash(key, key_size) % table->size;
    HashLnkLstNode* pNode = table->table[hashcode];
    while (pNode != NULL)
    {
        if (pNode->key_size == key_size
            && 0 == memcmp(pNode->key, key, key_size))
        {
            if (pNode->val == NULL || pNode->val_size == 0)
            {
                return NULL;
            }
            BYTE* tmpval = NULL;
            tmpval = (BYTE*)malloc(pNode->val_size);
            if (tmpval != NULL)
            {
                memcpy(tmpval, pNode->val, pNode->val_size);
            }
            return tmpval;
        }
        pNode = pNode->next;
    }
    return NULL;
}
コード例 #3
0
ファイル: hashtable.c プロジェクト: FOSSEE-Internship/scilab
/*--------------------------------------------------------------------------*/
int
hashtable_insert(struct hashtable *h, void *k, void *v)
{
    /* This method allows duplicate keys - but they shouldn't be used */
    unsigned int index_;
    struct entry *e;
    if (++(h->entrycount) > h->loadlimit)
    {
        /* Ignore the return value. If expand fails, we should
         * still try cramming just this value into the existing table
         * -- we may not have memory for a larger table, but one more
         * element may be ok. Next time we insert, we'll try expanding again.*/
        hashtable_expand(h);
    }
    e = (struct entry *)MALLOC(sizeof(struct entry));
    if (NULL == e)
    {
        --(h->entrycount);    /*oom*/
        return 0;
    }
    e->h = hashtable_hash(h, k);
    index_ = indexFor(h->tablelength, e->h);
    e->k = k;
    e->v = v;
    e->next = h->table[index_];
    h->table[index_] = e;
    return -1;
}
コード例 #4
0
void hashtable_set(struct hashtable *hashtable, hashtable_key_t key, hashtable_value_t value) {
	LOG(INFO, "hashtable_set called\n");
	int bin = 0;
	struct entry *newpair = NULL;
	struct entry *next = NULL;
	struct entry *last = NULL;

	bin = hashtable_hash(hashtable, key);

  	//TODO: Add all value to logs
  	LOG(DEBUG, "Adding value with key %d to bin %d, value=%d\n", key, bin, value);
	for (next = hashtable->table[bin]; next; last = next, next = next -> next) {
		//Key found, replacing
		if (key == next -> key) {
			LOG(DEBUG, "Key found\n");
			next -> value = value;
			return;
		}
	}

	//Pair was not found - add new one
	newpair = hashtable_newpair(key, value);
	if (last == NULL) {
		LOG(DEBUG, "Init bin\n");
		hashtable->table[bin] = newpair;
	}else{
		LOG(DEBUG, "Added new pair\n");
		last->next = newpair;
	}
}
コード例 #5
0
ファイル: hashtable.c プロジェクト: FOSSEE-Internship/scilab
/**
 * Returns value associated with key
 */
void * hashtable_search(struct hashtable *h, void *k)
{
    struct entry *e;
    unsigned int hashvalue, index_;
    if (h == NULL)
    {
        /* Check that the hashtable does exist. */
        printf("Internal error: cannot search into an NULL hashtable !\n");
        exit(-1);
    }
    hashvalue = hashtable_hash(h, k);
    index_ = indexFor(h->tablelength, hashvalue);
    e = h->table[index_];
    while (NULL != e)
    {
        /* Check hash value to short circuit heavier comparison */
        if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
        {
            return e->v;
        }

        e = e->next;
    }
    return NULL;
}
コード例 #6
0
ファイル: hashtable.c プロジェクト: networkfusion/alt64
/**
 * Find an available slot for the given key, using linear probing.
 */
unsigned int hashtable_find_slot(hashtable* t, char* key)
{
	int index = hashtable_hash(key) % t->capacity;
	while (t->body[index].key != NULL && strcmp(t->body[index].key, key) != 0) {
		index = (index + 1) % t->capacity;
	}
	return index;
}
コード例 #7
0
ファイル: domain.c プロジェクト: carriercomm/ovh-cli-1
// ./ovh domain domain.ext record toto add www type CNAME
// TODO: ttl (optionnal)
static command_status_t record_add(COMMAND_ARGS)
{
    bool request_success;
    json_document_t *doc;
    domain_record_argument_t *args;

    USED(mainopts);
    args = (domain_record_argument_t *) arg;
    assert(NULL != args->domain);
    assert(NULL != args->record);
    {
        json_document_t *reqdoc;

        // data
        {
            json_value_t root;

            reqdoc = json_document_new();
            root = json_object();
            json_object_set_property(root, "target", json_string(args->value));
            json_object_set_property(root, "fieldType", json_string(domain_record_types[args->type]));
//             if ('\0' != *subdomain)
                json_object_set_property(root, "subDomain", json_string(args->record));
            json_document_set_root(reqdoc, root);
        }
        // request
        {
            request_t *req;

            req = request_new(REQUEST_FLAG_SIGN | REQUEST_FLAG_JSON, HTTP_POST, reqdoc, error, API_BASE_URL "/domain/zone/%s/record", args->domain);
            request_success = request_execute(req, RESPONSE_JSON, (void **) &doc, error);
            request_destroy(req);
            json_document_destroy(reqdoc);
        }
    }
    // result
    if (request_success) {
        domain_t *d;
        ht_hash_t h;
        domain_set_t *ds;

        d = NULL;
        ds = NULL;
        account_current_get_data(MODULE_NAME, (void **) &ds);
        assert(NULL != ds);
        h = hashtable_hash(ds->domains, args->domain);
        if (!hashtable_quick_get(ds->domains, h, args->domain, &d)) {
            d = domain_new();
            hashtable_quick_put(ds->domains, 0, h, args->domain, d, NULL);
        }
        parse_record(d->records, doc);
        ask_for_refresh(RELAY_COMMAND_ARGS);
    }

    return request_success ? COMMAND_SUCCESS : COMMAND_FAILURE;
}
コード例 #8
0
ファイル: CHashTable.c プロジェクト: functionGHW/CHashTable
//Add a new element into the hashtable,
//if the key has exist in the hashtable, do nothing;
void hashtable_add(Hashtable* table,
                   const BYTE* key,
                   size_t key_size,
                   const BYTE* val,
                   size_t val_size)
{
    if (table == NULL || key == NULL || key_size < 1)
    {
        return;
    }
    size_t hashcode = hashtable_hash(key, key_size) % table->size;
    HashLnkLstNode* pNode = table->table[hashcode];
    while (pNode != NULL)
    {
        if (pNode->key_size == key_size
            && 0 == memcmp(pNode->key, key, key_size))
        {
            return;
        }
        pNode = pNode->next;
    }
    HashLnkLstNode* node = (HashLnkLstNode*)malloc(sizeof(HashLnkLstNode));
    if (node == NULL)
    {
        return;
    }
    node->key = (BYTE*)malloc(key_size);
    if (node->key == NULL)
    {
        free(node);
        return;
    }
    if (val == NULL || val_size == 0)
    {
        node->val = NULL;
        node->val_size = 0;
    }
    else
    {
        node->val = (BYTE*)malloc(val_size);
        if (node->val == NULL)
        {
            free(node->key);
            free(node);
            return;
        }
        memcpy(node->val, val, val_size);
        node->val_size = val_size;
    }
    memcpy(node->key, key, key_size);
    node->key_size = key_size;

    node->next = table->table[hashcode];
    table->table[hashcode] = node;
    table->elemts_count++;
}
コード例 #9
0
ファイル: neodistribute.c プロジェクト: ooloo/xiaomifeng
// return 1:found 0:notfound
int link_search(TDict * dict, char *keyStr)
{
    unsigned slotNo, key;
    TNodeInfo info;

    assert(dict && keyStr);

    hashtable_hash(dict, &slotNo, &key, keyStr);

    return hashtable_seek(dict, slotNo, key, &info);
}
コード例 #10
0
ファイル: neodistribute.c プロジェクト: ooloo/xiaomifeng
void link_insert(TDict * dict, char *keyStr)
{
    unsigned slotNo, key;
    TNodeInfo info;

    assert(dict && keyStr);

    hashtable_hash(dict, &slotNo, &key, keyStr);

    assert(hashtable_insert(dict, slotNo, key, &info) != -1);

    return;
}
コード例 #11
0
ファイル: hashtable.c プロジェクト: fermitools/frontier
void * /* returns value associated with key */
hashtable_search(struct hashtable *h, void *k)
{
    struct entry *e;
    unsigned int hashvalue, index;
    hashvalue = hashtable_hash(h,k);
    index = indexFor(h->tablelength,hashvalue);
    e = h->table[index];
    while (NULL != e)
    {
        /* Check hash value to short circuit heavier comparison */
        if ((hashvalue == e->h) && (h->eqfn(k, e->k))) return e->v;
        e = e->next;
    }
    return NULL;
}
コード例 #12
0
hashtable_value_t hashtable_get(struct hashtable *hashtable, hashtable_key_t key) {
	LOG(INFO, "hashtable_get called\n");
	int bin = -1;
	struct entry *next = NULL;

	bin = hashtable_hash(hashtable, key);
	next = hashtable->table[bin];

	LOG(DEBUG, "Searching value with key %d in bin %d\n", key, bin);
	for (next = hashtable->table[bin]; next; next = next -> next) {
		//Key found, replacing
		if (key == next -> key) {
			LOG(DEBUG, "Value found by key %d: %d\n", key, next->value);
			return next -> value;
		}
	}
	LOG(DEBUG, "No value found\n");
	return hashtable_value_nil;
}
コード例 #13
0
ファイル: CHashTable.c プロジェクト: functionGHW/CHashTable
//Determines whether the hashtable contains a specific element;
int hashtable_contains_key(const Hashtable* table, const BYTE* key, size_t key_size)
{
    if (table == NULL || key == NULL || key_size < 1)
    {
        return 0;
    }
    size_t hashcode = hashtable_hash(key, key_size) % table->size;
    HashLnkLstNode* pNode = table->table[hashcode];
    while (pNode != NULL)
    {
        if (pNode->key_size == key_size
            && 0 == memcmp(pNode->key, key, key_size))
        {
            return 1;
        }
        pNode = pNode->next;
    }
    return 0;
}
コード例 #14
0
ファイル: CHashTable.c プロジェクト: functionGHW/CHashTable
//Remove a specific element from the hashtable;
void hashtable_remove(Hashtable* table, const BYTE* key, size_t key_size)
{
    if (table == NULL || key == NULL || key_size < 1)
    {
        return;
    }
    size_t hashcode = hashtable_hash(key, key_size) % table->size;
    HashLnkLstNode* tmp_prev = table->table[hashcode];
    if (tmp_prev->key_size == key_size
        && 0 == memcmp(tmp_prev->key, key, key_size))
    {
        table->table[hashcode]->next = tmp_prev->next;
        free(tmp_prev->key);
        tmp_prev->key_size = 0;
        tmp_prev->key = NULL;
        free(tmp_prev->val);
        tmp_prev->val_size = 0;
        tmp_prev->val = NULL;
        free(tmp_prev);
        table->table[hashcode] = NULL;
        table->elemts_count--;
        return;
    }
    HashLnkLstNode* tmp = table->table[hashcode]->next;
    while (tmp != NULL)
    {
        if (tmp->key_size == key_size
            && 0 == memcmp(tmp->key, key, key_size))
        {
            tmp_prev->next = tmp->next;
            free(tmp->key);
            tmp->key_size = 0;
            tmp->key = NULL;
            free(tmp->val);
            tmp->val_size = 0;
            free(tmp);
            table->elemts_count--;
            return;
        }
        tmp_prev = tmp;
        tmp = tmp->next;
    }
}
コード例 #15
0
/* hashtable_change
 *
 * function to change the value associated with a key, where there already
 * exists a value bound to the key in the hashtable.
 * Source due to Holger Schemel.
 *
 *  */
int
hashtable_change(struct hashtable *h, void *k, void *v)
{
    struct entry *e;
    unsigned int hashvalue, index_;
    hashvalue = hashtable_hash(h, k);
    index_ = indexFor(h->tablelength, hashvalue);
    e = h->table[index_];
    while (NULL != e)
    {
        /* Check hash value to short circuit heavier comparison */
        if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
        {
            FREE(e->v);
            e->v = v;
            return -1;
        }
        e = e->next;
    }
    return 0;
}
コード例 #16
0
ファイル: hash.c プロジェクト: giaviv/COSC301
// remove a string from the hashtable; if the string
// doesn't exist in the hashtable, do nothing
void hashtable_remove(hashtable_t *hashtable, const char *s) {
        int i = hashtable_hash(hashtable, s);
        list_remove(&hashtable->table[i], s);
}
コード例 #17
0
ファイル: hash.c プロジェクト: giaviv/COSC301
// add a new string to the hashtable
void hashtable_add(hashtable_t * hashtable, const char * s) {
	int i = hashtable_hash(hashtable, s);
	list_add(&hashtable->table[i], s);
}
コード例 #18
0
ファイル: cache.c プロジェクト: Distrotech/conntrack-tools
struct cache_object *cache_find(struct cache *c, void *ptr, int *id)
{
	*id = hashtable_hash(c->h, ptr);
	return ((struct cache_object *) hashtable_find(c->h, ptr, *id));
}