src/common/map.c
// Inserts a pre-hashed entry into the map
Inserts a pre-hashed entry into the map
Inserts a pre - hashed entry into the map
static enum natwm_error map_insert_entry(struct map *map, struct map_entry *entry)
{
        uint32_t initial_index = entry->hash % map->length;
        struct map_entry *present_entry = map->entries[initial_index];
        bool is_hash_collision = is_entry_present(present_entry);
        size_t key_size = map->key_size_function(entry->key);
        if (is_hash_collision
            && map->key_compare_function(entry->key, present_entry->key, key_size)) {
                map_entry_destroy(map, present_entry);
                map->entries[initial_index] = entry;
                return NO_ERROR;
        }
        int resize_direction = get_resize_direction(map, map->bucket_count + 1.0);
        if (resize_direction != 0) {
                enum natwm_error resize_error = map_resize(map, resize_direction);
                if (resize_error != NO_ERROR) {
                        return resize_error;
                }
                return map_insert_entry(map, entry);
        }
        if (!is_hash_collision) {
                if (map->entries[initial_index] != NULL
                    && map->entries[initial_index]->value == &EMPTY_ENTRY) {
                        map_entry_destroy(map, map->entries[initial_index]);
                }
                map->entries[initial_index] = entry;
                map->bucket_count += 1;
                return NO_ERROR;
        }
        if ((map->bucket_count + 1) > map->length) {
                map_entry_destroy(map, entry);
                return CAPACITY_ERROR;
        }
        enum natwm_error error = map_probe(map, entry, initial_index);
        if (error != NO_ERROR) {
                return error;
        }
        map->bucket_count += 1;
        return NO_ERROR;
}
static enum natwm_error map_insert_entry ( struct map * map , struct map_entry * entry ) { uint32_t initial_index = entry -> hash % map -> length ; struct map_entry * present_entry = map -> entries [ initial_index ] ; bool is_hash_collision = is_entry_present ( present_entry ) ; size_t key_size = map -> key_size_function ( entry -> key ) ; if ( is_hash_collision && map -> key_compare_function ( entry -> key , present_entry -> key , key_size ) ) { map_entry_destroy ( map , present_entry ) ; map -> entries [ initial_index ] = entry ; return NO_ERROR ; } int resize_direction = get_resize_direction ( map , map -> bucket_count + 1.0 ) ; if ( resize_direction != 0 ) { enum natwm_error resize_error = map_resize ( map , resize_direction ) ; if ( resize_error != NO_ERROR ) { return resize_error ; } return map_insert_entry ( map , entry ) ; } if ( ! is_hash_collision ) { if ( map -> entries [ initial_index ] != NULL && map -> entries [ initial_index ] -> value == & EMPTY_ENTRY ) { map_entry_destroy ( map , map -> entries [ initial_index ] ) ; } map -> entries [ initial_index ] = entry ; map -> bucket_count += 1 ; return NO_ERROR ; } if ( ( map -> bucket_count + 1 ) > map -> length ) { map_entry_destroy ( map , entry ) ; return CAPACITY_ERROR ; } enum natwm_error error = map_probe ( map , entry , initial_index ) ; if ( error != NO_ERROR ) { return error ; } map -> bucket_count += 1 ; return NO_ERROR ; }
Inserts a pre-hashed entry into the map
Inserts a pre - hashed entry into the map
// Get the new initial idex // If there is a collision with the same key overwrite it // Now we need to increase map->bucket_count // Insert with the new size // No collision - just insert here // We previously had a value here - delete it and // replace // Handle collision