const char *find_symref(const char *symbol, struct expr **expp) { struct expr *exp; const char *p; exp = NULL; p = NULL; if(s->guesses != NULL) { exp = map_get(s->guesses, symbol); } if(exp == NULL) { exp = map_get(s->sym_table, symbol); if(exp == NULL) { static char buf[1024]; /* error, symbol not found */ sprintf(buf, "symbol %s not found", symbol); p = buf; LOG(LOG_DEBUG, ("%s\n", p)); return p; } } if(expp != NULL) { *expp = exp; } return p; }
void struct_maps() { // here we have a custom type which has a LIST_HEADER and a string key // `true` indicates that we should use string ordering. Map *map = map_new_node(true); // since the key is part of the data we don't use `map_put`. `map_put_struct` is used // for inserting single values. map_put_structs(map, Data_new("bob",22,1), Data_new("alice",21,0), Data_new("roger",40,1), Data_new("liz",16,0), NULL ); printf("size was %d\n",map_size(map)); // you can now look up data using the key dump(D map_get(map,"alice")); dump(D map_get(map,"roger")); Data *rog = (Data*)map_remove(map,"roger"); printf("size was %d\n",map_size(map)); FOR_MAP(iter,map) { printf("[%s]=%d,",(char*)iter->key,((Data*)(iter->value))->age ); }
hint32 hdata_set_tree(hdata_set_t data_set, hdata_set_tree_key_t key,hdata_set_tree_key_t parent_key,hash_code_t key_hash,equal_t key_equal,list_compare_t comparator,hany param,InvokeTickDeclare){ data_set_t * ds = (data_set_t *)data_set; hany k; hany pk; hint32 i,c; hdata_t items,item; data_set_tree_item_t * tree_item,*tree_pitem; data_set_tree_item_compare_param_t compare_param; if(ds && key && parent_key){ data_set_tree_item_dealloc(ds->tree_root,InvokeTickArg); ds->tree_root = data_set_tree_item_alloc( NULL, ext_data(ds->data),NULL,NULL,NULL,InvokeTickArg); ds->tree_level = 0; if(ds->tree_cache == NULL){ ds->tree_cache = map_alloc( key_hash, key_equal); } else{ map_clear(ds->tree_cache); } items = ext_data(ds->data); c = hdata_array_size(ext_data_class(ds->data), items); for(i=0;i<c;i++){ item = hdata_array(ext_data_class(ds->data),items,i); k = (*key)(data_set,item,param,InvokeTickArg); pk = (*parent_key)(data_set,item,param,InvokeTickArg); tree_pitem = map_get(ds->tree_cache, pk); tree_item = data_set_tree_item_alloc( k, item, tree_pitem == NULL?ds->tree_root:tree_pitem,comparator,param,InvokeTickArg); data_set_tree_item_dealloc( map_put(ds->tree_cache, k, tree_item),InvokeTickArg); if(ds->tree_level < tree_item->level){ ds->tree_level = tree_item->level; } } c = list_count(ds->tree_root->childs); for(i=0;i<c;i++){ tree_item = list_get(ds->tree_root->childs, i); pk = (*parent_key)(data_set,tree_item->data,param,InvokeTickArg); tree_pitem = map_get(ds->tree_cache, pk); if(tree_pitem){ list_remove_at(ds->tree_root->childs, i--); c = list_count(ds->tree_root->childs); if(tree_pitem->childs==NULL){ tree_pitem->childs = list_alloc(20, 20); } if(comparator){ compare_param.compare = comparator; compare_param.param = param; list_add_and_order(tree_pitem->childs, tree_item, data_set_tree_item_compare, &compare_param); } else{ list_add(tree_pitem->childs,tree_item); } tree_item->level = tree_pitem->level +1; if(ds->tree_level < tree_item->level){ ds->tree_level = tree_item->level; } } } return ds->tree_level; } return 0; }
/* HTTP response sending a code 301 Moved Permanently response. */ void http_response_moved(FILE *resp, http_header_t *header) { fprintf(resp, "HTTP/1.1 301 Moved Permanently\r\n"); fprintf(resp, "Location: http://%s/%s\r\n\r\n", (char *)map_get(header->header_fields, "Host"), "index.html"); fprintf(resp, "<a href='http://%s/%s'>%s/%s</a>\r\n", (char *)map_get(header->header_fields, "Host"), "index.html", (char *)map_get(header->header_fields, "Host"), "index.html"); return; }
value_t map_get(map_t map, key_t key){ value_t resultado = NULL; if (map != NULL) { if (string_eq(key, map->key)){ resultado = map->value; } else if (string_less(key, map->key)){ resultado = map_get(map->leftnode, key); } else { resultado = map_get(map->rightnode, key); } } return resultado; }
void getheader(vector * vec){ char * header, * value; header = vector_get(vec, 1); value = map_get(headers, header); if(value!=NULL) printf("%s\n", value); }
int main() { //printf("Some random hashes:\n"); //printf("\tBazinga: %d\n", map_hash_key("Bazinga")); //printf("\tFlabagoobie: %d\n", map_hash_key("Flabagoobie")); //printf("\tScooby: %d\n", map_hash_key("Scooby")); //printf("\tMcDooby: %d\n", map_hash_key("McDooby")); //printf("\tagnizaB: %d\n", map_hash_key("agnizaB")); Map map; map_init(&map); map_set(&map, "bazinga", "bazonga"); map_set(&map, "zabinga", "zabonga"); map_set(&map, "zabniga", "maponya"); map_set(&map, "kabanooga", "daboogawoofa"); MapItem *item; print_map(&map); map_get(&map, "abzinga"); printf("================================\n"); print_map(&map); map_free(&map); }
static int wasFinalPass(void) { int result = 1; struct map_iterator i[1]; struct map_entry *me; for(map_get_iterator(s->guesses, i); (me = (struct map_entry*)map_iterator_next(i)) != NULL;) { struct expr *guess_expr; struct expr *sym_expr; LOG(LOG_VERBOSE, ("Checking guessed symbol %s: ", me->key)); /* Was this guessed symbol used in this pass? */ if((sym_expr = map_get(s->sym_table, me->key)) == NULL) { /* No, skip it */ continue; } guess_expr = me->value; LOG(LOG_VERBOSE, ("expected %d, actual %d\n", resolve_expr(guess_expr), resolve_expr(sym_expr))); if(expr_cmp_cb(me->value, sym_expr) != 0) { /* Not the same, not the last pass. * copy the actual value from the sym table */ me->value = sym_expr; result = 0; } } return result; }
//! //! Main entry point of the application //! //! @param[in] argc the number of parameter passed on the command line //! @param[in] argv the list of arguments //! //! @return EUCA_OK on success or EUCA_ERROR on failure. //! //! @note little unit test: compile with gcc -g -D_TEST_MAP map.c //! int main(int argc, char *argv[]) { char *s1 = "string 1"; char *s2 = "string 2"; map *m = map_create(10); assert(map_get(m, "foo") == NULL); map_set(m, "k1", s1); assert(map_get(m, "k1") == s1); map_set(m, "k2", s2); assert(map_get(m, "k2") == s2); map_set(m, "k2", s1); assert(map_get(m, "k2") == s1); return (EUCA_OK); }
int queue_remove(QUEUE* queue, char* value) { QNODE* qnode; qnode = map_get(queue->map, value); if (qnode != NULL) { if (qnode->prev != NULL) { qnode->prev->next = qnode->next; } else { queue->head = qnode->next; } if (qnode->next != NULL) { qnode->next->prev = qnode->prev; } else { queue->tail = qnode->prev; } map_remove(queue->map, value); free(qnode->value); free(qnode); return 0; } return -1; }
void *incoming_connection(void *arg) { char readline[MAXLINE]; struct thread_argument *ta = (struct thread_argument *)arg; struct context *context = context_new(true, true); context->find = ta->find; for (;;) { bzero(readline, sizeof(readline)); int n; if (((n = CyaSSL_read(ta->ssl, readline, MAXLINE)) <= 0)) { fprintf(stderr, "client closed connection\n"); goto free_ssl; } fprintf(stderr, "%d bytes received: %s\n", n, readline); struct byte_array *raw_message = byte_array_new_size(n); raw_message->data = (uint8_t*)readline; int32_t raw_message_length = serial_decode_int(raw_message); assert_message(raw_message_length < MAXLINE, "todo: handle long messages"); struct variable *message = variable_deserialize(context, raw_message); struct variable *listener = (struct variable *)map_get(server_listeners, (void*)(VOID_INT)ta->fd); vm_call(context, listener, message); } free_ssl: CyaSSL_free(ta->ssl); // Free CYASSL object free(ta); context_del(context); return NULL; }
char* lrucache_get(LRUCACHE* lrucache, char* key) { queue_remove(lrucache->queue, key); queue_insert(lrucache->queue, key); return map_get(lrucache->map, key); }
void add_int_variable(_map *mem, char *_name, int _value) { _variable *var = (_variable *)malloc(sizeof(_variable) + (strlen(_name) + 1) * sizeof(char)); // variable length name if (var) { var->int_value = _value; var->type = INT; var->is_constant = false; strcpy(var->name, _name); void **p_old_var; _variable *old_var; if ((p_old_var = map_get(mem, _name)) && (old_var = (_variable *)*p_old_var)) { free(var); error = VAR_REDEFINE; return; } map_add(mem, _name, var); } else { error = MALLOC_ERROR; } return; }
int network_close(struct network *net, const char *addr, uint16_t port) { endpoint_array_t *ep_array = map_get(&net->endpoints, addr); if (ep_array == NULL) { return kr_error(ENOENT); } /* Close endpoint in array. */ for (size_t i = ep_array->len; i--;) { struct endpoint *ep = ep_array->at[i]; if (ep->port == port) { close_endpoint(ep); array_del(*ep_array, i); break; } } /* Collapse key if it has no endpoint. */ if (ep_array->len == 0) { free(ep_array); map_del(&net->endpoints, addr); } return kr_ok(); }
TEST(UniqueMap, MapGet) { struct UniqueMap* map = create_unique_map(0, 10); int values[] = {1,2,3}; int k[3]; k[0] = map_put_unique(map, &values[0]); k[1] = map_put_unique(map, &values[1]); k[2] = map_put_unique(map, &values[2]); TEST_ASSERT_TRUE(map_get(map, k[0]) == &values[0]); TEST_ASSERT_TRUE(map_get(map, k[1]) == &values[1]); TEST_ASSERT_TRUE(map_get(map, k[2]) == &values[2]); TEST_ASSERT_TRUE(map_get(map, k[2]) == &values[2]); // cached result destroy_unique_map(map); }
void perform_request(CURL * curl, char * url, char * path){ char * full_url; int i, line_len; char * line; char * header, * value; struct curl_slist *slist=NULL; full_url = (char*)malloc(strlen(url)+strlen(path)+1); strcpy(full_url, url); strcat(full_url, path); curl_easy_setopt(curl, CURLOPT_URL, full_url); curl_easy_setopt(curl, CURLOPT_HEADER, 1); /* attach all the headers */ for(i=0; i<headers->keys->length; i++){ header = vector_get(headers->keys, i); value = map_get(headers, header); line_len = strlen(header)+strlen(value)+3; line = (char*)calloc(line_len, sizeof(char)); sprintf(line, "%s: %s", header, value); slist = curl_slist_append(slist, line); free(line); } curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); curl_easy_perform(curl); /* print a newline incase the response has no ending newline */ printf("\n"); /* cleanup */ curl_slist_free_all(slist); free(full_url); }
int su_map_has(su_state *s, int idx) { value_t v = *STK(-1); unsigned hash = hash_value(&v); v = map_get(s, STK(TOP(idx))->obj.m, &v, hash); s->stack_top--; return v.type != SU_INV; }
// get the indexed item and push on operand stack void lookup(struct context *context, struct variable *indexable, struct variable *index, bool really) { if (!really && custom_method(context, RESERVED_GET, indexable, index, NULL)) { return; } struct variable *item=0; switch (index->type) { case VAR_INT: item = list_get_int(context, indexable, index->integer); break; case VAR_STR: if (indexable->map) item = (struct variable*)map_get(indexable->map, index->str); if (!item) item = builtin_method(context, indexable, index); if (!item) item = variable_new_nil(context); break; case VAR_NIL: item = variable_new_nil(context); break; default: vm_exit_message(context, "bad index type"); break; } // DEBUGPRINT(" found %s\n", variable_value_str(context, item)); variable_push(context, item); }
void * get_symbol (void *handle, char *symbol_name) { void *symptr; #ifdef DJGPP st_symbol_t *sym = map_get (dxe_map, handle); if (sym == NULL) { fprintf (stderr, "ERROR: Invalid handle: %x\n", (int) handle); exit (1); } symptr = sym->dxe_symbol (symbol_name); if (symptr == NULL) { fprintf (stderr, "ERROR: Could not find symbol: %s\n", symbol_name); exit (1); } #elif defined __unix__ || defined __APPLE__ // Mac OS X actually, see char *strptr; // comment in open_module() symptr = dlsym (handle, symbol_name); if ((strptr = dlerror ()) != NULL) // this is "the correct way" { // according to the info page fputs ("ERROR: ", stderr); fputs (strptr, stderr); fputc ('\n', stderr); exit (1); } #elif defined _WIN32 u_func_ptr_t sym; sym.func_ptr = (void (*) (void)) GetProcAddress ((HINSTANCE) handle, symbol_name); symptr = sym.void_ptr; if (symptr == NULL) { LPTSTR strptr; fputs ("ERROR: ", stderr); FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError (), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &strptr, 0, NULL); fputs (strptr, stderr); LocalFree (strptr); exit (1); } #elif defined __BEOS__ int status = get_image_symbol ((int) handle, symbol_name, B_SYMBOL_TYPE_ANY, &symptr); // B_SYMBOL_TYPE_TEXT/B_SYMBOL_TYPE_DATA if (status != B_OK) { fprintf (stderr, "ERROR: Could not find symbol: %s\n", symbol_name); exit (1); } #endif return symptr; }
void helpCmd() { char tmpbuf[MAX_TMPBUF_LEN]; char *next_tok = strtok(NULL, " \n"); cli_entry_t *n_clie; if (next_tok == NULL) CLIPrintHelp(); else { n_clie = (cli_entry_t *)map_get(cli_map, next_tok); if (n_clie == NULL) printf("ERROR! No help for command: %s \n", next_tok); else { if (strstr(n_clie->long_helpstr, ".hlp") != NULL) { sprintf(tmpbuf, "man %s/grouter/helpdefs/%s", getenv("GINI_SHARE"), n_clie->long_helpstr); system(tmpbuf); } else { printf("\n%s:: %s\n", n_clie->keystr, n_clie->usagestr); printf("%s\n", n_clie->long_helpstr); } } } }
int _hit_test( Map *map, float max_distance, int previous, float x, float y, float z, float vx, float vy, float vz, int *hx, int *hy, int *hz) { int m = 8; int px = 0; int py = 0; int pz = 0; for (int i = 0; i < max_distance * m; i++) { int nx = roundf(x); int ny = roundf(y); int nz = roundf(z); if (nx != px || ny != py || nz != pz) { int hw = map_get(map, nx, ny, nz); if (hw > 0) { if (previous) { *hx = px; *hy = py; *hz = pz; } else { *hx = nx; *hy = ny; *hz = nz; } return hw; } px = nx; py = ny; pz = nz; } x += vx / m; y += vy / m; z += vz / m; } return 0; }
const char *vis_keys_next(Vis *vis, const char *keys) { if (!keys || !*keys) return NULL; TermKeyKey key; TermKey *termkey = vis->ui->termkey_get(vis->ui); const char *next = NULL; /* first try to parse a special key of the form <Key> */ if (*keys == '<' && (next = termkey_strpkey(termkey, keys+1, &key, TERMKEY_FORMAT_VIM)) && *next == '>') return next+1; if (*keys == '<') { const char *start = keys + 1, *end = start; while (*end && *end != '>') end++; if (end > start && end - start - 1 < 64 && *end == '>') { char key[64]; memcpy(key, start, end - start); key[end - start] = '\0'; if (map_get(vis->actions, key)) return end + 1; } } while (!ISUTF8(*keys)) keys++; return termkey_strpkey(termkey, keys, &key, TERMKEY_FORMAT_VIM); }
void printObjects(world* w) { const char* blank = " "; map_object* o = map_get(&w->Map, w->Player.Location); npc* n = NULL; int color; float hPercent; mvwprintw(w->Windows.Status, 2, 2, blank); if(o != NULL) n = npc_get(&w->Npcs, o->Id); if(n == NULL) { mvwprintw(w->Windows.Status, 2, 2, "Nothing is here."); return; } hPercent = ((float)n->Health) / n->MaxHealth; if(hPercent > 0.66) color = 3; else if(hPercent > 0.33) color = 4; else color = 1; wattron(w->Windows.Status, COLOR_PAIR(color)); mvwaddch(w->Windows.Status, 2, 2, n->MapObject.Symbol); wattroff(w->Windows.Status, COLOR_PAIR(color)); mvwprintw(w->Windows.Status, 2, 4, n->Desc); }
float map_height (float x, float y, float z) { float a = 1000; int i = x, j = y, k = z; assert (x < a && y < a && z < a); for (; k >= 0 && !map_get (i, j, k); --k); return z - (k + 1); }
static void rdfbot_main() { Box (*my_map)[GRID_HEIGHT]; int x,y; Message m; for (;;) { while (messages()) { receive_msg(&m); if (m.opcode == OP_ACK) { my_map = map_get(); /* * Erase the old RDF bits * * You can get a RDF pattern in the map, then * drive around, and get another WITHOUT clearing * the map, and the X bit will be set at the * intersections. */ for (y = 0; y < GRID_HEIGHT; y++) { for (x = 0; x < GRID_WIDTH; x++) { my_map[x][y].flags &= ~((unsigned int) 0 | ANY_RDF | X_RDF ); } } rdf_map(my_map); printf("\n"); for (y = 0; y < GRID_HEIGHT; y++) { for (x = 0; x < GRID_WIDTH; x++) { if (my_map[x][y].flags & X_RDF) printf("X"); else if (my_map[x][y].flags & RED_RDF) printf("R"); else if (my_map[x][y].flags & GREEN_RDF) printf("G"); else if (my_map[x][y].flags & YELLOW_RDF) printf("Y"); else if (my_map[x][y].flags & INSIDE_MAZE) printf("#"); else printf(" "); } printf("\n"); } } } done(); } }
static value_t get_global(su_state *s, const char *var, unsigned hash, int size) { 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); return map_get(s, m.obj.m, &key, hash); }
static void test_map_stack(void) { Map *m1 = make_map(NULL); map_put(m1, "x", (void *)1); map_put(m1, "y", (void *)2); assert_int(1, (int)(intptr_t)map_get(m1, "x")); Map *m2 = make_map(m1); assert_int(1, (int)(intptr_t)map_get(m2, "x")); map_put(m2, "x", (void *)3); assert_int(3, (int)(intptr_t)map_get(m2, "x")); assert_int(1, (int)(intptr_t)map_get(m1, "x")); MapIter *iter = map_iter(m2); assert_string("x", map_next(iter, NULL)); assert_string("y", map_next(iter, NULL)); assert_null(map_next(iter, NULL)); }
void new_symbol_expr_guess(const char *symbol, struct expr *arg) { /* Set a soft symbol only if it is not set */ if(map_get(s->guesses, symbol) == NULL) { map_put(s->guesses, symbol, arg); } }
static message_fifo_t *get_fifo(receiver_t *self, atom_t a) { map_value_t pfifo = map_get(&self->fifos, (unsigned long)a); message_fifo_t *fifo = map_value_is_there(pfifo) ? map_value_obtain(pfifo) : NULL; return fifo; }
void Router::update_weights( const std::vector<float>& probs, const std::unordered_map<std::string, size_t>& symbol_counts, const std::unordered_map<Ob, size_t>& ob_counts, std::vector<float>& symbol_weights, std::vector<float>& ob_weights, float reltol) const { POMAGMA_INFO("Updating weights"); const size_t symbol_count = m_types.size(); const size_t ob_count = m_carrier.item_count(); POMAGMA_ASSERT_EQ(probs.size(), 1 + ob_count); POMAGMA_ASSERT_EQ(symbol_weights.size(), symbol_count); POMAGMA_ASSERT_EQ(ob_weights.size(), 1 + ob_count); const float max_increase = 1.0 + reltol; std::vector<float> temp_symbol_weights(symbol_weights.size()); std::vector<float> temp_ob_weights(ob_weights.size()); update_weights_loop : { POMAGMA_DEBUG("distributing route weight"); std::fill(temp_symbol_weights.begin(), temp_symbol_weights.end(), 0); for (size_t i = 0; i < symbol_count; ++i) { temp_symbol_weights[i] = map_get(symbol_counts, m_types[i].name, 0); } std::fill(temp_ob_weights.begin(), temp_ob_weights.end(), 0); for (const auto& pair : ob_counts) { temp_ob_weights[pair.first] = pair.second; } #pragma omp parallel for schedule(dynamic, 1) for (size_t i = 0; i < ob_count; ++i) { Ob ob = 1 + i; const float weight = ob_weights[ob] / probs[ob]; for (const Segment& segment : iter_val(ob)) { float part = weight * get_prob(segment, probs); add_weight(part, segment, temp_symbol_weights, temp_ob_weights); } } std::swap(symbol_weights, temp_symbol_weights); std::swap(ob_weights, temp_ob_weights); for (size_t i = 0; i < symbol_count; ++i) { if (symbol_weights[i] > temp_symbol_weights[i] * max_increase) { goto update_weights_loop; } } for (size_t i = 0; i < ob_count; ++i) { Ob ob = 1 + i; if (ob_weights[ob] > temp_ob_weights[ob] * max_increase) { goto update_weights_loop; } } } }