lib/linked/mutable.rb
def split_before_and_insert(head_b = self)
      # Get the current chain head. It will remain the head
      # of sub chain a (ii). If this item is the first then
      # chain a will be empty.
      chain_a_head = chain_head? ? nil : chain_head

      # The head of sub chain b (iii) is self.
      chain_b_head = self

      # Find the tail of sub chain b by iterating over each
      # item, starting with this one. Set the the new head
      # of these while counting them.
      chain_b_tail = self
      chain_b_length = 1

      loop do
        chain_b_tail.chain_head = head_b
        chain_b_tail = chain_b_tail.next
        chain_b_length += 1
      end

      # If sub chain a is not empty it needs to be updated.
      # Shrink its count by the number of items in sub
      # chain b and complete it by connecting the head to
      # the tail.
      if chain_a_head
        chain_a_head.shrink chain_b_length

        chain_a_tail = chain_b_head.prev
        chain_a_head.prev = chain_a_tail
        chain_a_tail.next = nil
      end

      # Tell the new chain to grow. If sub chain b is to be
      # the new head we can insert the count directly. We
      # also complete the chain by connecting the head to
      # the tail. The next field of the tail should already
      # be nil.
      if chain_b_head.equal? head_b
        chain_b_head.chain_head = chain_b_length
        chain_b_head.prev = chain_b_tail
      else
        head_b.grow chain_b_length
      end

      # Chain a is now either empty (nil) or completed.
      # Chain b however is only complete if the given head
      # is equal to self (default). If it is not chain b
      # will need a) the next field of the tail set to the
      # item after, unless nil, and b) the prev field of
      # head set to the item before.

      chain_b_tail
    end
def split_before_and_insert(head_b = self)
      # Get the current chain head. It will remain the head
      # of sub chain a (ii). If this item is the first then
      # chain a will be empty.
      chain_a_head = chain_head? ? nil : chain_head

      # The head of sub chain b (iii) is self.
      chain_b_head = self

      # Find the tail of sub chain b by iterating over each
      # item, starting with this one. Set the the new head
      # of these while counting them.
      chain_b_tail = self
      chain_b_length = 1

      loop do
        chain_b_tail.chain_head = head_b
        chain_b_tail = chain_b_tail.next
        chain_b_length += 1
      end

      # If sub chain a is not empty it needs to be updated.
      # Shrink its count by the number of items in sub
      # chain b and complete it by connecting the head to
      # the tail.
      if chain_a_head
        chain_a_head.shrink chain_b_length

        chain_a_tail = chain_b_head.prev
        chain_a_head.prev = chain_a_tail
        chain_a_tail.next = nil
      end

      # Tell the new chain to grow. If sub chain b is to be
      # the new head we can insert the count directly. We
      # also complete the chain by connecting the head to
      # the tail. The next field of the tail should already
      # be nil.
      if chain_b_head.equal? head_b
        chain_b_head.chain_head = chain_b_length
        chain_b_head.prev = chain_b_tail
      else
        head_b.grow chain_b_length
      end

      # Chain a is now either empty (nil) or completed.
      # Chain b however is only complete if the given head
      # is equal to self (default). If it is not chain b
      # will need a) the next field of the tail set to the
      # item after, unless nil, and b) the prev field of
      # head set to the item before.

      chain_b_tail
    end
rubocop:disable Metrics/AbcSize
 rubocop:disable Metrics/MethodLength
 Split the chain on this item and insert the latter part into the chain
 with head as its first item.

 Calling C.split_before_and_insert(.) yields the two chains (ii) and (iii)

   A <> B <> C <> D    A <> B    C <> D
         (i)            (ii)     (iii)

 Chain (ii) is guaranteed to be complete. Chain (iii) will however be left
 in an inclomplete state unless head_b == self (default). The first item in
 (iii) must then be connected to the one preceeding it.

 head_b - the head of a new chain that (iii) will be added to.

 Returns the last element of (iii).