[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A very basic thing I don't get
- From: steve donovan <steve.j.donovan@...>
- Date: Sun, 2 Oct 2011 15:32:44 +0200
On Sun, Oct 2, 2011 at 9:58 AM, Philippe Lhoste <PhiLho@gmx.net> wrote:
> Pure linear arrays in Lua are implemented with just a linear space in
> memory.
It's a bit like the famous wave/particle duality of quantum mechanics;
is an electron a particle (defined position, uncertain momentum) or a
wave (defined momentum, uncertain position)? The answer of course is
neither.
A common conceptual mistake with Lua tables is to see them as arrays
or hash maps. They have limits where they behave like one or the
other, sure, but thinking of them as _either_ a linear O(1) structure
_or_ a O(N log N) map is a mistake. In practice they are implemented
as having a hash-part and an array-part, but this is an implementation
detail and it isn't always obvious in what part a key/value is kept.
One of the casualties of this flexibility is the length operator #
which is only useful for tables used strictly as arrays.
steve d.