@@ -1980,15 +1980,20 @@ character is one byte.)
19801980
19811981The length operator applied on a table
19821982returns 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
19841984that 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}
19881989In 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
19931998A table with exactly one border is called a @def{sequence}.
19941999For 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),
19972002and therefore it is not a sequence.
19982003(The @nil at index 4 is called a @emphx{hole}.)
19992004The 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),
20022006so it is not a sequence, too.
20032007The 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
20072009When @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
20172019The computation of the length of a table
20182020has 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
20212023A program can modify the behavior of the length operator for
20222024any value but strings through the @idx{__len} metamethod @see{metatable}.
0 commit comments