Skip to content

Commit

Permalink
[Core] Add LocalVector::has for convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
AThousandShips committed May 6, 2024
1 parent d8aa2c6 commit 86de59d
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ bool ProjectSettings::get_ignore_value_in_docs(const String &p_name) const {
}

void ProjectSettings::add_hidden_prefix(const String &p_prefix) {
ERR_FAIL_COND_MSG(hidden_prefixes.find(p_prefix) > -1, vformat("Hidden prefix '%s' already exists.", p_prefix));
ERR_FAIL_COND_MSG(hidden_prefixes.has(p_prefix), vformat("Hidden prefix '%s' already exists.", p_prefix));
hidden_prefixes.push_back(p_prefix);
}

Expand Down
4 changes: 4 additions & 0 deletions core/templates/local_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ class LocalVector {
return -1;
}

bool has(const T &p_val) const {
return find(p_val) != -1;
}

template <typename C>
void sort_custom() {
U len = count;
Expand Down
2 changes: 1 addition & 1 deletion drivers/vulkan/rendering_device_driver_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2603,7 +2603,7 @@ Error RenderingDeviceDriverVulkan::swap_chain_resize(CommandQueueID p_cmd_queue,
break;
}

bool present_mode_available = present_modes.find(present_mode) >= 0;
bool present_mode_available = present_modes.has(present_mode);
if (present_mode_available) {
print_verbose("Using present mode: " + present_mode_name);
} else {
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_property_name_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ String EditorPropertyNameProcessor::_capitalize_name(const String &p_name) const
Vector<String> parts = p_name.split("_", false);
for (int i = 0; i < parts.size(); i++) {
// Articles/conjunctions/prepositions which should only be capitalized when not at beginning and end.
if (i > 0 && i + 1 < parts.size() && stop_words.find(parts[i]) != -1) {
if (i > 0 && i + 1 < parts.size() && stop_words.has(parts[i])) {
continue;
}
HashMap<String, String>::ConstIterator remap = capitalize_string_remaps.find(parts[i]);
Expand Down
2 changes: 1 addition & 1 deletion editor/multi_node_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class MultiNodeEdit : public RefCounted {
return false;
}
for (int i = 0; i < get_node_count(); i++) {
if (nodes.find(p_other->get_node(i)) == -1) {
if (!nodes.has(p_other->get_node(i))) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/csg/csg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ void CSGBrushOperation::Build2DFaces::_find_edge_intersections(const Vector2 p_s
};

// Check if edge has already been processed.
if (processed_edges.find(edge_points_and_uvs) != -1) {
if (processed_edges.has(edge_points_and_uvs)) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion modules/navigation/3d/godot_navigation_server_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ bool GodotNavigationServer3D::map_is_active(RID p_map) const {
NavMap *map = map_owner.get_or_null(p_map);
ERR_FAIL_NULL_V(map, false);

return active_maps.find(map) >= 0;
return active_maps.has(map);
}

COMMAND_2(map_set_up, RID, p_map, Vector3, p_up) {
Expand Down
4 changes: 2 additions & 2 deletions modules/navigation/nav_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ void NavMap::remove_link(NavLink *p_link) {
}

bool NavMap::has_agent(NavAgent *agent) const {
return (agents.find(agent) >= 0);
return agents.has(agent);
}

void NavMap::add_agent(NavAgent *agent) {
Expand All @@ -754,7 +754,7 @@ void NavMap::remove_agent(NavAgent *agent) {
}

bool NavMap::has_obstacle(NavObstacle *obstacle) const {
return (obstacles.find(obstacle) >= 0);
return obstacles.has(obstacle);
}

void NavMap::add_obstacle(NavObstacle *obstacle) {
Expand Down
4 changes: 2 additions & 2 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ void Viewport::_gui_hide_control(Control *p_control) {
if (gui.key_focus == p_control) {
gui_release_focus();
}
if (gui.mouse_over == p_control || gui.mouse_over_hierarchy.find(p_control) >= 0) {
if (gui.mouse_over == p_control || gui.mouse_over_hierarchy.has(p_control)) {
_drop_mouse_over(p_control->get_parent_control());
}
if (gui.drag_mouse_over == p_control) {
Expand All @@ -2394,7 +2394,7 @@ void Viewport::_gui_remove_control(Control *p_control) {
if (gui.key_focus == p_control) {
gui.key_focus = nullptr;
}
if (gui.mouse_over == p_control || gui.mouse_over_hierarchy.find(p_control) >= 0) {
if (gui.mouse_over == p_control || gui.mouse_over_hierarchy.has(p_control)) {
_drop_mouse_over(p_control->get_parent_control());
}
if (gui.drag_mouse_over == p_control) {
Expand Down
2 changes: 1 addition & 1 deletion servers/physics_3d/godot_shape_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ void GodotConvexPolygonShape3D::_setup(const Vector<Vector3> &p_vertices) {
max_support = s;
}
}
if (extreme_vertices.find(best_vertex) == -1)
if (!extreme_vertices.has(best_vertex))
extreme_vertices.push_back(best_vertex);
}
}
Expand Down
4 changes: 2 additions & 2 deletions servers/physics_3d/godot_soft_body_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,11 @@ void GodotSoftBody3D::generate_bending_constraints(int p_distance) {
for (Link &link : links) {
const int ia = (int)(link.n[0] - &nodes[0]);
const int ib = (int)(link.n[1] - &nodes[0]);
if (node_links[ia].find(ib) == -1) {
if (!node_links[ia].has(ib)) {
node_links[ia].push_back(ib);
}

if (node_links[ib].find(ia) == -1) {
if (!node_links[ib].has(ia)) {
node_links[ib].push_back(ia);
}
}
Expand Down
11 changes: 10 additions & 1 deletion tests/core/templates/test_local_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ TEST_CASE("[LocalVector] Push Back.") {
CHECK(vector[4] == 4);
}

TEST_CASE("[LocalVector] Find.") {
TEST_CASE("[LocalVector] Find, has.") {
LocalVector<int> vector;
vector.push_back(3);
vector.push_back(1);
Expand All @@ -85,6 +85,15 @@ TEST_CASE("[LocalVector] Find.") {

CHECK(vector.find(-1) == -1);
CHECK(vector.find(5) == -1);

CHECK(vector.has(0));
CHECK(vector.has(1));
CHECK(vector.has(2));
CHECK(vector.has(3));
CHECK(vector.has(4));

CHECK(!vector.has(-1));
CHECK(!vector.has(5));
}

TEST_CASE("[LocalVector] Remove.") {
Expand Down

0 comments on commit 86de59d

Please sign in to comment.