// shallow copying except when entry is a dictionary // // parameter: k: key, v: value, a: dictionary // static void dict_entry_copy(char *k, void *v, void *a) { dict_entry *e = (dict_entry *)v; pdf_obj *val; char *p; val = pdf_malloc(sizeof(pdf_obj)); #ifdef HASHMAP *val = *(pdf_obj*)v; #else *val = *(pdf_obj*)(e->v); #endif switch (val->t) { case eDict: val->value.d.dict = dict_copy(((pdf_obj*)(e->v))->value.d.dict); break; case eName: case eString: case eHexString: case eArray: break; default: break; } #ifdef HASHMAP if (pdf_keyword_find(k, strlen(k))) { p = k; } else { p = pdfname_search(k); if (!p) { // memory leak p = pdf_malloc(strlen(k)+1); memcpy(p, k, strlen(k)+1); } } #else if (pdf_keyword_find(e->k, strlen(e->k))) { p = e->k; } else { p = pdf_malloc(strlen(e->k)+1); memcpy(p, e->k, strlen(e->k)+1); } #endif dict_insert((dict*)a, p, val); }
awlenv* awlenv_copy(awlenv* e) { awlenv* n = safe_malloc(sizeof(awlenv)); n->parent = e->parent; if (n->parent) { n->parent->references++; } n->internal_dict = dict_copy(e->internal_dict); n->top_level = e->top_level; n->references = 1; return n; }
STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { mp_obj_t dict; switch (n_args) { case 0: dict = mp_obj_new_dict(0); break; case 1: { if (MP_OBJ_IS_TYPE(args[0], &mp_type_dict)) { return dict_copy(args[0]); } // TODO create dict from an arbitrary mapping! // Make dict from iterable of pairs mp_obj_t iterable = mp_getiter(args[0]); mp_obj_t dict = mp_obj_new_dict(0); // TODO: support arbitrary seq as a pair mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { mp_obj_t *sub_items; mp_obj_get_array_fixed_n(item, 2, &sub_items); mp_obj_dict_store(dict, sub_items[0], sub_items[1]); } return dict; } default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "dict takes at most 1 argument")); } // add to the new dict any keyword args for (const mp_obj_t *a = args + n_args; n_kw > 0; n_kw--, a += 2) { mp_obj_dict_store(dict, a[0], a[1]); } return dict; }
int main(void) { char *option = NULL, *word = NULL, *def = NULL, *filename = NULL; bool exit = false; dict_t dict = NULL,copy = NULL; dict = dict_empty(); do { option = print_menu(); switch (*option) { case ADD: printf("\tPlease enter the word to add to the dict: "); word = readline_from_stdin(); if (!dict_exists(dict, word)) { printf("\tPlease enter the definition: "); def = readline_from_stdin(); dict_add(dict,word,def); printf("\t-> The word and definition were added.\n"); free(def); def = NULL; } else { printf("\t-> The word %s already exists.\n", word); } assert(dict_exists(dict,word)); free(word); word = NULL; break; case COPY: copy = dict_copy(dict); printf("\t-> The dictionary was duplicated. Showing the duplicated dict:\n"); printf("{\n"); dict_dump(copy, stdout); printf("\n}\n"); copy = dict_destroy(copy); break; case DELETE: printf("\tPlease enter the word to delete from the dict: "); word = readline_from_stdin(); if(dict_exists(dict,word)) { dict = dict_remove(dict,word); printf("\t-> The word was successfully removed.\n"); } else { printf("\t-> The word %s does not exist.\n", word); } assert(!dict_exists(dict, word)); free(word); word = NULL; break; case DUMP: printf("\tPlease enter the filename to dump the dict to: "); filename = readline_from_stdin(); dict_to_file(dict,filename); printf("\t-> The dictionary was successfully dumped.\n"); free(filename); filename = NULL; break; case EMPTY: dict = dict_destroy(dict); dict = dict_empty(); printf("\t-> The dictionary was successfully emptied.\n"); break; case EXIT: printf("\t-> Exiting.\n"); exit = true; break; case LOAD: printf("\tPlease enter the filename to load the dict from: "); filename = readline_from_stdin(); copy = dict_from_file(filename); if (copy == NULL) { printf("Can not load dict from filename %s\n", filename); } else { dict = dict_destroy(dict); dict = copy; printf("\t-> The dictionary was successfully loaded.\n"); } free(filename); filename = NULL; break; case SEARCH: printf("\tPlease enter the word to search in the dict: "); word = readline_from_stdin(); if(dict_exists(dict,word)) { def = dict_search(dict,word); printf("\t-> The definition for \"%s\" is \"%s\".\n", word, def); free(def); def = NULL; } else { printf("\t-> The word \"%s\" does not exist in the dict.\n", word); } free(word); word = NULL; break; case SHOW: printf("{\n"); dict_dump(dict, stdout); printf("\n}\n"); break; case SIZE: printf("\t-> The size is %u.\n", dict_length(dict)); break; default: printf("\n\"%c\" is invalid. Please choose a valid option.\n\n", *option); } free(option); option = NULL; } while (!exit); dict = dict_destroy(dict); return (0); }
int zdefault_make_font(gs_font_dir * pdir, const gs_font * oldfont, const gs_matrix * pmat, gs_font ** ppfont) { gs_font *newfont = *ppfont; gs_memory_t *mem = newfont->memory; /* HACK: we know this font was allocated by the interpreter. */ gs_ref_memory_t *imem = (gs_ref_memory_t *)mem; ref *fp = pfont_dict(oldfont); font_data *pdata; ref newdict, newmat, scalemat; uint dlen = dict_maxlength(fp); uint mlen = dict_length(fp) + 3; /* FontID, OrigFont, ScaleMatrix */ int code; if (dlen < mlen) dlen = mlen; if ((pdata = gs_alloc_struct(mem, font_data, &st_font_data, "make_font(font_data)")) == 0 ) return_error(e_VMerror); /* * This dictionary is newly created: it's safe to pass NULL as the * dstack pointer to dict_copy and dict_put_string. */ if ((code = dict_alloc(imem, dlen, &newdict)) < 0 || (code = dict_copy(fp, &newdict, NULL)) < 0 || (code = gs_alloc_ref_array(imem, &newmat, a_all, 12, "make_font(matrices)")) < 0 ) return code; refset_null_new(newmat.value.refs, 12, imemory_new_mask(imem)); ref_assign(&scalemat, &newmat); r_set_size(&scalemat, 6); scalemat.value.refs += 6; /* * Create the scaling matrix. We could do this several different * ways: by "dividing" the new FontMatrix by the base FontMatrix, by * multiplying the current scaling matrix by a ScaleMatrix kept in * the gs_font, or by multiplying the current scaling matrix by the * ScaleMatrix from the font dictionary. We opt for the last of * these. */ { gs_matrix scale, prev_scale; ref *ppsm; if (!(dict_find_string(fp, "ScaleMatrix", &ppsm) > 0 && read_matrix(mem, ppsm, &prev_scale) >= 0 && gs_matrix_multiply(pmat, &prev_scale, &scale) >= 0) ) scale = *pmat; write_matrix_new(&scalemat, &scale, imem); } r_clear_attrs(&scalemat, a_write); r_set_size(&newmat, 6); write_matrix_new(&newmat, &newfont->FontMatrix, imem); r_clear_attrs(&newmat, a_write); if ((code = dict_put_string(&newdict, "FontMatrix", &newmat, NULL)) < 0 || (code = dict_put_string(&newdict, "OrigFont", pfont_dict(oldfont->base), NULL)) < 0 || (code = dict_put_string(&newdict, "ScaleMatrix", &scalemat, NULL)) < 0 || (code = add_FID(NULL, &newdict, newfont, imem)) < 0 ) return code; newfont->client_data = pdata; *pdata = *pfont_data(oldfont); pdata->dict = newdict; r_clear_attrs(dict_access_ref(&newdict), a_write); return 0; }
static void test_dict(void) { DictObject *mp = dict_cnew(10000, 0, 0, 0, 0, 0, 0, 0); //DictObject *mp = dict_new(); char keybuf[100]; size_t valuebuf[] = { 1 }; dict_add(mp, "a", valuebuf); dict_del(mp, "a"); dict_add(mp, "b", valuebuf); dict_del(mp, "b"); dict_add(mp, "c", valuebuf); dict_del(mp, "c"); dict_add(mp, "d", valuebuf); dict_del(mp, "d"); dict_add(mp, "e", valuebuf); dict_del(mp, "e"); dict_add(mp, "xffff", valuebuf); *valuebuf = 123456789; dict_set(mp, "test", valuebuf); DictObject *copy = dict_copy(mp); dict_print_by_value_desc(copy); fprintf(stdout, "===above is copy===\n"); dict_free(copy); *valuebuf = 1; size_t *vp; // while (fscanf(stdin, "%s", keybuf) == 1) { // vp = dict_get(mp, keybuf); // if (vp) // *vp += 1; // else // dict_add(mp, keybuf, valuebuf); // } // this is another faster version while (fscanf(stdin, "%s", keybuf) == 1) { vp = dict_fget(mp, keybuf); *vp += 1; } *valuebuf = 123456789; dict_set(mp, "the", valuebuf); *valuebuf = 145678999; dict_set(mp, "xxx", valuebuf); dict_del(mp, "xxx"); DictObject *mp2 = dict_new(); *valuebuf = 99999999; dict_set(mp2, "xiangnan", valuebuf); *valuebuf = 89999999; dict_add(mp2, "laoma", valuebuf); dict_update(mp, mp2); dict_free(mp2); //dict_print_by_value_desc(mp); dict_print(mp); void *key, *value; IterObject *dio = iter(mp); printf("\ntest iterw...\n"); while(iterw(dio, &key)) { value = dict_get(mp, key); fprintf(stdout, "%s\t%u\n", (char*)key, *(size_t*)value); } *valuebuf = 9888888; dict_set(mp, "emacs", valuebuf); iterf(dio); printf("\nnow test dict_iterkv...\n"); while(dict_iterkv(dio, &key, &value)) { fprintf(stdout, "%s\t%u\n", (char*)key, *(size_t*)value); } free(dio); dict_clear(mp); dict_clear(mp); //just for test dict_free(mp); }
awlval* awlval_copy(const awlval* v) { awlval* x = safe_malloc(sizeof(awlval)); x->type = v->type; switch (v->type) { case AWLVAL_BUILTIN: x->builtin = v->builtin; x->builtin_name = safe_malloc(strlen(v->builtin_name) + 1); strcpy(x->builtin_name, v->builtin_name); break; case AWLVAL_FN: case AWLVAL_MACRO: x->env = awlenv_copy(v->env); x->formals = awlval_copy(v->formals); x->body = awlval_copy(v->body); x->called = v->called; break; case AWLVAL_INT: x->lng = v->lng; break; case AWLVAL_FLOAT: x->dbl = v->dbl; break; case AWLVAL_ERR: x->err = safe_malloc(strlen(v->err) + 1); strcpy(x->err, v->err); break; case AWLVAL_SYM: case AWLVAL_QSYM: x->length = v->length; x->sym = safe_malloc(strlen(v->sym) + 1); strcpy(x->sym, v->sym); break; case AWLVAL_STR: x->length = v->length; x->str = safe_malloc(strlen(v->str) + 1); strcpy(x->str, v->str); break; case AWLVAL_BOOL: x->bln = v->bln; break; case AWLVAL_DICT: x->count = v->count; x->length = v->length; x->d = dict_copy(v->d); break; case AWLVAL_SEXPR: case AWLVAL_QEXPR: case AWLVAL_EEXPR: case AWLVAL_CEXPR: x->count = v->count; x->length = v->length; x->cell = safe_malloc(sizeof(awlval*) * x->count); for (int i = 0; i < x->count; i++) { x->cell[i] = awlval_copy(v->cell[i]); } break; } return x; }