wp_find_hierarchy_loop_tortoise_hare()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
wp_find_hierarchy_loop_tortoise_hare( callable $callback, int $start, array $override = array(), array $callback_args = array(), bool $_return_loop = false ): mixed
Описание
На каждом шаге алгоритма заяц делает два шага, а черепаха — один.
Если заяц когда-либо обгоняет черепаху, значит есть цикл.
Оригинал (английский)
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
обязательный
$start
int
обязательный
$override
array
необязательный
= array()
$callback_args
array
необязательный
= array()
$_return_loop
bool
необязательный
= false
Возвращаемое значение
mixed
Исходный код
wp-includes/functions.php:7152
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 = $override[ $hare ] ?? call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) )
&&
( $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 = $override[ $tortoise ] ?? call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) );
}
return false;
}
История изменений
| Версия | Описание |
|---|---|
| 3.1.0 | Introduced. |

