Difference between revisions of "Lists"

From Bitfighter
(New page: TODO: Explain these list functions function test () local SIZE = 10000 -- create a list with elements 1..SIZE local l1 = List:new() for i=1,SIZE do l1:pushright(i) end -- ...)
 
Line 1: Line 1:
 
TODO: Explain these list functions
 
TODO: Explain these list functions
  
 +
<source lang="lua">
 
function test ()
 
function test ()
 
   local SIZE = 10000
 
   local SIZE = 10000
Line 35: Line 36:
 
end
 
end
 
print(result)
 
print(result)
--
+
</source>
--
+

Revision as of 04:42, 21 September 2009

TODO: Explain these list functions

function test ()
  local SIZE = 10000
  -- create a list with elements 1..SIZE
  local l1 = List:new()
  for i=1,SIZE do
    l1:pushright(i)
  end
  -- creates a copy of l1
  local l2 = l1:new()
  -- remove each individual item from left side of l2 and
  -- append to right side of l3 (preserving order)
  local l3 = List:new()
  while l2:length() > 0 do
    l3:pushright(l2:popleft())
  end
  -- remove each individual item from right side of l3 and
  -- append to right side of l2 (reversing list)
  while l3:length() > 0 do
    l2:pushright(l3:popright())
  end
  -- reverse l1 in place
  l1:reverse()
  -- compare Li1 and Li2 for equality
  -- and return length of the list
  if not l1:equal(l2) then return nil
  else return l1:length()
  end
end
 
N = tonumber((arg and arg[1])) or 1
for i=1, N do
  result = test()
end
print(result)