def upsert(self, insert_index, val, fn=None):
        """Inserts or updates an existing index within the vector.

        Args:
            - insert_index (int): The index at which the element should be
                inserted.
            - val (int|float): The value to be inserted into the vector.
            - fn (callable, optional): An optional callable taking two
                arguments, the current value and the passed value to generate
                the final inserted value at the position in case of collision.
        """
        fn = fn or (lambda current, passed: passed)
        self._magnitude = 0
        position = self.position_for_index(insert_index)
        if position < len(self.elements) and self.elements[position] == insert_index:
            self.elements[position + 1] = fn(self.elements[position + 1], val)
        else:
            self.elements.insert(position, val)
            self.elements.insert(position, insert_index)