home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!keele!nott-cs!piaggio!anw
- From: anw@maths.nott.ac.uk (Dr A. N. Walker)
- Newsgroups: comp.lang.misc
- Subject: Re: Pointers
- Message-ID: <1992Nov12.152032.1576@maths.nott.ac.uk>
- Date: 12 Nov 92 15:20:32 GMT
- References: <1992Nov7.115620.29967@syacus.acus.oz.au> <1992Nov10.024021.8724@linus.mitre.org> <92Nov10.125426est.47525@neat.cs.toronto.edu> <BxIoDv.72J@mentor.cc.purdue.edu>
- Reply-To: anw@maths.nott.ac.uk (Dr A. N. Walker)
- Organization: Maths Dept., Nott'm Univ., UK.
- Lines: 46
-
- In article <BxIoDv.72J@mentor.cc.purdue.edu> hrubin@pop.stat.purdue.edu
- (Herman Rubin) writes:
- [various uses of pointers to functions, to arrays of pointers, etc.,
- deleted]
-
- Indeed these things all have their uses, but they too are all
- in Algol, and have been for 24 years now. Two examples that may [or
- may not!] gladden Dr Rubin's heart:
-
- a) a self-seeding PRNG:
-
- PROC REAL myrand := REAL: (myseed; myrand := myprng);
-
- ["myrand" is a pointer to a procedure that delivers a real. It is
- initialised to point to a procedure that first does the seeding, then
- makes "myrand" point instead to the real PRNG. The result of following
- the pointer and then obeying the procedure is delivered as the first
- PRN. Uses of "myrand" after the first go straight to "myprng"; note
- that only one procedure is actually called, as "myrand" itself is a
- pointer, *not* a procedure.]
-
- b) a self-buffering geometric random-number generator:
-
- INT bufsize = 1024;
- [bufsize] REAL buffer; INT bufp;
- [bufsize] PROC REAL geombuff;
- PROC REAL geom := geombuff[bufsize] :=
- REAL: (fill buffer; geom := geombuff[1]; buffer[bufp := 1]);
- FOR i TO bufsize - 1
- DO geombuff[i] := REAL: (geom := geombuff[bufp +:= 1]; buffer[bufp])
- OD;
- PROC fill buffer = ( ... some code to generate the random numbers
- and fill buffer with them ...);
-
- ["geom" points to one of an array of (anonymous) procedures. All but
- one of these simply returns an element of "buffer", having updated
- "geom"; the last instead fills the buffer and reinitialises the
- various pointers. Here there is an implied context switch (but only
- one, except when the buffer is filled) each time "geom" is used, so
- we don't get the full benefits of the buffering unless a heavily-
- optimising compiler recognises that the procedure denotation inside
- the DO-loop can be in-lined.]
-
- --
- Andy Walker, Maths Dept., Nott'm Univ., UK.
- anw@maths.nott.ac.uk
-