Method: Immutable::Vector#insert
- Defined in:
- lib/immutable/vector.rb
#insert(index, *items) ⇒ Vector
Return a new Vector with the given values inserted before the element at index. If index is greater than the current length, nil values are added to pad the Vector to the required size.
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/immutable/vector.rb', line 356 def insert(index, *items) raise IndexError if index < -@size index += @size if index < 0 if index < @size suffix = flatten_suffix(@root, @levels * BITS_PER_LEVEL, index, []) suffix.unshift(*items) elsif index == @size suffix = items else suffix = Array.new(index - @size, nil).concat(items) index = @size end replace_suffix(index, suffix) end |