2

I am working with a copy of an array so I can modify it. Sample is

var x:[1..4] int = [1,2,3,4];
writeln(x);
x.remove(3);
writeln(x);

var y = x;
writeln(y);
y.remove(2);
writeln(y);

The last line, however, gives me

error: halt reached - cannot call remove on an array defined over a domain with multiple arrays

So what is the correct method to copy and allow edits?

0

1 Answer 1

3

When you say:

var y = x;

you're getting a new array y whose domain (index set) is the same as x's. That is, this declaration would be similar to saying:

var D = {1..4};      // create a domain
var x, y: [D] int;   // create two arrays that share that domain

As the error message suggests, Chapel does not permit directly resizing an array that shares its domain with another array because, if it did, removing an element from y would also cause that element to be removed from x which we believe would be far too subtle and surprising to support.

So in order to create a copy of x that is modifiable, you'll want to declare y in a way that gives it its own individual domain. The simplest way to do this for your specific program would be as follows:

var y: [1..3] int = x;

You could also write it a bit more flexibly as:

var y: [1..x.size] int = x;

or even:

var y: [1..x.size] x.eltType = x;

Since each of these declarations results in y getting its own unique domain, it can be modified independently of x.

2
  • 1
    I've tried var y: [x.domain] x.eltType = x; and var y: x.type = x;, but both of them resulted in the same error as var y = x;. So is it OK to understand that the former two also share the domain of x? (Btw, var D = x.domain; var y: [D] x.eltType = x; worked for me, so this may be convenient to preserve bounds?) (It might also be convenient if a method like var y = copy( x ); exists... :-)
    – roygvib
    Mar 19, 2018 at 17:22
  • 1
    @roygbiv: You're correct that x.domain gives a reference to x's domain, so will share it. And that x.type gives x's type which includes its domain, so those are two other ways of sharing a domain, by intention. Your D declaration creates a new domain that takes its value from another, which is why it works. My thinking about a copy method was that maybe we should have domains support one so you could write var y: [x.domain.copy()] x.eltType = x; I'm not opposed to a copy() method/function for arrays, but think it's ambiguous whether it'd naturally copy the domain or not...
    – Brad
    Mar 19, 2018 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.