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 all 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
70 changes: 70 additions & 0 deletions spec/std/array_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,76 @@ describe "Array" do
end
end

describe "insert_all" do
it "inserts with index 0" do
a = [2, 3]
a.insert_all(0, [0, 1]).should be a
a.should eq([0, 1, 2, 3])
end

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

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

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

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

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

it "inserts when other is empty" do
a = [1, 2, 3]
a.insert_all(1, [] of Int32).should be a
a.should eq([1, 2, 3])
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 indexable" do
a = [1, 9, 10]
a.insert_all(1, Slice.new(3, 8)).should be a
a.should eq([1, 8, 8, 8, 9, 10])

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

a.insert_all(4, Deque{5, 6, 7}).should be a
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
83 changes: 63 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,68 @@ 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
# For example, `-1` indicates insertion after the last element, `-2` before
# the last element.
#
# 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 : Indexable) : self
other_size = other.size

return self if other_size == 0

if index < 0
index += size + 1
end

unless 0 <= index <= size
raise IndexError.new
end

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

insert_elements_at(other, index)

@size += other_size

self
end

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

private def insert_elements_at(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

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