Skip to content

Commit

Permalink
Get multi-dim object arrays working on JVM.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Jul 13, 2015
1 parent 032f577 commit cd7a532
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Expand Up @@ -22,7 +22,7 @@ public SixModelObject type_object_for(ThreadContext tc, SixModelObject HOW) {
public SixModelObject allocate(ThreadContext tc, STable st) {
MultiDimArrayREPRData rd = (MultiDimArrayREPRData)st.REPRData;
if (rd != null) {
MultiDimArrayInstanceBase obj = new MultiDimArrayInstanceBase();
MultiDimArrayInstance obj = new MultiDimArrayInstance();
obj.dimensions = new long[rd.numDimensions];
obj.st = st;
return obj;
Expand Down
@@ -0,0 +1,27 @@
package org.perl6.nqp.sixmodel.reprs;

import java.lang.System;

import org.perl6.nqp.runtime.ExceptionHandling;
import org.perl6.nqp.runtime.ThreadContext;
import org.perl6.nqp.sixmodel.SixModelObject;

public class MultiDimArrayInstance extends MultiDimArrayInstanceBase {
public SixModelObject[] slots;

public void set_dimensions(ThreadContext tc, long[] dims) {
super.set_dimensions(tc, dims);
if (slots == null)
slots = new SixModelObject[numSlots()];
else
duplicateSetDimensions(tc);
}

public SixModelObject at_pos_multidim_boxed(ThreadContext tc, long[] indices) {
return slots[indicesToFlatIndex(tc, indices)];
}

public void bind_pos_multidim_boxed(ThreadContext tc, long[] indices, SixModelObject value) {
slots[indicesToFlatIndex(tc, indices)] = value;
}
}
Expand Up @@ -6,7 +6,7 @@
import org.perl6.nqp.runtime.ThreadContext;
import org.perl6.nqp.sixmodel.SixModelObject;

public class MultiDimArrayInstanceBase extends SixModelObject {
public abstract class MultiDimArrayInstanceBase extends SixModelObject {
public long[] dimensions;

public long[] dimensions(ThreadContext tc) {
Expand All @@ -24,4 +24,42 @@ public void set_dimensions(ThreadContext tc, long[] dims) {
rd.numDimensions, dims.length));
}
}

protected int numSlots() {
long result = dimensions[0];
for (int i = 1; i < dimensions.length; i++)
result *= dimensions[i];
return (int)result;
}

protected void duplicateSetDimensions(ThreadContext tc) {
throw ExceptionHandling.dieInternal(tc,
"MultiDimArray: can only set dimensions once");
}

protected int indicesToFlatIndex(ThreadContext tc, long[] indices) {
if (indices.length == dimensions.length) {
long multiplier = 1;
long result = 0;
for (int i = dimensions.length - 1; i >= 0; i--) {
long dim_size = dimensions[i];
long index = indices[i];
if (index >= 0 && index < dim_size) {
result += index * multiplier;
multiplier *= dim_size;
}
else {
throw ExceptionHandling.dieInternal(tc, String.format(
"Index %d for dimension %d out of range (must be 0..%d)",
index, i + 1, dim_size));
}
}
return (int)result;
}
else {
throw ExceptionHandling.dieInternal(tc, String.format(
"Cannot access %d dimension array with %d indices",
dimensions.length, indices.length));
}
}
}

0 comments on commit cd7a532

Please sign in to comment.