Skip to content

Commit

Permalink
Merge pull request #70773 from KoBeWi/lector
Browse files Browse the repository at this point in the history
Add range iterator to LocalVector
  • Loading branch information
akien-mga committed Jan 21, 2023
2 parents c781537 + 615c517 commit c3539b4
Show file tree
Hide file tree
Showing 42 changed files with 473 additions and 503 deletions.
8 changes: 2 additions & 6 deletions core/math/bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,7 @@ class BVH_Manager {
params.result_array = nullptr;
params.subindex_array = nullptr;

for (unsigned int n = 0; n < changed_items.size(); n++) {
const BVHHandle &h = changed_items[n];

for (const BVHHandle &h : changed_items) {
// use the expanded aabb for pairing
const BOUNDS &expanded_aabb = tree._pairs[h.id()].expanded_aabb;
BVHABB_CLASS abb;
Expand All @@ -465,9 +463,7 @@ class BVH_Manager {
params.result_count_overall = 0; // might not be needed
tree.cull_aabb(params, false);

for (unsigned int i = 0; i < tree._cull_hits.size(); i++) {
uint32_t ref_id = tree._cull_hits[i];

for (const uint32_t ref_id : tree._cull_hits) {
// don't collide against ourself
if (ref_id == changed_item_ref_id) {
continue;
Expand Down
12 changes: 6 additions & 6 deletions core/math/delaunay_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,20 +313,20 @@ class Delaunay3D {
//remove simplex and continue
simplex_list.erase(simplex->SE);

for (uint32_t k = 0; k < simplex->grid_positions.size(); k++) {
Vector3i p = simplex->grid_positions[k].pos;
acceleration_grid[p.x][p.y][p.z].erase(simplex->grid_positions[k].E);
for (const GridPos &gp : simplex->grid_positions) {
Vector3i p = gp.pos;
acceleration_grid[p.x][p.y][p.z].erase(gp.E);
}
memdelete(simplex);
}
E = N;
}

for (uint32_t j = 0; j < triangles.size(); j++) {
if (triangles[j].bad) {
for (const Triangle &triangle : triangles) {
if (triangle.bad) {
continue;
}
Simplex *new_simplex = memnew(Simplex(triangles[j].triangle[0], triangles[j].triangle[1], triangles[j].triangle[2], i));
Simplex *new_simplex = memnew(Simplex(triangle.triangle[0], triangle.triangle[1], triangle.triangle[2], i));
circum_sphere_compute(points, new_simplex);
new_simplex->SE = simplex_list.push_back(new_simplex);
{
Expand Down
28 changes: 13 additions & 15 deletions core/math/geometry_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,19 @@ real_t Geometry3D::get_closest_distance_between_segments(const Vector3 &p_p0, co
void Geometry3D::MeshData::optimize_vertices() {
HashMap<int, int> vtx_remap;

for (uint32_t i = 0; i < faces.size(); i++) {
for (uint32_t j = 0; j < faces[i].indices.size(); j++) {
int idx = faces[i].indices[j];
if (!vtx_remap.has(idx)) {
for (MeshData::Face &face : faces) {
for (int &index : face.indices) {
if (!vtx_remap.has(index)) {
int ni = vtx_remap.size();
vtx_remap[idx] = ni;
vtx_remap[index] = ni;
}

faces[i].indices[j] = vtx_remap[idx];
index = vtx_remap[index];
}
}

for (uint32_t i = 0; i < edges.size(); i++) {
int a = edges[i].vertex_a;
int b = edges[i].vertex_b;
for (MeshData::Edge edge : edges) {
int a = edge.vertex_a;
int b = edge.vertex_b;

if (!vtx_remap.has(a)) {
int ni = vtx_remap.size();
Expand All @@ -166,8 +164,8 @@ void Geometry3D::MeshData::optimize_vertices() {
vtx_remap[b] = ni;
}

edges[i].vertex_a = vtx_remap[a];
edges[i].vertex_b = vtx_remap[b];
edge.vertex_a = vtx_remap[a];
edge.vertex_b = vtx_remap[b];
}

LocalVector<Vector3> new_vertices;
Expand Down Expand Up @@ -673,18 +671,18 @@ Geometry3D::MeshData Geometry3D::build_convex_mesh(const Vector<Plane> &p_planes
MeshData::Face face;

// Add face indices.
for (uint32_t j = 0; j < vertices.size(); j++) {
for (const Vector3 &vertex : vertices) {
int idx = -1;
for (uint32_t k = 0; k < mesh.vertices.size(); k++) {
if (mesh.vertices[k].distance_to(vertices[j]) < 0.001f) {
if (mesh.vertices[k].distance_to(vertex) < 0.001f) {
idx = k;
break;
}
}

if (idx == -1) {
idx = mesh.vertices.size();
mesh.vertices.push_back(vertices[j]);
mesh.vertices.push_back(vertex);
}

face.indices.push_back(idx);
Expand Down
3 changes: 1 addition & 2 deletions core/object/undo_redo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ void UndoRedo::create_action(const String &p_name, MergeMode p_mode) {
}
}

for (unsigned int i = 0; i < to_remove.size(); i++) {
List<Operation>::Element *E = to_remove[i];
for (List<Operation>::Element *E : to_remove) {
// Delete all object references
E->get().delete_reference();
E->erase();
Expand Down
12 changes: 6 additions & 6 deletions core/object/worker_thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,11 @@ void WorkerThreadPool::wait_for_group_task_completion(GroupID p_group) {
Group *group = *groupp;

if (group->low_priority_native_tasks.size() > 0) {
for (uint32_t i = 0; i < group->low_priority_native_tasks.size(); i++) {
group->low_priority_native_tasks[i]->low_priority_thread->wait_to_finish();
native_thread_allocator.free(group->low_priority_native_tasks[i]->low_priority_thread);
for (Task *task : group->low_priority_native_tasks) {
task->low_priority_thread->wait_to_finish();
native_thread_allocator.free(task->low_priority_thread);
task_mutex.lock();
task_allocator.free(group->low_priority_native_tasks[i]);
task_allocator.free(task);
task_mutex.unlock();
}

Expand Down Expand Up @@ -449,8 +449,8 @@ void WorkerThreadPool::finish() {
task_available_semaphore.post();
}

for (uint32_t i = 0; i < threads.size(); i++) {
threads[i].thread.wait_to_finish();
for (ThreadData &data : threads) {
data.thread.wait_to_finish();
}

threads.clear();
Expand Down
64 changes: 64 additions & 0 deletions core/templates/local_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,70 @@ class LocalVector {
return data[p_index];
}

struct Iterator {
_FORCE_INLINE_ T &operator*() const {
return *elem_ptr;
}
_FORCE_INLINE_ T *operator->() const { return elem_ptr; }
_FORCE_INLINE_ Iterator &operator++() {
elem_ptr++;
return *this;
}
_FORCE_INLINE_ Iterator &operator--() {
elem_ptr--;
return *this;
}

_FORCE_INLINE_ bool operator==(const Iterator &b) const { return elem_ptr == b.elem_ptr; }
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return elem_ptr != b.elem_ptr; }

Iterator(T *p_ptr) { elem_ptr = p_ptr; }
Iterator() {}
Iterator(const Iterator &p_it) { elem_ptr = p_it.elem_ptr; }

private:
T *elem_ptr = nullptr;
};

struct ConstIterator {
_FORCE_INLINE_ const T &operator*() const {
return *elem_ptr;
}
_FORCE_INLINE_ const T *operator->() const { return elem_ptr; }
_FORCE_INLINE_ ConstIterator &operator++() {
elem_ptr++;
return *this;
}
_FORCE_INLINE_ ConstIterator &operator--() {
elem_ptr--;
return *this;
}

_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return elem_ptr == b.elem_ptr; }
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return elem_ptr != b.elem_ptr; }

ConstIterator(const T *p_ptr) { elem_ptr = p_ptr; }
ConstIterator() {}
ConstIterator(const ConstIterator &p_it) { elem_ptr = p_it.elem_ptr; }

private:
const T *elem_ptr = nullptr;
};

_FORCE_INLINE_ Iterator begin() {
return Iterator(data);
}
_FORCE_INLINE_ Iterator end() {
return Iterator(data + size());
}

_FORCE_INLINE_ ConstIterator begin() const {
return ConstIterator(ptr());
}
_FORCE_INLINE_ ConstIterator end() const {
return ConstIterator(ptr() + size());
}

void insert(U p_pos, T p_val) {
ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
if (p_pos == count) {
Expand Down
8 changes: 4 additions & 4 deletions core/variant/variant_setget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ void unregister_named_setters_getters() {
bool Variant::has_member(Variant::Type p_type, const StringName &p_member) {
ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false);

for (uint32_t i = 0; i < variant_setters_getters_names[p_type].size(); i++) {
if (variant_setters_getters_names[p_type][i] == p_member) {
for (const StringName &member : variant_setters_getters_names[p_type]) {
if (member == p_member) {
return true;
}
}
Expand All @@ -172,8 +172,8 @@ Variant::Type Variant::get_member_type(Variant::Type p_type, const StringName &p
}

void Variant::get_member_list(Variant::Type p_type, List<StringName> *r_members) {
for (uint32_t i = 0; i < variant_setters_getters_names[p_type].size(); i++) {
r_members->push_back(variant_setters_getters_names[p_type][i]);
for (const StringName &member : variant_setters_getters_names[p_type]) {
r_members->push_back(member);
}
}

Expand Down
8 changes: 4 additions & 4 deletions drivers/vulkan/vulkan_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,11 @@ Error VulkanContext::_get_preferred_validation_layers(uint32_t *count, const cha
ERR_FAIL_V(ERR_CANT_CREATE);
}

for (uint32_t i = 0; i < instance_validation_layers_alt.size(); i++) {
if (_check_layers(instance_validation_layers_alt[i].size(), instance_validation_layers_alt[i].ptr(), instance_layer_count, instance_layers)) {
*count = instance_validation_layers_alt[i].size();
for (const LocalVector<const char *> &layer : instance_validation_layers_alt) {
if (_check_layers(layer.size(), layer.ptr(), instance_layer_count, instance_layers)) {
*count = layer.size();
if (names != nullptr) {
*names = instance_validation_layers_alt[i].ptr();
*names = layer.ptr();
}
break;
}
Expand Down
3 changes: 1 addition & 2 deletions editor/editor_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2181,8 +2181,7 @@ void EditorInspectorArray::_notification(int p_what) {
odd_style->set_bg_color(color.darkened(-0.08));
even_style->set_bg_color(color.darkened(0.08));

for (int i = 0; i < (int)array_elements.size(); i++) {
ArrayElement &ae = array_elements[i];
for (ArrayElement &ae : array_elements) {
if (ae.move_texture_rect) {
ae.move_texture_rect->set_texture(get_theme_icon(SNAME("TripleBar"), SNAME("EditorIcons")));
}
Expand Down
8 changes: 4 additions & 4 deletions editor/editor_toaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ void EditorToaster::_auto_hide_or_free_toasts() {
}

// Delete the control right away (removed as child) as it might cause issues otherwise when iterative over the vbox_container children.
for (unsigned int i = 0; i < to_delete.size(); i++) {
vbox_container->remove_child(to_delete[i]);
to_delete[i]->queue_free();
toasts.erase(to_delete[i]);
for (Control *c : to_delete) {
vbox_container->remove_child(c);
c->queue_free();
toasts.erase(c);
}

if (toasts.is_empty()) {
Expand Down
38 changes: 19 additions & 19 deletions editor/export/editor_export_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ bool EditorExportPlatform::_export_customize_dictionary(Dictionary &dict, LocalV
case Variant::OBJECT: {
Ref<Resource> res = v;
if (res.is_valid()) {
for (uint32_t j = 0; j < customize_resources_plugins.size(); j++) {
Ref<Resource> new_res = customize_resources_plugins[j]->_customize_resource(res, "");
for (Ref<EditorExportPlugin> &plugin : customize_resources_plugins) {
Ref<Resource> new_res = plugin->_customize_resource(res, "");
if (new_res.is_valid()) {
changed = true;
if (new_res != res) {
Expand Down Expand Up @@ -550,8 +550,8 @@ bool EditorExportPlatform::_export_customize_array(Array &arr, LocalVector<Ref<E
case Variant::OBJECT: {
Ref<Resource> res = v;
if (res.is_valid()) {
for (uint32_t j = 0; j < customize_resources_plugins.size(); j++) {
Ref<Resource> new_res = customize_resources_plugins[j]->_customize_resource(res, "");
for (Ref<EditorExportPlugin> &plugin : customize_resources_plugins) {
Ref<Resource> new_res = plugin->_customize_resource(res, "");
if (new_res.is_valid()) {
changed = true;
if (new_res != res) {
Expand Down Expand Up @@ -597,8 +597,8 @@ bool EditorExportPlatform::_export_customize_object(Object *p_object, LocalVecto
case Variant::OBJECT: {
Ref<Resource> res = p_object->get(E.name);
if (res.is_valid()) {
for (uint32_t j = 0; j < customize_resources_plugins.size(); j++) {
Ref<Resource> new_res = customize_resources_plugins[j]->_customize_resource(res, "");
for (Ref<EditorExportPlugin> &plugin : customize_resources_plugins) {
Ref<Resource> new_res = plugin->_customize_resource(res, "");
if (new_res.is_valid()) {
changed = true;
if (new_res != res) {
Expand Down Expand Up @@ -715,16 +715,16 @@ String EditorExportPlatform::_export_customize(const String &p_path, LocalVector
ERR_FAIL_COND_V(ps.is_null(), p_path);
Node *node = ps->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE); // Make sure the child scene root gets the correct inheritance chain.
ERR_FAIL_COND_V(node == nullptr, p_path);
if (customize_scenes_plugins.size()) {
for (uint32_t i = 0; i < customize_scenes_plugins.size(); i++) {
Node *customized = customize_scenes_plugins[i]->_customize_scene(node, p_path);
if (!customize_scenes_plugins.is_empty()) {
for (Ref<EditorExportPlugin> &plugin : customize_scenes_plugins) {
Node *customized = plugin->_customize_scene(node, p_path);
if (customized != nullptr) {
node = customized;
modified = true;
}
}
}
if (customize_resources_plugins.size()) {
if (!customize_resources_plugins.is_empty()) {
if (_export_customize_scene_resources(node, node, customize_resources_plugins)) {
modified = true;
}
Expand All @@ -746,9 +746,9 @@ String EditorExportPlatform::_export_customize(const String &p_path, LocalVector
Ref<Resource> res = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_IGNORE);
ERR_FAIL_COND_V(res.is_null(), p_path);

if (customize_resources_plugins.size()) {
for (uint32_t i = 0; i < customize_resources_plugins.size(); i++) {
Ref<Resource> new_res = customize_resources_plugins[i]->_customize_resource(res, p_path);
if (!customize_resources_plugins.is_empty()) {
for (Ref<EditorExportPlugin> &plugin : customize_resources_plugins) {
Ref<Resource> new_res = plugin->_customize_resource(res, p_path);
if (new_res.is_valid()) {
modified = true;
if (new_res != res) {
Expand Down Expand Up @@ -960,7 +960,7 @@ Error EditorExportPlatform::export_project_files(const Ref<EditorExportPreset> &

bool convert_text_to_binary = GLOBAL_GET("editor/export/convert_text_resources_to_binary");

if (convert_text_to_binary || customize_resources_plugins.size() || customize_scenes_plugins.size()) {
if (convert_text_to_binary || !customize_resources_plugins.is_empty() || !customize_scenes_plugins.is_empty()) {
// See if we have something to open
Ref<FileAccess> f = FileAccess::open(export_base_path.path_join("file_cache"), FileAccess::READ);
if (f.is_valid()) {
Expand Down Expand Up @@ -1179,7 +1179,7 @@ Error EditorExportPlatform::export_project_files(const Ref<EditorExportPreset> &
idx++;
}

if (convert_text_to_binary || customize_resources_plugins.size() || customize_scenes_plugins.size()) {
if (convert_text_to_binary || !customize_resources_plugins.is_empty() || !customize_scenes_plugins.is_empty()) {
// End scene customization

String fcache = export_base_path.path_join("file_cache");
Expand All @@ -1196,12 +1196,12 @@ Error EditorExportPlatform::export_project_files(const Ref<EditorExportPreset> &
ERR_PRINT("Error opening export file cache: " + fcache);
}

for (uint32_t i = 0; i < customize_resources_plugins.size(); i++) {
customize_resources_plugins[i]->_end_customize_resources();
for (Ref<EditorExportPlugin> &plugin : customize_resources_plugins) {
plugin->_end_customize_resources();
}

for (uint32_t i = 0; i < customize_scenes_plugins.size(); i++) {
customize_scenes_plugins[i]->_end_customize_scenes();
for (Ref<EditorExportPlugin> &plugin : customize_scenes_plugins) {
plugin->_end_customize_scenes();
}
}
//save config!
Expand Down
Loading

0 comments on commit c3539b4

Please sign in to comment.