Page 1 of 1

random access incidence vector of HASSE_DIAGRAM->FACES

Posted: 14 Nov 2016, 12:49
by ren
Not sure if this is intended behaviour, but this works:

Code: Select all

$simplexHasse = simplex(2)->HASSE_DIAGRAM; $simplexFace = $simplexHasse->FACES->[4]; print $simplexFace->[0]; print $simplexFace->[1];
while this does not:

Code: Select all

$simplexHasse = simplex(2)->HASSE_DIAGRAM; $simplexFace = $simplexHasse->FACES->[4]; print $simplexFace->[1];
And random access seems to be the only way to access the content of $simplexFace.

Re: random access incidence vector of HASSE_DIAGRAM->FACES

Posted: 14 Nov 2016, 13:38
by hampe
The faces of a Hasse diagram are of type Set<Int>, which can be determined by calling

Code: Select all

print $simplexFace->type->full_name;
so random access is not actually foreseen in their interface (not quite sure, why the first version works, probably some perlish voodoo...).
There are various ways to access its elements on the perl side:
  • You can convert it, for example to a perl array or an Array<Int> via

    Code: Select all

    @sface = @{$simplexFace}; $sface_arr = new Array<Int>($simplexFace);
  • You can iterate over it using entire:

    Code: Select all

    for($it = entire($simplexFace);$it; $it++) { print $$it,"\n";};
Hope that helps!

Re: random access incidence vector of HASSE_DIAGRAM->FACES

Posted: 14 Nov 2016, 14:31
by ren
Thanks, I will convert it to an array then!

Re: random access incidence vector of HASSE_DIAGRAM->FACES

Posted: 14 Nov 2016, 16:07
by gawrilow
... not quite sure, why the first version works, probably some perlish voodoo...
No vodoo at all. The Set behaves as any other container allowing to visit elements in ascending order. These three ways of iterating over set elements are equivalent, the first one having the smallest runtime overhead:

for my $elem (@$simplexFace) { ... }

for my $index (0..$#$simplexFace) { my $elem=$simplexFace->[$index]; ... }

for (my $iter=entire($simplexFace); $iter; ++$iter) { my $elem=$$iter; ... }

If you need true random access to its elements, then you indeed have to copy it into a plain perl array:

my @face=@$simplexFace;
my $elem=$face[$index];
...