A function which replaces some portion od doubly linked list (DLL) with items from specified array.
<?php
/**
* Replace some portion (specified by index and length) of DLL with items
* from the specified replacement array.
*/
public function dll_splice(
\SplDoublyLinkedList $dll,
int $start,
int $length,
array $replacement,
): void {
if ($start < 0 || ($start + $length) > $dll->count()) {
throw new \OutOfRangeException("Invalid range for splicing");
}
for ($i = 0; $i < $length; $i++) {
$dll->offsetUnset($start);
}
foreach ($replacement as $item) {
$dll->add($start, $item);
}
}
?>