Skip to content

Commit

Permalink
[Truffle] Hash#keys and #values specs passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Mar 3, 2015
1 parent edd7034 commit d7ca496
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 103 deletions.
2 changes: 0 additions & 2 deletions spec/truffle/tags/core/hash/keys_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/truffle/tags/core/hash/values_tags.txt

This file was deleted.

100 changes: 0 additions & 100 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/HashNodes.java
Expand Up @@ -830,57 +830,6 @@ public boolean keyBuckets(VirtualFrame frame, RubyHash hash, Object key) {

}

@CoreMethod(names = "keys")
public abstract static class KeysNode extends HashCoreMethodNode {

public KeysNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public KeysNode(KeysNode prev) {
super(prev);
}

@Specialization(guards = "isNull")
public RubyArray keysNull(RubyHash hash) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0);
}

@Specialization(guards = {"!isNull", "!isBuckets"})
public RubyArray keysPackedArray(RubyHash hash) {
notDesignedForCompilation();

final Object[] store = (Object[]) hash.getStore();

final Object[] keys = new Object[hash.getSize()];

for (int n = 0; n < keys.length; n++) {
keys[n] = store[n * 2];
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), keys, keys.length);
}

@Specialization(guards = "isBuckets")
public RubyArray keysBuckets(RubyHash hash) {
notDesignedForCompilation();

final Object[] keys = new Object[hash.getSize()];

Entry entry = hash.getFirstInSequence();
int n = 0;

while (entry != null) {
keys[n] = entry.getKey();
n++;
entry = entry.getNextInSequence();
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), keys, keys.length);
}

}

@CoreMethod(names = {"map", "collect"}, needsBlock = true)
@ImportGuards(HashGuards.class)
public abstract static class MapNode extends YieldingCoreMethodNode {
Expand Down Expand Up @@ -1191,55 +1140,6 @@ public int sizePackedArray(RubyHash hash) {

}

@CoreMethod(names = "values")
public abstract static class ValuesNode extends HashCoreMethodNode {

public ValuesNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public ValuesNode(ValuesNode prev) {
super(prev);
}

@Specialization(guards = "isNull")
public RubyArray valuesNull(RubyHash hash) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0);
}

@Specialization(guards = {"!isNull", "!isBuckets"})
public RubyArray valuesPackedArray(RubyHash hash) {
final Object[] store = (Object[]) hash.getStore();

final Object[] values = new Object[hash.getSize()];

for (int n = 0; n < values.length; n++) {
values[n] = store[n * 2 + 1];
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), values, values.length);
}

@Specialization(guards = "isBuckets")
public RubyArray valuesBuckets(RubyHash hash) {
notDesignedForCompilation();

final Object[] values = new Object[hash.getSize()];

Entry entry = hash.getFirstInSequence();
int n = 0;

while (entry != null) {
values[n] = entry.getValue();
n++;
entry = entry.getNextInSequence();
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), values, values.length);
}

}

@CoreMethod(names = "to_a")
public abstract static class ToArrayNode extends HashCoreMethodNode {

Expand Down
18 changes: 18 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/hash.rb
Expand Up @@ -71,4 +71,22 @@ def each_value
self
end

def keys
ary = []
each_item do |item|
ary << item.key
end
ary
end

def values
ary = []

each_item do |item|
ary << item.value
end

ary
end

end

0 comments on commit d7ca496

Please sign in to comment.