Skip to content

Add a new HashMap implementation #60881

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

Merged
merged 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ bool ProjectSettings::has_custom_feature(const String &p_feature) const {
return custom_features.has(p_feature);
}

OrderedHashMap<StringName, ProjectSettings::AutoloadInfo> ProjectSettings::get_autoload_list() const {
const HashMap<StringName, ProjectSettings::AutoloadInfo> &ProjectSettings::get_autoload_list() const {
return autoloads;
}

Expand Down Expand Up @@ -1135,21 +1135,21 @@ void ProjectSettings::_bind_methods() {

void ProjectSettings::_add_builtin_input_map() {
if (InputMap::get_singleton()) {
OrderedHashMap<String, List<Ref<InputEvent>>> builtins = InputMap::get_singleton()->get_builtins();
HashMap<String, List<Ref<InputEvent>>> builtins = InputMap::get_singleton()->get_builtins();

for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
for (KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
Array events;

// Convert list of input events into array
for (List<Ref<InputEvent>>::Element *I = E.get().front(); I; I = I->next()) {
for (List<Ref<InputEvent>>::Element *I = E.value.front(); I; I = I->next()) {
events.push_back(I->get());
}

Dictionary action;
action["deadzone"] = Variant(0.5f);
action["events"] = events;

String action_name = "input/" + E.key();
String action_name = "input/" + E.key;
GLOBAL_DEF(action_name, action);
input_presets.push_back(action_name);
}
Expand Down
6 changes: 3 additions & 3 deletions core/config/project_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

#include "core/object/class_db.h"
#include "core/os/thread_safe.h"
#include "core/templates/ordered_hash_map.h"
#include "core/templates/hash_map.h"
#include "core/templates/set.h"

class ProjectSettings : public Object {
Expand Down Expand Up @@ -94,7 +94,7 @@ class ProjectSettings : public Object {
Set<String> custom_features;
Map<StringName, StringName> feature_overrides;

OrderedHashMap<StringName, AutoloadInfo> autoloads;
HashMap<StringName, AutoloadInfo> autoloads;

String project_data_dir_name;

Expand Down Expand Up @@ -181,7 +181,7 @@ class ProjectSettings : public Object {

bool has_custom_feature(const String &p_feature) const;

OrderedHashMap<StringName, AutoloadInfo> get_autoload_list() const;
const HashMap<StringName, AutoloadInfo> &get_autoload_list() const;
void add_autoload(const AutoloadInfo &p_autoload);
void remove_autoload(const StringName &p_autoload);
bool has_autoload(const StringName &p_autoload) const;
Expand Down
16 changes: 8 additions & 8 deletions core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,21 +636,21 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
}
}

for (OrderedHashMap<StringName, InputMap::Action>::ConstElement E = InputMap::get_singleton()->get_action_map().front(); E; E = E.next()) {
if (InputMap::get_singleton()->event_is_action(p_event, E.key())) {
for (const KeyValue<StringName, InputMap::Action> &E : InputMap::get_singleton()->get_action_map()) {
if (InputMap::get_singleton()->event_is_action(p_event, E.key)) {
// If not echo and action pressed state has changed
if (!p_event->is_echo() && is_action_pressed(E.key(), false) != p_event->is_action_pressed(E.key())) {
if (!p_event->is_echo() && is_action_pressed(E.key, false) != p_event->is_action_pressed(E.key)) {
Action action;
action.physics_frame = Engine::get_singleton()->get_physics_frames();
action.process_frame = Engine::get_singleton()->get_process_frames();
action.pressed = p_event->is_action_pressed(E.key());
action.pressed = p_event->is_action_pressed(E.key);
action.strength = 0.0f;
action.raw_strength = 0.0f;
action.exact = InputMap::get_singleton()->event_is_action(p_event, E.key(), true);
action_state[E.key()] = action;
action.exact = InputMap::get_singleton()->event_is_action(p_event, E.key, true);
action_state[E.key] = action;
}
action_state[E.key()].strength = p_event->get_action_strength(E.key());
action_state[E.key()].raw_strength = p_event->get_action_raw_strength(E.key());
action_state[E.key].strength = p_event->get_action_strength(E.key);
action_state[E.key].raw_strength = p_event->get_action_raw_strength(E.key);
}
}

Expand Down
40 changes: 20 additions & 20 deletions core/input/input_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ List<StringName> InputMap::get_actions() const {
return actions;
}

for (OrderedHashMap<StringName, Action>::Element E = input_map.front(); E; E = E.next()) {
actions.push_back(E.key());
for (const KeyValue<StringName, Action> &E : input_map) {
actions.push_back(E.key);
}

return actions;
Expand Down Expand Up @@ -203,20 +203,20 @@ Array InputMap::_action_get_events(const StringName &p_action) {
}

const List<Ref<InputEvent>> *InputMap::action_get_events(const StringName &p_action) {
const OrderedHashMap<StringName, Action>::Element E = input_map.find(p_action);
HashMap<StringName, Action>::Iterator E = input_map.find(p_action);
if (!E) {
return nullptr;
}

return &E.get().inputs;
return &E->value.inputs;
}

bool InputMap::event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match) const {
return event_get_action_status(p_event, p_action, p_exact_match);
}

bool InputMap::event_get_action_status(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match, bool *r_pressed, float *r_strength, float *r_raw_strength) const {
OrderedHashMap<StringName, Action>::Element E = input_map.find(p_action);
HashMap<StringName, Action>::Iterator E = input_map.find(p_action);
ERR_FAIL_COND_V_MSG(!E, false, suggest_actions(p_action));

Ref<InputEventAction> input_event_action = p_event;
Expand All @@ -235,11 +235,11 @@ bool InputMap::event_get_action_status(const Ref<InputEvent> &p_event, const Str
return input_event_action->get_action() == p_action;
}

List<Ref<InputEvent>>::Element *event = _find_event(E.get(), p_event, p_exact_match, r_pressed, r_strength, r_raw_strength);
List<Ref<InputEvent>>::Element *event = _find_event(E->value, p_event, p_exact_match, r_pressed, r_strength, r_raw_strength);
return event != nullptr;
}

const OrderedHashMap<StringName, InputMap::Action> &InputMap::get_action_map() const {
const HashMap<StringName, InputMap::Action> &InputMap::get_action_map() const {
return input_map;
}

Expand Down Expand Up @@ -360,7 +360,7 @@ String InputMap::get_builtin_display_name(const String &p_name) const {
return p_name;
}

const OrderedHashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() {
const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() {
// Return cache if it has already been built.
if (default_builtin_cache.size()) {
return default_builtin_cache;
Expand Down Expand Up @@ -686,19 +686,19 @@ const OrderedHashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() {
return default_builtin_cache;
}

const OrderedHashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins_with_feature_overrides_applied() {
const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins_with_feature_overrides_applied() {
if (default_builtin_with_overrides_cache.size() > 0) {
return default_builtin_with_overrides_cache;
}

OrderedHashMap<String, List<Ref<InputEvent>>> builtins = get_builtins();
HashMap<String, List<Ref<InputEvent>>> builtins = get_builtins();

// Get a list of all built in inputs which are valid overrides for the OS
// Key = builtin name (e.g. ui_accept)
// Value = override/feature names (e.g. macos, if it was defined as "ui_accept.macos" and the platform supports that feature)
Map<String, Vector<String>> builtins_with_overrides;
for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
String fullname = E.key();
for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
String fullname = E.key;

Vector<String> split = fullname.split(".");
String name = split[0];
Expand All @@ -709,8 +709,8 @@ const OrderedHashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins_with
}
}

for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
String fullname = E.key();
for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
String fullname = E.key;

Vector<String> split = fullname.split(".");
String name = split[0];
Expand All @@ -726,22 +726,22 @@ const OrderedHashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins_with
continue;
}

default_builtin_with_overrides_cache.insert(name, E.value());
default_builtin_with_overrides_cache.insert(name, E.value);
}

return default_builtin_with_overrides_cache;
}

void InputMap::load_default() {
OrderedHashMap<String, List<Ref<InputEvent>>> builtins = get_builtins_with_feature_overrides_applied();
HashMap<String, List<Ref<InputEvent>>> builtins = get_builtins_with_feature_overrides_applied();

for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
String name = E.key();
for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
String name = E.key;

add_action(name);

List<Ref<InputEvent>> inputs = E.get();
for (List<Ref<InputEvent>>::Element *I = inputs.front(); I; I = I->next()) {
const List<Ref<InputEvent>> &inputs = E.value;
for (const List<Ref<InputEvent>>::Element *I = inputs.front(); I; I = I->next()) {
Ref<InputEventKey> iek = I->get();

// For the editor, only add keyboard actions.
Expand Down
14 changes: 7 additions & 7 deletions core/input/input_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "core/input/input_event.h"
#include "core/object/class_db.h"
#include "core/object/object.h"
#include "core/templates/ordered_hash_map.h"
#include "core/templates/hash_map.h"

class InputMap : public Object {
GDCLASS(InputMap, Object);
Expand All @@ -54,9 +54,9 @@ class InputMap : public Object {
private:
static InputMap *singleton;

mutable OrderedHashMap<StringName, Action> input_map;
OrderedHashMap<String, List<Ref<InputEvent>>> default_builtin_cache;
OrderedHashMap<String, List<Ref<InputEvent>>> default_builtin_with_overrides_cache;
mutable HashMap<StringName, Action> input_map;
HashMap<String, List<Ref<InputEvent>>> default_builtin_cache;
HashMap<String, List<Ref<InputEvent>>> default_builtin_with_overrides_cache;

List<Ref<InputEvent>>::Element *_find_event(Action &p_action, const Ref<InputEvent> &p_event, bool p_exact_match = false, bool *r_pressed = nullptr, float *r_strength = nullptr, float *r_raw_strength = nullptr) const;

Expand Down Expand Up @@ -85,16 +85,16 @@ class InputMap : public Object {
bool event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match = false) const;
bool event_get_action_status(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match = false, bool *r_pressed = nullptr, float *r_strength = nullptr, float *r_raw_strength = nullptr) const;

const OrderedHashMap<StringName, Action> &get_action_map() const;
const HashMap<StringName, Action> &get_action_map() const;
void load_from_project_settings();
void load_default();

String suggest_actions(const StringName &p_action) const;

String get_builtin_display_name(const String &p_name) const;
// Use an Ordered Map so insertion order is preserved. We want the elements to be 'grouped' somewhat.
const OrderedHashMap<String, List<Ref<InputEvent>>> &get_builtins();
const OrderedHashMap<String, List<Ref<InputEvent>>> &get_builtins_with_feature_overrides_applied();
const HashMap<String, List<Ref<InputEvent>>> &get_builtins();
const HashMap<String, List<Ref<InputEvent>>> &get_builtins_with_feature_overrides_applied();

InputMap();
~InputMap();
Expand Down
27 changes: 15 additions & 12 deletions core/io/config_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void ConfigFile::set_value(const String &p_section, const String &p_key, const V

} else {
if (!values.has(p_section)) {
values[p_section] = OrderedHashMap<String, Variant>();
values[p_section] = HashMap<String, Variant>();
}

values[p_section][p_key] = p_value;
Expand Down Expand Up @@ -102,16 +102,16 @@ bool ConfigFile::has_section_key(const String &p_section, const String &p_key) c
}

void ConfigFile::get_sections(List<String> *r_sections) const {
for (OrderedHashMap<String, OrderedHashMap<String, Variant>>::ConstElement E = values.front(); E; E = E.next()) {
r_sections->push_back(E.key());
for (const KeyValue<String, HashMap<String, Variant>> &E : values) {
r_sections->push_back(E.key);
}
}

void ConfigFile::get_section_keys(const String &p_section, List<String> *r_keys) const {
ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot get keys from nonexistent section \"%s\".", p_section));

for (OrderedHashMap<String, Variant>::ConstElement E = values[p_section].front(); E; E = E.next()) {
r_keys->push_back(E.key());
for (const KeyValue<String, Variant> &E : values[p_section]) {
r_keys->push_back(E.key);
}
}

Expand Down Expand Up @@ -174,18 +174,21 @@ Error ConfigFile::save_encrypted_pass(const String &p_path, const String &p_pass
}

Error ConfigFile::_internal_save(Ref<FileAccess> file) {
for (OrderedHashMap<String, OrderedHashMap<String, Variant>>::Element E = values.front(); E; E = E.next()) {
if (E != values.front()) {
bool first = true;
for (const KeyValue<String, HashMap<String, Variant>> &E : values) {
if (first) {
first = false;
} else {
file->store_string("\n");
}
if (!E.key().is_empty()) {
file->store_string("[" + E.key() + "]\n\n");
if (!E.key.is_empty()) {
file->store_string("[" + E.key + "]\n\n");
}

for (OrderedHashMap<String, Variant>::Element F = E.get().front(); F; F = F.next()) {
for (const KeyValue<String, Variant> &F : E.value) {
String vstr;
VariantWriter::write_to_string(F.get(), vstr);
file->store_string(F.key().property_name_encode() + "=" + vstr + "\n");
VariantWriter::write_to_string(F.value, vstr);
file->store_string(F.key.property_name_encode() + "=" + vstr + "\n");
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/io/config_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@

#include "core/io/file_access.h"
#include "core/object/ref_counted.h"
#include "core/templates/ordered_hash_map.h"
#include "core/templates/hash_map.h"
#include "core/variant/variant_parser.h"

class ConfigFile : public RefCounted {
GDCLASS(ConfigFile, RefCounted);

OrderedHashMap<String, OrderedHashMap<String, Variant>> values;
HashMap<String, HashMap<String, Variant>> values;

PackedStringArray _get_sections() const;
PackedStringArray _get_section_keys(const String &p_section) const;
Expand Down
4 changes: 2 additions & 2 deletions core/io/missing_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ bool MissingResource::_get(const StringName &p_name, Variant &r_ret) const {
}

void MissingResource::_get_property_list(List<PropertyInfo> *p_list) const {
for (OrderedHashMap<StringName, Variant>::ConstElement E = properties.front(); E; E = E.next()) {
p_list->push_back(PropertyInfo(E.value().get_type(), E.key()));
for (const KeyValue<StringName, Variant> &E : properties) {
p_list->push_back(PropertyInfo(E.value.get_type(), E.key));
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/io/missing_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

class MissingResource : public Resource {
GDCLASS(MissingResource, Resource)
OrderedHashMap<StringName, Variant> properties;
HashMap<StringName, Variant> properties;

String original_class;
bool recording_properties = false;
Expand Down
17 changes: 6 additions & 11 deletions core/io/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,8 @@ void ResourceCache::clear() {
if (resources.size()) {
ERR_PRINT("Resources still in use at exit (run with --verbose for details).");
if (OS::get_singleton()->is_stdout_verbose()) {
const String *K = nullptr;
while ((K = resources.next(K))) {
Resource *r = resources[*K];
print_line(vformat("Resource still in use: %s (%s)", *K, r->get_class()));
for (const KeyValue<String, Resource *> &E : resources) {
print_line(vformat("Resource still in use: %s (%s)", E.key, E.value->get_class()));
}
}
}
Expand Down Expand Up @@ -516,10 +514,8 @@ Resource *ResourceCache::get(const String &p_path) {

void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) {
lock.read_lock();
const String *K = nullptr;
while ((K = resources.next(K))) {
Resource *r = resources[*K];
p_resources->push_back(Ref<Resource>(r));
for (KeyValue<String, Resource *> &E : resources) {
p_resources->push_back(Ref<Resource>(E.value));
}
lock.read_unlock();
}
Expand All @@ -544,9 +540,8 @@ void ResourceCache::dump(const char *p_file, bool p_short) {
ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file at path '" + String::utf8(p_file) + "'.");
}

const String *K = nullptr;
while ((K = resources.next(K))) {
Resource *r = resources[*K];
for (KeyValue<String, Resource *> &E : resources) {
Resource *r = E.value;

if (!type_count.has(r->get_class())) {
type_count[r->get_class()] = 0;
Expand Down
Loading