bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question on the behavior of length()


From: Andrew J. Schorr
Subject: Re: Question on the behavior of length()
Date: Mon, 11 Dec 2023 08:25:23 -0500
User-agent: Mutt/1.5.21 (2010-09-15)

Hi,

Internally, associative arrays are stored in different ways depending on the
nature of the indices. There are currently 3 array implementations: string
arrays, integer arrays, and arays with (mostly) consecutive non-negative integer
indices.

Regards,
Andy

On Mon, Dec 11, 2023 at 02:06:38PM +0100, Wolfgang Laun wrote:
> That's interesting; I must have missed this in the documentation. - What
> causes the switch from fast to slow? Would an index such as ToS = -42
> result in a speed reduction? What happens if a string as index is
> introduced late in the game? Will all accesses to integer keys be slowed
> down?
> 
> Also, you wrote "makes *the script* run about 3 times slower". Is it true
> that these accesses to a single array affect the entire script?
> 
> -W
> 
> On Mon, 11 Dec 2023 at 13:55, Ed Morton <mortoneccc@comcast.net> wrote:
> 
> > Using a string `"tos"` instead of a number `0` for the top of stack index
> > makes the script run about 3 times slower. You can always do something like
> > `Tos=0` and `x[Tos]` if you like.
> >
> >      Ed.
> >
> > On 12/11/2023 6:45 AM, Wolfgang Laun wrote:
> >
> > Call it x["tos"] and x["doc"] to avoid me being confused and having to
> > remember what is what.
> > -W
> >
> > On Mon, 11 Dec 2023 at 13:38, Ed Morton <mortoneccc@comcast.net> wrote:
> >
> >> Just a suggestion regarding calling `length()` for push()/pop() stack
> >> operations:
> >>
> >> On 12/10/2023 8:18 AM, Christian Schmidt wrote:
> >> > Hi all,
> >> >
> >> > First of all I'd like to state that I do not consider the following
> >> > necessarily a bug; however I'd like to discuss the current
> >> > implementation, and have not found a better place to do so.
> >> >
> >> > My issue is that I can't use
> >> >
> >> > x[length(x)] = y
> >> >
> >> > e.g. to emulate to push to a stack, without explicitly converting x
> >> > into an array first, e.g. by "delete x".
> >>
> >> No need to call `length(x)` for every push()/pop(), you can implement a
> >> stack by storing it's top index in `x[0]` (or some other unused,
> >> probably negative, index instead if you prefer):
> >>
> >>       function push(x,y) { x[++x[0]] = y }
> >>       function pop(x) { delete x[x[0]--] }
> >>
> >> As well as not having the problem you described that also allows you to
> >> use other unused indices to store other attributes about the stack, e.g.
> >> a description like `x[-1] = "this is a stack of connection requests"`,
> >> which you couldn't do if you were relying on `length(x)` to tell you the
> >> index of the top of the stack.
> >>
> >> Regards,
> >>
> >>      Ed.
> >>
> >
> >
> > --
> > Wolfgang Laun
> >
> >
> >
> 
> -- 
> Wolfgang Laun

-- 
Andrew Schorr                      e-mail: aschorr@telemetry-investments.com
Telemetry Investments, L.L.C.      phone:  917-305-1748
152 W 36th St, #402                fax:    212-425-5550
New York, NY 10018-8765



reply via email to

[Prev in Thread] Current Thread [Next in Thread]