Skip to content

Commit

Permalink
Use Primitive/nil instead of beginless? and endless?
Browse files Browse the repository at this point in the history
  • Loading branch information
Lillian Zhang committed Nov 25, 2020
1 parent 3ab063a commit d81bbb9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
22 changes: 10 additions & 12 deletions src/main/ruby/truffleruby/core/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def bsearch(&block)
end

private def bsearch_float(&block)
normalized_begin = Truffle::RangeOperations.beginless?(self) ? -Float::INFINITY : self.begin.to_f
normalized_end = Truffle::RangeOperations.endless?(self) ? Float::INFINITY : self.end.to_f
normalized_begin = Primitive.nil?(self.begin) ? -Float::INFINITY : self.begin.to_f
normalized_end = Primitive.nil?(self.end) ? Float::INFINITY : self.end.to_f
normalized_end = normalized_end.prev_float if self.exclude_end?
min = normalized_begin
max = normalized_end
Expand Down Expand Up @@ -263,12 +263,10 @@ def bsearch(&block)
end

def count(item = undefined)
if Truffle::RangeOperations.beginless?(self) || Truffle::RangeOperations.endless?(self)
if Primitive.nil?(self.begin) || Primitive.nil?(self.end)
return Float::INFINITY unless block_given? || !Primitive.undefined?(item)
end

p "count for here"

super
end

Expand Down Expand Up @@ -386,15 +384,15 @@ def inspect
end

def last(n=undefined)
raise RangeError, 'cannot get the last element of endless range' if Truffle::RangeOperations.endless?(self)
raise RangeError, 'cannot get the last element of endless range' if Primitive.nil? self.end
return self.end if Primitive.undefined? n

to_a.last(n)
end

def max
raise RangeError, 'cannot get the maximum of endless range' if Truffle::RangeOperations.endless?(self)
if Truffle::RangeOperations.beginless?(self)
raise RangeError, 'cannot get the maximum of endless range' if Primitive.nil? self.end
if Primitive.nil? self.begin
raise RangeError, 'cannot get the maximum of beginless range with custom comparison method' if block_given?
return exclude_end? ? self.end - 1 : self.end
end
Expand All @@ -415,8 +413,8 @@ def max
end

def min
raise RangeError, 'cannot get the minimum of beginless range' if Truffle::RangeOperations.beginless?(self)
if Truffle::RangeOperations.endless?(self)
raise RangeError, 'cannot get the minimum of beginless range' if Primitive.nil? self.begin
if Primitive.nil? self.end
raise RangeError, 'cannot get the minimum of endless range with custom comparison method' if block_given?
return self.begin
end
Expand Down Expand Up @@ -519,9 +517,9 @@ def cover?(value)
end

def size
return Float::INFINITY if Truffle::RangeOperations.beginless?(self)
return Float::INFINITY if Primitive.nil? self.begin
return nil unless self.begin.kind_of?(Numeric)
return Float::INFINITY if Truffle::RangeOperations.endless?(self)
return Float::INFINITY if Primitive.nil? self.end

delta = self.end - self.begin
return 0 if delta < 0
Expand Down
10 changes: 1 addition & 9 deletions src/main/ruby/truffleruby/core/truffle/range_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def self.cover?(range, value)
return false unless beg_compare

if Comparable.compare_int(beg_compare) <= 0
return true if endless?(range)
return true if Primitive.nil? range.end
end_compare = (value <=> range.end)

if range.exclude_end?
Expand All @@ -126,14 +126,6 @@ def self.cover?(range, value)
false
end

def self.beginless?(range)
Primitive.nil? range.begin
end

def self.endless?(range)
Primitive.nil? range.end
end

# MRI: r_less
def self.range_less(a, b)
compare = a <=> b
Expand Down

0 comments on commit d81bbb9

Please sign in to comment.