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

The Key is limited as int. #2

Open
RichardWQJ opened this issue Dec 22, 2020 · 1 comment
Open

The Key is limited as int. #2

RichardWQJ opened this issue Dec 22, 2020 · 1 comment

Comments

@RichardWQJ
Copy link

Suppose that I want a unique "std::string name;" as Key, this queue is not work.

@Ten0
Copy link
Owner

Ten0 commented Dec 22, 2020

That is true. In that case what you probably want to do is first remap your name to an int key:

int new_indexes = 0;
unordered_map<string, int> m;
auto f = m.find(name);
// and for each entry string
int idx; // Will store the remapping
if (f != m.end()) {
    idx = f->first;
} else {
    idx = new_indexes++;
    m[name] = idx;
}

Another alternative if you need this often would be to create a wrapper around the existing updatable_priority_queue structure that does this process transparently (remapping the inputs and outputs on the fly), but I've found it's usually easier and more performant to just remap to ints before running the algorithm.

As explained in the README, this is on purpose that this is not supported in the current structure: otherwise this would have to be a hashmap, making the whole structure much slower (hashing would be called log(n) times for each operation). So in any case, creating another wrapper around it is the most performant.

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