Use and Analysis of tables in lua

Source: Internet
Author: User

Use and Analysis of tables in lua
1. table has four basic operations: read, write, iteration, and get length.
Lua does not delete the operation, but sets the value of the corresponding key to nil.
The internal definition of lua is in lobject. h

 
/*** Tables*/typedef union TKey {  struct {    TValuefields;    struct Node *next;  /* for chaining */  } nk;  TValue tvk;} TKey;typedef struct Node {  TValue i_val;  TKey i_key;} Node;


Typedef struct Table {CommonHeader; lu_byte flags;/* 1 <



We can see from the subsequent table acquisition length, read/write and iteration that the table Storage in lua is divided into two parts: an array and a hash table. The corresponding pointer and length are annotated in the code.

The basic operations c in table are implemented in ltable. c.


LUAI_FUNC Table * luaH_new (lua_State * L );

Create a new table to allocate memory and initialize the member variables.



LUAI_FUNC void luaH_free (lua_State * L, Table * t );

Release the memory. Release the hash table and then the array.


LUAI_FUNC void luaH_resizearray (lua_State * L, Table * t, int nasize)

When new memory needs to be opened up,


LUAI_FUNC void luaH_resize (lua_State * L, Table * t, int nasize, int nhsize );

Is the specific implementation of the above function.


LUAI_FUNC TValue * luaH_newkey (lua_State * L, Table * t, const TValue * key );

Insert a new key to the hash table in lua. The insert method first looks for the hash table. Is there a value for the created master location? If it is worthwhile, first, check whether the primary position of the value is on this Index. If yes, the new key is inserted and the next value is inserted in a hash. If it is not the main position, the newly inserted sword inserts this position, and the replaced value is placed in another position. If there is no value in this position, it is inserted directly.

LUAI_FUNC int luaH_getn (Table * t );

Obtain the length. First, find the array part in the table. If the array part has a value, return the length of the array part. If the array part has no value, take the length of the hash table part.


LUAI_FUNC int luaH_next (lua_State * L, Table * t, StkId key );

Lua uses this function to implement table recursion and finds the next key-value pair through the previous key. First, search for the array part. If no hash table part is found. First, use finxdindex to locate the location of the corresponding array part based on the input key, or store the index in the hash table part,

2. In addition to basic operations, lua also uses c to implement the direct table operation function for lua. In the lua official document, Table Manipulation writes: this database provides a general method for table operations. It provides all other methods in table. Remember, when an operation requires the length of a table, the table must be a queue or a _ len metadata method, all methods ignore providing non-numeric keys in the table as parameters. Because of the efficiency, all the table access operations through these methods are not processed (raw)

The storage function address Array

static const luaL_Reg tab_funcs[] = {  {"concat", tconcat},#if defined(LUA_COMPAT_MAXN)  {"maxn", maxn},#endif  {"insert", tinsert},  {"pack", pack},  {"unpack", unpack},  {"remove", tremove},  {"sort", sort},  {NULL, NULL}};



Maxn function, where luaH_next is used to find the maximum key value of all positive key values in the table. If no positive key element exists, 0 is returned, including the array and hash table parts.

The tinsert function inserts an element at a certain position in the table.

The tremove function removes an element and overwrites the previous element from the pos position through t [pos] = t [pos + 1.

All the elements in the table of the tconcat function are of the string type. They are connected to a string using the specified connector.

The pack function returns a new table that stores the corresponding values through arrays.

The unpack function returns the table element. This method is equivalent to return list [I], list [I + 1],..., list [j]. The default value is 1 to # table.

The sort function sorts the elements in the table. You can set the sorting rule by setting the comparator. If no sorting rule is given <号来代替。同c++中快速排序.


We can see that the table. getn function has been removed from lua5.2. This function is always unavailable for reading books and other people's blogs ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.