Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.postscript > #1423 > unrolled thread

What is the best way to work with a dictionary

Started byCecil Westerhof <Cecil@decebal.nl>
First post2013-07-13 22:13 +0200
Last post2013-07-15 06:27 -0700
Articles 9 — 4 participants

Back to article view | Back to comp.lang.postscript


Contents

  What is the best way to work with a dictionary Cecil Westerhof <Cecil@decebal.nl> - 2013-07-13 22:13 +0200
    Re: What is the best way to work with a dictionary Helge Blischke <h.blischke@acm.org> - 2013-07-13 22:32 +0200
      Re: What is the best way to work with a dictionary Cecil Westerhof <Cecil@decebal.nl> - 2013-07-13 23:25 +0200
        Re: What is the best way to work with a dictionary luser- -droog <mijoryx@yahoo.com> - 2013-07-13 17:04 -0700
          Re: What is the best way to work with a dictionary Cecil Westerhof <Cecil@decebal.nl> - 2013-07-14 15:11 +0200
            Re: What is the best way to work with a dictionary luser- -droog <mijoryx@yahoo.com> - 2013-07-14 08:04 -0700
              Re: What is the best way to work with a dictionary Cecil Westerhof <Cecil@decebal.nl> - 2013-07-14 20:04 +0200
                Re: What is the best way to work with a dictionary Olle <olavi.sakari@gmail.com> - 2013-07-15 06:13 -0700
                  Re: What is the best way to work with a dictionary Olle <olavi.sakari@gmail.com> - 2013-07-15 06:27 -0700

#1423 — What is the best way to work with a dictionary

FromCecil Westerhof <Cecil@decebal.nl>
Date2013-07-13 22:13 +0200
SubjectWhat is the best way to work with a dictionary
Message-ID<87zjtqdxrl.fsf@Aspire.decebal.nl>
Until now I wrote functions like:
    /ellipse { % x y unitX unitY colour
        5 dict begin
            /colour  exch def
            /unitY   exch def
            /unitX   exch def
            /y       exch def
            /x       exch def
    
            gsave
            newpath
            colour  setrgbcolor
            x  y  translate
            unitX  unitY  scale
            1  1  1  0  360  arc closepath fill
            stroke
            grestore
        end
    } def

I just saw in ‘PostScript Drawing: An Economist's Guide’ that it also
should be possible to do it in the following way:
    /ellipse { % x y unitX unitY colour
        <<
            /colour  exch def
            /unitY   exch def
            /unitX   exch def
            /y       exch def
            /x       exch def
        >>  begin
            gsave
            newpath
            colour  setrgbcolor
            x  y  translate
            unitX  unitY  scale
            1  1  1  0  360  arc closepath fill
            stroke
            grestore
        end
    } def

Which of the two is the better one and why? I think I like the second
one more.
The only problem is that it does not work. I get:
    Error: /unmatchedmark in -->>--

I am using:
    GPL Ghostscript 9.06

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [next] | [standalone]


#1424

FromHelge Blischke <h.blischke@acm.org>
Date2013-07-13 22:32 +0200
Message-ID<b4drrlFju4mU1@mid.individual.net>
In reply to#1423
Cecil Westerhof wrote:

> Until now I wrote functions like:
>     /ellipse { % x y unitX unitY colour
>         5 dict begin
>             /colour  exch def
>             /unitY   exch def
>             /unitX   exch def
>             /y       exch def
>             /x       exch def
>     
>             gsave
>             newpath
>             colour  setrgbcolor
>             x  y  translate
>             unitX  unitY  scale
>             1  1  1  0  360  arc closepath fill
>             stroke
>             grestore
>         end
>     } def
> 
> I just saw in ‘PostScript Drawing: An Economist's Guide’ that it also
> should be possible to do it in the following way:
>     /ellipse { % x y unitX unitY colour
>         <<
>             /colour  exch 
% operand stack at this point:
% x y unitX unitY color /color mark
>		             def
>             /unitY   exch def
>             /unitX   exch def
>             /y       exch def
>             /x       exch def
>         >>  begin
>             gsave
>             newpath
>             colour  setrgbcolor
>             x  y  translate
>             unitX  unitY  scale
>             1  1  1  0  360  arc closepath fill
>             stroke
>             grestore
>         end
>     } def
> 
> Which of the two is the better one and why? I think I like the second
> one more.
> The only problem is that it does not work. I get:
>     Error: /unmatchedmark in -->>--
> 
> I am using:
>     GPL Ghostscript 9.06
> 

You see that the name /color gets associated with the object mark, i. e. the
--<<--, which is equivalent to --mark--, gets consumed and therefore is 
missing at the time -->>-- is executed.

Think about it.

Helge

[toc] | [prev] | [next] | [standalone]


#1425

FromCecil Westerhof <Cecil@decebal.nl>
Date2013-07-13 23:25 +0200
Message-ID<87vc4edufk.fsf@Aspire.decebal.nl>
In reply to#1424
Op zaterdag 13 jul 2013 22:32 CEST schreef Helge Blischke:

> Cecil Westerhof wrote:
>
>> Until now I wrote functions like:
>> /ellipse { % x y unitX unitY colour
>> 5 dict begin
>> /colour  exch def
>> /unitY   exch def
>> /unitX   exch def
>> /y       exch def
>> /x       exch def
>>
>> gsave
>> newpath
>> colour  setrgbcolor
>> x  y  translate
>> unitX  unitY  scale
>> 1  1  1  0  360  arc closepath fill
>> stroke
>> grestore
>> end
>> } def
>>
>> I just saw in ‘PostScript Drawing: An Economist's Guide’ that it
>> also should be possible to do it in the following way: /ellipse { %
>> x y unitX unitY colour << /colour exch
> % operand stack at this point:
> % x y unitX unitY color /color mark
>> 		             def
>> /unitY   exch def
>> /unitX   exch def
>> /y       exch def
>> /x       exch def
>>>> begin
>> gsave
>> newpath
>> colour  setrgbcolor
>> x  y  translate
>> unitX  unitY  scale
>> 1  1  1  0  360  arc closepath fill
>> stroke
>> grestore
>> end
>> } def
>>
>> Which of the two is the better one and why? I think I like the
>> second one more. The only problem is that it does not work. I get:
>> Error: /unmatchedmark in -->>--
>>
>> I am using:
>> GPL Ghostscript 9.06
>>
>
> You see that the name /color gets associated with the object mark,
> i. e. the --<<--, which is equivalent to --mark--, gets consumed and
> therefore is missing at the time -->>-- is executed.
>
> Think about it.
>
> Helge

I had been looking a little further. The << … >> combination is used
when assigning constants. I could use a roll, but my parameter list
does not always has the same length. So I will keep working in my old
way.

By the way: I read somewhere that the value before dict does not
matter. Is that true, or not?

A nice effect with this function: ;-)
    0 .25 3 {
        /count exch def
    
        40  40 40 count mul add
        40  40 4 count sub mul
        {1 count 10 div sub  0  0}
        ellipse
    
        200 40 count mul add  40
        40 4 count sub mul  40
        {0 count 4 div add  dup dup}
        ellipse
    } for

I like the grey better as the red.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#1427

Fromluser- -droog <mijoryx@yahoo.com>
Date2013-07-13 17:04 -0700
Message-ID<0a7afe99-aaf2-474f-b03d-b5ac41a76b77@googlegroups.com>
In reply to#1425
On Saturday, July 13, 2013 4:25:03 PM UTC-5, Cecil Westerhof wrote:

> By the way: I read somewhere that the value before dict does not
> matter. Is that true, or not?

Since the Level-2 standard, dictionaries will auto-expand, so you
don't have to worry about /dictfull errors if the initial size is 
too small.

As to how much it "matters", my theory is explained here:

  http://stackoverflow.com/a/11354218/733077

[toc] | [prev] | [next] | [standalone]


#1430

FromCecil Westerhof <Cecil@decebal.nl>
Date2013-07-14 15:11 +0200
Message-ID<87fvvhe15z.fsf@Aspire.decebal.nl>
In reply to#1427
Op zondag 14 jul 2013 02:04 CEST schreef luser:

> On Saturday, July 13, 2013 4:25:03 PM UTC-5, Cecil Westerhof wrote:
>
>> By the way: I read somewhere that the value before dict does not
>> matter. Is that true, or not?
>
> Since the Level-2 standard, dictionaries will auto-expand, so you
> don't have to worry about /dictfull errors if the initial size is 
> too small.

That means that when I use local variables, I can not change global
variables?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#1432

Fromluser- -droog <mijoryx@yahoo.com>
Date2013-07-14 08:04 -0700
Message-ID<8ab668f6-20b3-48a0-99f6-9dd14cf4b35d@googlegroups.com>
In reply to#1430
On Sunday, July 14, 2013 8:11:52 AM UTC-5, Cecil Westerhof wrote:
> Op zondag 14 jul 2013 02:04 CEST schreef luser:
> 
> 
> > On Saturday, July 13, 2013 4:25:03 PM UTC-5, Cecil Westerhof wrote:
> >
> >> By the way: I read somewhere that the value before dict does not
> >> matter. Is that true, or not?
> >
> > Since the Level-2 standard, dictionaries will auto-expand, so you
> > don't have to worry about /dictfull errors if the initial size is 
> > too small.
> 
> 
> That means that when I use local variables, I can not change global
> variables?
> 

I think I misinterpreted the question. I thought you were talking about
the 'int' in `int dict`, the value you use to create a dict of a certain
size.

For a dictionary that is "local" to a procedure, you can bypass it
to access dictionaries lower on the dictstack. 

/n 5 def

/x {
    << /n 2 >> begin
        ...
        currentdict end /n 3 def begin  % change "global" n
        ...
    emd
} def

/y {
    << /lo 3 >> begin
        /n 27 store   % redefine "global" n 
    end
} def

[toc] | [prev] | [next] | [standalone]


#1435

FromCecil Westerhof <Cecil@decebal.nl>
Date2013-07-14 20:04 +0200
Message-ID<8761wddnm0.fsf@Aspire.decebal.nl>
In reply to#1432
Op zondag 14 jul 2013 17:04 CEST schreef luser:

> On Sunday, July 14, 2013 8:11:52 AM UTC-5, Cecil Westerhof wrote:
>> Op zondag 14 jul 2013 02:04 CEST schreef luser:
>>
>>
>>> On Saturday, July 13, 2013 4:25:03 PM UTC-5, Cecil Westerhof wrote:
>>>
>>>> By the way: I read somewhere that the value before dict does not
>>>> matter. Is that true, or not?
>>>
>>> Since the Level-2 standard, dictionaries will auto-expand, so you
>>> don't have to worry about /dictfull errors if the initial size is 
>>> too small.
>>
>>
>> That means that when I use local variables, I can not change global
>> variables?
>>
>
> I think I misinterpreted the question. I thought you were talking
> about the 'int' in `int dict`, the value you use to create a dict of
> a certain size.

No, that is what I meant.


> For a dictionary that is "local" to a procedure, you can bypass it
> to access dictionaries lower on the dictstack. 
>
> /n 5 def
>
> /x {
> << /n 2 >> begin
> ...
> currentdict end /n 3 def begin  % change "global" n
> ...
> emd
> } def
>
> /y {
> << /lo 3 >> begin
> /n 27 store   % redefine "global" n 
> end
> } def

This is what I meant in a way. But because I often fill the values
from the stack I would then need to do something like this I think:
    % lo  y  -
    / y {
        << /lo -1 >> begin
        /lo exch def
        .
        .
        .
    }

Or can this be done more efficient?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#1442

FromOlle <olavi.sakari@gmail.com>
Date2013-07-15 06:13 -0700
Message-ID<c5b21d7a-20b6-4619-b4c8-dc5d69b651cd@googlegroups.com>
In reply to#1435
On Sunday, July 14, 2013 9:04:39 PM UTC+3, Cecil Westerhof wrote:
> Op zondag 14 jul 2013 17:04 CEST schreef luser:
> 
> 
> 
> > On Sunday, July 14, 2013 8:11:52 AM UTC-5, Cecil Westerhof wrote:
> 
> >> Op zondag 14 jul 2013 02:04 CEST schreef luser:
> 
> >>
> 
> >>
> 
> >>> On Saturday, July 13, 2013 4:25:03 PM UTC-5, Cecil Westerhof wrote:
> 
> >>>
> 
> >>>> By the way: I read somewhere that the value before dict does not
> 
> >>>> matter. Is that true, or not?
> 
> >>>
> 
> >>> Since the Level-2 standard, dictionaries will auto-expand, so you
> 
> >>> don't have to worry about /dictfull errors if the initial size is 
> 
> >>> too small.
> 
> >>
> 
> >>
> 
> >> That means that when I use local variables, I can not change global
> 
> >> variables?
> 
> >>
> 
> >
> 
> > I think I misinterpreted the question. I thought you were talking
> 
> > about the 'int' in `int dict`, the value you use to create a dict of
> 
> > a certain size.
> 
> 
> 
> No, that is what I meant.
> 
> 
> 
> 
> 
> > For a dictionary that is "local" to a procedure, you can bypass it
> 
> > to access dictionaries lower on the dictstack. 
> 
> >
> 
> > /n 5 def
> 
> >
> 
> > /x {
> 
> > << /n 2 >> begin
> 
> > ...
> 
> > currentdict end /n 3 def begin  % change "global" n
> 
> > ...
> 
> > emd
> 
> > } def
> 
> >
> 
> > /y {
> 
> > << /lo 3 >> begin
> 
> > /n 27 store   % redefine "global" n 
> 
> > end
> 
> > } def
> 
> 
> 
> This is what I meant in a way. But because I often fill the values
> 
> from the stack I would then need to do something like this I think:
> 
>     % lo  y  -
> 
>     / y {
> 
>         << /lo -1 >> begin
> 
>         /lo exch def
> 
>         .
> 
>         .
> 
>         .
> 
>     }
> 
> 
> 
> Or can this be done more efficient?
> 
> 
> 
> -- 
> 
> Cecil Westerhof
> 
> Senior Software Engineer
> 
> LinkedIn: http://www.linkedin.com/in/cecilwesterhof

If you use the dictionary to save arguments locally to variables, I think your

5 dict begin
...
end

is the best approach.
You don't gain anything by using something like "<< /lo -1 >> begin" here.
Even your first trial, corrected, looks awkward to me, especially, when you make it current using "begin" after creating it:

<< 
  /colour  3 -1 roll
  /unitY   5 -1 roll
  /unitX   7 -1 roll
  /y       9 -1 roll
  /x      11 -1 roll
>>  begin

Thanks,

Olle

[toc] | [prev] | [next] | [standalone]


#1443

FromOlle <olavi.sakari@gmail.com>
Date2013-07-15 06:27 -0700
Message-ID<fb21d555-87fa-4781-95c3-b4e6ad527a30@googlegroups.com>
In reply to#1442
On Monday, July 15, 2013 4:13:59 PM UTC+3, Olle wrote:
> On Sunday, July 14, 2013 9:04:39 PM UTC+3, Cecil Westerhof wrote:
> 
> > Op zondag 14 jul 2013 17:04 CEST schreef luser:
> 
> > 
> 
> > 
> 
> > 
> 
> > > On Sunday, July 14, 2013 8:11:52 AM UTC-5, Cecil Westerhof wrote:
> 
> > 
> 
> > >> Op zondag 14 jul 2013 02:04 CEST schreef luser:
> 
> > 
> 
> > >>
> 
> > 
> 
> > >>
> 
> > 
> 
> > >>> On Saturday, July 13, 2013 4:25:03 PM UTC-5, Cecil Westerhof wrote:
> 
> > 
> 
> > >>>
> 
> > 
> 
> > >>>> By the way: I read somewhere that the value before dict does not
> 
> > 
> 
> > >>>> matter. Is that true, or not?
> 
> > 
> 
> > >>>
> 
> > 
> 
> > >>> Since the Level-2 standard, dictionaries will auto-expand, so you
> 
> > 
> 
> > >>> don't have to worry about /dictfull errors if the initial size is 
> 
> > 
> 
> > >>> too small.
> 
> > 
> 
> > >>
> 
> > 
> 
> > >>
> 
> > 
> 
> > >> That means that when I use local variables, I can not change global
> 
> > 
> 
> > >> variables?
> 
> > 
> 
> > >>
> 
> > 
> 
> > >
> 
> > 
> 
> > > I think I misinterpreted the question. I thought you were talking
> 
> > 
> 
> > > about the 'int' in `int dict`, the value you use to create a dict of
> 
> > 
> 
> > > a certain size.
> 
> > 
> 
> > 
> 
> > 
> 
> > No, that is what I meant.
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > > For a dictionary that is "local" to a procedure, you can bypass it
> 
> > 
> 
> > > to access dictionaries lower on the dictstack. 
> 
> > 
> 
> > >
> 
> > 
> 
> > > /n 5 def
> 
> > 
> 
> > >
> 
> > 
> 
> > > /x {
> 
> > 
> 
> > > << /n 2 >> begin
> 
> > 
> 
> > > ...
> 
> > 
> 
> > > currentdict end /n 3 def begin  % change "global" n
> 
> > 
> 
> > > ...
> 
> > 
> 
> > > emd
> 
> > 
> 
> > > } def
> 
> > 
> 
> > >
> 
> > 
> 
> > > /y {
> 
> > 
> 
> > > << /lo 3 >> begin
> 
> > 
> 
> > > /n 27 store   % redefine "global" n 
> 
> > 
> 
> > > end
> 
> > 
> 
> > > } def
> 
> > 
> 
> > 
> 
> > 
> 
> > This is what I meant in a way. But because I often fill the values
> 
> > 
> 
> > from the stack I would then need to do something like this I think:
> 
> > 
> 
> >     % lo  y  -
> 
> > 
> 
> >     / y {
> 
> > 
> 
> >         << /lo -1 >> begin
> 
> > 
> 
> >         /lo exch def
> 
> > 
> 
> >         .
> 
> > 
> 
> >         .
> 
> > 
> 
> >         .
> 
> > 
> 
> >     }
> 
> > 
> 
> > 
> 
> > 
> 
> > Or can this be done more efficient?
> 
> > 
> 
> > 
> 
> > 
> 
> > -- 
> 
> > 
> 
> > Cecil Westerhof
> 
> > 
> 
> > Senior Software Engineer
> 
> > 
> 
> > LinkedIn: http://www.linkedin.com/in/cecilwesterhof
> 
> 
> 
> If you use the dictionary to save arguments locally to variables, I think your
> 
> 
> 
> 5 dict begin
> 
> ...
> 
> end
> 
> 
> 
> is the best approach.
> 
> You don't gain anything by using something like "<< /lo -1 >> begin" here.
> 
> Even your first trial, corrected, looks awkward to me, especially, when you make it current using "begin" after creating it:
> 
> 
> 
> << 
> 
>   /colour  3 -1 roll
> 
>   /unitY   5 -1 roll
> 
>   /unitX   7 -1 roll
> 
>   /y       9 -1 roll
> 
>   /x      11 -1 roll
> 
> >>  begin
> 
> 
> 
> Thanks,
> 
> 
> 
> Olle

But you could also change your function to take a dictionary as the (only) argument and just make it current in the function. (You might even leave, say, unitY undefined (a circle?), and in case it is not known, make it the same as unitX.)

Thanks,

Olle

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.postscript


csiph-web