Example #1
0
/*
 * if key exists, set the new value.else add a pair of 'key => value' to dict
 */
void dict_set(dict_t *dict,  char *keyarg, void *value)
{
	//if need resize
	char *key = strToHeap(keyarg);
	if (prime_array[dict->size_pos] - dict->ele_num <= 1) dict_resize(dict); 
	//get index by hash function
	int index = get_index(dict, key), flag = 0; //flag for if find the key
	
	//get its bucket's head node's pointer
	bucket_t *first = (dict->bucket)[index], *temp = first; 
	
	for (; temp; temp = temp->next)
		if (strcmp(temp->key, key) == 0){
			//free the old value
			free(temp->value);
			temp->value = value; 
			flag = 1; 
		}
	
	if (!flag){//not find key, new a node , and append it to the list
		
		bucket_t *node = (bucket_t *)malloc(sizeof(bucket_t)); 
		node->key = key; 
		node->value = value; 
		node->next = NULL; 
		
		if (!first) (dict->bucket)[index] = node; 
		else{
			for (; first->next; first = first->next); 
			first->next = node; 
		}
		dict->ele_num += 1; 
	}
}
Example #2
0
//creates a new, empty dict
LispObject *new_dict() {
    Dict *out = alloc(sizeof(*out));
    out->type = &DictType;
    out->array_size = 0;
    out->primei = -1;
    out->keys = NULL;
    out->values = NULL;
    dict_resize(out);
    return (LispObject*)out;
}
Example #3
0
/* Grow a dictionary for dict_put. */
int
dict_grow(ref * pdref, dict_stack_t *pds)
{
    dict *pdict = pdref->value.pdict;
    /* We might have maxlength < npairs, if */
    /* dict_round_size increased the size. */
    ulong new_size = (ulong) d_maxlength(pdict);
    /* Adobe does this */
    if (new_size < 20)
        new_size += 10;
    else if (new_size < 200)
        new_size *= 2;
    else
        new_size += new_size / 2;
#if ARCH_SIZEOF_INT < ARCH_SIZEOF_LONG
    if (new_size > max_uint)
        new_size = max_uint;
#endif
    if (new_size > npairs(pdict)) {
        int code = dict_resize(pdref, (uint) new_size, pds);

        if (code >= 0)
            return code;
        /* new_size was too big. */
        if (npairs(pdict) < dict_max_size) {
            code = dict_resize(pdref, dict_max_size, pds);
            if (code >= 0)
                return code;
        }
        if (npairs(pdict) == d_maxlength(pdict)) {	/* Can't do it. */
            return code;
        }
        /* We can't grow to new_size, but we can grow to npairs. */
        new_size = npairs(pdict);
    }
    /* maxlength < npairs, we can grow in place */
    ref_save_in(dict_memory(pdict), pdref, &pdict->maxlength,
                "dict_put(maxlength)");
    d_set_maxlength(pdict, new_size);
    return 0;
}
Example #4
0
//sets the value for key key to value in d
void dict_setitem(Dict *d, LispObject *key, LispObject *value) {
    int i;
    if(dict_find_index(d, key, &i))
        d->values[i] = value;
    else if (i >= 0) {
        d->keys[i] = key;
        d->values[i] = value;
        d->size++;
        if(d->size > d->array_size / 2)
            dict_resize(d);
    } else
        error("dict full somehow\n");
}
Example #5
0
void dict_set(struct dict *dict, const char *key, void *data)
{
    if (dict->length + 1 >= dict->resize_threshold) {
        dict_resize(dict);
    }

    dict->length++;

    uint32_t hash = lookup3_hash(key);
    uint32_t pos = hash % dict->capacity;
    uint32_t probe_dist, dist = 0;

    const char *temp_key;
    uint32_t temp_hash;
    void *temp_data;

    struct bucket *cur_bucket;

    while (1) {
        if (!dict->buckets[pos].setted) {
            set_bucket(&dict->buckets[pos], hash, (char*)key, data);
            return;
        }

        probe_dist = probe_distance(dict, dict->buckets[pos].hash, pos);
        if (probe_dist < dist) {
            if (dict->buckets[pos].deleted) {
                set_bucket(&dict->buckets[pos], hash, (char*)key, data);
                return;
            }
            cur_bucket = &dict->buckets[pos];
            temp_hash = cur_bucket->hash;
            cur_bucket->hash = hash;
            hash = temp_hash;

            temp_key = cur_bucket->key;
            cur_bucket->key = (char*)key;
            key = temp_key;

            temp_data = cur_bucket->data;
            cur_bucket->data = data;
            data = temp_data;

            dist = probe_dist;
        }
        pos = (pos + 1) % dict->capacity;
        ++dist;
    }
}
Example #6
0
File: dict.c Project: ktosiu/C-Snip
/* Set a key into dict. */
int
dict_iset(struct dict *dict, char *key, size_t len, void *val)
{
    assert(dict != NULL);

    if ((dict_table_sizes[dict->idx] * DICT_LOAD_LIMIT < dict->len + 1) &&
            dict_resize(dict) != DICT_OK)
        return DICT_ENOMEM;

    size_t index = dict_table_idx(dict->idx, key, len);
    struct dict_node *node = (dict->table)[index];

    /* try to find this key. */
    while (node != NULL) {
        if (dict_key_equals(node->key, node->len, key, len)) {
            node->key = key;
            node->len = len;
            node->val = val;
            return DICT_OK;
        }
        node = node->next;
    }

    /* create node if not found */
    struct dict_node *new_node = dict_node_new(key, len, val);

    if (new_node == NULL)
        return DICT_ENOMEM;

    /* rewind to list head */
    node = (dict->table)[index];

    if (node == NULL) {
        /* if list is empty, set as head node */
        (dict->table)[index] = new_node;
    } else {
        /* else append as tail node */
        while (node->next != NULL)
            node = node->next;
        node->next = new_node;
    }

    dict->len += 1;
    return DICT_OK;
}
Example #7
0
static void parse_group(PrintDest *pd, ppd_group_t *group)
{
    int           i, j;           /* Looping vars */
    ppd_option_t  *option;        /* Current option */
    ppd_choice_t  *choice;        /* Current choice */
    ppd_group_t   *subgroup;      /* Current subgroup */
    PrintOptGroup *og;
    PrintOption   *po;

    pd->ogroups = xrealloc(pd->ogroups, (pd->nogroups + 1)*sizeof(PrintOptGroup));
    if (!pd->ogroups) {
        return;
    }
    
    og = &pd->ogroups[pd->nogroups];
    pd->nogroups++;
    
    og->name = copy_string(NULL, group->name);
    og->text = copy_string(NULL, group->text);
    
    og->opts = xcalloc(group->num_options, sizeof(PrintOption));
    if (!og->opts) {
        return;
    }
    og->nopts = 0;

    for (i = 0, option = group->options, po = og->opts; i < group->num_options; i++, option++) {
        po->name = copy_string(NULL, option->keyword);
        po->text = copy_string(NULL, option->text);

        po->choices = dict_new();
        po->selected = -1;
        dict_resize(po->choices, option->num_choices);
        for (j = 0, choice = option->choices; j < option->num_choices; j++, choice++) {
            DictEntry de;
            
            de.key   = j;
            de.name  = choice->choice;
            de.descr = choice->text;
            dict_entry_copy(&po->choices->entries[j], &de);

            if (choice->marked) {
                po->selected = de.key;
                dict_entry_copy(&po->choices->defaults, &de);
            }
        }
        if (po->selected != -1) {
            og->nopts++;
            po++;
            pd->nopts++;
        } else {
            xfree(po->name);
            xfree(po->text);
            dict_free(po->choices);
        }
    }

    for (i = 0, subgroup = group->subgroups; i < group->num_subgroups; i++, subgroup++) {
        parse_group(pd, subgroup);
    }
}