From 473b9e067e2462914fc83d7d3c580b8e7202b6aa Mon Sep 17 00:00:00 2001 From: Garrison Jensen Date: Mon, 29 Apr 2024 20:18:56 -0700 Subject: [PATCH] Make SortedArray work with arrays --- lib/sorted_containers/sorted_array.rb | 6 +++--- spec/sorted_array_spec.rb | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/sorted_containers/sorted_array.rb b/lib/sorted_containers/sorted_array.rb index 7af17de..a19ddd2 100644 --- a/lib/sorted_containers/sorted_array.rb +++ b/lib/sorted_containers/sorted_array.rb @@ -526,7 +526,7 @@ def update(iterable) # # @return [Array] An array representation of the sorted array. def to_a - @lists.flatten + @lists.flatten(1) end # Array is already sorted. Duplicates the sorted array and returns it. @@ -591,7 +591,7 @@ def each(&block) # @param value [Object] The value to bisect with. # @return [Integer] The index where the value should be inserted. def internal_bisect_left(array, value) - array.bsearch_index { |x| x >= value } || array.size + array.bsearch_index { |x| (x <=> value) >= 0 } || array.size end # Performs a right bisect on the array. @@ -600,7 +600,7 @@ def internal_bisect_left(array, value) # @param value [Object] The value to bisect with. # @return [Integer] The index where the value should be inserted. def internal_bisect_right(array, value) - array.bsearch_index { |x| x > value } || array.length + array.bsearch_index { |x| (x <=> value) == 1 } || array.length end # Gets the value at a given index. Supports negative indices. diff --git a/spec/sorted_array_spec.rb b/spec/sorted_array_spec.rb index 319620e..f707cee 100644 --- a/spec/sorted_array_spec.rb +++ b/spec/sorted_array_spec.rb @@ -307,6 +307,14 @@ expect(array.to_a).to eq((1..1000).to_a) end + it "should handle arrays of arrays" do + array = SortedContainers::SortedArray.new + array.add([1, 2]) + array.add([3, 4]) + array.add([5, 6]) + expect(array.to_a).to eq([[1, 2], [3, 4], [5, 6]]) + end + it "should return the array" do array = SortedContainers::SortedArray.new expect(array.add(5)).to eq(array)