Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Array#insert_all #14486

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 83 additions & 0 deletions spec/std/array_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,89 @@ describe "Array" do
end
end

describe "insert_all" do
it "inserts with index 0" do
a = [2, 3]
expected = [0, 1, 2, 3]
a.insert_all(0, [0, 1])
summer-alice marked this conversation as resolved.
Show resolved Hide resolved
a.should eq(expected)
summer-alice marked this conversation as resolved.
Show resolved Hide resolved
end

it "inserts with positive index" do
a = [1, 2, 5, 6]
expected = [1, 2, 3, 4, 5, 6]
a.insert_all(2, [3, 4])
a.should eq(expected)
end

it "inserts with index of #size" do
a = [1, 2, 3]
expected = [1, 2, 3, 4, 5]
a.insert_all(a.size, [4, 5])
a.should eq expected
end

it "inserts with negative index" do
a = [1, 2, 3]
expected = [1, 2, 3, 4, 5]
a.insert_all(-1, [4, 5])
a.should eq expected
end

it "inserts with negative index (2)" do
a = [1, 2, 5, 6]
expected = [1, 2, 3, 4, 5, 6]
a.insert_all(-3, [3, 4])
a.should eq expected
end

it "inserts when empty" do
a = [] of Int32
expected = [1, 2, 3]
a.insert_all(0, [1, 2, 3])
a.should eq(expected)
end

it "inserts when other is empty" do
a = [1, 2, 3]
expected = [1, 2, 3]
a.insert_all(1, [] of Int32)
a.should eq(expected)
end

it "raises with index greater than size" do
a = [1, 2, 3]
expect_raises IndexError do
a.insert_all(4, [4, 5])
end
end

it "raises with negative index greater than size" do
a = [1, 2, 3]
expect_raises IndexError do
a.insert_all(-5, [4, 5])
end
end

it "inserts enumerables" do
a = [1, 2, 999, 1000]
a.insert_all(2, 3...999)
a.should eq((1..1000).to_a)
end

it "inserts indexable" do
a = [1, 9, 10]
a.insert_all(1, Slice.new(3, 8))
a.should eq([1, 8, 8, 8, 9, 10])

a.insert_all(-6, StaticArray(Int32, 3).new { |i| i + 2 })
a.should eq([1, 2, 3, 4, 8, 8, 8, 9, 10])

a.insert_all(4, Deque{5, 6, 7})
a.should eq([1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 10])
end
end

describe "inspect" do
it { [1, 2, 3].inspect.should eq("[1, 2, 3]") }
end
Expand Down
79 changes: 59 additions & 20 deletions src/array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -745,32 +745,13 @@ class Array(T)

resize_if_cant_insert(other_size)

concat_indexable(other)
insert_elements_at(other, @size)

@size += other_size

self
end

private def concat_indexable(other : Array | Slice | StaticArray)
(@buffer + @size).copy_from(other.to_unsafe, other.size)
end

private def concat_indexable(other : Deque)
ptr = @buffer + @size
Deque.half_slices(other) do |slice|
ptr.copy_from(slice.to_unsafe, slice.size)
ptr += slice.size
end
end

private def concat_indexable(other)
appender = (@buffer + @size).appender
other.each do |elem|
appender << elem
end
end

# :ditto:
def concat(other : Enumerable) : self
left_before_resize = remaining_capacity - @size
Expand Down Expand Up @@ -1045,6 +1026,64 @@ class Array(T)
self
end

# Inserts all of the elements from *other* before the element at *index*.
#
# This method shifts the element currently at *index* (if any) and any
# subsequent elements to the right, increasing their indices. If the value
# of *index* is negative, counting starts from the end of the array.
summer-alice marked this conversation as resolved.
Show resolved Hide resolved
#
# Raises `IndexError` if the *index* is out of bounds.
#
# ```
# fruits = ["Apple"]
# newFruits = ["Dragonfruit", "Elderberry"]
#
# fruits.insert_all(1, newFruits) # => ["Apple", "Dragonfruit", "Elderberry"]
# fruits.insert_all(-3, ["Banana", "Cherry"]) # => ["Apple", "Banana", "Cherry", "Dragonfruit", "Elderberry"]
#
# fruits.insert_all(6, ["invalid"]) # raises IndexError
# fruits.insert_all(-7, ["indices"]) # raises IndexError
# ```
def insert_all(index : Int, other : Enumerable(T) | Iterable(T)) : self
summer-alice marked this conversation as resolved.
Show resolved Hide resolved
other_size = other.size

return self if other_size == 0

normalized_index = index < 0 ? index + @size + 1 : index

unless 0 <= normalized_index <= @size
raise IndexError.new "insertion index of #{index} should be between (0..#{@size}) or (-#{@size + 1}..-1)"
end
summer-alice marked this conversation as resolved.
Show resolved Hide resolved

resize_if_cant_insert(other_size)
(@buffer + normalized_index).move_to(@buffer + normalized_index + other_size, @size - normalized_index)

insert_elements_at(other, normalized_index)

@size += other_size

self
end

private def insert_elements(other : Array | Slice | StaticArray, index : Int) : Nil
(@buffer + index).copy_from(other.to_unsafe, other.size)
end

private def insert_elements(other : Deque, index : Int) : Nil
ptr = @buffer + index
Deque.half_slices(other) do |slice|
ptr.copy_from(slice.to_unsafe, slice.size)
ptr += slice.size
end
end
summer-alice marked this conversation as resolved.
Show resolved Hide resolved

private def insert_elements_at(other, index : Int) : Nil
appender = (@buffer + index).appender
other.each do |elem|
appender << elem
end
end

def inspect(io : IO) : Nil
to_s io
end
Expand Down