Skip to content

Commit

Permalink
add reject and reject! to SortedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrisonJ committed May 6, 2024
1 parent 545b3b3 commit aa8c610
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/sorted_containers/sorted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,39 @@ def rassoc(obj)
index.nil? ? nil : self[index]
end

# Returns a new SortedArray whose elements are all those from +self+ for which the block returns false or nil.
#
# Returns an Enumerator if no block is given.
#
# @yield [value] The block to reject with.
# @return [SortedArray, Enumerator] The rejected array.
def reject
return to_enum(:reject) unless block_given?

select { |value| !yield(value) }
end

# Deletes every element of +self+ for which block evaluates to true.
#
# Returns +self+ if any changes were made, otherwise returns +nil+.
#
# Returns an Enumerator if no block is given.
#
# @yield [value] The block to reject with.
# @return [SortedArray, Enumerator] The rejected array.
def reject!
return to_enum(:reject!) unless block_given?

indexes_to_delete = []
each_with_index do |value, index|
indexes_to_delete << index if yield(value)
end
return nil if indexes_to_delete.empty?

indexes_to_delete.reverse.each { |index| delete_at(index) }
self
end

private

# Performs a left bisect on the array.
Expand Down
41 changes: 41 additions & 0 deletions spec/sorted_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,47 @@ def dig(key)
end
end

describe "reject" do
it "should reject elements that meet the given criterion" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array.reject { |i| i > 3 }).to eq(SortedContainers::SortedArray.new([1, 2, 3]))
end

it "should not modify the original array" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
array.reject { |i| i > 3 }
expect(array).to eq(SortedContainers::SortedArray.new([1, 2, 3, 4, 5]))
end

it "should return an enumerator if no block is given" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array.reject).to be_a(Enumerator)
end
end

describe "reject!" do
it "should reject elements that meet the given criterion" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
array.reject! { |i| i > 3 }
expect(array).to eq(SortedContainers::SortedArray.new([1, 2, 3]))
end

it "should return self if elements are rejected" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array.reject! { |i| i > 3 }).to eq(array)
end

it "should return an enumerator if no block is given" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array.reject!).to be_a(Enumerator)
end

it "should return nil if no elements meet the given criterion" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array.reject! { |i| i > 5 }).to be_nil
end
end

describe "shift" do
it "should shift the first value from the array" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
Expand Down

0 comments on commit aa8c610

Please sign in to comment.