Skip to content

Commit

Permalink
always keep the load factor when returning a SortedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrisonJ committed May 8, 2024
1 parent ca55a19 commit d3a8caa
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions lib/sorted_containers/sorted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def self.[](*args)
# @param other [SortedArray] The other array to union with.
# @return [SortedArray] The union of the two arrays.
def intersection(other)
new_instance = self.class.new
new_instance.update(to_a & other.to_a)
new_instance
self.class.new(to_a & other.to_a, load_factor: @load_factor)
end
alias & intersection

Expand All @@ -53,10 +51,7 @@ def intersection(other)
# @param num [Integer] The integer to multiply the array by.
# @return [SortedArray] The multiplied sorted array.
def multiply(num)
values = @lists.flatten * num
new_instance = self.class.new
new_instance.update(values)
new_instance
self.class.new(to_a * num, load_factor: @load_factor)
end
alias * multiply

Expand All @@ -65,19 +60,15 @@ def multiply(num)
# @param other [SortedArray] The other array to add.
# @return [SortedArray] The combined array.
def +(other)
new_instance = self.class.new
new_instance.update(to_a + other.to_a)
new_instance
self.class.new(to_a + other.to_a, load_factor: @load_factor)
end

# Returns a new SortedArray with the values from the difference of the two arrays.
#
# @param other [SortedArray] The other array to subtract.
# @return [SortedArray] The difference of the two arrays.
def difference(other)
new_instance = self.class.new
new_instance.update(to_a - other.to_a)
new_instance
self.class.new(to_a - other.to_a, load_factor: @load_factor)
end
alias - difference

Expand Down Expand Up @@ -509,11 +500,9 @@ def dig(index, *identifiers)
# @return [SortedArray] The array with the dropped values.
def drop(n)
raise ArgumentError, "attempt to drop negative size" if n.negative?
return self.class.new if n >= @size
return self.class.new(load_factor: @load_factor) if n >= @size

new_instance = self.class.new
new_instance.update(to_a.drop(n))
new_instance
self.class.new(to_a.drop(n), load_factor: @load_factor)
end

# rubocop:enable Naming/MethodParameterName
Expand All @@ -527,7 +516,7 @@ def drop(n)
def drop_while
return to_enum(:drop_while) unless block_given?

self.class.new(super)
self.class.new(super(), load_factor: @load_factor)
end

# Iterates over each value in the sorted array.
Expand Down

0 comments on commit d3a8caa

Please sign in to comment.