132 lines
4.5 KiB
HTML
Executable File

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>LOOP: Unordered Array</title>
<style type="text/css" media="all"><!--
@import "../../loop.css";
@import "../../layout1.css";
--></style>
</head>
<body>
<div id="Header">Class Models for Lua</div>
<div id="Logo"><img alt="small (1K)" src="../../small.gif" height="70"></div>
<div id="Menu">
<div class="outside"><div class="inside"><ul>
<li><a href="../../index.html", title="">Home</a></li>
<li><a href="../../release/index.html", title="Installation">Install</a></li>
<li><a href="../../manual/index.html", title="User Manual">Manual</a></li>
<li><a href="../index.html", title="Class Library">Library</a>
<div class="outside"><div class="inside"><ul>
<li><a href="../overview.html#collection", title="Collections">collection</a>
</li>
<li><a href="../overview.html#compiler", title="Compiling">compiler</a>
</li>
<li><a href="../overview.html#debug", title="Debugging">debug</a>
</li>
<li><a href="../overview.html#object", title="Objects">object</a>
</li>
<li><a href="../overview.html#serial", title="Serialization">serial</a>
</li>
<li><a href="../overview.html#thread", title="Threading">thread</a>
</li>
</ul></div></div>
</li>
<li><a href="../../contact.html", title="Contact People">Contact</a></li>
<li><a href="http://luaforge.net/projects/oil/", title="Project at LuaForge">LuaForge</a></li>
</ul></div></div>
</div>
<div class="content">
<h1>Unordered Array</h1>
<h2><code>loop.collection.UnorderedArray</code></h2><br>
<p>Class of objects that store a sequence of values as a Lua array but removes the elements from any position without shifting the following elements.
However it does not guarantee the order the values are stored in the array.
This class is useful for storing efficiently a sequence of values that may be iterated in any order and the number of elements varies constantly during interation.</p>
<p>Behaves like an ordinary Lua array but provides an operation that removes an element at given position by replacing it with the last element of the array and then frees the last position.</p>
<h2>Behavior</h2>
<h3>Methods</h3>
<dl>
<dt><code><b>add</b>(value)</code></dt>
<dd>
Adds value <code>value</code> to the end of the array.
Equivalent to <code>self[#self + 1] = element</code>.
</dd>
<dt><code><b>remove</b>(index)</code></dt>
<dd>
Replaces the element at position <code>index</code> with the last element of the array and frees the last position.
If <code>index</code> is out of the array bounds then the call has no effect.
</dd>
</dl>
<h2>Examples</h2>
<h3><a name="SimpleScheduler">Scheduler of independent threads</a> (<i>i.e.</i>, does not interact with each other).</h3>
<pre>
local oo = require "loop.simple"
local UnorderedArray = require "loop.collection.UnorderedArray"
Scheduler = oo.class({}, UnorderedArray)
function Scheduler:run()
while #self > 0 do
local i = 1
repeat
local thread = self[i]
coroutine.resume(thread)
if coroutine.status(thread) == "dead" then
self:remove(i)
else
i = i + 1
end
until i > #self
end
end
function thread(name)
return coroutine.create(function()
for i=1, 3 do
print(string.format("%s: add '%s'", name, name..i))
scheduler:add(coroutine.create(function()
for step=1, 3 do
print(string.format("%s: step %d of 3", name..i, step))
coroutine.yield()
end
end))
end
end)
end
scheduler = Scheduler{ thread("A"), thread("B"), thread("C") }
scheduler:run()
</pre>
</div>
<div class="content">
<p><small><strong>Copyright (C) 2004-2008 Tecgraf, PUC-Rio</strong></small></p>
<small>This project is currently being maintained by <a href="http://www.tecgraf.puc-rio.br">Tecgraf</a> at <a href="http://www.puc-rio.br">PUC-Rio</a>.</small>
</div>
</body>
</html>