Skip to content

Commit

Permalink
Merge pull request #86518 from AThousandShips/array_iter
Browse files Browse the repository at this point in the history
[Core] Add iteration support to `Array`
  • Loading branch information
akien-mga committed Apr 10, 2024
2 parents 1f0f810 + 64146cb commit 7670b81
Show file tree
Hide file tree
Showing 15 changed files with 251 additions and 73 deletions.
4 changes: 2 additions & 2 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1330,8 +1330,8 @@ void ProjectSettings::load_scene_groups_cache() {
for (const String &E : scene_paths) {
Array scene_groups = cf->get_value(E, "groups", Array());
HashSet<StringName> cache;
for (int i = 0; i < scene_groups.size(); ++i) {
cache.insert(scene_groups[i]);
for (const Variant &scene_group : scene_groups) {
cache.insert(scene_group);
}
add_scene_groups_cache(E, cache);
}
Expand Down
24 changes: 10 additions & 14 deletions core/extension/extension_api_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1313,9 +1313,7 @@ static bool compare_value(const String &p_path, const String &p_field, const Var
} else if (p_old_value.get_type() == Variant::DICTIONARY && p_new_value.get_type() == Variant::DICTIONARY) {
Dictionary old_dict = p_old_value;
Dictionary new_dict = p_new_value;
Array old_keys = old_dict.keys();
for (int i = 0; i < old_keys.size(); i++) {
Variant key = old_keys[i];
for (const Variant &key : old_dict.keys()) {
if (!new_dict.has(key)) {
failed = true;
print_error(vformat("Validate extension JSON: Error: Field '%s': %s was removed.", p_path, key));
Expand All @@ -1328,9 +1326,7 @@ static bool compare_value(const String &p_path, const String &p_field, const Var
failed = true;
}
}
Array new_keys = old_dict.keys();
for (int i = 0; i < new_keys.size(); i++) {
Variant key = new_keys[i];
for (const Variant &key : old_dict.keys()) {
if (!old_dict.has(key)) {
failed = true;
print_error(vformat("Validate extension JSON: Error: Field '%s': %s was added with value %s.", p_path, key, new_dict[key]));
Expand All @@ -1356,8 +1352,8 @@ static bool compare_dict_array(const Dictionary &p_old_api, const Dictionary &p_
Array new_api = p_new_api[p_base_array];
HashMap<String, Dictionary> new_api_assoc;

for (int i = 0; i < new_api.size(); i++) {
Dictionary elem = new_api[i];
for (const Variant &var : new_api) {
Dictionary elem = var;
ERR_FAIL_COND_V_MSG(!elem.has(p_name_field), false, vformat("Validate extension JSON: Element of base_array '%s' is missing field '%s'. This is a bug.", base_array, p_name_field));
String name = elem[p_name_field];
if (p_compare_operators && elem.has("right_type")) {
Expand All @@ -1367,8 +1363,8 @@ static bool compare_dict_array(const Dictionary &p_old_api, const Dictionary &p_
}

Array old_api = p_old_api[p_base_array];
for (int i = 0; i < old_api.size(); i++) {
Dictionary old_elem = old_api[i];
for (const Variant &var : old_api) {
Dictionary old_elem = var;
if (!old_elem.has(p_name_field)) {
failed = true;
print_error(vformat("Validate extension JSON: JSON file: element of base array '%s' is missing the field: '%s'.", base_array, p_name_field));
Expand Down Expand Up @@ -1508,16 +1504,16 @@ static bool compare_sub_dict_array(HashSet<String> &r_removed_classes_registered
Array new_api = p_new_api[p_outer];
HashMap<String, Dictionary> new_api_assoc;

for (int i = 0; i < new_api.size(); i++) {
Dictionary elem = new_api[i];
for (const Variant &var : new_api) {
Dictionary elem = var;
ERR_FAIL_COND_V_MSG(!elem.has(p_outer_name), false, vformat("Validate extension JSON: Element of base_array '%s' is missing field '%s'. This is a bug.", p_outer, p_outer_name));
new_api_assoc.insert(elem[p_outer_name], elem);
}

Array old_api = p_old_api[p_outer];

for (int i = 0; i < old_api.size(); i++) {
Dictionary old_elem = old_api[i];
for (const Variant &var : old_api) {
Dictionary old_elem = var;
if (!old_elem.has(p_outer_name)) {
failed = true;
print_error(vformat("Validate extension JSON: JSON file: element of base array '%s' is missing the field: '%s'.", p_outer, p_outer_name));
Expand Down
11 changes: 7 additions & 4 deletions core/io/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_
case Variant::PACKED_STRING_ARRAY:
case Variant::ARRAY: {
Array a = p_var;
if (a.size() == 0) {
if (a.is_empty()) {
return "[]";
}
String s = "[";
Expand All @@ -95,12 +95,15 @@ String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_
ERR_FAIL_COND_V_MSG(p_markers.has(a.id()), "\"[...]\"", "Converting circular structure to JSON.");
p_markers.insert(a.id());

for (int i = 0; i < a.size(); i++) {
if (i > 0) {
bool first = true;
for (const Variant &var : a) {
if (first) {
first = false;
} else {
s += ",";
s += end_statement;
}
s += _make_indent(p_indent, p_cur_indent + 1) + _stringify(a[i], p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
s += _make_indent(p_indent, p_cur_indent + 1) + _stringify(var, p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
}
s += end_statement + _make_indent(p_indent, p_cur_indent) + "]";
p_markers.erase(a.id());
Expand Down
4 changes: 2 additions & 2 deletions core/io/marshalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1729,9 +1729,9 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
}
r_len += 4;

for (int i = 0; i < array.size(); i++) {
for (const Variant &var : array) {
int len;
Error err = encode_variant(array.get(i), buf, len, p_full_objects, p_depth + 1);
Error err = encode_variant(var, buf, len, p_full_objects, p_depth + 1);
ERR_FAIL_COND_V(err, err);
ERR_FAIL_COND_V(len % 4, ERR_BUG);
if (buf) {
Expand Down
8 changes: 3 additions & 5 deletions core/io/resource_format_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1844,8 +1844,8 @@ void ResourceFormatSaverBinaryInstance::write_variant(Ref<FileAccess> f, const V
f->store_32(VARIANT_ARRAY);
Array a = p_property;
f->store_32(uint32_t(a.size()));
for (int i = 0; i < a.size(); i++) {
write_variant(f, a[i], resource_map, external_resources, string_map);
for (const Variant &var : a) {
write_variant(f, var, resource_map, external_resources, string_map);
}

} break;
Expand Down Expand Up @@ -2017,9 +2017,7 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant
case Variant::ARRAY: {
Array varray = p_variant;
_find_resources(varray.get_typed_script());
int len = varray.size();
for (int i = 0; i < len; i++) {
const Variant &v = varray.get(i);
for (const Variant &v : varray) {
_find_resources(v);
}

Expand Down
5 changes: 3 additions & 2 deletions core/io/resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,8 +1035,9 @@ void ResourceLoader::load_translation_remaps() {
Array langs = remaps[E];
Vector<String> lang_remaps;
lang_remaps.resize(langs.size());
for (int i = 0; i < langs.size(); i++) {
lang_remaps.write[i] = langs[i];
String *lang_remaps_ptrw = lang_remaps.ptrw();
for (const Variant &lang : langs) {
*lang_remaps_ptrw++ = lang;
}

translation_remaps[String(E)] = lang_remaps;
Expand Down
16 changes: 8 additions & 8 deletions core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,16 @@ MethodInfo MethodInfo::from_dict(const Dictionary &p_dict) {
args = p_dict["args"];
}

for (int i = 0; i < args.size(); i++) {
Dictionary d = args[i];
for (const Variant &arg : args) {
Dictionary d = arg;
mi.arguments.push_back(PropertyInfo::from_dict(d));
}
Array defargs;
if (p_dict.has("default_args")) {
defargs = p_dict["default_args"];
}
for (int i = 0; i < defargs.size(); i++) {
mi.default_arguments.push_back(defargs[i]);
for (const Variant &defarg : defargs) {
mi.default_arguments.push_back(defarg);
}

if (p_dict.has("return")) {
Expand Down Expand Up @@ -1233,8 +1233,8 @@ void Object::_add_user_signal(const String &p_name, const Array &p_args) {
MethodInfo mi;
mi.name = p_name;

for (int i = 0; i < p_args.size(); i++) {
Dictionary d = p_args[i];
for (const Variant &arg : p_args) {
Dictionary d = arg;
PropertyInfo param;

if (d.has("name")) {
Expand Down Expand Up @@ -1585,8 +1585,8 @@ void Object::_clear_internal_resource_paths(const Variant &p_var) {
} break;
case Variant::ARRAY: {
Array a = p_var;
for (int i = 0; i < a.size(); i++) {
_clear_internal_resource_paths(a[i]);
for (const Variant &var : a) {
_clear_internal_resource_paths(var);
}

} break;
Expand Down
12 changes: 6 additions & 6 deletions core/object/script_language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ void ScriptServer::init_languages() {
if (ProjectSettings::get_singleton()->has_setting("_global_script_classes")) {
Array script_classes = GLOBAL_GET("_global_script_classes");

for (int i = 0; i < script_classes.size(); i++) {
Dictionary c = script_classes[i];
for (const Variant &script_class : script_classes) {
Dictionary c = script_class;
if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base")) {
continue;
}
Expand All @@ -263,8 +263,8 @@ void ScriptServer::init_languages() {
#endif

Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
for (int i = 0; i < script_classes.size(); i++) {
Dictionary c = script_classes[i];
for (const Variant &script_class : script_classes) {
Dictionary c = script_class;
if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base")) {
continue;
}
Expand Down Expand Up @@ -463,8 +463,8 @@ void ScriptServer::save_global_classes() {
Dictionary class_icons;

Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
for (int i = 0; i < script_classes.size(); i++) {
Dictionary d = script_classes[i];
for (const Variant &script_class : script_classes) {
Dictionary d = script_class;
if (!d.has("name") || !d.has("icon")) {
continue;
}
Expand Down
36 changes: 18 additions & 18 deletions core/object/script_language_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ class ScriptLanguageExtension : public ScriptLanguage {
}
if (r_errors != nullptr && ret.has("errors")) {
Array errors = ret["errors"];
for (int i = 0; i < errors.size(); i++) {
Dictionary err = errors[i];
for (const Variant &error : errors) {
Dictionary err = error;
ERR_CONTINUE(!err.has("line"));
ERR_CONTINUE(!err.has("column"));
ERR_CONTINUE(!err.has("message"));
Expand All @@ -339,8 +339,8 @@ class ScriptLanguageExtension : public ScriptLanguage {
if (r_warnings != nullptr && ret.has("warnings")) {
ERR_FAIL_COND_V(!ret.has("warnings"), false);
Array warnings = ret["warnings"];
for (int i = 0; i < warnings.size(); i++) {
Dictionary warn = warnings[i];
for (const Variant &warning : warnings) {
Dictionary warn = warning;
ERR_CONTINUE(!warn.has("start_line"));
ERR_CONTINUE(!warn.has("end_line"));
ERR_CONTINUE(!warn.has("leftmost_column"));
Expand Down Expand Up @@ -402,8 +402,8 @@ class ScriptLanguageExtension : public ScriptLanguage {

if (r_options != nullptr && ret.has("options")) {
Array options = ret["options"];
for (int i = 0; i < options.size(); i++) {
Dictionary op = options[i];
for (const Variant &var : options) {
Dictionary op = var;
CodeCompletionOption option;
ERR_CONTINUE(!op.has("kind"));
option.kind = CodeCompletionKind(int(op["kind"]));
Expand Down Expand Up @@ -502,8 +502,8 @@ class ScriptLanguageExtension : public ScriptLanguage {
}
if (p_values != nullptr && ret.has("values")) {
Array values = ret["values"];
for (int i = 0; i < values.size(); i++) {
p_values->push_back(values[i]);
for (const Variant &value : values) {
p_values->push_back(value);
}
}
}
Expand All @@ -522,8 +522,8 @@ class ScriptLanguageExtension : public ScriptLanguage {
}
if (p_values != nullptr && ret.has("values")) {
Array values = ret["values"];
for (int i = 0; i < values.size(); i++) {
p_values->push_back(values[i]);
for (const Variant &value : values) {
p_values->push_back(value);
}
}
}
Expand All @@ -549,8 +549,8 @@ class ScriptLanguageExtension : public ScriptLanguage {
}
if (p_values != nullptr && ret.has("values")) {
Array values = ret["values"];
for (int i = 0; i < values.size(); i++) {
p_values->push_back(values[i]);
for (const Variant &value : values) {
p_values->push_back(value);
}
}
}
Expand All @@ -562,9 +562,9 @@ class ScriptLanguageExtension : public ScriptLanguage {
TypedArray<Dictionary> ret;
GDVIRTUAL_REQUIRED_CALL(_debug_get_current_stack_info, ret);
Vector<StackInfo> sret;
for (int i = 0; i < ret.size(); i++) {
for (const Variant &var : ret) {
StackInfo si;
Dictionary d = ret[i];
Dictionary d = var;
ERR_CONTINUE(!d.has("file"));
ERR_CONTINUE(!d.has("func"));
ERR_CONTINUE(!d.has("line"));
Expand Down Expand Up @@ -595,8 +595,8 @@ class ScriptLanguageExtension : public ScriptLanguage {
virtual void get_public_functions(List<MethodInfo> *p_functions) const override {
TypedArray<Dictionary> ret;
GDVIRTUAL_REQUIRED_CALL(_get_public_functions, ret);
for (int i = 0; i < ret.size(); i++) {
MethodInfo mi = MethodInfo::from_dict(ret[i]);
for (const Variant &var : ret) {
MethodInfo mi = MethodInfo::from_dict(var);
p_functions->push_back(mi);
}
}
Expand All @@ -615,8 +615,8 @@ class ScriptLanguageExtension : public ScriptLanguage {
virtual void get_public_annotations(List<MethodInfo> *p_annotations) const override {
TypedArray<Dictionary> ret;
GDVIRTUAL_REQUIRED_CALL(_get_public_annotations, ret);
for (int i = 0; i < ret.size(); i++) {
MethodInfo mi = MethodInfo::from_dict(ret[i]);
for (const Variant &var : ret) {
MethodInfo mi = MethodInfo::from_dict(var);
p_annotations->push_back(mi);
}
}
Expand Down
16 changes: 16 additions & 0 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ void Array::_unref() const {
_p = nullptr;
}

Array::Iterator Array::begin() {
return Iterator(_p->array.ptrw(), _p->read_only);
}

Array::Iterator Array::end() {
return Iterator(_p->array.ptrw() + _p->array.size(), _p->read_only);
}

Array::ConstIterator Array::begin() const {
return ConstIterator(_p->array.ptr(), _p->read_only);
}

Array::ConstIterator Array::end() const {
return ConstIterator(_p->array.ptr() + _p->array.size(), _p->read_only);
}

Variant &Array::operator[](int p_idx) {
if (unlikely(_p->read_only)) {
*_p->read_only = _p->array[p_idx];
Expand Down
Loading

0 comments on commit 7670b81

Please sign in to comment.