瀏覽代碼

[Core] Add `LocalVector::has` for convenience

A Thousand Ships 1 年之前
父節點
當前提交
86de59d60a

+ 1 - 1
core/config/project_settings.cpp

@@ -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);
 }
 

+ 4 - 0
core/templates/local_vector.h

@@ -264,6 +264,10 @@ public:
 		return -1;
 	}
 
+	bool has(const T &p_val) const {
+		return find(p_val) != -1;
+	}
+
 	template <typename C>
 	void sort_custom() {
 		U len = count;

+ 1 - 1
drivers/vulkan/rendering_device_driver_vulkan.cpp

@@ -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 {

+ 1 - 1
editor/editor_property_name_processor.cpp

@@ -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]);

+ 1 - 1
editor/multi_node_edit.h

@@ -73,7 +73,7 @@ public:
 			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;
 			}
 		}

+ 1 - 1
modules/csg/csg.cpp

@@ -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;
 			}
 

+ 1 - 1
modules/navigation/3d/godot_navigation_server_3d.cpp

@@ -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) {

+ 2 - 2
modules/navigation/nav_map.cpp

@@ -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) {
@@ -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) {

+ 2 - 2
scene/main/viewport.cpp

@@ -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) {
@@ -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) {

+ 1 - 1
servers/physics_3d/godot_shape_3d.cpp

@@ -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);
 				}
 			}

+ 2 - 2
servers/physics_3d/godot_soft_body_3d.cpp

@@ -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);
 				}
 			}

+ 10 - 1
tests/core/templates/test_local_vector.h

@@ -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);
@@ -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.") {