-
-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4690 from jruby/test-set-native
native implementation of set.rb
- 9.4.12.0
- 9.4.11.0
- 9.4.10.0
- 9.4.9.0
- 9.4.8.0
- 9.4.7.0
- 9.4.6.0
- 9.4.5.0
- 9.4.4.0
- 9.4.3.0
- 9.4.2.0
- 9.4.1.0
- 9.4.0.0
- 9.3.15.0
- 9.3.14.0
- 9.3.13.0
- 9.3.12.0
- 9.3.11.0
- 9.3.10.0
- 9.3.9.0
- 9.3.8.0
- 9.3.7.0
- 9.3.6.0
- 9.3.5.0
- 9.3.4.0
- 9.3.3.0
- 9.3.2.0
- 9.3.1.0
- 9.3.0.0
- 9.2.21.0
- 9.2.20.1
- 9.2.20.0
- 9.2.19.0
- 9.2.18.0
- 9.2.17.0
- 9.2.16.0
- 9.2.15.0
- 9.2.14.0
- 9.2.13.0
- 9.2.12.0
- 9.2.11.1
- 9.2.11.0
- 9.2.10.0
- 9.2.9.0
- 9.2.8.0
- 9.2.7.0
- 9.2.6.0
- 9.2.5.0
- 9.2.4.1
- 9.2.4.0
- 9.2.3.0
- 9.2.2.0
- 9.2.1.0
- 9.2.0.0
Showing
12 changed files
with
1,875 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/***** BEGIN LICENSE BLOCK ***** | ||
* Version: EPL 1.0/GPL 2.0/LGPL 2.1 | ||
* | ||
* The contents of this file are subject to the Eclipse Public | ||
* License Version 1.0 (the "License"); you may not use this file | ||
* except in compliance with the License. You may obtain a copy of | ||
* the License at http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Software distributed under the License is distributed on an "AS | ||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* rights and limitations under the License. | ||
* | ||
* Copyright (C) 2016 Karol Bucek | ||
* | ||
* Alternatively, the contents of this file may be used under the terms of | ||
* either of the GNU General Public License Version 2 or later (the "GPL"), | ||
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | ||
* in which case the provisions of the GPL or the LGPL are applicable instead | ||
* of those above. If you wish to allow use of your version of this file only | ||
* under the terms of either the GPL or the LGPL, and not to allow others to | ||
* use your version of this file under the terms of the EPL, indicate your | ||
* decision by deleting the provisions above and replace them with the notice | ||
* and other provisions required by the GPL or the LGPL. If you do not delete | ||
* the provisions above, a recipient may use your version of this file under | ||
* the terms of any one of the EPL, the GPL or the LGPL. | ||
***** END LICENSE BLOCK *****/ | ||
package org.jruby.ext.set; | ||
|
||
import org.jruby.Ruby; | ||
import org.jruby.RubyClass; | ||
import org.jruby.anno.JRubyMethod; | ||
import org.jruby.runtime.Block; | ||
import org.jruby.runtime.ThreadContext; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
|
||
/** | ||
* Enumerable#to_set (from require 'set') | ||
* | ||
* @author kares | ||
*/ | ||
public abstract class EnumerableExt { | ||
|
||
//@JRubyMethod | ||
public static IRubyObject to_set(final ThreadContext context, final IRubyObject self, final Block block) { | ||
final Ruby runtime = context.runtime; | ||
|
||
RubySet set = new RubySet(runtime, runtime.getClass("Set")); | ||
set.initialize(context, self, block); | ||
return set; // return runtime.getClass("Set").newInstance(context, self, block); | ||
} | ||
|
||
@JRubyMethod(rest = true) // to_set(klass = Set, *args, &block) | ||
public static IRubyObject to_set(final ThreadContext context, final IRubyObject self, | ||
final IRubyObject[] args, final Block block) { | ||
|
||
if ( args.length == 0 ) return to_set(context, self, block); | ||
|
||
final IRubyObject klass = args[0]; args[0] = self; | ||
return ((RubyClass) klass).newInstance(context, args, block); | ||
} | ||
|
||
} |
1,253 changes: 1,253 additions & 0 deletions
1,253
core/src/main/java/org/jruby/ext/set/RubySet.java
Large diffs are not rendered by default.
Oops, something went wrong.
227 changes: 227 additions & 0 deletions
227
core/src/main/java/org/jruby/ext/set/RubySortedSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
/* | ||
**** BEGIN LICENSE BLOCK ***** | ||
* Version: EPL 1.0/GPL 2.0/LGPL 2.1 | ||
* | ||
* The contents of this file are subject to the Eclipse Public | ||
* License Version 1.0 (the "License"); you may not use this file | ||
* except in compliance with the License. You may obtain a copy of | ||
* the License at http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Software distributed under the License is distributed on an "AS | ||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* rights and limitations under the License. | ||
* | ||
* Copyright (C) 2016 Karol Bucek | ||
* | ||
* Alternatively, the contents of this file may be used under the terms of | ||
* either of the GNU General Public License Version 2 or later (the "GPL"), | ||
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | ||
* in which case the provisions of the GPL or the LGPL are applicable instead | ||
* of those above. If you wish to allow use of your version of this file only | ||
* under the terms of either the GPL or the LGPL, and not to allow others to | ||
* use your version of this file under the terms of the EPL, indicate your | ||
* decision by deleting the provisions above and replace them with the notice | ||
* and other provisions required by the GPL or the LGPL. If you do not delete | ||
* the provisions above, a recipient may use your version of this file under | ||
* the terms of any one of the EPL, the GPL or the LGPL. | ||
***** END LICENSE BLOCK *****/ | ||
package org.jruby.ext.set; | ||
|
||
import org.jruby.*; | ||
import org.jruby.anno.JRubyMethod; | ||
import org.jruby.runtime.*; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
|
||
import java.util.*; | ||
|
||
import static org.jruby.RubyArray.DefaultComparator; | ||
|
||
/** | ||
* Native implementation of Ruby's SortedSet (set.rb replacement). | ||
* | ||
* @author kares | ||
*/ | ||
@org.jruby.anno.JRubyClass(name="SortedSet", parent = "Set") | ||
public class RubySortedSet extends RubySet implements SortedSet { | ||
|
||
static RubyClass createSortedSetClass(final Ruby runtime) { | ||
RubyClass SortedSet = runtime.defineClass("SortedSet", runtime.getClass("Set"), ALLOCATOR); | ||
|
||
SortedSet.setReifiedClass(RubySortedSet.class); | ||
SortedSet.defineAnnotatedMethods(RubySortedSet.class); | ||
|
||
return SortedSet; | ||
} | ||
|
||
private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() { | ||
public RubySortedSet allocate(Ruby runtime, RubyClass klass) { | ||
return new RubySortedSet(runtime, klass); | ||
} | ||
}; | ||
|
||
private static class OrderComparator extends DefaultComparator { | ||
|
||
private final Ruby runtime; | ||
|
||
OrderComparator(final Ruby runtime) { | ||
super(null); this.runtime = runtime; | ||
} | ||
|
||
@Override | ||
protected ThreadContext context() { | ||
return runtime.getCurrentContext(); | ||
} | ||
} | ||
|
||
private final TreeSet<IRubyObject> order; | ||
|
||
protected RubySortedSet(Ruby runtime, RubyClass klass) { | ||
super(runtime, klass); | ||
order = new TreeSet<>(new OrderComparator(runtime)); | ||
} | ||
|
||
@Override | ||
void unmarshal() { | ||
super.unmarshal(); | ||
IRubyObject[] elems = hash.keys().toJavaArrayMaybeUnsafe(); | ||
for ( int i=0; i<elems.length; i++ ) order.add( elems[i] ); | ||
} | ||
|
||
@JRubyMethod(name = "[]", rest = true, meta = true) // re-def Set[] for SortedSet | ||
public static RubySortedSet create(final ThreadContext context, IRubyObject self, IRubyObject... ary) { | ||
final Ruby runtime = context.runtime; | ||
|
||
RubySortedSet set = new RubySortedSet(runtime, (RubyClass) self); | ||
return (RubySortedSet) set.initSet(context, ary, 0, ary.length); | ||
} | ||
|
||
@Override | ||
protected void addImpl(final Ruby runtime, final IRubyObject obj) { | ||
// NOTE: we're able to function without the check - comparator will raise ArgumentError | ||
//if ( ! obj.respondsTo("<=>") ) { | ||
// throw runtime.newArgumentError("value must respond to <=>"); | ||
//} | ||
super.addImpl(runtime, obj); // @hash[obj] = true | ||
order.add(obj); | ||
} | ||
|
||
@Override | ||
protected void addImplSet(final ThreadContext context, final RubySet set) { | ||
super.addImplSet(context, set); | ||
order.addAll(set.elements()); | ||
} | ||
|
||
@Override | ||
protected boolean deleteImpl(final IRubyObject obj) { | ||
if ( super.deleteImpl(obj) ) { | ||
order.remove(obj); return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
protected void deleteImplIterator(final IRubyObject obj, final Iterator it) { | ||
super.deleteImpl(obj); | ||
// iterator over elementsOrdered() | ||
it.remove(); // order.remove(obj) | ||
} | ||
|
||
@Override | ||
protected void clearImpl() { | ||
hash.rb_clear(); | ||
order.clear(); | ||
} | ||
|
||
@JRubyMethod(name = "sort") // re-def Enumerable#sort | ||
public RubyArray sort(final ThreadContext context) { | ||
return RubyArray.newArray(context.runtime, order); // instead of this.hash.keys(); | ||
} | ||
|
||
@Override | ||
public RubyArray to_a(final ThreadContext context) { | ||
return sort(context); // instead of this.hash.keys(); | ||
} | ||
|
||
// NOTE: weirdly Set/SortedSet in Ruby do not have sort! | ||
|
||
@Override | ||
protected Set<IRubyObject> elementsOrdered() { return order; } | ||
|
||
@Override | ||
public Iterator<Object> iterator() { | ||
return new JavaIterator(); | ||
} | ||
|
||
// java.util.SortedSet : | ||
|
||
public Comparator<? super IRubyObject> comparator() { | ||
return order.comparator(); | ||
} | ||
|
||
public Object first() { | ||
return firstValue().toJava(Object.class); | ||
} | ||
|
||
public IRubyObject firstValue() { | ||
return order.first(); | ||
} | ||
|
||
public Object last() { | ||
return lastValue().toJava(Object.class); | ||
} | ||
|
||
public IRubyObject lastValue() { | ||
return order.last(); | ||
} | ||
|
||
public SortedSet headSet(Object toElement) { | ||
throw new UnsupportedOperationException("NOT IMPLEMENTED"); | ||
} | ||
|
||
public SortedSet subSet(Object fromElement, Object toElement) { | ||
throw new UnsupportedOperationException("NOT IMPLEMENTED"); | ||
} | ||
|
||
public SortedSet tailSet(Object fromElement) { | ||
throw new UnsupportedOperationException("NOT IMPLEMENTED"); | ||
} | ||
|
||
public SortedSet<IRubyObject> rawHeadSet(IRubyObject toElement) { | ||
return order.headSet(toElement); | ||
} | ||
|
||
public SortedSet<IRubyObject> rawSubSet(IRubyObject fromElement, IRubyObject toElement) { | ||
return order.subSet(fromElement, toElement); | ||
} | ||
|
||
public SortedSet<IRubyObject> rawTailSet(IRubyObject fromElement) { | ||
return order.tailSet(fromElement); | ||
} | ||
|
||
private class JavaIterator implements Iterator<Object> { | ||
|
||
private final Iterator<IRubyObject> rawIterator; | ||
|
||
JavaIterator() { | ||
rawIterator = RubySortedSet.this.order.iterator(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return rawIterator.hasNext(); | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return rawIterator.next().toJava(Object.class); | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
rawIterator.remove(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/***** BEGIN LICENSE BLOCK ***** | ||
* Version: EPL 1.0/GPL 2.0/LGPL 2.1 | ||
* | ||
* The contents of this file are subject to the Eclipse Public | ||
* License Version 1.0 (the "License"); you may not use this file | ||
* except in compliance with the License. You may obtain a copy of | ||
* the License at http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Software distributed under the License is distributed on an "AS | ||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* rights and limitations under the License. | ||
* | ||
* Copyright (C) 2016 Karol Bucek | ||
* | ||
* Alternatively, the contents of this file may be used under the terms of | ||
* either of the GNU General Public License Version 2 or later (the "GPL"), | ||
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | ||
* in which case the provisions of the GPL or the LGPL are applicable instead | ||
* of those above. If you wish to allow use of your version of this file only | ||
* under the terms of either the GPL or the LGPL, and not to allow others to | ||
* use your version of this file under the terms of the EPL, indicate your | ||
* decision by deleting the provisions above and replace them with the notice | ||
* and other provisions required by the GPL or the LGPL. If you do not delete | ||
* the provisions above, a recipient may use your version of this file under | ||
* the terms of any one of the EPL, the GPL or the LGPL. | ||
***** END LICENSE BLOCK *****/ | ||
package org.jruby.ext.set; | ||
|
||
import org.jruby.Ruby; | ||
import org.jruby.runtime.load.Library; | ||
|
||
public final class SetLibrary implements Library { | ||
|
||
public void load(Ruby runtime, boolean wrap) { | ||
SetLibrary.load(runtime); | ||
} | ||
|
||
public static void load(Ruby runtime) { | ||
RubySet.createSetClass(runtime); | ||
RubySortedSet.createSortedSetClass(runtime); | ||
runtime.getModule("Enumerable").defineAnnotatedMethods(EnumerableExt.class); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
# | ||
# .rb part for JRuby's native Set impl (taken from set.rb) | ||
|
||
class Set | ||
|
||
def pretty_print(pp) # :nodoc: | ||
pp.text sprintf('#<%s: {', self.class.name) | ||
pp.nest(1) { | ||
pp.seplist(self) { |o| | ||
pp.pp o | ||
} | ||
} | ||
pp.text '}>' | ||
end | ||
|
||
def pretty_print_cycle(pp) # :nodoc: | ||
pp.text sprintf('#<%s: {%s}>', self.class.name, empty? ? '' : '...') | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
require 'test/unit' | ||
require 'set.rb' | ||
|
||
# JRuby's Set impl specific or low-level details. | ||
class TestSet < Test::Unit::TestCase | ||
|
||
class SubSet < Set ; end | ||
class SubSortedSet < SortedSet ; end | ||
|
||
def test_sub_set | ||
set = SubSet.new | ||
assert_same SubSet, set.class | ||
assert set.is_a?(SubSet) | ||
assert set.is_a?(Set) | ||
assert hash = set.instance_variable_get(:@hash) | ||
assert hash.is_a?(Hash) | ||
assert_equal Hash.new, hash | ||
assert_equal '#<TestSet::SubSet: {}>', set.inspect | ||
|
||
assert_false Set.new.equal?(SubSet.new) | ||
assert_true Set.new.eql?(SubSet.new) | ||
assert_true ( SubSet.new == Set.new ) | ||
|
||
assert_false SortedSet.new.equal?(SubSortedSet.new) | ||
assert_true SortedSet.new.eql?(SubSortedSet.new) | ||
assert_true ( SubSortedSet.new == Set.new ) | ||
assert_true ( SubSortedSet.new == SortedSet.new ) | ||
assert_true ( SortedSet.new == SubSortedSet.new ) | ||
end | ||
|
||
def test_allocate | ||
set = Set.allocate | ||
assert_same Set, set.class | ||
assert_nil set.instance_variable_get(:@hash) # same on MRI | ||
|
||
# set not really usable : | ||
begin | ||
set << 1 ; fail 'set << 1 did not fail!' | ||
rescue # NoMethodError # JRuby: NPE | ||
# NoMethodError: undefined method `[]=' for nil:NilClass | ||
# from /opt/local/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/set.rb:313:in `add' | ||
end | ||
end | ||
|
||
def test_marshal_dump | ||
assert_equal "\x04\b{\x00".force_encoding('ASCII-8BIT'), Marshal.dump(Hash.new) | ||
|
||
# MRI internally uses a @hash with a default: `Hash.new(false)' | ||
empty_set = "\x04\bo:\bSet\x06:\n@hash}\x00F".force_encoding('ASCII-8BIT') | ||
assert_equal empty_set, Marshal.dump(Set.new) | ||
|
||
dump = Marshal.dump(Set.new) | ||
assert_equal Set.new, Marshal.load(dump) | ||
|
||
set = Marshal.load Marshal.dump(Set.new([1, 2])) | ||
assert_same Set, set.class | ||
assert_equal Set.new([1, 2]), set | ||
set << 3 | ||
assert_equal 3, set.size | ||
|
||
set = Marshal.load Marshal.dump(Set.new([1, 2]).dup) | ||
assert_same Set, set.class | ||
assert_equal Set.new([1, 2]), set | ||
end | ||
|
||
def test_sorted_marshal_dump | ||
dump = Marshal.dump(SortedSet.new) | ||
assert_equal SortedSet.new, Marshal.load(dump) | ||
|
||
set = Marshal.load Marshal.dump(SortedSet.new([2, 1])) | ||
assert_same SortedSet, set.class | ||
assert_equal SortedSet.new([1, 2]), set | ||
assert_equal [1, 2], set.sort | ||
set << 3 | ||
assert_equal 3, set.size | ||
assert_equal [1, 2, 3], set.sort | ||
|
||
set = Marshal.load Marshal.dump(SortedSet.new([2, 1]).dup) | ||
assert_same SortedSet, set.class | ||
assert_equal SortedSet.new([1, 2]), set | ||
assert_equal [1, 2], set.to_a | ||
|
||
set = Marshal.load Marshal.dump(SortedSet.new([2, 3, 1])) | ||
each = []; set.each { |e| each << e } | ||
assert_equal [1, 2, 3], each | ||
end | ||
|
||
def test_dup | ||
set = Set.new [1, 2] | ||
assert_same Set, set.dup.class | ||
assert_equal set, set.dup | ||
dup = set.dup | ||
set << 3 | ||
assert_equal 3, set.size | ||
assert_equal 2, dup.size | ||
|
||
set = SortedSet.new [1, 2] | ||
assert_same SortedSet, set.dup.class | ||
assert_equal set, set.dup | ||
dup = set.dup | ||
set << 0 | ||
assert_equal 3, set.size | ||
assert_equal 2, dup.size | ||
end | ||
|
||
def test_to_java | ||
assert set = Set.new.to_java | ||
assert set.toString.start_with?('#<Set:0x') | ||
assert_equal org.jruby.ext.set.RubySet, set.class | ||
assert set.is_a?(java.util.Set) | ||
assert_equal java.util.HashSet.new, set | ||
|
||
assert set = SortedSet.new([2, 1]).to_java | ||
assert set.toString.start_with?('#<SortedSet:0x') | ||
assert_equal org.jruby.ext.set.RubySortedSet, set.class | ||
assert set.is_a?(java.util.Set) | ||
assert set.is_a?(java.util.SortedSet) | ||
assert_equal java.util.TreeSet.new([1, 2]), set | ||
end if defined? JRUBY_VERSION | ||
|
||
end |