MapEnsurePtr
Description
Look up the value for lookup_key. If not present, insert an entry mapping lookup_key to default_value first. Returns a pointer to the value slot in either case so the caller may inspect or mutate it in place.
Parameters
| Name | Direction | Description |
|---|---|---|
m |
in,out | Map handle. |
lookup_key |
in | Key to find or insert (r-value). |
default_value |
in | Initial value to install if no entry exists (r-value). |
Usage example (from documentation)
int *counter = MapEnsurePtr(&counts, key, 0);
if (counter) { (*counter)++; }Success
Returns a pointer to the value slot of type MAP_VALUE_TYPE(m) *. If the key already existed the returned pointer references the existing value (no mutation of the map is performed). If the key was absent, a new (key, value) entry with default_value is inserted, length grows by one, and the returned pointer references that newly-installed slot. Subsequent in-place mutations through the pointer are valid until the next rehash.
Failure
Returns NULL on allocation failure during the insert path. The map is unchanged.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Map.Insert.c:124:
bool result;
value_ptr = MapEnsurePtr(&map, 8, 80);
result = value_ptr && (*value_ptr == 80);
result = result && (MapPairCount(&map) == 1);- In
Map.Insert.c:129:
result = result && (MapValueCountForKey(&map, 8) == 1);
value_ptr = MapEnsurePtr(&map, 8, 800);
result = result && value_ptr && (*value_ptr == 80);
result = result && (MapPairCount(&map) == 1);
GraphForeachNode(&graph, node) {
(void)MapEnsurePtr(&counts, GraphNodeGetId(node), 0);
GraphNodeForeachNeighbor(node, neighbor) {
u64 *count = MapEnsurePtr(&counts, GraphNodeGetId(neighbor), 0); (void)MapEnsurePtr(&counts, GraphNodeGetId(node), 0);
GraphNodeForeachNeighbor(node, neighbor) {
u64 *count = MapEnsurePtr(&counts, GraphNodeGetId(neighbor), 0);
*count += 1;
}
Last updated on