-
-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ceaf178
commit 43b68cd
Showing
5 changed files
with
193 additions
and
98 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
72 changes: 72 additions & 0 deletions
72
truffle/src/main/java/org/jruby/truffle/nodes/core/array/ArrayView.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,72 @@ | ||
/* | ||
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 1.0 | ||
* GNU General Public License version 2 | ||
* GNU Lesser General Public License version 2.1 | ||
*/ | ||
package org.jruby.truffle.nodes.core.array; | ||
|
||
public interface ArrayView { | ||
|
||
Object get(int index); | ||
|
||
class IntegerArrayView implements ArrayView { | ||
|
||
private final int[] array; | ||
|
||
public IntegerArrayView(int[] array) { | ||
this.array = array; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
return array[index]; | ||
} | ||
} | ||
|
||
class LongArrayView implements ArrayView { | ||
|
||
private final long[] array; | ||
|
||
public LongArrayView(long[] array) { | ||
this.array = array; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
return array[index]; | ||
} | ||
} | ||
|
||
class DoubleArrayView implements ArrayView { | ||
|
||
private final double[] array; | ||
|
||
public DoubleArrayView(double[] array) { | ||
this.array = array; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
return array[index]; | ||
} | ||
} | ||
|
||
class ObjectArrayView implements ArrayView { | ||
|
||
private final Object[] array; | ||
|
||
public ObjectArrayView(Object[] array) { | ||
this.array = array; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
return array[index]; | ||
} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
truffle/src/main/java/org/jruby/truffle/nodes/core/array/ArrayViews.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,30 @@ | ||
/* | ||
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 1.0 | ||
* GNU General Public License version 2 | ||
* GNU Lesser General Public License version 2.1 | ||
*/ | ||
package org.jruby.truffle.nodes.core.array; | ||
|
||
public abstract class ArrayViews { | ||
|
||
public static ArrayView.IntegerArrayView view(int[] array) { | ||
return new ArrayView.IntegerArrayView(array); | ||
} | ||
|
||
public static ArrayView.LongArrayView view(long[] array) { | ||
return new ArrayView.LongArrayView(array); | ||
} | ||
|
||
public static ArrayView.DoubleArrayView view(double[] array) { | ||
return new ArrayView.DoubleArrayView(array); | ||
} | ||
|
||
public static ArrayView.ObjectArrayView view(Object[] array) { | ||
return new ArrayView.ObjectArrayView(array); | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
truffle/src/main/java/org/jruby/truffle/nodes/core/array/PopOneNode.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,69 @@ | ||
/* | ||
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 1.0 | ||
* GNU General Public License version 2 | ||
* GNU Lesser General Public License version 2.1 | ||
*/ | ||
package org.jruby.truffle.nodes.core.array; | ||
|
||
import com.oracle.truffle.api.dsl.*; | ||
import com.oracle.truffle.api.source.SourceSection; | ||
import org.jruby.truffle.nodes.RubyNode; | ||
import org.jruby.truffle.runtime.RubyContext; | ||
import org.jruby.truffle.runtime.core.RubyArray; | ||
import org.jruby.truffle.runtime.core.RubyBasicObject; | ||
|
||
@NodeChildren({ | ||
@NodeChild("array") | ||
}) | ||
@ImportStatic(ArrayGuards.class) | ||
public abstract class PopOneNode extends RubyNode { | ||
|
||
public PopOneNode(RubyContext context, SourceSection sourceSection) { | ||
super(context, sourceSection); | ||
} | ||
|
||
public abstract Object executePopOne(RubyArray array); | ||
|
||
// Pop from an empty array | ||
|
||
@Specialization(guards = "isEmpty(array)") | ||
public RubyBasicObject popOneEmpty(RubyArray array) { | ||
return nil(); | ||
} | ||
|
||
// Pop from a non-empty array | ||
|
||
@Specialization(guards = {"!isEmpty(array)", "isIntegerFixnum(array)"}) | ||
public Object popOneInteger(RubyArray array) { | ||
return popOne(array, ArrayViews.view((int[]) array.getStore())); | ||
} | ||
|
||
@Specialization(guards = {"!isEmpty(array)", "isLongFixnum(array)"}) | ||
public Object popOneLong(RubyArray array) { | ||
return popOne(array, ArrayViews.view((long[]) array.getStore())); | ||
} | ||
|
||
@Specialization(guards = {"!isEmpty(array)", "isFloat(array)"}) | ||
public Object popOneDouble(RubyArray array) { | ||
return popOne(array, ArrayViews.view((double[]) array.getStore())); | ||
} | ||
|
||
@Specialization(guards = {"!isEmpty(array)", "isObject(array)"}) | ||
public Object popOneObject(RubyArray array) { | ||
return popOne(array, ArrayViews.view((Object[]) array.getStore())); | ||
} | ||
|
||
// General implementation | ||
|
||
private Object popOne(RubyArray array, ArrayView view) { | ||
final int size = array.getSize(); | ||
final Object value = view.get(size - 1); | ||
array.setSize(size - 1); | ||
return value; | ||
} | ||
|
||
} |
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