[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

# Re: st: Mata: Recursive structures and pointers

 From rgates@stata.com (Richard Gates) To statalist@hsphsun2.harvard.edu Subject Re: st: Mata: Recursive structures and pointers Date Thu, 18 May 2006 07:58:09 -0500

```Ulrich Kohler <kohler@wz-berlin.de> inquired about implementing
a linked list in Mata.  This is what I came up with.

mata

struct listelem {
real scalar a
real scalar b
pointer(struct listelem scalar) scalar nextpoint
}

struct listelem scalar function setelem(real scalar s)
{
struct listelem scalar e

e.a=s
e.b=s

return(e)
}

struct pointlist {
struct listelem scalar first
pointer(struct listelem scalar) scalar last
}

struct pointlist scalar function setpoint( real vector seq)
{
struct pointlist scalar v
real scalar i
real scalar l

l = length(seq)
if (l == 0) return(v)

v.first.a = seq[1]
v.first.b = seq[1]
v.last = &(v.first)
liststruct(v)
for (i=2; i<=l; i++) {
v.last->nextpoint = &setelem(seq[i])
v.last = v.last->nextpoint
}
return(v)
}

void function testit()
{
real scalar i
real colvector v
pointer(struct listelem scalar) scalar el
struct pointlist scalar l

v = 1::10
l = setpoint(v)

liststruct(l)

printf("first\n")
el = &(l.first)
liststruct(*el)

i = 1
while(el->nextpoint!=NULL) {
printf("\nnext %g\n", ++i)
printf("el = ")
el
printf("next = ")
el->nextpoint
printf("\n")
el = el->nextpoint
liststruct(*el)
}
}
end

. mata: testit()
1  structure of 2 elements
1.1  structure of 3 elements
1.1.1  1 x 1 real = 1
1.1.2  1 x 1 real = 1
1.1.3  1 x 1 pointer = NULL
1.2  1 x 1 pointer != NULL
1  structure of 2 elements
1.1  structure of 3 elements
1.1.1  1 x 1 real = 1
1.1.2  1 x 1 real = 1
1.1.3  1 x 1 pointer != NULL
1.2  1 x 1 pointer != NULL
first
1  structure of 3 elements
1.1  1 x 1 real = 1
1.2  1 x 1 real = 1
1.3  1 x 1 pointer != NULL

next 2
el =   0x9022868
next =   0x9020b68

1  structure of 3 elements
1.1  1 x 1 real = 2
1.2  1 x 1 real = 2
1.3  1 x 1 pointer != NULL

next 3
el =   0x9020b68
next =   0x9022738

1  structure of 3 elements
1.1  1 x 1 real = 3
1.2  1 x 1 real = 3
1.3  1 x 1 pointer != NULL

next 4
el =   0x9022738
next =   0x90208b8

1  structure of 3 elements
1.1  1 x 1 real = 4
1.2  1 x 1 real = 4
1.3  1 x 1 pointer != NULL

next 5
el =   0x90208b8
next =   0x9026758

1  structure of 3 elements
1.1  1 x 1 real = 5
1.2  1 x 1 real = 5
1.3  1 x 1 pointer != NULL

next 6
el =   0x9026758
next =   0x9020e98

1  structure of 3 elements
1.1  1 x 1 real = 6
1.2  1 x 1 real = 6
1.3  1 x 1 pointer != NULL

next 7
el =   0x9020e98
next =   0x90225e8

1  structure of 3 elements
1.1  1 x 1 real = 7
1.2  1 x 1 real = 7
1.3  1 x 1 pointer != NULL

next 8
el =   0x90225e8
next =   0x9026578

1  structure of 3 elements
1.1  1 x 1 real = 8
1.2  1 x 1 real = 8
1.3  1 x 1 pointer != NULL

next 9
el =   0x9026578
next =   0x90269d8

1  structure of 3 elements
1.1  1 x 1 real = 9
1.2  1 x 1 real = 9
1.3  1 x 1 pointer != NULL

next 10
el =   0x90269d8
next =   0x90214b8

1  structure of 3 elements
1.1  1 x 1 real = 10
1.2  1 x 1 real = 10
1.3  1 x 1 pointer = NULL

-Rich
rgates@stata.com

>
> ------------------------------------------------xyz.mata---
> ...
> struct listelem {
> 	real scalar a
> 	real scalar b
> 	pointer(struct listelem scalar) scalar nextpoint
> }
>
> struct listelem scalar function setelem(real scalar s) {
> 	struct listelem scalar e
> 	e.a=s
> 	e.b=s
> 	return e;
> }
>
> ------------------------------------------java code-----------
> public class listelem {
>
> 	public int a;
> 	public int b;
> 	public listelem nextpoint;
>
> 	public void setelem(int s){
> 		a=s;
> 		b=s;
> 	}
> }
>
>
> public class pointlist {
>
> 	public listelem first;		//the first element of the list
> 	public listelem last;		//the last element of the list
>
>
> //initialisation of the first element
> 	pointlist(int k){
> 		first = new listelem();
> 		first.a = k;
> 		first.b = k;
> 		last = first;
> 	}
>
>
> 	public static pointlist setpoint(int[] seq){
>
> 		pointlist v;		//return linked list
> 		int length;			//length of the argument vector seq
>
> 		v = new pointlist(seq[0]);
> 		length = seq.length;
>
> //filling out of the list
> 		for(int i=1; i<length; i++){
> 			listelem newelem = new listelem();
> 			newelem.setelem(seq[i]);
>
> 			v.last.nextpoint = newelem;
> 			v.last = v.last.nextpoint;
> 		}
> 		return v;
> 	}
> -----------------------------------------------------------
>
> Now let us turn to our attempts to implement this in Mata.
> Basically we have defined the structure "pointlist" that implements a "linked
> list" of points (a,b) and a function "setpoint" that fills out this list with
> the values of the argument vector "seq". Below is how far we got. We have
> indicated the line that causes trouble with a comment.
>
> ------------------------------------------------xyz.mata---
> ...
> struct pointlist {
> 	struct listelem scalar first
> 	pointer(struct listelem scalar) scalar last
> }
>
>
> struct pointlist scalar function setpoint( real vector seq) {
>
> 	struct pointlist scalar v
> 	real scalar i
> 	real scalar length
>
> 		length = length(seq)
> 		v.first.a = seq[1]
> 		v.first.b = seq[1]
> 		v.last = &v.first
> 		liststruct(v)
>
>
> 	for (i=2; i<=length; i++) {
>
> 		struct listelem m
> 		m= setelem(seq[i])
> 		v.last.nextpoint= m        // <- See error message below
> 		v.last = &last.nextpoint
> 	}
> 	return(v)
> }
> ...
> -----------------------------------------------------------
>
> Compiling this code we receive the following error message at the indicated
> line:
>
> ------------------------------------------------------------
> type mismatch:  exp.exp:  pointer found where struct expected
> -------------------------------------------------------------
>
> We somehow stick with this error message. Our main problem, however, concerns
> the using of pointers. The pointer "last" should move along the list and
> always point to the last element of the list (it means, it should allow to
> set a new point at the end of the list). It does not work. How can we add a
> new element at the end of the list?
>
> Many regards
>
> Uli and Magdalena
>
> --
> kohler@wz-berlin.de, luniak@wz-berlin.de
> +49 (030) 25491-361
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```

 © Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index