int main () { int res; res = NCDStringIndex_Init(&string_index); ASSERT_FORCE(res) NCDValMem_Init(&mem, &string_index); res = NCDValCons_Init(&cons, &mem); ASSERT_FORCE(res) NCDValRef val1 = complete(list_prepend(list_prepend(list_prepend(make_list(), make_string("hello")), make_string("world")), make_list())); char *str1 = NCDValGenerator_Generate(val1); ASSERT_FORCE(str1) ASSERT_FORCE(!strcmp(str1, "{{}, \"world\", \"hello\"}")) free(str1); NCDValRef val2 = complete(map_insert(map_insert(map_insert(make_map(), make_list(), make_list()), make_string("A"), make_list()), make_string("B"), make_list())); char *str2 = NCDValGenerator_Generate(val2); ASSERT_FORCE(str2) printf("%s\n", str2); ASSERT_FORCE(!strcmp(str2, "[\"A\":{}, \"B\":{}, {}:{}]")) free(str2); NCDValCons_Free(&cons); NCDValMem_Free(&mem); NCDStringIndex_Free(&string_index); return 0; }
int main() { std::map<announcer,announcer>m1; std::cout<<"checkpoint A\n"; map_insert(m1, announcer(1.0), announcer(2.0)); std::cout<<"checkpoint B\n"; map_insert(m1, announcer(3.0), 4.0); std::cout<<"checkpoint C\n"; map_insert(m1, 5.0, announcer(6.0));//error: static assertion failed: implicit conversions disallowed for the key! std::cout<<"map size="<<m1.size()<<'\n'; }
int main() { int i; map_init(); map_insert("nihao",1); map_insert("nishi",2); map_insert("nishi",3); map_delete("nishi"); printf("%d %d\n",map_find("nishi"),map_find("nihao")); printf("\n"); return 0; }
/* * Insert symbol into environment. If the symbol is already in the environment, * return 0. Otherwise, insert it and return 1 */ int Env_put(Env * env, char * id, Symbol * sym) { if (map_find(&env->table, id) != NULL) return 0; if (sym->type != TYPE_FN) if (map_find(&env->prot_table, id) != NULL) return 0; //sym->scope = env; if (sym->type == TYPE_FN_PROT) map_insert(&env->prot_table, id, sym); else map_insert(&env->table, id, sym); return 1; }
/* A function that given a file (struct file*, see filesys/file.h) and a process id INSERT this in a list of files. Return an integer that can be used to find the opened file later. */ int32_t flist_add_file(struct file* file, struct thread* t) { int32_t k = map_insert(&(t->open_files), file); return k; }
static void push_fnc(struct context *context, struct byte_array *program) { uint32_t num_closures = serial_decode_int(program); struct map *closures = NULL; for (int i=0; i<num_closures; i++) { struct byte_array *name = serial_decode_string(program); if (context->runtime) { if (!closures) closures = map_new(); struct variable *c = find_var(context, name); c = variable_copy(context, c); map_insert(closures, name, c); } } struct byte_array *body = serial_decode_string(program); DEBUGPRINT("FNC %u,%u\n", num_closures, body->length); //display_code(context, body); if (context->runtime) { struct variable *f = variable_new_fnc(context, body, closures); variable_push(context, f); } }
int map_resize(struct map *m, size_t size) { //DEBUGPRINT("map_resize\n"); struct map newtbl; size_t n; struct hash_node *node,*next; newtbl.size = size; newtbl.hash_func = m->hash_func; if ((newtbl.nodes = (struct hash_node**)calloc(size, sizeof(struct hash_node*))) == NULL) return -1; for (n = 0; n<m->size; ++n) { for(node = m->nodes[n]; node; node = next) { next = node->next; map_insert(&newtbl, node->key, node->data); map_remove(m, node->key); } } free(m->nodes); m->size = newtbl.size; m->nodes = newtbl.nodes; return 0; }
static void test_map_insert(void *o, sha1_t *keys, size_t count) { size_t i; map_t *m = o; for (i = 0; i < count; i++) map_insert(m, &keys[i], GINT_TO_POINTER(i+1)); }
bool sys_create(const char* file, unsigned initial_size) { if (filesys_create(file, initial_size) && initial_size >= 0) { map_insert(get_filemap(), filesys_open(file)); return true; } return false; }
static void set_global(su_state *s, const char *var, unsigned hash, int size, value_t *val) { value_t key, m; key.type = SU_STRING; key.obj.gc_object = string_from_cache(s, var, size); m = unref_local(s, s->stack[SU_GLOBAL_INDEX].obj.loc); m = map_insert(s, m.obj.m, &key, hash, val); set_local(s, s->stack[SU_GLOBAL_INDEX].obj.loc, &m); }
/* * str_insert -- hs_insert wrapper which works on strings */ static void str_insert(const char *str) { uint64_t key; if (sscanf(str, "%lu", &key) > 0) map_insert(mapc, map, key, OID_NULL); else fprintf(stderr, "insert: invalid syntax\n"); }
void* tech_malloc(uint32 size) { void* ptr; ptr = Malloc(size); assert (ptr); map_insert(memory, ptr, (void*)size); return ptr; }
void map_insert(struct map * m, int fd, int id) { int hash = fd & (m->size-1); struct node * n = &m->hash[hash]; if (n->fd < 0) { n->fd = fd; n->id = id; return; } int ohash = n->fd & (m->size-1); if (hash != ohash) { struct node * last = &m->hash[ohash]; while (last->next != &m->hash[hash]) { last = last->next; } last->next = n->next; int ofd = n->fd; int oid = n->id; n->fd = fd; n->id = id; n->next = NULL; map_insert(m,ofd, oid); return; } { // cover old value struct node *iter_n = n; for (; iter_n!=NULL; iter_n=iter_n->next) { if (iter_n->fd == fd) { iter_n->id = id; return; } } } int last = (n - m->hash) * 2; int i; for (i=0;i<m->size;i++) { int idx = (i + last + 1) & (m->size - 1); struct node * temp = &m->hash[idx]; if (temp->fd < 0) { temp->fd = fd; temp->id = id; temp->next = n->next; n->next = temp; return; } } assert(0); }
void va_attribute_count_add( int size, int count, container *attributes, int argc, va_list *ap ) { ant[0]++; int i; char *id = va_arg(*ap, char*); container *subattr; iterator it = map_find(attributes, id); if (it.valid) { ant[1]++; subattr = pair(map_val(it)).first.ptr; container *M = pair(map_val(it)).second.ptr; it = map_find(M, count); if (it.valid) { ant[2]++; map_val(it).i+= size; } else { ant[3]++; map_insert(M, count, size); } //for (i=0; i<len; i++) //((int*)pair(map_val(it)).second.ptr)[i]+= count[i]; } else { ant[4]++; subattr = map_container( string_container(), pair_container( ptr_container(), ptr_container() ) ); container *M = map_container( int_container(), int_container() ); map_insert(M, count, size); //int *C = malloc(sizeof(int)*len); //for (i=0; i<len; i++) C[i] = count[i]; map_insert(attributes, id, subattr, M); } if (argc > 1) va_attribute_count_add( size, count, subattr, argc-1, ap ); }
void* tech_calloc(uint32 value, uint32 size) { void* ptr; ptr = Malloc(size); assert (ptr); memset(ptr, value, size); map_insert(memory, ptr, (void*)size); return ptr; }
Map* count(Tree tree) { Map* accum = map_new(); char c; for (c = MIN_ITEM; c <= MAX_ITEM; c++) { int* i = malloc(sizeof(int)); *i = 0; map_insert(accum, c, i); } count_accum(tree, accum); return accum; }
struct _map * rdis_g_references (struct _rdis * rdis) { struct _map * references = map_create(); struct _graph_it * git; // for each node for (git = graph_iterator(rdis->graph); git != NULL; git = graph_it_next(git)) { struct _graph_node * node = graph_it_node(git); struct _list_it * lit; // for each instruction for (lit = list_iterator(node->data); lit != NULL; lit = lit->next) { struct _ins * ins = lit->data; struct _list_it * rit; // for each reference for (rit = list_iterator(ins->references); rit != NULL; rit = rit->next) { struct _reference * reference = rit->data; int delete_reference = 0; if (reference->type == REFERENCE_CONSTANT) { uint64_t lower = map_fetch_max_key(rdis->memory, reference->address); struct _buffer * buffer = map_fetch(rdis->memory, lower); if (buffer == NULL) continue; uint64_t upper = lower + buffer->size; if ( (reference->address < lower) || (reference->address >= upper)) continue; reference = object_copy(reference); reference->type = REFERENCE_CONSTANT_ADDRESSABLE; delete_reference = 1; } struct _list * ref_list = map_fetch(references, reference->address); if (ref_list == NULL) { ref_list = list_create(); map_insert(references, reference->address, ref_list); object_delete(ref_list); ref_list = map_fetch(references, reference->address); } list_append(ref_list, reference); if (delete_reference) object_delete(reference); } } } return references; }
void su_map(su_state *s, int num) { int i; value_t k, v; value_t m = map_create_empty(s); for (i = num * 2; i > 0; i -= 2) { k = *STK(-i); v = *STK(-i + 1); m = map_insert(s, m.obj.m, &k, hash_value(&k), &v); } s->stack_top -= num * 2; push_value(s, &m); }
void *su_reg_reference(su_state *s, int idx) { value_t key; unsigned hash; su_assert(s, s->main_state == s, MAIN_STATE_ONLY_MSG); key.type = SU_NATIVEPTR; key.obj.ptr = (void*)s->msi->ref_counter; hash = hash_value(&key); s->stack[SU_REGISTRY_INDEX] = map_insert(s, s->stack[SU_REGISTRY_INDEX].obj.m, &key, hash, STK(TOP(idx))); s->msi->ref_counter++; assert(s->msi->ref_counter); return key.obj.ptr; }
void test1(void) { int i; map_t * map = map_new(strcmp); for (i = 0; test1_vals[i].key != NULL; i++) map_insert(map, test1_vals[i].key, test1_vals[i].val); for (i = 0; test1_vals[i].key != NULL; i++) assert(strcmp((char*)map_get(map, test1_vals[i].key), test1_vals[i].val) == 0); map_free(map); }
void map_grow(Map* map) { Map* new_map = map_new_sz(map->length * 2); int i; for (i = 0; i < map->length; i++) { if (map->items[i].key) { map_insert(new_map, map->items[i].key, map->items[i].value); } } free(map->items); map->length = new_map->length; map->items = new_map->items; free(new_map); }
static value_t init_globals(su_state *s) { value_t key, m, tmp; key.type = SU_STRING; key.obj.gc_object = string_from_cache(s, "_G", 2); tmp.type = SU_NIL; m = map_create_empty(s); tmp = ref_local(s, &tmp); m = map_insert(s, m.obj.m, &key, hash_value(&key), &tmp); set_local(s, tmp.obj.loc, &m); return tmp; }
int sys_open(const char* file) { struct file* temp = filesys_open(file); if(temp != NULL) { // Mata in pekaren till filen i mapen // filemap finns initierad i thread.h/c return map_insert(get_filemap(), temp); // returnerar värdet i mapen } return -1; // filen fanns inte }
static void test(struct map *m) { int a[MAX * 2]; int i; int s = 0; for (i=0;i<MAX*2;i++) { int inc = random() % 3 + 1; s += inc; a[i] = s; } for (i=0;i<MAX * 2;i++) { int x = random()%(MAX*2); int y = random()%(MAX*2); int temp = a[x]; a[x] = a[y]; a[y] = temp; } for (i=0;i<MAX;i++) { map_insert(m, a[i], i); } for (i=0;i<MAX;i++) { int id = map_search(m,a[i]); assert(id == i); } for (i=0;i<MAX/2;i++) { map_erase(m, a[i]); } for (i=0;i<MAX/2;i++) { map_insert(m,a[i+MAX],i); } for (i=0;i<MAX;i++) { int id = map_search(m,a[i+MAX/2]); if (i>=MAX/2) { assert(id == i - MAX/2); } else { assert(id == i + MAX/2); } } }
void bimap_insert(bimap_t bimap, const key_type * k1, const key_type * k2, const data_type * data) { size_t key_1_idx = bimap_get_index_by_key_1(bimap, k1); size_t key_2_idx = bimap_get_index_by_key_2(bimap, k2); assert(key_1_idx == -1); /// Trying to insert with an existing key is an error (#1) assert(key_2_idx == -1); /// Trying to insert with an existing key is an error (#2) if (bimap->count == bimap->size) { /// \todo realloc assert(0); } memcpy(bimap->datas + bimap->count * bimap->storage_size , data, bimap->data_size ); memcpy(bimap->datas + bimap->count * bimap->storage_size + bimap->data_size , k1 , bimap->key_1_size); memcpy(bimap->datas + bimap->count * bimap->storage_size + bimap->data_size + bimap->key_1_size, k2 , bimap->key_2_size); map_insert(bimap->key_1_map, k1, &(bimap->count)); map_insert(bimap->key_2_map, k2, &(bimap->count)); bimap->count++; }
static void map_insert(struct aoi_space * space , struct map * m, uint32_t id , struct object *obj) { struct map_slot *s = mainposition(m,id); if (s->id == INVALID_ID) { s->id = id; s->obj = obj; return; } if (mainposition(m, s->id) != s) { struct map_slot * last = mainposition(m,s->id); while (last->next != s - m->slot) { assert(last->next >= 0); last = &m->slot[last->next]; } uint32_t temp_id = s->id; struct object * temp_obj = s->obj; last->next = s->next; s->id = id; s->obj = obj; s->next = -1; if (temp_obj) { map_insert(space, m, temp_id, temp_obj); } return; } while (m->lastfree >= 0) { struct map_slot * temp = &m->slot[m->lastfree--]; if (temp->id == INVALID_ID) { temp->id = id; temp->obj = obj; temp->next = s->next; s->next = (int)(temp - m->slot); return; } } rehash(space,m); map_insert(space, m, id , obj); }
struct _map * elf32_functions_wqueue (struct _elf32 * elf32, struct _map * memory, struct _list * entries) { struct _map * functions = map_create(); struct _wqueue * wqueue = wqueue_create(); struct _list_it * it; for (it = list_iterator(entries); it != NULL; it = it->next) { struct _function * function = it->data; if (map_fetch(functions, function->address) == NULL) map_insert(functions, function->address, function); struct _x86_wqueue * x86w; x86w = x86_wqueue_create(function->address, memory); wqueue_push(wqueue, WQUEUE_CALLBACK(x86_functions_wqueue), x86w); object_delete(x86w); } wqueue_wait(wqueue); struct _map * fmap; while ((fmap = wqueue_peek(wqueue)) != NULL) { struct _map_it * mit; for (mit = map_iterator(fmap); mit != NULL; mit = map_it_next(mit)) { struct _function * function = map_it_data(mit); if (map_fetch(functions, function->address) == NULL) map_insert(functions, function->address, function); } wqueue_pop(wqueue); } object_delete(wqueue); return functions; }
void map_insert(Map* map, char key, void* value) { int start = hash(key) % map->length; int end = start ? start : map->length; int i; for (i = start; i != end - 1; i = (i + 1) % map->length) { if (key == map->items[i].key || !map->items[i].value) { map->items[i].key = key; map->items[i].value = value; return; } } map_grow(map); map_insert(map, key, value); }
/** * Adds a map. * * Returns -1 if the maplist has not been initialised yet or zero if everything * went OK. */ int avahi_natpm_maplist_add(AvahiNatpmMap *map) { assert(map != NULL); if (!initialised) return -1; AVAHI_LLIST_INIT(AvahiNatpmMap, map, map); map_insert(map); ++map_count; return 0; }
void set_named_variable(struct context *context, struct program_state *state, const struct byte_array *name, const struct variable *value) { // DEBUGPRINT(" set_named_variable: %p\n", state); if (!state) state = (struct program_state*)stack_peek(context->program_stack, 0); struct map *var_map = state->named_variables; struct variable *to_var = variable_copy(context, value); map_insert(var_map, name, to_var); //DEBUGPRINT("SET %s to %s\n", byte_array_to_string(name), variable_value_str(context, value)); // DEBUGPRINT(" SET %s at %p in {p:%p, s:%p, m:%p}\n", byte_array_to_string(name), to_var, context->program_stack, state, var_map); }