示例#1
0
Bool hashtable_contains(HashTable ht, Type key) { //compreuba que exista el key
    if (ht == NULL || key == NULL)
        return FALSE;
    int i;

    i = ht->hashcodeF(key) % ht->capacity;
    if (ht->l_array[i] == NULL)
        return FALSE;
    Iterator ite;
    ite = list_begin(ht->l_array[i]);
    while (list_hasNext(ite)) {
        ite = list_next(ite);
        if (ht->isKeyEqual(retrieveKeyFromIterator(ite), key)) {
            return TRUE;
        }
    }
    return FALSE;
}
示例#2
0
Type hashtable_get(HashTable ht, Type key) {  //recupera el value con el key
    if (ht == NULL || key == NULL)
        return NULL;
    int i;

    i = ht->hashcodeF(key) % ht->capacity;
    if (ht->l_array[i] == NULL)
        return NULL;
    Iterator ite;
    ite = list_begin(ht->l_array[i]);
    while (list_hasNext(ite)) {
        ite = list_next(ite);
        if (ht->isKeyEqual(retrieveKeyFromIterator(ite), key)) {
            return retrieveValueFromIterator(ite);
        }
    }
    return NULL;
}
示例#3
0
void hashtable_put(HashTable ht, Type key, Type value) { 
    int hashcode;
    if (key == NULL || ht == NULL) //clave o ht vacia
        return;
    hashcode = ht->hashcodeF(key);
    if (hashcode > ht->capacity) {
        hashcode = hashcode % ht->capacity; //aritmética modular si clave > capacidad
    }
    int i = hashcode;
    int iguales;
    int flag = 0;
    HtEntry nueva;

    Iterator it;
    if (ht->l_array[i] == NULL) { // si aun no existe la lista la crea
        ht->l_array[i] = list_create();
        nueva = (HtEntry) malloc(sizeof (struct strHtEntry));
        nueva->Value = value;
        nueva->Key = key;
        list_add(ht->l_array[i], nueva); // y agrega la nueva entrada
        ht->size++;
    } else { //si ya existia la lista
        it = list_begin(ht->l_array[i]);
        while (list_hasNext(it)) { //la recorre para ver si ya existe la key
            it = list_next(it);
            iguales = ht->isKeyEqual(retrieveKeyFromIterator(it), key);
            if (iguales == TRUE) { //si ya existía remplaza el valor
                setEntryValue(it, value);
                flag = 1;
            }
        }
        if (flag = 0) { //si recorrió y no encontro la clave debemos agregarla
            nueva = (HtEntry) malloc(sizeof (struct strHtEntry));
            nueva->Value = value;
            nueva->Key = key;
            list_add(ht->l_array[i], nueva);
        }
    }
}