AKever

导航

< 2025年6月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 1 2 3 4 5
6 7 8 9 10 11 12

Lua Array Matrice List set bag

1.Lua Array

For instance, after the following code, any attempt to access a field outside the range 1–1000 will return nil, instead of zero

a = {} -- new array
for i = 1, 1000 do
a[i] = 0
end

The length operator (‘#’) uses this fact to find the size of an array:

print(#a) --> 1000

You can start an array at index 0, 1, or any other value:
-- creates an array with indices from -5 to 5

a = {}
for i = -5, 5 do
a[i] = 0
end

2.Matrices and Multi-Dimensional Arrays

There are two main ways to represent matrices in Lua

1.The first one is to use an array of arrays, that is, a table wherein each element is another table.

   For instance, you can create a matrix of zeros with dimensions N by M with the following code

mt = {} -- create the matrix
for i = 1, N do
  mt[i] = {} -- create a new row
  for j = 1, M do
    mt[i][j] = 0
  end
end

 this is certainly more verbose, but it gives you more flexibility.

2. The second way to represent a matrix in Lua is by composing the two indices into a single one.

mt = {} -- create the matrix
for i = 1, N do
  for j = 1, M do
    mt[(i - 1)*M + j] = 0
  end
end

3.Linked Lists

 Because tables are dynamic entities, it is easy to implement linked lists in Lua.

 For instance, let us implement a basic list, where each node has two fields, next and value. A simple variable is the list root:

复制代码
list = nil
--To insert an element at the beginning of the list, with a value v, we do: list = {next = list, value = v}
--To traverse the list, we write: local l = list while l do   <visit l.value>   l = l.next end
复制代码

 4.Queues and Double Queues

  1.A simple way to implement queues in Lua is with functions insert and remove from the table library.

function ListNew ()
  return {first = 0, last = -1}
end

To avoid polluting the global space, we will define all list operations inside a table, properly called List (that is, we will create a module). Therefore, we rewrite our last example like this:

List = {}
function List.new ()
    return {first = 0, last = -1}
end

Now, we can insert or remove an element at both ends in constant time:

复制代码
function List.pushfirst (list, value)
  local first = list.first - 1
  list.first = first
  list[first] = value
end
function List.pushlast (list, value)
  local last = list.last + 1
  list.last = last
  list[last] = value
end
function List.popfirst (list)
    local first = list.first
    if first > list.last then 
        error("list is empty") 
    end
    local value = list[first]
    list[first] = nil -- to allow garbage collection
    list.first = first + 1
    return value
end

function List.poplast (list)
    local last = list.last
    if list.first > last then 
        error("list is empty") 
    end
    local value = list[last]
    list[last] = nil -- to allow garbage collection
    list.last = last - 1
    return value
end        
复制代码

5.Sets and Bags

In Lua, an efficient and simple way to represent such sets is to put the set elements as indices in a table.

复制代码
reserved = {
["while"] = true, ["end"] = true,
["function"] = true, ["local"] = true,
}
for w in allwords() do
  if not reserved[w] then
    <do something with ’w’> -- 'w' is not a reserved word
  end
end
复制代码

You can have a clearer initialization using an auxiliary function to build the set:

function Set (list)
local set = {}
for _, l in ipairs(list) do set[l] = true end
  return set
end
reserved = Set{"while", "end", "function", "local", }

Bags, also called multisets, differ from regular sets in that each element can appear multiple times.

function insert (bag, element)
  bag[element] = (bag[element] or 0) + 1
end

To remove an element we decrement its counter:

function remove (bag, element)
  local count = bag[element]
  bag[element] = (count and count > 1) and count - 1 or nil
end

We only keep the counter if it already exists and it is still greater than zero.

 

 

 

 

posted on 2014-06-11 22:34  AKever  阅读(691)  评论(0)    收藏  举报

努力加载评论中...
点击右上角即可分享
微信分享提示