/**
 * Initialize element auxiliary function
 */
void _hashtable_init_elem_auxiliary(_hashtable_t* pt_hashtable, _hashnode_t* pt_node)
{
    assert(pt_hashtable != NULL);
    assert(pt_node != NULL);
    assert(_hashtable_is_inited(pt_hashtable) || _hashtable_is_created(pt_hashtable));

    /* initialize new elements */
    if (_GET_HASHTABLE_TYPE_STYLE(pt_hashtable) == _TYPE_CSTL_BUILTIN) {
        /* get element type name */
        char s_elemtypename[_TYPE_NAME_SIZE + 1];
        _type_get_elem_typename(_GET_HASHTABLE_TYPE_NAME(pt_hashtable), s_elemtypename);

        _GET_HASHTABLE_TYPE_INIT_FUNCTION(pt_hashtable)(pt_node->_pby_data, s_elemtypename);
    } else {
        bool_t b_result = _GET_HASHTABLE_TYPE_SIZE(pt_hashtable);
        _GET_HASHTABLE_TYPE_INIT_FUNCTION(pt_hashtable)(pt_node->_pby_data, &b_result);
        assert(b_result);
    }
}
Beispiel #2
0
/**
 * Inserts an array of unique element into a hashtable.
 */
void _hashtable_insert_unique_array(_hashtable_t* pt_hashtable, const void* cpv_array, size_t t_count)
{
    size_t i = 0;

    assert(pt_hashtable != NULL);
    assert(_hashtable_is_inited(pt_hashtable));
    assert(cpv_array != NULL);

    /*
     * Copy the elements from src array to dest hashtable
     * The array of c builtin and user define or cstl builtin are different,
     * the elements of c builtin array are element itself, but the elements of 
     * c string, user define or cstl are pointer of element.
     */
    if (strncmp(_GET_HASHTABLE_TYPE_BASENAME(pt_hashtable), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
        /*
         * We need built a string_t for c string element.
         */
        string_t* pstr_elem = create_string();
        assert(pstr_elem != NULL);
        string_init(pstr_elem);
        for (i = 0; i < t_count; ++i) {
            string_assign_cstr(pstr_elem, *((const char**)cpv_array + i));
            _hashtable_insert_unique(pt_hashtable, pstr_elem);
        }
        string_destroy(pstr_elem);
    } else if (_GET_HASHTABLE_TYPE_STYLE(pt_hashtable) == _TYPE_C_BUILTIN) {
        for (i = 0; i < t_count; ++i) {
            _hashtable_insert_unique(pt_hashtable, (unsigned char*)cpv_array + i * _GET_HASHTABLE_TYPE_SIZE(pt_hashtable));
        }
    } else {
        for (i = 0; i < t_count; ++i) {
            _hashtable_insert_unique(pt_hashtable, *((void**)cpv_array + i));
        }
    }
}