Example #1
0
		//-----------------------------------------------------------------//
		uint32_t install(const std::string& path)
		{
			auto fpath = create_full_path(path);
			if(fpath.empty()) {
				return false;
			}

			return install_(strip_last_of_delimita_path(fpath));
		}
Example #2
0
		//-----------------------------------------------------------------//
		uint32_t make_directory(const std::string& path)
		{
			auto fpath = create_full_path(path);
			if(fpath.empty()) {
				return 0;
			}

			if(fpath.back() != '/') fpath += '/';

			return install_(fpath);
		}
Example #3
0
// allocate new bucket array; rehash keys and populate buckets; free old bucket array
static void resize_(cs_hash_tab *tab, float factor) {
    size_t size = (int)(factor * tab->size);
    if (size == 0)
        size = 1;
    cs_knode **buckets = calloc(sizeof(cs_knode *) * size, 1);
    for (int i = 0; i < tab->size; i++) {
        for (cs_knode *n = tab->buckets[i]; n != NULL;) {
            size_t index = tab->hash(n->key) % size;
            // install_() alters member 'next'; preserve it for now
            cs_knode *temp = n->next;
            install_(buckets, n, index);
            n = temp;
        }
    }
    tab->size = size;
    free(tab->buckets);
    tab->buckets = buckets;
}
Example #4
0
static void map_(cs_hash_tab *tab, const char *key, void *val) {
    size_t index = tab->hash(key) % tab->size;
    cs_knode *n = lookup_i_(tab, key, index);
    
    // a value is already defined for this key
    // cleanup old; assign new
    if (n != NULL) {
        if (tab->cleanup)
            tab->cleanup(n->key, n->val);
        n->val = val;
        return;
    }
        
    cs_knode *node = malloc(sizeof(cs_knode));
    node->key = (tab->copy_keys ? strdup(key) : (char *)key);
    node->val = val;
    
    install_(tab->buckets, node, index);
    
    if (tab->count++ / (float)tab->size >= tab->max_load)
        resize_(tab, 1.5f);
}