Exemplo n.º 1
0
/*
 * Insert key in map
 * Returns A pointer to the hash_entry corresponding to key
 *         Null in case of error
 * status is set to:
 *         The position of hash_entry in case of success
 *         One of the following in case of errors:
 *          MOCA_HASHMAP_ALREADY_IN_MAP
 *          MOCA_HASHMAP_FULL
 *          MOCA_HASHMAP_ERROR
 */
hash_entry Moca_AddToMap(hash_map map, hash_entry e, int *status)
{
    unsigned long h;
    int ind=0;
    unsigned int nextPos;
    if(!map)
    {
        *status=MOCA_HASHMAP_ERROR;
        return NULL;
    }
    if(map->nbentry==map->tableSize)
    {
        *status=MOCA_HASHMAP_FULL;
        return NULL;
    }
    //Do the insertion
    nextPos=Moca_FindNextAvailPosMap(map);
    MOCA_DEBUG_PRINT("Moca inserting %p ind %d/%lu total %d\n",
            e->key,nextPos,map->tableSize, map->nbentry);
    if(nextPos >= map->tableSize)
    {
        *status=MOCA_HASHMAP_ERROR;
        Moca_Panic("Moca hashmap BUG in AddToMap");
        return NULL;
    }
    //Update the link
    h=hash_ptr(e->key, map->hash_bits);
    ind=map->hashs[h];
    //TODO refactor here
    if(ind<0)
    {
        memcpy(tableElt(map,nextPos),e,map->elt_size);
        tableElt(map,nextPos)->next=MOCA_HASHMAP_END;
        map->hashs[h]=nextPos;
    }
    else
    {
        while(map->comp(tableElt(map,ind),e)!=0 &&
                tableElt(map,ind)->next>=0)
            ind=tableElt(map,ind)->next;
        if(map->comp(tableElt(map,ind),e)==0)
        {
            MOCA_DEBUG_PRINT("Moca %p already in map %p\n", e->key, map);
            *status=MOCA_HASHMAP_ALREADY_IN_MAP;
            //This seems useless
            tableElt(map,nextPos)->key=NULL;
            return tableElt(map,ind);
        }
        MOCA_DEBUG_PRINT("Moca collision in map %p key %p\n", map, e->key);
        //TODO: Use Memcpy
        memcpy(tableElt(map,nextPos),e,map->elt_size);
        tableElt(map,nextPos)->next=MOCA_HASHMAP_END;
        tableElt(map,ind)->next=nextPos;
    }
    ++map->nbentry;
    MOCA_DEBUG_PRINT("Moca Inserted %p in map %p\n", e->key, map);
    *status=nextPos;
    return tableElt(map,nextPos);
}
Exemplo n.º 2
0
hash_entry Moca_RemoveFromMap(hash_map map,hash_entry e)
{
    unsigned long h;
    int ind, ind_prev=MOCA_HASHMAP_END;
    if(!map)
        return NULL;
    MOCA_DEBUG_PRINT("Moca removing %p from %p\n", e->key, map);
    h=hash_ptr(e->key, map->hash_bits);
    ind=map->hashs[h];
    while(ind>=0 && map->comp(tableElt(map,ind),e)!=0 )
    {
        ind_prev=ind;
        ind=tableElt(map,ind)->next;
    }
    MOCA_DEBUG_PRINT("Moca removing %p from %p ind %d prev %d\n", e->key, map,
            ind, ind_prev);
    //key wasn't in map
    if(ind<0 )
        return NULL;
    //Remove from list
    if(ind_prev>=0)
    {
        tableElt(map,ind_prev)->next=tableElt(map,ind)->next;
    }
    else
    {
        map->hashs[h]=tableElt(map,ind)->next;
    }
    tableElt(map,ind)->next=MOCA_HASHMAP_UNUSED;
    --map->nbentry;
    MOCA_DEBUG_PRINT("Moca removing %p from %p ind %d ok\n", e->key, map, ind);
    return tableElt(map,ind);
}
Exemplo n.º 3
0
/*
 * Returns -1 if key is not in map
 *         the position of key in the map if it is present
 */
int Moca_PosInMap(hash_map map,hash_entry e)
{
    unsigned long h;
    int ind=0;
    if(!map)
        return -1;
    h=hash_ptr(e->key, map->hash_bits);
    ind=map->hashs[h];
    while(ind>=0 && map->comp(tableElt(map,ind),e)!=0 )
        ind=tableElt(map,ind)->next;
    return ind;
}
Exemplo n.º 4
0
/*
 * Return the hash entry corresponding to key,
 *        NULL if key is not in the map
 */
hash_entry Moca_EntryFromKey(hash_map map, hash_entry e)
{
    unsigned long h;
    int ind=0;
    if(!map || !e)
        return NULL;
    h=hash_ptr(e->key, map->hash_bits);
    ind=map->hashs[h];
    while(ind>=0 && map->comp(tableElt(map,ind),e)!=0 )
        ind=tableElt(map,ind)->next;
    if(ind >=0)
        return tableElt(map,ind);
    return NULL;
}