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

Allow modification of map values #105

Open
peter-moran opened this issue Nov 6, 2020 · 7 comments
Open

Allow modification of map values #105

peter-moran opened this issue Nov 6, 2020 · 7 comments

Comments

@peter-moran
Copy link
Contributor

Is it possible, or could it be possible, for this library to let you modify the values in a map? In particular, to maintain the perfect hashing of keys and the fact that there is a constant number of keys, but add the ability to update the values associated with the keys.

Toy example:

constexpr frozen::unordered_map<frozen::string, int, 2> fruits = {
    {"n_apples", 0},
    {"n_pears", 0},
};

fruits["n_apples"] = 10;
fruits["n_pears"] = fruits["n_apples"] * 2;
@serge-sans-paille
Copy link
Owner

Well, this would be incompatible with the constexpr keyword, as a constexpr variable is readonly.

Would you write the following?

constexpr char data[3] = "yo";
data[1] = 'a';

@peter-moran
Copy link
Contributor Author

Sorry, my example was bad. I did not mean for the map to be constexpr. Here's a better example:

#include <frozen/set.h>
#include <frozen/string.h>
#include <frozen/unordered_map.h>


int main() {
  frozen::unordered_map<frozen::string, int, 2> fruits = {
      {"n_apples", 0},
      {"n_pears", 0},
  };

  fruits.at("n_apples") = 10;
  fruits.at("n_pears") = fruits.at("n_apples") * 2;
}

I see this does not compile, answering my question if it currently works or not.

At a technical level, do you see any reason this could not work? I may be interested in doing it myself.

@peter-moran
Copy link
Contributor Author

With a little modification of the code this example is possible.

In particular, it just needs a version of at and lookup that does not return a const reference.

peter-moran pushed a commit to peter-moran/frozen that referenced this issue Nov 6, 2020
…r non-const `frozen::unordered_map`s

So far this works. I question if the const-cast is appropriate,
however, even if it is recomended. I believe it leaves room for
error.
peter-moran pushed a commit to peter-moran/frozen that referenced this issue Nov 6, 2020
@peter-moran
Copy link
Contributor Author

Here's some rough example code peter-moran#1

@serge-sans-paille
Copy link
Owner

Oh interesting. Indeed the value does not take part in the computation of the keys, so it's legit to think about it.

@peter-moran
Copy link
Contributor Author

If it helps to explain why I am interested in this: I work on a lot of embedded projects where I would love to have a map that does not require dynamic allocation. In most cases I know the keys ahead of time (eg a list of enums or specific strings) but want to change the values associated with those keys over time. Typically for enums I just use an array and use the enums as the array index, but I find it very messy (and it only works with things that implicitly convert to unique integers).

In other words, as it stands this library provides a constexpr map, but with a little work it could also provide a fixed size map (or perhaps I would call it an associative array due to its fixed size nature) that allows runtime modification of values.

@peter-moran
Copy link
Contributor Author

Closed by #106

serge-sans-paille pushed a commit that referenced this issue Dec 7, 2020
…e modification (#106)

* Complete implementation of non-const functions for map and unordered_map
* Add unit tests
* Update README to explain non-constexpr maps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants