Hotkey/Item: optimized add_hotkey

Since this is the only way to add things to the hotkey list, there can be at most one
matching entry in the list. Since order doesn't matter, just swap in the new hotkey at
that position rather than erasing and re-adding at end. If we want efficient mid-list
removal we should use std::list.
This commit is contained in:
Charles Dang 2022-05-27 13:02:04 -04:00
parent 96ac3fd973
commit 8fd4304c88
2 changed files with 13 additions and 13 deletions

View File

@ -110,8 +110,9 @@ bool hotkey_base::bindings_equal(hotkey_ptr other)
return false;
}
hk_scopes scopematch
= hotkey::get_hotkey_command(get_command()).scope & hotkey::get_hotkey_command(other->get_command()).scope;
const hk_scopes scopematch =
hotkey::get_hotkey_command(get_command()).scope &
hotkey::get_hotkey_command(other->get_command()).scope;
if(scopematch.none()) {
return false;
@ -354,19 +355,18 @@ void del_hotkey(hotkey_ptr item)
}
}
void add_hotkey(const hotkey_ptr item)
void add_hotkey(hotkey_ptr item)
{
if(item == nullptr) {
return;
}
if(item) {
auto iter = std::find_if(hotkeys_.begin(), hotkeys_.end(),
[&item](const hotkey::hotkey_ptr& hk) { return hk->bindings_equal(item); });
if(!hotkeys_.empty()) {
hotkeys_.erase(std::remove_if(hotkeys_.begin(), hotkeys_.end(),
[item](const hotkey::hotkey_ptr& hk) { return hk->bindings_equal(item); }),
hotkeys_.end());
if(iter != hotkeys_.end()) {
iter->swap(item);
} else {
hotkeys_.push_back(std::move(item));
}
}
hotkeys_.push_back(item);
}
void clear_hotkeys(const std::string& command)

View File

@ -350,7 +350,7 @@ bool has_hotkey_item(const std::string& command);
* Add a hotkey to the list of hotkeys.
* @param item The item to add.
*/
void add_hotkey(const hotkey_ptr item);
void add_hotkey(hotkey_ptr item);
/**
* Remove a hotkey from the list of hotkeys