Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua's hybrid array and hash table; does it exist anywhere else?

Lua's implementation of tables keep its elements in two parts: an array part and a hash part.

Does such a thing exist in any other languages?

Take a look at section 4, Tables, in The Implementation of Lua 5.0.

Lua 5.1 Source Code - table.c

like image 459
Rudiger Avatar asked Jan 24 '10 02:01

Rudiger


People also ask

What are the data structures available in Lua?

Tables are the only data structure available in Lua that helps us create different types like arrays and dictionaries. Lua uses associative arrays and which can be indexed with not only numbers but also with strings except nil. Tables have no fixed size and can grow based on our need.

What is an array in Lua?

Lua - Arrays. Arrays are ordered arrangement of objects, which may be a one-dimensional array containing a collection of rows or a multi-dimensional array containing multiple rows and columns. In Lua, arrays are implemented using indexing tables with integers.

How do you create a table in Lua?

The table data structure used in programming to create an array and dictionary. In Lua the table is created by {} as table = {}, which create an empty table or with elements to create non-empty table. After creating a table an element can be add as key-value pair as table1 [key]= “value”.

What is the difference between one-dimensional and multi-dimensional array in Lua?

If we have created a one-dimensional array then it will contain several rows inside it, on the other hand on the creation of a multi-dimensional array will contain multiple columns and rows. Array in Lua uses indexing table, size of the array in Lua is not fixed can be changed or grow at runtime or based on the requirement.


1 Answers

This idea was original with Roberto Ierusalimschy and the rest of the Lua team. I heard Roberto give a talk about it at the MIT Lightweight Languages workshop in 2003, and in this talk he discussed prior work and argued convincingly that the idea was new. I don't know if other languages have copied it since.

The original Awk has a somewhat more restricted language model than Lua; either a number or a string can be used as a key in an array, but arrays themselves are not first-class values: an array must have a name, and an array cannot be used as a key in the array.

Regarding the implementation, I have checked the sources for the original Awk as maintained by Brian Kernighan, and the implementation of Awk uses a hash table, not Lua's hybrid array/table structure. The distinction is important because in Lua, when a table is used with consecutive integer keys, the space overhead is the same as for a C array. This is not true for original Awk.

I have not bothered to investigate all later implementations of awk, e.g., Gnu Awk, mawk, and so on.

like image 107
Norman Ramsey Avatar answered Oct 25 '22 04:10

Norman Ramsey