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)