Skip to content

Commit

Permalink
add concat to SortedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrisonJ committed May 2, 2024
1 parent 9efb6e3 commit e6a922d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/sorted_containers/sorted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,25 @@ def compact

# Removes nil elements from the SortedArray.
#
# @return [SortedArray] The compacted array.
# @return [SortedArray] +self+. The compacted array.
def compact!
values = to_a.compact
clear
update(values)
self
end

# Adds the elements of one or more arrays to the SortedArray.
#
# @param other_arrays [Array] The arrays to concatenate.
# @return [SortedArray] +self+. The SortedArray with the concatenated values.
def concat(*other_arrays)
other_arrays.each do |array|
update(array)
end
self
end

# rubocop:enable Naming/MethodParameterName

# Returns a string representation of the sorted array.
Expand Down
49 changes: 49 additions & 0 deletions spec/sorted_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,55 @@
end
end

describe "concat" do
it "should concatenate two arrays" do
array1 = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
array2 = SortedContainers::SortedArray.new([6, 7, 8, 9, 10])
array1.concat(array2)
expect(array1.to_a).to eq([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
end

it "should concatenate an empty array with a non-empty array" do
array1 = SortedContainers::SortedArray.new
array2 = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
array1.concat(array2)
expect(array1.to_a).to eq([1, 2, 3, 4, 5])
end

it "should concatenate a non-empty array with an empty array" do
array1 = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
array2 = SortedContainers::SortedArray.new
array1.concat(array2)
expect(array1.to_a).to eq([1, 2, 3, 4, 5])
end

it "should concatenate two empty arrays" do
array1 = SortedContainers::SortedArray.new
array2 = SortedContainers::SortedArray.new
array1.concat(array2)
expect(array1.to_a).to eq([])
end

it "should concatenate 3 arrays" do
array1 = SortedContainers::SortedArray.new([1, 2, 3])
array2 = SortedContainers::SortedArray.new([4, 5, 6])
array3 = SortedContainers::SortedArray.new([7, 8, 9])
array1.concat(array2, array3)
expect(array1.to_a).to eq([1, 2, 3, 4, 5, 6, 7, 8, 9])
end

it "should concatenate 6 arrays" do
array1 = SortedContainers::SortedArray.new([1, 2, 3])
array2 = SortedContainers::SortedArray.new([4, 5, 6])
array3 = SortedContainers::SortedArray.new([7, 8, 9])
array4 = SortedContainers::SortedArray.new([10, 11, 12])
array5 = SortedContainers::SortedArray.new([13, 14, 15])
array6 = SortedContainers::SortedArray.new([16, 17, 18])
array1.concat(array2, array3, array4, array5, array6)
expect(array1.to_a).to eq([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18])
end
end

describe "load_factor" do
it "should set the load factor to the provided value" do
array = SortedContainers::SortedArray.new([], load_factor: 100)
Expand Down

0 comments on commit e6a922d

Please sign in to comment.