Brian Dolan
Brian Dolan

Reputation: 3136

How to check if a domain contains an element in Chapel

Suppose I have a sparse domain and I want to check if an element has been added.

var D = {1..5, 1..5},
    SD: sparse subdomain(D);

SD += (1,3);

Now I want to see if (1,3) and (2,3) are in SD. This is not correct and this page doesn't .contain() an example. HA! See what I did there?

//Don't do this
SD.contains(1,3)  // want true;
SD.contains(2,3)  // want false;

Upvotes: 2

Views: 82

Answers (1)

David Iten
David Iten

Reputation: 545

Domains in Chapel all support a method with the signature member(i:idxType ...rank) that returns true if the specified index is a member of the domain and false otherwise. This includes associative, opaque, rectangular, and sparse domains. Examples for each of these types follow:

Associative:

var D: domain(string);
D += "hello";

writeln("Associative");
writeln(D.member("world")); // false
writeln(D.member("hello")); // true

Opaque:

var D: domain(opaque);
var i1 = D.create();
var i2: i1.type;

writeln("Opaque");
writeln(D.member(i2)); // false
writeln(D.member(i1)); // true

Rectangular:

  var D = {1..4, 3..5};

  writeln("Rectangular");
  writeln(D.member(2,6)); // false
  writeln(D.member(3,3)); // true

Sparse:

  var D = {1..10, 1..10};
  var SD: sparse subdomain(D);
  SD += (2,3);

  writeln("Sparse");
  writeln(SD.member(2,7)); // false
  writeln(SD.member(2,3)); // true

Upvotes: 4

Related Questions