wp_find_hierarchy_loop_tortoise_hare()
云策文档标注
概述
wp_find_hierarchy_loop_tortoise_hare() 是一个 WordPress 函数,用于检测层次结构中的循环。它基于“龟兔赛跑”算法,通过比较移动速度不同的指针来高效识别循环。
关键要点
- 使用“龟兔赛跑”算法检测循环:龟指针每次移动一步,兔指针每次移动两步,如果兔指针追上龟指针,则存在循环。
- 参数包括回调函数 $callback(必需,用于获取父 ID)、起始 ID $start(必需)、覆盖数组 $override(可选,用于替代回调函数)和回调参数 $callback_args(可选)。
- 返回循环中的某个 ID 或循环成员数组,具体取决于 $_return_loop 参数。
- 函数在 WordPress 3.1.0 版本中引入,常用于 wp_find_hierarchy_loop() 等函数中。
代码示例
function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
$tortoise = $start;
$hare = $start;
$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 ] = true;
$return[ $evanescent_hare ] = true;
$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;
}
原文内容
Uses 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.
Parameters
$callbackcallablerequired-
Function that accepts ( ID, callback_arg, … ) and outputs parent_ID.
$startintrequired-
The ID to start the loop check at.
$overridearrayoptional-
An array of ( ID => parent_ID, … ) to use instead of $callback.
Default:
array() $callback_argsarrayoptional-
Additional arguments to send to $callback.
Default:
array() $_return_loopbooloptional-
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:
false
Source
function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
$tortoise = $start;
$hare = $start;
$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 ] = true;
$return[ $evanescent_hare ] = true;
$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;
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |