Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multiarray usage?

10 views
Skip to first unread message

Jerome Quelin

unread,
Nov 29, 2002, 10:22:59 AM11/29/02
to perl6-i...@perl.org
Hi,

I'd like to use multi-arrays, but I can't understand how they're
working. I looked at $PARROT/t/pmc/multiarray.t, but it's a bit
obscure.
Could you help me to understand them please? Or are they deprecated?
Should I use something else to have arrays of arrays? What are the
limitations of multiarrays? I've read in classes/multiarray.pmc that
multiarrays can only grow in their _last_ direction: is there a
multidimensionnal pmc that can grow on whatever the dimesion?

About multiarray, I tried the following, and it works, but please help
me to understand what I wrote:

new P2, .Key
set P2, 10
new P3, .Key
set P3, 5
push P2, P3
# Ok, I can understand that I'm building its width and height with
# P2 and P3, and concatenate all the keys in one "multidimensinal"
# key.
new P1, .PerlArray
# I understand that building a multiarray requires parameters,
# that we provide in a list (does multiarray require a PerlArray,
# or may I use something else?)
set P1[0], 0 # what does this param means?
set P1[1], 200 # what does this param means?
set P1[2], 1 # what does this param means?
set P1[3], P2 # The dimensions of the multiarray?
new P0, .MultiArray, P1

set P0[2;3], "foobar"
set S10, P0[10;10]
print S10
print "\n"
end

How can I make the multiarray grow? Is it self expandable, like the
PerlArray (that is, accessing an element out of bounds make the
PerlArray grow to fit till that new bound)?

Jerome, who'd like to understand...
--
jqu...@mongueurs.net

Leopold Toetsch

unread,
Nov 29, 2002, 11:18:40 AM11/29/02
to Jerome Quelin, perl6-i...@perl.org
Jerome Quelin wrote:

> Hi,
>
> I'd like to use multi-arrays, but I can't understand how they're
> working. I looked at $PARROT/t/pmc/multiarray.t, but it's a bit
> obscure.


underdocumented


> Could you help me to understand them please? Or are they deprecated?


Yes/No


> Should I use something else to have arrays of arrays?


multiarrays are mainly intended for huge packed multi dim arrays.

> ... What are the

> limitations of multiarrays? I've read in classes/multiarray.pmc that
> multiarrays can only grow in their _last_ direction: is there a
> multidimensionnal pmc that can grow on whatever the dimesion?


Yes/no. The first-1 dimensions are used for index calculation.


>
> About multiarray, I tried the following, and it works, but please help
> me to understand what I wrote:

> new P2, .Key
> set P2, 10
> new P3, .Key
> set P3, 5
> push P2, P3
> # Ok, I can understand that I'm building its width and height with
> # P2 and P3, and concatenate all the keys in one "multidimensinal"
> # key.


key_append would be a better term (s. classes/key.pmc:push()) - it
creates a chained key.


> new P1, .PerlArray
> # I understand that building a multiarray requires parameters,
> # that we provide in a list (does multiarray require a PerlArray,
> # or may I use something else?)


Array or PerlArray are both fine. Actually any PMC, that supports
- get_elements, get_integer_keyed_int and get_pmc_keyed_int
(s. list.c:list_new_init())


> set P1[0], 0 # what does this param means?
> set P1[1], 200 # what does this param means?


s. docs/pdds/pdd02_vtable2.pod and the usage of these params in list.c
/*
* list_new_init uses these initializers:
* 0 ... size (set initial size of list)
* 1 ... array dimensions (multiarray)
* 2 ... type (overriding type parameter)
* 3 ... item_size for enum_type_sized
* 4 ... items_per_chunk

so above lines use key 0, value 200 i.e. set size to 200.


> set P1[2], 1 # what does this param means?
> set P1[3], P2 # The dimensions of the multiarray?


Yes


> How can I make the multiarray grow? Is it self expandable, like the
> PerlArray (that is, accessing an element out of bounds make the
> PerlArray grow to fit till that new bound)?


As the definition of Multiarray is (classes/multiarray.pmc):

pmclass MultiArray extends Array {

it has the same growing policy as the Array class i.e. none.


> Jerome, who'd like to understand...


I hope so,
leo

Jerome Quelin

unread,
Nov 29, 2002, 11:53:50 AM11/29/02
to Leopold Toetsch, perl6-i...@perl.org
On Vendredi 29 Novembre 2002 17:18, Leopold Toetsch wrote:
> > Should I use something else to have arrays of arrays?
> multiarrays are mainly intended for huge packed multi dim arrays.

Then what should I use for a 2D array?


> > ... What are the
> > limitations of multiarrays? I've read in classes/multiarray.pmc
> > that multiarrays can only grow in their _last_ direction: is there
> > a multidimensionnal pmc that can grow on whatever the dimesion?
> Yes/no. The first-1 dimensions are used for index calculation.

So, a so-called MultiArray is in fact one array, but the first n elems
are the elems of the 1st line, the next n elems are the 2nd line, etc.?

Speaking of limitations, what can hold a multiarray? Only integers, only
strings, only pmc, a mix of everything?


> > new P1, .PerlArray
> > # I understand that building a multiarray requires parameters,
> > # that we provide in a list (does multiarray require a PerlArray,
> > # or may I use something else?)
> Array or PerlArray are both fine. Actually any PMC, that supports
> - get_elements, get_integer_keyed_int and get_pmc_keyed_int
> (s. list.c:list_new_init())

Ok, I understand.


> s. docs/pdds/pdd02_vtable2.pod and the usage of these params in
> list.c /*
> * list_new_init uses these initializers:
> * 0 ... size (set initial size of list)
> * 1 ... array dimensions (multiarray)
> * 2 ... type (overriding type parameter)
> * 3 ... item_size for enum_type_sized
> * 4 ... items_per_chunk

If I understand correctly, this means that:
* P1[0] is the size of the "flat" one-dim array. So, for a 2d array
supporting 25 rows and 80 columns, I should set P1[0] to 80*25, right?
* P1[1] is the multidimension, plus one for the index calculation? And
does it start at 0 or 1? If I want a 2D multiarray, should I set P1[1]
to 2, or what? And isn't it redundant with P1[3]?
* P1[2], I can't understand what does this one stand for
* P1[3] is the packed key, telling that first dim goes from 0 to n, and
2nd dim from 0 to m. So, if I want a 2D array of 80x25, ie (0..79,
0..24), then I should issue the following:
new P2, .Key
set P2, 79 # 79 or 80?
new P3, .Key
set P3, 24 # 24 or 25?
push P2, P3


set P1[3], P2

* P1[4]: I can't understand this one, neither. And in fact it isn't used
in t/pmc/multiarray.t


> > How can I make the multiarray grow? Is it self expandable, like the
> > PerlArray (that is, accessing an element out of bounds make the
> > PerlArray grow to fit till that new bound)?
> As the definition of Multiarray is (classes/multiarray.pmc):
> pmclass MultiArray extends Array {
> it has the same growing policy as the Array class i.e. none.

Well, that's pretty clear... :-(
Then, what should I use for 2d arrays that need to grow? MultiArray for
now, and I must wait for a pmc that will allow growing and auto-
resizing multiarray?
Will there ever be such a PMC anyway?


Thanks for the explanations,
Jerome
--
jqu...@mongueurs.net

Leopold Toetsch

unread,
Nov 29, 2002, 1:02:05 PM11/29/02
to Jerome Quelin, perl6-i...@perl.org
Jerome Quelin wrote:

> On Vendredi 29 Novembre 2002 17:18, Leopold Toetsch wrote:
>
> Then what should I use for a 2D array?


The example (test) is an 2D array.


> So, a so-called MultiArray is in fact one array, but the first n elems
> are the elems of the 1st line, the next n elems are the 2nd line, etc.?


Yes, the base is the plain Array class, which uses list.c. Only index
calculation is done in multiarray.


> Speaking of limitations, what can hold a multiarray? Only integers, only
> strings, only pmc, a mix of everything?


Everything a list can hold (i.e. all above, but no mix), though it would
need a new class, which takes an list_type initializer. And datatypes
smaller then INTVAL aren't done yet.


>>s. docs/pdds/pdd02_vtable2.pod and the usage of these params in
>>list.c /*
>> * list_new_init uses these initializers:
>> * 0 ... size (set initial size of list)
>> * 1 ... array dimensions (multiarray)
>> * 2 ... type (overriding type parameter)
>> * 3 ... item_size for enum_type_sized
>> * 4 ... items_per_chunk
>>
>
> If I understand correctly, this means that:
> * P1[0] is the size of the "flat" one-dim array. So, for a 2d array
> supporting 25 rows and 80 columns, I should set P1[0] to 80*25, right?


Not exactly: you set P1[n] to the initializer key '0' and P1[n+1] to the
initializer data = size for key #0, and that is indeed 80x25.


> * P1[1] is the multidimension,


Same here: you set P[x] to 1 and P[x+1] to the dimension key. Please
look again at the first test, which is an 2D array of 10x5.


> * P1[2], I can't understand what does this one stand for


key #2 is one of the datatypes defined in datatypes.h.


> * P1[3] is the packed key,


it happens to be P1[3] here. The dimension key is the data entry for the
initializer key #1 - brrr.

You could do it like this (modulo the undefs inbetween):
new P1, .PerlArray
set P1[0], 2 # data type
findtype I0, "double" # untested
set P1[1], I0 # set data type # untested
set P1[2], 0 # size key
set P1[3], 2000 # size 80x25
set P1[6], 1 # multi dim key
set P1[7], P2 #

But we have no access functions in multiarray for an array of doubles.
So again, you fill the initializer PMC with key/value pairs. Supported
keys are listed above. Where in the initializer array these things are,
is not important.

> ... telling that first dim goes from 0 to n, and

> 2nd dim from 0 to m. So, if I want a 2D array of 80x25, ie (0..79,
> 0..24), then I should issue the following:
> new P2, .Key
> set P2, 79 # 79 or 80?


80


> * P1[4]: I can't understand this one, neither. And in fact it isn't used
> in t/pmc/multiarray.t


Only key #1 is used in multiarray (the multidimension key), but not the
initializer is used there - it's here really user_data[1]. Please look
at list.c:list_new_init().


> Will there ever be such a PMC anyway?


A multiarray based on perlarray can grow (in the last dimension).

Growing in all dimensions isn't.

We will have more of these, when perl native types are done, and when we
know, how to subclass pmcs. I really don't want to create a class for
each type/growing policy/whatever combination.


> Thanks for the explanations,
> Jerome


HTH
leo

Jerome Quelin

unread,
Nov 30, 2002, 3:22:41 AM11/30/02
to Leopold Toetsch, perl6-i...@perl.org
On Vendredi 29 Novembre 2002 19:02, you wrote:
> > Speaking of limitations, what can hold a multiarray? Only integers,
> > only strings, only pmc, a mix of everything?
> Everything a list can hold (i.e. all above, but no mix), though it
> would need a new class, which takes an list_type initializer. And
> datatypes smaller then INTVAL aren't done yet.

This makes me think... A PerlArray can hold PMCs... So I can have a
PerlArray of PerlArrays:
new P0, .PerlArray
set P0[0], 54
set P0[1], 45
new P1, .PerlArray
set P1[0], 19
set P1[1], 10
new P2, .PerlArray
set P2[0], P0
set P2[1], P1

set P10, P2[1]
set I0, P10[0]
print I0
print "\n"
end

It seems to work, and it is (sort of) expandable in every dimension (so
long as I'm carefully expand all the PerlArrays correctly). Is there
any reason not to use this trick?


> >>s. docs/pdds/pdd02_vtable2.pod and the usage of these params in
> >>list.c /*
> >> * list_new_init uses these initializers:
> >> * 0 ... size (set initial size of list)
> >> * 1 ... array dimensions (multiarray)
> >> * 2 ... type (overriding type parameter)
> >> * 3 ... item_size for enum_type_sized
> >> * 4 ... items_per_chunk

> Not exactly: you set P1[n] to the initializer key '0' and P1[n+1] to
> the initializer data = size for key #0, and that is indeed 80x25.

I had a hard time to understand what you meant, but I (finally) got it.
The initializer list works in pairs. So, I have two questions:
* why such a scheme, instead of using the indexes ot the list / array /
perlarray?
* what are the mandatory fields? size and array dimensions?


> Please
> look again at the first test, which is an 2D array of 10x5.

Then, why does the test set the size to 100 instead of 50 (10*5)?
Maybe that's why I've been so long to understand the examples...


Jerome
--
jqu...@mongueurs.net

Leopold Toetsch

unread,
Nov 30, 2002, 4:48:18 AM11/30/02
to Jerome Quelin, perl6-i...@perl.org
Jerome Quelin wrote:

> On Vendredi 29 Novembre 2002 19:02, you wrote:
> This makes me think... A PerlArray can hold PMCs... So I can have a
> PerlArray of PerlArrays:


This is always ok.


> set P10, P2[1]
> set I0, P10[0]


or directly:
set I0, P2[1;0]


> It seems to work, and it is (sort of) expandable in every dimension (so
> long as I'm carefully expand all the PerlArrays correctly). Is there
> any reason not to use this trick?


No, not as long as you are using PMCs. Multiarrays shine for big packed
arrays.


> The initializer list works in pairs. So, I have two questions:
> * why such a scheme, instead of using the indexes ot the list / array /
> perlarray?


You should ask the author of the cited pddd02 ;-)


> * what are the mandatory fields? size and array dimensions?


Yes.


> Then, why does the test set the size to 100 instead of 50 (10*5)?
> Maybe that's why I've been so long to understand the examples...


I'll change that - sorrry for the confusion.


> Jerome


leo

0 new messages