Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider introducing a map function combining contains check and value retrieval #1608

Closed
bbannier opened this issue Dec 5, 2023 · 0 comments
Assignees
Labels
Codegen Runtime Library Issues related to the HILTI or Spicy runtime libraries

Comments

@bbannier
Copy link
Contributor

bbannier commented Dec 5, 2023

If one wants to check whether a map contains a value and retrieve it one currently is nudged into a double iteration pattern, e.g.,

global xs: map<uint64, string>;
xs[0] = "0";
xs[1] = "1";
xs[2] = "2";

global needle = 1;

if (needle in xs)      # iterates for search.
    print xs[needle];  # iterates for value retrieval.

We currently provide map::get which bundles this into one operation (though it also generates code which does a double iteration), but this can only be used for this use case if the there is some magic "default" value which cannot appear in the genuine value domain (else we couldn't distinguish entries at default from absent values).

We should consider introducing a map method (e.g., get_if or similar) which given a key returns an optional value (set if present, else unset). We could then also use this to provide a more efficient implementation of get.

@bbannier bbannier added Runtime Library Issues related to the HILTI or Spicy runtime libraries Codegen labels Dec 5, 2023
@rsmmr rsmmr self-assigned this Jun 6, 2024
rsmmr added a commit that referenced this issue Jun 6, 2024
This returns an optional either containing the map's element for the
given key if that entry exists, or an unset optional if it does not.

Closes #1608.

(Note that we cannot use the same, improved implementation for the
existing `get()` method because that needs to return a reference.)
rsmmr added a commit that referenced this issue Jun 7, 2024
This returns an optional either containing the map's element for the
given key if that entry exists, or an unset optional if it does not.

Closes #1608.

(Note that we cannot use the same, improved implementation for the
existing `get()` method because that needs to return a reference.)
rsmmr added a commit that referenced this issue Jun 12, 2024
This returns an optional either containing the map's element for the
given key if that entry exists, or an unset optional if it does not.

Closes #1608.

(Note that we cannot use the same, improved implementation for the
existing `get()` method because that needs to return a reference.)
@rsmmr rsmmr closed this as completed in 233f387 Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Codegen Runtime Library Issues related to the HILTI or Spicy runtime libraries
Projects
None yet
Development

No branches or pull requests

2 participants