Function list:
Table. insert (table, [POs,] value)
Table. Remove (Table [, POS])
Table. Concat (Table [, SEP [, I [, J])
Table. Sort (Table [, comp])
1. Insert and remove can only be used for the insertion and removal of array elements. during insertion and removal, the following elements are aligned.
Therefore, when performing insert and remove operations in the for loop, pay attention to whether certain items are missing during insertion and removal:
Local T = {1, 2, 3, 5, 3, 6}
For I, V in ipairs (t) Do
If v = 3 then
Table. Remove (t, I)
End
End
-- Error. The fourth 3 is not removed. ipairs maintains a position for variable record traversal. After removing the third number 3, the next value returned by ipairs is 5 instead of 3.
Local T = {1, 2, 3, 5, 3, 6}
For I = 1, # t do
If t [I] = 3 then
Table. Remove (t, I)
I = I-1
End
End
-- Error, I = I-1 this code is useless, I value is always from 1 to # T, for the loop to modify the I value does not work
Local T = {1, 2, 3, 5, 3, 6}
For I = # T, 1,-1 do
If t [I] = 3 then
Table. Remove (t, I)
End
End
-- Correct. Traverse from the back
Local T = {1, 2, 3, 5, 3, 6}
Local I = 1
While T [I] Do
If t [I] = 3 then
Table. Remove (t, I)
Else
I = I + 1
End
End
-- Correct. Check whether the I value is increased.
2. Concat can splice the array part of the table into a string separated by seq.
The string storage method in Lua is different from that in C. Each string in Lua is a separate copy. Splicing two strings produces a new copy. If there are many concatenation operations, performance will be affected:
Local begintime = OS. Clock ()
Local STR = ""
For I = 1, 30000 do
STR = STR .. I
End
Local endtime = OS. Clock ()
Print (endtime-begintime)
-- Consumes 0.613 seconds and generates 30000 string copies, but only the last one is useful.
Local begintime = OS. Clock ()
Local T = {}
For I = 1, 30000 do
T [I] = I
End
Local STR = table. Concat (T ,"")
Local endtime = OS. Clock ()
Print (endtime-begintime)
-- It takes 0.024 seconds. Using Concat, strings are spliced at one time, and only one string is copied.
3. Sort can sort the elements in the table array. The comp function must be provided. If a is located before B, comp (a, B) returns true.
Note: If a = B, false must be returned:
Local function comp (A, B)
Return a <= B
End
Table. Sort (T, comp)
-- Error. An exception may occur: Attempt to compare number with nil
Local function comp (A, B)
If a = nil or B = nil then
Return false
End
Return a <= B
End
Table. Sort (T, comp)
-- Error. An exception may occur: Invalid Order Function for sorting
-- This exception may not be reported, but the result is incorrect;
The reason why a = B returns true is that table. sort does not perform boundary detection during fast sorting:
For (;;){
While (lua_rawgeti (L, 1, ++ I), sort_comp (L,-1,-2) {// undetected boundary, I will always increase
If (I> = u) lual_error (L, "invalid Order Function for sorting ");
Lua_pop (L, 1 );
}
While (lua_rawgeti (L, 1, -- J), sort_comp (L,-3,-1) {// undetected boundary, J will be reduced all the time
If (j <= L) lual_error (L, "invalid Order Function for sorting ");
Lua_pop (L, 1 );
}
If (j <I ){
Lua_pop (L, 3 );
Break;
}
Set2 (L, I, j );
}
Based on the above Code, if true is returned when a = B and several values on the boundary are equal, sort_comp will not be able to prevent I from continuing to grow, until the boundary is exceeded, the exception attempt to compare number with nil is thrown. Even if we make a non-null judgment on a and B, the exception invalid Order Function for sorting is thrown because I exceeds the boundary.
What is quick sorting and how to implement quick sorting in Lua? refer to the description in the Lua source code, which is not described here;
Lua table Library