Skip to content

Commit 5d708c3

Browse files
committed
Explanation of borders in the manual
The explanation includes the limit case of maxinteger being a border. It also avoids the term "natural", which might include large floats with natural values.
1 parent 8dd2c91 commit 5d708c3

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

manual/manual.of

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,15 +1980,20 @@ character is one byte.)
19801980

19811981
The length operator applied on a table
19821982
returns a @x{border} in that table.
1983-
A @def{border} in a table @id{t} is any natural number
1983+
A @def{border} in a table @id{t} is any non-negative integer
19841984
that satisfies the following condition:
19851985
@verbatim{
1986-
(border == 0 or t[border] ~= nil) and t[border + 1] == nil
1986+
(border == 0 or t[border] ~= nil) and
1987+
(t[border + 1] == nil or border == math.maxinteger)
19871988
}
19881989
In words,
1989-
a border is any (natural) index present in the table
1990-
that is followed by an absent index
1991-
(or zero, when index 1 is absent).
1990+
a border is any positive integer index present in the table
1991+
that is followed by an absent index,
1992+
plus two limit cases:
1993+
zero, when index 1 is absent;
1994+
and the maximum value for an integer, when that index is present.
1995+
Note that keys that are not positive integers
1996+
do not interfere with borders.
19921997

19931998
A table with exactly one border is called a @def{sequence}.
19941999
For instance, the table @T{{10, 20, 30, 40, 50}} is a sequence,
@@ -1997,12 +2002,9 @@ The table @T{{10, 20, 30, nil, 50}} has two borders (3 and 5),
19972002
and therefore it is not a sequence.
19982003
(The @nil at index 4 is called a @emphx{hole}.)
19992004
The table @T{{nil, 20, 30, nil, nil, 60, nil}}
2000-
has three borders (0, 3, and 6) and three holes
2001-
(at indices 1, 4, and 5),
2005+
has three borders (0, 3, and 6),
20022006
so it is not a sequence, too.
20032007
The table @T{{}} is a sequence with border 0.
2004-
Note that non-natural keys do not interfere
2005-
with whether a table is a sequence.
20062008

20072009
When @id{t} is a sequence,
20082010
@T{#t} returns its only border,
@@ -2016,7 +2018,7 @@ the memory addresses of its non-numeric keys.)
20162018

20172019
The computation of the length of a table
20182020
has a guaranteed worst time of @M{O(log n)},
2019-
where @M{n} is the largest natural key in the table.
2021+
where @M{n} is the largest integer key in the table.
20202022

20212023
A program can modify the behavior of the length operator for
20222024
any value but strings through the @idx{__len} metamethod @see{metatable}.

0 commit comments

Comments
 (0)