返回介绍

wp_find_hierarchy_loop_tortoise_hare()

发布于 2017-09-11 11:57:30 字数 3476 浏览 958 评论 0 收藏 0

Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

wp_find_hierarchy_loop_tortoise_hare( callable $callback,  int $start,  array $override = array(),  array $callback_args = array(),  bool $_return_loop = false )

Use the “The Tortoise and the Hare” algorithm to detect loops.


description

For every step of the algorithm, the hare takes two steps and the tortoise one. If the hare ever laps the tortoise, there must be a loop.


参数

$callback

(callable) (Required) Function that accepts ( ID, callback_arg, ... ) and outputs parent_ID.

$start

(int) (Required) The ID to start the loop check at.

$override

(array) (Optional) An array of ( ID => parent_ID, ... ) to use instead of $callback.

Default value: array()

$callback_args

(array) (Optional) Additional arguments to send to $callback.

Default value: array()

$_return_loop

(bool) (Optional) Return loop members or just detect presence of loop? Only set to true if you already know the given $start is part of a loop (otherwise the returned array might include branches).

Default value: false


返回值

(mixed) Scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if $_return_loop


源代码

File: wp-includes/functions.php

function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
	$tortoise = $hare = $evanescent_hare = $start;
	$return = array();

	// Set evanescent_hare to one past hare
	// Increment hare two steps
	while (
		$tortoise
	&&
		( $evanescent_hare = isset( $override[$hare] ) ? $override[$hare] : call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) )
	&&
		( $hare = isset( $override[$evanescent_hare] ) ? $override[$evanescent_hare] : call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) )
	) {
		if ( $_return_loop )
			$return[$tortoise] = $return[$evanescent_hare] = $return[$hare] = true;

		// tortoise got lapped - must be a loop
		if ( $tortoise == $evanescent_hare || $tortoise == $hare )
			return $_return_loop ? $return : $tortoise;

		// Increment tortoise by one step
		$tortoise = isset( $override[$tortoise] ) ? $override[$tortoise] : call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) );
	}

	return false;
}

更新日志

Versiondescription
3.1.0Introduced.

相关函数

Used By

  • wp-includes/functions.php: wp_find_hierarchy_loop()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文