diff --git a/core/io/json.cpp b/core/io/json.cpp index 408e455ab03..fcb6182fc50 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -122,8 +122,7 @@ String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_ ERR_FAIL_COND_V_MSG(p_markers.has(d.id()), "\"{...}\"", "Converting circular structure to JSON."); p_markers.insert(d.id()); - List keys; - d.get_key_list(&keys); + LocalVector keys = d.get_key_list(); if (p_sort_keys) { keys.sort_custom(); diff --git a/core/io/resource.cpp b/core/io/resource.cpp index 9c216bf3491..fac829d9cb4 100644 --- a/core/io/resource.cpp +++ b/core/io/resource.cpp @@ -272,9 +272,7 @@ void Resource::_dupe_sub_resources(Variant &r_variant, Node *p_for_scene, HashMa } break; case Variant::DICTIONARY: { Dictionary d = r_variant; - List keys; - d.get_key_list(&keys); - for (Variant &k : keys) { + for (Variant &k : d.get_key_list()) { if (k.get_type() == Variant::OBJECT) { // Replace in dictionary key. Ref sr = k; diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cpp index 6d89e3a1aed..7f7dd2f36ff 100644 --- a/core/variant/dictionary.cpp +++ b/core/variant/dictionary.cpp @@ -57,14 +57,14 @@ Dictionary::ConstIterator Dictionary::end() const { return _p->variant_map.end(); } -void Dictionary::get_key_list(List *p_keys) const { - if (_p->variant_map.is_empty()) { - return; - } +LocalVector Dictionary::get_key_list() const { + LocalVector keys; + keys.reserve(_p->variant_map.size()); for (const KeyValue &E : _p->variant_map) { - p_keys->push_back(E.key); + keys.push_back(E.key); } + return keys; } Variant Dictionary::get_key_at_index(int p_index) const { diff --git a/core/variant/dictionary.h b/core/variant/dictionary.h index 781b9c50fc9..d170a5d3942 100644 --- a/core/variant/dictionary.h +++ b/core/variant/dictionary.h @@ -32,7 +32,7 @@ #include "core/string/ustring.h" #include "core/templates/hash_map.h" -#include "core/templates/list.h" +#include "core/templates/local_vector.h" #include "core/templates/pair.h" #include "core/variant/array.h" @@ -55,7 +55,7 @@ public: ConstIterator begin() const; ConstIterator end() const; - void get_key_list(List *p_keys) const; + LocalVector get_key_list() const; Variant get_key_at_index(int p_index) const; Variant get_value_at_index(int p_index) const; diff --git a/core/variant/variant.h b/core/variant/variant.h index 4b247492a81..e0b2a14ff04 100644 --- a/core/variant/variant.h +++ b/core/variant/variant.h @@ -54,6 +54,7 @@ #include "core/os/keyboard.h" #include "core/string/node_path.h" #include "core/string/ustring.h" +#include "core/templates/list.h" #include "core/templates/paged_allocator.h" #include "core/templates/rid.h" #include "core/variant/array.h" diff --git a/core/variant/variant_parser.cpp b/core/variant/variant_parser.cpp index 8133dc50b4e..752b9172f32 100644 --- a/core/variant/variant_parser.cpp +++ b/core/variant/variant_parser.cpp @@ -2248,8 +2248,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str ERR_PRINT("Max recursion reached"); p_store_string_func(p_store_string_ud, "{}"); } else { - List keys; - dict.get_key_list(&keys); + LocalVector keys = dict.get_key_list(); keys.sort_custom(); if (keys.is_empty()) { @@ -2260,11 +2259,12 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str p_store_string_func(p_store_string_ud, "{\n"); - for (List::Element *E = keys.front(); E; E = E->next()) { - write(E->get(), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat); + for (uint32_t i = 0; i < keys.size(); i++) { + const Variant &key = keys[i]; + write(key, p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat); p_store_string_func(p_store_string_ud, ": "); - write(dict[E->get()], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat); - if (E->next()) { + write(dict[key], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat); + if (i + 1 < keys.size()) { p_store_string_func(p_store_string_ud, ",\n"); } else { p_store_string_func(p_store_string_ud, "\n"); diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 44c6ff56106..71314430a31 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -3702,10 +3702,8 @@ void EditorNode::replace_resources_in_object(Object *p_object, const Vectorget(E.name); - List keys; bool dictionary_requires_updating = false; - d.get_key_list(&keys); - for (const Variant &F : keys) { + for (const Variant &F : d.get_key_list()) { Variant v = d[F]; Ref res = v; diff --git a/editor/export/editor_export_platform.cpp b/editor/export/editor_export_platform.cpp index d2a82cbf2e1..477444aec90 100644 --- a/editor/export/editor_export_platform.cpp +++ b/editor/export/editor_export_platform.cpp @@ -638,9 +638,7 @@ EditorExportPlatform::ExportNotifier::~ExportNotifier() { bool EditorExportPlatform::_export_customize_dictionary(Dictionary &dict, LocalVector> &customize_resources_plugins) { bool changed = false; - List keys; - dict.get_key_list(&keys); - for (const Variant &K : keys) { + for (const Variant &K : dict.get_key_list()) { Variant v = dict[K]; switch (v.get_type()) { case Variant::OBJECT: { diff --git a/editor/filesystem_dock.cpp b/editor/filesystem_dock.cpp index aea97507eb2..26991dbb08b 100644 --- a/editor/filesystem_dock.cpp +++ b/editor/filesystem_dock.cpp @@ -1732,9 +1732,7 @@ void FileSystemDock::_folder_removed(const String &p_folder) { // Remove assigned folder color for all subfolders. bool folder_colors_updated = false; - List paths; - assigned_folder_colors.get_key_list(&paths); - for (const Variant &E : paths) { + for (const Variant &E : assigned_folder_colors.get_key_list()) { const String &path = E; // These folder paths are guaranteed to end with a "/". if (path.begins_with(p_folder)) { diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index 7b4d6673ee3..aa7de570fcb 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -2199,8 +2199,7 @@ void VisualShaderEditor::_update_nodes() { } } - List keys; - added.get_key_list(&keys); + LocalVector keys = added.get_key_list(); keys.sort_custom(); for (const Variant &key : keys) { diff --git a/modules/gdscript/editor/gdscript_docgen.cpp b/modules/gdscript/editor/gdscript_docgen.cpp index 37c0c46b216..733de258857 100644 --- a/modules/gdscript/editor/gdscript_docgen.cpp +++ b/modules/gdscript/editor/gdscript_docgen.cpp @@ -216,15 +216,15 @@ String GDScriptDocGen::_docvalue_from_variant(const Variant &p_variant, int p_re } else { result += "{"; - List keys; - dict.get_key_list(&keys); + LocalVector keys = dict.get_key_list(); keys.sort_custom(); - for (List::Element *E = keys.front(); E; E = E->next()) { - if (E->prev()) { + for (uint32_t i = 0; i < keys.size(); i++) { + const Variant &key = keys[i]; + if (i > 0) { result += ", "; } - result += _docvalue_from_variant(E->get(), p_recursion_level + 1) + ": " + _docvalue_from_variant(dict[E->get()], p_recursion_level + 1); + result += _docvalue_from_variant(key, p_recursion_level + 1) + ": " + _docvalue_from_variant(dict[key], p_recursion_level + 1); } result += "}"; diff --git a/servers/rendering_server.cpp b/servers/rendering_server.cpp index a3b3884aa97..8fb81e59f8a 100644 --- a/servers/rendering_server.cpp +++ b/servers/rendering_server.cpp @@ -1317,8 +1317,7 @@ Error RenderingServer::mesh_create_surface_data_from_arrays(SurfaceData *r_surfa } Vector lods; if (index_array_len) { - List keys; - p_lods.get_key_list(&keys); + LocalVector keys = p_lods.get_key_list(); keys.sort(); // otherwise lod levels may get skipped for (const Variant &E : keys) { float distance = E; diff --git a/tests/core/variant/test_dictionary.h b/tests/core/variant/test_dictionary.h index cef6a8ef749..e5eee6eca5e 100644 --- a/tests/core/variant/test_dictionary.h +++ b/tests/core/variant/test_dictionary.h @@ -115,19 +115,18 @@ TEST_CASE("[Dictionary] List init") { CHECK_EQ(tdict[5.0], Variant(2.0)); } -TEST_CASE("[Dictionary] get_key_lists()") { +TEST_CASE("[Dictionary] get_key_list()") { Dictionary map; - List keys; - List *ptr = &keys; - map.get_key_list(ptr); + LocalVector keys; + keys = map.get_key_list(); CHECK(keys.is_empty()); map[1] = 3; - map.get_key_list(ptr); + keys = map.get_key_list(); CHECK(keys.size() == 1); - CHECK(int(keys.front()->get()) == 1); + CHECK(int(keys[0]) == 1); map[2] = 4; - map.get_key_list(ptr); - CHECK(keys.size() == 3); + keys = map.get_key_list(); + CHECK(keys.size() == 2); } TEST_CASE("[Dictionary] get_key_at_index()") {