lua-users home
lua-l archive

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

Declaring Sorted Tables Constructors:
A sorted table constructor would be declared via a keyword prepending the table constructor.  if no keyword is present, the table is assumed to be a normal hashed table with no ordering.
1. Implementation alternative where a red/black tree is simply appended to a normal table.  (see previous post).
The table constructor would be prepended with the optional keyword "sorted" to indicate that the table was sorted.
table = sorted { "fred" = "second", "abe" = "first" }
foreach(table, print)
Would always return
"abe" "first"
"fred" "second"
2. Implementation alternative where table format could be set to "sorted", "hashed" or "both".
The table constructor would be prepended with the optional keyword "sortmode" followed by a string constant indicating the desired sort mode.
table = sortmode "hashed" { "fred" = "first", "abe" = "second" }
Would produce a hashed table where fred could be first (depending on it's hash value), and abe could be second.
table = sortmode "sorted" { "fred" = "second", "abe" = "first" }
Would produce a red/black tree based sorted table where "abe" would always be first.  No hash table would be created.
table = sortmode "both" { "fred" = "second", "abe" = "first" }
A table with both a red/black tree and a hash table would be constructed.  Elements in the table could be accessed randomly via the hash table, and inserted, deleted, and sequentially traversed in sorted order via the red/black tree.
Ordering the Sorted Table:
Table ordering would use the same semantic definition as comparisons between "string" and "number" variables.