Пример #1
0
int hashtab_insert(hashtab_t h, hashtab_key_t key, hashtab_datum_t datum)
{
    int hvalue;
    hashtab_ptr_t current, newnode;


    if (!h)
	    return HASHTAB_OVERFLOW;
    
    hvalue = h->hash_value(key);
    current = h->htable[hvalue];
    while (current != NULL && h->keycmp(current->key, key) != 0)
	    current = current->next;

    if (current != NULL)
	    return HASHTAB_PRESENT;

    MALLOC(newnode, hashtab_ptr_t, sizeof(struct hashtab_node_t), M_TEMP, M_WAITOK);
    if (newnode == NULL)
	    return HASHTAB_OVERFLOW;
    newnode->key = key;
    newnode->datum = datum;
    newnode->next = h->htable[hvalue];
    h->htable[hvalue] = newnode;
    return HASHTAB_SUCCESS;
}
Пример #2
0
int hashtab_remove(hashtab_t h, hashtab_key_t key, 
		   void (*destroy)(hashtab_key_t, hashtab_datum_t, void *),
		   void *args)
{
    int hvalue;
    hashtab_ptr_t current, last;


    if (!h)
	    return HASHTAB_MISSING;
    
    hvalue = h->hash_value(key);
    last = NULL;
    current = h->htable[hvalue];
    while (current != NULL && h->keycmp(current->key, key))
    {
	last = current;
	current = current->next;
    }

    if (current == NULL)
	    return HASHTAB_MISSING;

    if (last == NULL)
	    h->htable[hvalue] = current->next;
    else
	    last->next = current->next;

    if (destroy)
	    destroy(current->key,current->datum,args);
    FREE(current, M_TEMP);
    return HASHTAB_SUCCESS;
}
Пример #3
0
int hashtab_remove(hashtab_t h, hashtab_key_t key,
		   void (*destroy) (hashtab_key_t k,
				    hashtab_datum_t d, void *args), void *args)
{
	int hvalue;
	hashtab_ptr_t cur, last;

	if (!h)
		return SEPOL_ENOENT;

	hvalue = h->hash_value(h, key);
	last = NULL;
	cur = h->htable[hvalue];
	while (cur != NULL && h->keycmp(h, key, cur->key) > 0) {
		last = cur;
		cur = cur->next;
	}

	if (cur == NULL || (h->keycmp(h, key, cur->key) != 0))
		return SEPOL_ENOENT;

	if (last == NULL)
		h->htable[hvalue] = cur->next;
	else
		last->next = cur->next;

	if (destroy)
		destroy(cur->key, cur->datum, args);
	free(cur);
	h->nel--;
	return SEPOL_OK;
}
Пример #4
0
hashtab_datum_t hashtab_search(hashtab_t h, hashtab_key_t key)
{
    int hvalue;
    hashtab_ptr_t current;


    if (!h)
	    return NULL;
    
    hvalue = h->hash_value(key);
    current = h->htable[hvalue];
    while (current != NULL && h->keycmp(current->key, key))
	    current = current->next;

    if (current == NULL)
	    return NULL;

    return current->datum;
}	    
Пример #5
0
hashtab_datum_t hashtab_search(hashtab_t h, const_hashtab_key_t key)
{

	int hvalue;
	hashtab_ptr_t cur;

	if (!h)
		return NULL;

	hvalue = h->hash_value(h, key);
	cur = h->htable[hvalue];
	while (cur != NULL && h->keycmp(h, key, cur->key) > 0)
		cur = cur->next;

	if (cur == NULL || (h->keycmp(h, key, cur->key) != 0))
		return NULL;

	return cur->datum;
}
Пример #6
0
int hashtab_replace(hashtab_t h, hashtab_key_t key, hashtab_datum_t datum,
		    void (*destroy) (hashtab_key_t k,
				     hashtab_datum_t d, void *args), void *args)
{
	int hvalue;
	hashtab_ptr_t prev, cur, newnode;

	if (!h)
		return SEPOL_ENOMEM;

	hvalue = h->hash_value(h, key);
	prev = NULL;
	cur = h->htable[hvalue];
	while (cur != NULL && h->keycmp(h, key, cur->key) > 0) {
		prev = cur;
		cur = cur->next;
	}

	if (cur && (h->keycmp(h, key, cur->key) == 0)) {
		if (destroy)
			destroy(cur->key, cur->datum, args);
		cur->key = key;
		cur->datum = datum;
	} else {
		newnode = (hashtab_ptr_t) malloc(sizeof(hashtab_node_t));
		if (newnode == NULL)
			return SEPOL_ENOMEM;
		memset(newnode, 0, sizeof(struct hashtab_node));
		newnode->key = key;
		newnode->datum = datum;
		if (prev) {
			newnode->next = prev->next;
			prev->next = newnode;
		} else {
			newnode->next = h->htable[hvalue];
			h->htable[hvalue] = newnode;
		}
	}

	return SEPOL_OK;
}
Пример #7
0
int hashtab_insert(hashtab_t h, hashtab_key_t key, hashtab_datum_t datum)
{
	int hvalue;
	hashtab_ptr_t prev, cur, newnode;

	if (!h)
		return SEPOL_ENOMEM;

	hvalue = h->hash_value(h, key);
	prev = NULL;
	cur = h->htable[hvalue];
	while (cur && h->keycmp(h, key, cur->key) > 0) {
		prev = cur;
		cur = cur->next;
	}

	if (cur && (h->keycmp(h, key, cur->key) == 0))
		return SEPOL_EEXIST;

	newnode = (hashtab_ptr_t) malloc(sizeof(hashtab_node_t));
	if (newnode == NULL)
		return SEPOL_ENOMEM;
	memset(newnode, 0, sizeof(struct hashtab_node));
	newnode->key = key;
	newnode->datum = datum;
	if (prev) {
		newnode->next = prev->next;
		prev->next = newnode;
	} else {
		newnode->next = h->htable[hvalue];
		h->htable[hvalue] = newnode;
	}

	h->nel++;
	return SEPOL_OK;
}