For Programmers: Free Programming Magazines  


Home > Archive > PostScript > July 2005 > Null object = Zero









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Null object = Zero
hoffmann@fho-emden.de

2005-07-26, 5:03 pm

What's the fastest method for initializing a new
array not by null objects but by numerical zeros ?

Best regards --Gernot Hoffmann

Aandi Inston

2005-07-26, 5:03 pm

hoffmann@fho-emden.de wrote:

>What's the fastest method for initializing a new
>array not by null objects but by numerical zeros ?


A simple general method is

[ exch { 0 } repeat ]

but arguably

[ exch 2 idiv { 0 0 } repeat ]

is going to be quicker for large even values; etc.
----------------------------------------
Aandi Inston quite@dial.pipex.com http://www.quite.com
Please support usenet! Post replies and follow-ups, don't e-mail them.

hoffmann@fho-emden.de

2005-07-27, 4:03 am

Thanks.

It's obviously impossible to initialize an array by zeros
without a loop.

Best regards --Gernot Hoffmann

Aandi Inston

2005-07-27, 4:03 am

hoffmann@fho-emden.de wrote:

>Thanks.
>
>It's obviously impossible to initialize an array by zeros
>without a loop.


Au contraire.

[ 0 0 0 0 0 0 0 ]

does it nicely. (Perhaps you need to express this in a more
restricted way).
----------------------------------------
Aandi Inston quite@dial.pipex.com http://www.quite.com
Please support usenet! Post replies and follow-ups, don't e-mail them.

hoffmann@fho-emden.de

2005-07-27, 4:03 am

Yes, but my arrays have 1001 elements each ..

Best regards --Gernot Hoffmann

Aandi Inston

2005-07-27, 4:03 am

hoffmann@fho-emden.de wrote:

>Yes, but my arrays have 1001 elements each ..


Still not impossible to type 2000+ characters. It might also be the
quickest way, depending on the relative communication, parsing and
interpreting speed.
----------------------------------------
Aandi Inston quite@dial.pipex.com http://www.quite.com
Please support usenet! Post replies and follow-ups, don't e-mail them.

David Kastrup

2005-07-27, 9:03 am

hoffmann@fho-emden.de writes:

> Yes, but my arrays have 1001 elements each ..


[ 0 0 0 0 4 copy 8 copy 16 copy 32 copy 64 copy 128 copy
256 copy 489 copy ]

--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
bugbear

2005-07-27, 9:04 am

David Kastrup wrote:
> hoffmann@fho-emden.de writes:
>
>
>
>
> [ 0 0 0 0 4 copy 8 copy 16 copy 32 copy 64 copy 128 copy
> 256 copy 489 copy ]


Ah - hello old friend!

I've used a similar trick to fill a byte array
with a pattern. My code used cpybuf() to ropeatedly double
up the initial pattern.

BugBear
Aandi Inston

2005-07-27, 9:04 am

David Kastrup <dak@gnu.org> wrote:

>hoffmann@fho-emden.de writes:
>
>
>[ 0 0 0 0 4 copy 8 copy 16 copy 32 copy 64 copy 128 copy
>256 copy 489 copy ]


I knew there had to be a quicker way than mine...
----------------------------------------
Aandi Inston quite@dial.pipex.com http://www.quite.com
Please support usenet! Post replies and follow-ups, don't e-mail them.

David Kastrup

2005-07-27, 9:04 am

quite@dial.pipex.con (Aandi Inston) writes:

> David Kastrup <dak@gnu.org> wrote:
>
>
> I knew there had to be a quicker way than mine...


I'd be careful about that until after benchmarking.

--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
François Robert

2005-07-27, 9:04 am

David Kastrup <dak@gnu.org> wrote in news:85ek9kinxc.fsf@lola.goethe.zz:

> hoffmann@fho-emden.de writes:
>
>
> [ 0 0 0 0 4 copy 8 copy 16 copy 32 copy 64 copy 128 copy
> 256 copy 489 copy ]
>

More variation on that theme :

Many other decomposition exists. For instance :
[ 0 0 0 0 4 copy 8 copy 16 copy 31 copy 62 copy 125 copy 250 copy 500
copy dup ]

Note that 1001 = 7 * 11 * 13, which may yield a construct that takes
advantage of that.

Stack space usage can be minimized too, by using "get/putinterval",
1001 array
dup [0 0 0 0] exch copy
2 copy 4 exch putinterval pop
dup 0 8 getinterval
2 copy 8 exch putinterval pop
dup 0 16 getinterval
2 copy 16 exch putinterval pop
dup 0 31 getinterval
2 copy 32 exch putinterval pop
dup 0 62 getinterval
2 copy 63 exch putinterval pop
dup 0 125 getinterval
2 copy 125 exch putinterval pop
dup 0 250 getinterval
2 copy 250 exch putinterval pop
dup 0 500 getinterval
2 copy 500 exch putinterval pop
dup 1000 0 put


A generic version for any array length (with loop obviously):

%!PS

% <A> <any> InitArray <array>
% ---------------------------
% Initialize all elements of an array <A> of N elements with
% N identical copies of <any>. If <any> is composite, the
% copies are shared.
% Empty array (N == 0) are properly handled
% The max stack depth usage does not depend on N.
% The number of copy operations is ceiling(log2(N)) (I think)

/InitArray
{
exch dup length 0 eq % <any> <A> len(A)==0 ?
{ exch pop % <A>
}{
dup dup 0 5 4 roll % <A> <A> <A> 0 <any>
% copy into first index if exists...
put 0 1 getinterval % <A> <sub>
1 exch % <A> len(sub) <sub>
{
1 index 1 bitshift % <A> len(sub) <sub> 2*len(sub)
3 index length gt % <A> len(sub) <sub> 2*len(sub)>len(A) ?
{
% Adjust last self-copy to remaining length
pop exch dup 2 copy % len(sub) <A> <A> <A> <A>
length 4 index sub % len(sub) <A> <A> <A> len(A)-len(sub)
0 exch getinterval % len(sub) <A> <A> <sub">
4 3 roll exch % <A> <A> len(sub) <sub">
putinterval % <A>
exit
} if
% self-copy exponentially bigger chunks, until it no
% longer fits the array.
3 copy putinterval % <A> len(sub) <sub>
pop 1 bitshift % <A> 2*len(sub)
2 copy 0 exch % <A> len(sub') <A> 0 len(sub')
getinterval % <A> len(sub') <sub'>
} loop
} ifelse % empty array ?
} bind def

% An array of 1001 zeros
1001 array 0 InitArray ==

% Even works with strings, it turns out !
123 string 64 InitArray ==

%%EOF
________________________________________
_______________
François Robert
(to mail me, reverse character order in reply address)
hoffmann@fho-emden.de

2005-07-27, 5:06 pm

Thanks for all contributions.

I'm filling the first array by a simple loop.
Similar arrays may be filled by copy. That's perhaps
a good idea, but who knows ?

Maybe I'm wasting some milliseconds but that doesn't
matter - the code is short, half a line (if a line
can be 'half').
And the execution of the whole EPS needs anyway about
one second.

I thought there might be somewhere a straight PS
initialization by true numerical zeros.

Best regards --Gernot Hoffmann

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com