Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: e52c4a86ea9d
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f77b626ece44
Choose a head ref
  • 3 commits
  • 2 files changed
  • 1 contributor

Commits on Nov 23, 2016

  1. Copy the full SHA
    49be498 View commit details
  2. Copy the full SHA
    80e046d View commit details
  3. Cleanup.

    headius committed Nov 23, 2016
    Copy the full SHA
    f77b626 View commit details
Showing with 46 additions and 4 deletions.
  1. +31 −4 core/src/main/java/org/jruby/RubyComparable.java
  2. +15 −0 core/src/main/ruby/jruby/kernel/enumerable.rb
35 changes: 31 additions & 4 deletions core/src/main/java/org/jruby/RubyComparable.java
Original file line number Diff line number Diff line change
@@ -36,14 +36,11 @@
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyModule;
import org.jruby.runtime.CallSite;
import org.jruby.runtime.JavaSites;
import org.jruby.runtime.JavaSites.ComparableSites;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.callsite.RespondToCallSite;

import static org.jruby.runtime.Helpers.invokedynamic;
import static org.jruby.runtime.invokedynamic.MethodNames.OP_CMP;

/** Implementation of the Comparable module.
*
@@ -86,7 +83,6 @@ public static int cmpint(ThreadContext context, CallSite op_gt, CallSite op_lt,

RubyFixnum zero = RubyFixnum.zero(context.runtime);

ComparableSites sites = sites(context);
if (op_gt.call(context, val, val, zero).isTrue()) return 1;
if (op_lt.call(context, val, val, zero).isTrue()) return -1;

@@ -98,6 +94,16 @@ public static int cmpint(ThreadContext context, IRubyObject val, IRubyObject a,
return cmpint(context, sites.op_gt, sites.op_lt, val, a, b);
}

public static int cmpAndCmpint(ThreadContext context, IRubyObject a, IRubyObject b) {
IRubyObject cmpResult = sites(context).op_cmp.call(context, a, a, b);
return cmpint(context, cmpResult, a, b);
}

public static int cmpAndCmpint(ThreadContext context, CallSite op_cmp, CallSite op_gt, CallSite op_lt, IRubyObject a, IRubyObject b) {
IRubyObject cmpResult = op_cmp.call(context, a, a, b);
return cmpint(context, op_gt, op_lt, cmpResult, a, b);
}

/** rb_cmperr
*
*/
@@ -241,6 +247,27 @@ public static RubyBoolean between_p(ThreadContext context, IRubyObject recv, IRu
return context.runtime.newBoolean(op_lt(context, recv, first).isFalse() && op_gt(context, recv, second).isFalse());
}

@JRubyMethod(name = "clamp")
public static IRubyObject clamp(ThreadContext context, IRubyObject recv, IRubyObject min, IRubyObject max) {
int c;

ComparableSites sites = sites(context);
CallSite op_gt = sites.op_gt;
CallSite op_lt = sites.op_lt;
CallSite op_cmp = sites.op_cmp;

if (cmpAndCmpint(context, op_cmp, op_gt, op_lt, min, max) > 0) {
throw context.runtime.newArgumentError("min argument must be smaller than max argument");
}

c = cmpAndCmpint(context, op_cmp, op_gt, op_lt, recv, min);
if (c == 0) return recv;
if (c < 0) return min;
c = cmpAndCmpint(context, op_cmp, op_gt, op_lt, recv, max);
if (c > 0) return max;
return recv;
}

private static ComparableSites sites(ThreadContext context) {
return context.sites.Comparable;
}
15 changes: 15 additions & 0 deletions core/src/main/ruby/jruby/kernel/enumerable.rb
Original file line number Diff line number Diff line change
@@ -125,4 +125,19 @@ def lazy
klass = Enumerator::Lazy::LAZY_WITH_NO_BLOCK # Note: class_variable_get is private in 1.8
Enumerator::Lazy.new(klass.new(self, :each, []))
end

def uniq
hash = {}
if defined? yield
each do |obj|
ret = yield *obj
hash[ret] = obj unless hash.key? ret
end
else
each do |obj|
hash[obj] = obj unless hash.key? obj
end
end
hash.values
end
end