Example #1
0
 /**
  * Replace a range of elements with a second set of elements.
  *
  * @param integer      $index    The index of the first element to replace, if index is a negative number the replace begins that far from the end of the sequence.
  * @param mixed<mixed> $elements The elements to insert.
  * @param integer|null $count    The number of elements to replace, or null to replace all elements up to the end of the sequence.
  */
 public function replace($index, $elements, $count = null)
 {
     $this->validateIndex($index);
     $count = $this->clamp($count, 0, $this->size - $index);
     // Element count is available ...
     if (Collection::iteratorTraits($elements)->isCountable) {
         $diff = count($elements) - $count;
         if ($diff > 0) {
             $this->shiftRight($index + $count, $diff);
         } elseif ($diff < 0) {
             $this->shiftLeft($index + $count, abs($diff));
         }
         $this->size += $diff;
         foreach ($elements as $element) {
             $this->elements[$index++] = $element;
         }
         // No count is available ...
     } else {
         $originalSize = $this->size;
         $this->insertMany($index, $elements);
         $elementCount = $this->size - $originalSize;
         $this->removeMany($index + $elementCount, $count);
     }
 }