bool map_contains(map_t map, key_t key){ bool exist = false; if (map != NULL){ if(string_eq(key, map->key)){ exist = true; } else if (string_less(key, map->key)){ exist = map_contains(map->leftnode, key); } else { exist = map_contains(map->rightnode, key); } } return exist; }
SDL_Surface* graphics_load(graphics g, char* file_name) { struct graphics_t *gfx = g; if (!map_contains(gfx->sprite_sheets, file_name)) { map_put(gfx->sprite_sheets, file_name, IMG_Load(file_name), sizeof(SDL_Surface)); } return map_get(gfx->sprite_sheets, file_name); }
static void test_map_contains(void *o, sha1_t *keys, size_t count) { size_t i; size_t items; map_t *m = o; for (items = 0, i = 0; i < count; i++) items += map_contains(m, &keys[i]) ? 1 : 0; g_assert(items == count); }
TEST(UniqueMap, MapRemove) { 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_contains(map, k[2])); // hit cache TEST_ASSERT_TRUE(map_remove(map, k[2]) == &values[2]); TEST_ASSERT_FALSE(map_contains(map, k[2])); TEST_ASSERT_TRUE(map_remove(map, k[0]) == &values[0]); // uncached TEST_ASSERT_FALSE(map_contains(map, k[0])); TEST_ASSERT_NULL(map_remove(map, k[0])); // superfluous remove destroy_unique_map(map); }
/** * Allocate a new entry in the cache to hold the deserialized value. * * @param dw the DBM wrapper * @param key key we want a cache entry for * @param filled optionally, a new cache entry already filled with the data * * @attention * An older cache entry structure can be returned, and it will still * point to the previous data. Caller should normally invoke fill_entry() * immediately to make sure these stale data are not associated wrongly * with the new key, or supply his own filled structure directly. * * @return a cache entry object that can be filled with the value. */ static struct cached * allocate_entry(dbmw_t *dw, gconstpointer key, struct cached *filled) { struct cached *entry; gpointer saved_key; g_assert(!hash_list_contains(dw->keys, key)); g_assert(!map_contains(dw->values, key)); g_assert(!filled || (!filled->len == !filled->data)); saved_key = wcopy(key, dbmw_keylen(dw, key)); /* * If we have less keys cached than our maximum, add it. * Otherwise evict the least recently used key, at the head. */ if (hash_list_length(dw->keys) < dw->max_cached) { if (filled) entry = filled; else WALLOC0(entry); } else { gpointer head; g_assert(hash_list_length(dw->keys) == dw->max_cached); head = hash_list_head(dw->keys); entry = remove_entry(dw, head, filled != NULL, TRUE); g_assert(filled != NULL || entry != NULL); if (filled) entry = filled; } /* * Add entry into cache. */ g_assert(entry); hash_list_append(dw->keys, saved_key); map_insert(dw->values, saved_key, entry); return entry; }
void malloced(void* caller, size_t size, void* ptr) { debug_malloc_count++; if(is_debug) { is_debug = false; if(!map_contains(statistics, caller)) { Stat* stat = malloc(sizeof(Stat)); stat->count = 0; stat->size = 0; map_put(statistics, caller, stat); } Stat* stat = map_get(statistics, caller); stat->count++; stat->size += size; Trace* trace = malloc(sizeof(Trace)); trace->caller = caller; trace->size = size; map_put(tracing, ptr, trace); is_debug = true; } }
TEST(UniqueMap, MapContains) { const int A = 0; const int B = 4; int value = 0xFF; struct UniqueMap* map = create_unique_map(A, B); int k[3]; TEST_ASSERT_FALSE(map_contains(map, 1)); TEST_ASSERT_FALSE(map_contains(map, 2)); TEST_ASSERT_FALSE(map_contains(map, 3)); k[0] = map_put_unique(map, &value); k[1] = map_put_unique(map, &value); k[2] = map_put_unique(map, &value); TEST_ASSERT_TRUE(map_contains(map, k[0])); TEST_ASSERT_TRUE(map_contains(map, k[1])); TEST_ASSERT_TRUE(map_contains(map, k[2])); TEST_ASSERT_TRUE(map_contains(map, k[2])); // cached result destroy_unique_map(map); }
int BoyerMoore::get_bad_match(int key) { if (map_contains(key)) return bad_match_table[key]; else return -1; }
static void vis_keys_process(Vis *vis) { Buffer *buf = vis->keys; char *keys = buf->data, *start = keys, *cur = keys, *end; bool prefix = false, unknown_key = false; KeyBinding *binding = NULL; vis->keyhandler = true; while (cur && *cur) { if (!(end = (char*)vis_keys_next(vis, cur))) { unknown_key = true; goto out; } char tmp = *end; *end = '\0'; prefix = false; binding = NULL; for (Mode *mode = vis->mode; mode && !binding && !prefix; mode = mode->parent) { for (int global = 0; global < 2 && !binding && !prefix; global++) { Mode *mode_local = global || !vis->win ? mode : &vis->win->modes[mode->id]; if (!mode_local->bindings) continue; binding = map_get(mode_local->bindings, start); /* "<" is never treated as a prefix because it is used to denote * special key symbols */ if (strcmp(cur, "<")) prefix = !binding && map_contains(mode_local->bindings, start); } } *end = tmp; if (binding) { /* exact match */ if (binding->action) { end = (char*)binding->action->func(vis, end, &binding->action->arg); if (!end) break; start = cur = end; } else if (binding->alias) { buffer_put0(buf, end); buffer_prepend0(buf, binding->alias); start = cur = buf->data; } } else if (prefix) { /* incomplete key binding? */ cur = end; } else { /* no keybinding */ KeyAction *action = NULL; if (start[0] == '<' && end[-1] == '>') { /* test for special editor key command */ char tmp = end[-1]; end[-1] = '\0'; action = map_get(vis->actions, start+1); end[-1] = tmp; if (action) { end = (char*)action->func(vis, end, &action->arg); if (!end) break; } } if (!action && vis->mode->input) vis->mode->input(vis, start, end - start); start = cur = end; } } out: if (unknown_key) buffer_truncate(buf); else buffer_put0(buf, start); vis->keyhandler = false; }
void test_duplicates(unsigned int num) { const struct map_config map_conf = { .size = MAP_DEFAULT_SIZE, .lower_bound = MAP_DEFAULT_LOWER_BOUND, .upper_bound = MAP_DEFAULT_UPPER_BOUND, .static_size = false, .key_compare = &compare_int, .key_hash = &hash_uint, .data_delete = NULL, }; struct map *m; struct random *random; unsigned int i, n, dups1, dups2;; int err; m = map_new(&map_conf); random = random_new(); assert(m); assert(random); dups1 = 0; for(i = 0; i < num; ++i) { n = random_uint(random); if(map_contains(m, (void *)(long) n)) { dups1 += 1; } else { err = map_insert(m, (void *)(long) n, (void *)(long) n); if(err < 0) { fprintf(stderr, "Warning: " "Map unable to insert %u in %uth iteration: %s\n", n, i, strerror(-err)); } } } random_delete(random); map_clear(m); srand(time(NULL)); dups2 = 0; for(i = 0; i < num; ++i) { n = rand(); if(map_contains(m, (void *)(long) n)) { dups2 += 1; } else { err = map_insert(m, (void *)(long) n, (void *)(long) n); if(err < 0) { fprintf(stderr, "Warning: " "Map unable to insert %u in %uth iteration: %s\n", n, i, strerror(-err)); } } } map_delete(m); fprintf(stdout, "random: After %u iterations there are %u (%lf %%) duplicates\n" "rand(): After %u iterations there are %u (%lf %%) duplicates\n", num, dups1, (double) dups1 / num, num, dups2, (double) dups2 / num); } void test_range(unsigned int num) { struct random *rand; unsigned int i, n; rand = random_new(); assert(rand); for(i = 0; i < num; ++i) { n = random_uint_range(rand, 0, 100); assert(n <= 100); n = random_uint_range(rand, 1000, 10000); assert(n >= 1000 && n <= 10000); n = random_uint_range(rand, 99999, 123456); assert(n >= 99999 && n <= 123456); n = random_uint_range(rand, (unsigned int) 1e6 + 1, (unsigned int) 1e8); assert(n >= (unsigned int) 1e6 + 1 && n <= (unsigned int) 1e8); n = random_uint_range(rand, 0, 0); assert(n == 0); n = random_uint_range(rand, 100, 100); assert(n == 100); } random_delete(rand); } int main(int argc, char *argv[]) { unsigned int num; if(argc == 2) num = atoi(argv[1]); else num = 0; test_randomness(); if(num) { test_seeding(num); test_duplicates(num); test_range(num); } fprintf(stdout, "Tests finished\n"); return EXIT_SUCCESS; }
int main(void) { map *m; int contains; int value; /* * `map *map_new(void)` allocates a new hashmap with 256 buckets. * * To specify the number of buckets to use manually, use * `map *map_new_cap(int buckets)` instead. * * More buckets usually means less collisions, but the map will take up more * space. When two entries go in the same bucket, a linear search is necessary * to access the correct item. */ m = map_new(); /* * `int map_set(map *m, char *key, int value)` sets the map entry at `key` to * `value`. * * If no entry with such key exists, a new one is added. Otherwise, * the existing entry is overwritten. */ map_set(m, "foo", 1); map_set(m, "bar", 2); map_set(m, "baz", 4); map_set(m, "quux", 4); /* map (size: 4) { quux:4, foo:1, bar:2, baz:4 }; note: undefined order */ print_map(m); map_set(m, "baz", 3); /* * `int map_get(const map *m, char *key)` returns the value associated with * the key `key`. * * If no such entry exists in the map, a zeroed value is returned, 0 in our * case. */ printf("baz: %d\n", map_get(m, "baz")); /* baz: 3 */ /* * `int map_contains(const map *m, char *key)` returns 1 if `key` is set in * the map, 0 otherwise. */ printf("qwe in map: %s\n", map_contains(m, "qwe")?"yes":"no"); /* qwe in map: no */ /* * `int map_get_contains(const map *m, char *key, int *value)` returns 1 if * `key` is in `m`, 0 otherwise. If the key exists, `*value` is set to the * value of that entry. If it doesn't, then the space `value` points to is * not touched. */ value = -1; contains = map_get_contains(m, "foo", &value); printf("foo in map: %s, value: %d\n", contains?"yes":"no", value); /* foo in map: yes, value: 1 */ /* * `int map_get_default(const map *m, char *key, int value)` returns the value * associated with the key `key` if such an entry exists in the map, otherwise * it returns the value of the `value` parameter. */ printf("asd (default: -1): %d\n", map_get_default(m, "asd", -1)); /* asd (default: -1): -1 */ /* * `int map_delete(map *m, char *key)` removes the entry with key `key` from * `m`. If no such entry exists, this does nothing and returns 0; it returns * 1 if an entry was removed. */ map_delete(m, "quux"); map_delete(m, "asd"); print_map(m); /* map (size: 3) { foo:1, bar:2, baz:3 } */ /* `void map_free(map *m)` frees the map and its resources */ map_free(m); return 0; }
static const char *vis_keys_raw(Vis *vis, Buffer *buf, const char *input) { char *keys = buf->data, *start = keys, *cur = keys, *end; bool prefix = false; KeyBinding *binding = NULL; while (cur && *cur) { if (!(end = (char*)vis_keys_next(vis, cur))) goto err; // XXX: can't parse key this should never happen char tmp = *end; *end = '\0'; prefix = false; binding = NULL; for (Mode *mode = vis->mode; mode && !binding && !prefix; mode = mode->parent) { for (int global = 0; global < 2 && !binding && !prefix; global++) { Mode *mode_local = global ? mode : &vis->win->modes[mode->id]; if (!mode_local->bindings) continue; binding = map_get(mode_local->bindings, start); /* "<" is never treated as a prefix because it is used to denote * special key symbols */ if (strcmp(cur, "<")) prefix = !binding && map_contains(mode_local->bindings, start); } } *end = tmp; vis->keys = buf; if (binding) { /* exact match */ if (binding->action) { end = (char*)binding->action->func(vis, end, &binding->action->arg); if (!end) break; start = cur = end; } else if (binding->alias) { buffer_put0(buf, end); buffer_prepend0(buf, binding->alias); start = cur = buf->data; } } else if (prefix) { /* incomplete key binding? */ cur = end; } else { /* no keybinding */ KeyAction *action = NULL; if (start[0] == '<' && end[-1] == '>') { /* test for special editor key command */ char tmp = end[-1]; end[-1] = '\0'; action = map_get(vis->actions, start+1); end[-1] = tmp; if (action) { end = (char*)action->func(vis, end, &action->arg); if (!end) break; } } if (!action && vis->mode->input) vis->mode->input(vis, start, end - start); start = cur = end; } } vis->keys = NULL; buffer_put0(buf, start); return input + (start - keys); err: vis->keys = NULL; buffer_truncate(buf); return input + strlen(input); }
int main(int argc, char *argv[]) { const char *key = "404"; const int values[3] = { 0, 1, 2 }; plan_no_plan(); Map *map = map_new(); ok(map && map_empty(map), "Creation"); ok(map_first(map, &key) == NULL && strcmp(key, "404") == 0, "First on empty map"); ok(map_empty(map_prefix(map, "404")), "Empty prefix map"); ok(!map_get(map, "404"), "Get non-existing key"); ok(!map_contains(map, "404"), "Contains non-existing key"); ok(!map_closest(map, "404") && errno == ENOENT, "Closest non-existing key"); ok(!map_put(map, "a", NULL) && errno == EINVAL && map_empty(map) && !map_get(map, "a"), "Put NULL value"); ok(map_put(map, "a", &values[0]) && !map_empty(map) && get(map, "a", &values[0]), "Put 1"); ok(map_first(map, &key) == &values[0] && strcmp(key, "a") == 0, "First on map with 1 value"); key = NULL; ok(map_first(map_prefix(map, "a"), &key) == &values[0] && strcmp(key, "a") == 0, "First on prefix map"); ok(map_contains(map, "a"), "Contains existing key"); ok(map_closest(map, "a") == &values[0], "Closest match existing key"); ok(!map_put(map, "a", &values[1]) && errno == EEXIST && get(map, "a", &values[0]), "Put duplicate"); ok(map_put(map, "cafebabe", &values[2]) && get(map, "cafebabe", &values[2]), "Put 2"); ok(map_put(map, "cafe", &values[1]) && get(map, "cafe", &values[1]), "Put 3"); key = NULL; ok(map_first(map_prefix(map, "cafe"), &key) == &values[1] && strcmp(key, "cafe") == 0, "First on prefix map with multiple suffixes"); Map *copy = map_new(); ok(map_copy(copy, map), "Copy"); ok(!map_empty(copy), "Not empty after copying"); map_iterate(copy, compare, map); map_iterate(map, compare, copy); int counter = 0; map_iterate(copy, once, &counter); ok(counter == 1, "Iterate stop condition"); ok(!map_get(map, "ca") && !map_closest(map, "ca") && errno == 0, "Closest ambigious"); int visited[] = { 0, 0, 0 }; map_iterate(map, visit, &visited); ok(visited[0] == 1 && visited[1] == 1 && visited[2] == 1, "Iterate map"); memset(visited, 0, sizeof visited); order_counter = 0; map_iterate(map, order, &visited); ok(visited[0] == 1 && visited[1] == 2 && visited[2] == 3, "Ordered iteration"); memset(visited, 0, sizeof visited); map_iterate(map_prefix(map, "ca"), visit, &visited); ok(visited[0] == 0 && visited[1] == 1 && visited[2] == 1, "Iterate sub map"); memset(visited, 0, sizeof visited); order_counter = 0; map_iterate(map_prefix(map, "ca"), order, &visited); ok(visited[0] == 0 && visited[1] == 1 && visited[2] == 2, "Ordered sub map iteration"); ok(map_empty(map_prefix(map, "404")), "Empty map for non-existing prefix"); ok(!map_delete(map, "404"), "Delete non-existing key"); ok(map_delete(map, "cafe") == &values[1] && !map_get(map, "cafe"), "Delete existing key"); ok(map_closest(map, "cafe") == &values[2], "Closest unambigious"); ok(map_put(map, "cafe", &values[1]) && get(map, "cafe", &values[1]), "Put 3 again"); map_clear(map); ok(map_empty(map), "Empty after clean"); map_free(map); map_free(copy); return exit_status(); }
void GUIImport::updateTree() { if (treeCheckMapper) { delete treeCheckMapper; treeCheckMapper = NULL; } while (this->ui->viewTree->takeTopLevelItem(0)) ; treeCheckMapper = new QSignalMapper(this); for (StringMap::iterator viewIter = this->m->viewConfig.begin(); viewIter != this->m->viewConfig.end(); ++viewIter) { if (strchr(viewIter->first.c_str(), '.') != NULL) continue; // skip if it is not a view or empty Qt::CheckState state = viewIter->second.empty() ? Qt::Unchecked : Qt::Checked; // add view item QTreeWidgetItem* viewItem = new QTreeWidgetItem(); viewItem->setText(COL_ORGNAME, QString::fromStdString(viewIter->first)); viewItem->setText(COL_ARROW, "->"); viewItem->setText(COL_NEWNAME, QString::fromStdString(state == Qt::Checked ? viewIter->second : viewIter->first)); viewItem->setText(COL_TYPE, GetTypeString((DM::Components)this->m->viewConfigTypes[viewIter->first])); std::string epsgText; if (map_contains(&m->viewEPSGConfig, viewIter->first, epsgText) && atoi(epsgText.c_str()) == 0) viewItem->setText(COL_EPSG, QString::fromStdString(epsgText)); else viewItem->setText(COL_EPSG, DEFAULT_TRAFO_STRING); // add attributes for (StringMap::iterator attrIter = this->m->viewConfig.begin(); attrIter != this->m->viewConfig.end(); ++attrIter) { if (strchr(attrIter->first.c_str(), '.') == NULL) continue; // skip if it is a view or emtpy bool isAttributeActive = !attrIter->second.empty(); if (!isAttributeActive && state == Qt::Checked) state = Qt::PartiallyChecked; QStringList parsedString = QString::fromStdString(attrIter->first).split('.', QString::SkipEmptyParts); if (parsedString.size() != 2) { DM::Logger(DM::Error) << "error parsing parameter: " << attrIter->first; continue; } if (parsedString.first() == QString::fromStdString(viewIter->first)) { // this attribute is for this view QTreeWidgetItem* attrItem = new QTreeWidgetItem(); attrItem->setText(COL_ORGNAME, parsedString.last()); attrItem->setText(COL_ARROW, "->"); attrItem->setText(COL_NEWNAME, isAttributeActive ? QString::fromStdString(attrIter->second) : parsedString.last()); attrItem->setText(COL_TYPE, GetTypeString((DM::Attribute::AttributeType)this->m->viewConfigTypes[attrIter->first])); viewItem->addChild(attrItem); QCheckBox* check = new QCheckBox(); check->setChecked(isAttributeActive); connect(check, SIGNAL(clicked()), treeCheckMapper, SLOT(map())); treeCheckMapper->setMapping(check, (QObject*)attrItem); this->ui->viewTree->setItemWidget(attrItem, COL_CHECKBOX, check); } } this->ui->viewTree->addTopLevelItem(viewItem); QCheckBox* check = new QCheckBox(); check->setCheckState(state); connect(check, SIGNAL(clicked()), treeCheckMapper, SLOT(map())); treeCheckMapper->setMapping(check, (QObject*)viewItem); this->ui->viewTree->setItemWidget(viewItem, COL_CHECKBOX, check); } connect(treeCheckMapper, SIGNAL(mapped(QObject*)), this, SLOT(updateTreeChecks(QObject*))); this->ui->viewTree->setColumnWidth(COL_CHECKBOX, 70); this->ui->viewTree->resizeColumnToContents(COL_ORGNAME); this->ui->viewTree->resizeColumnToContents(COL_ARROW); this->ui->viewTree->resizeColumnToContents(COL_NEWNAME); this->ui->viewTree->resizeColumnToContents(COL_TYPE); this->ui->viewTree->resizeColumnToContents(COL_EPSG); }