Annotate

util/array.lua @ 1092:b547967d87fc

mod_posix: Don't let the server run as root without the magic run_as_root in config
author Matthew Wild <mwild1@gmail.com>
date Sat, 02 May 2009 20:32:43 +0100
parent 1027:fe2e3d3dba6a
child 1371:9e45bdf55353
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
1 local array = {};
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
3 local array_mt = { __index = array, __tostring = function (array) return array:concat(", "); end };
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
4 local function new_array(_, t)
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 return setmetatable(t or {}, array_mt);
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
8 setmetatable(array, { __call = new_array });
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
9
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
10 function array:map(func, t2)
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local t2 = t2 or array{};
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 for k,v in ipairs(self) do
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 t2[k] = func(v);
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 return t2;
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
18 function array:filter(func, t2)
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local t2 = t2 or array{};
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 for k,v in ipairs(self) do
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if func(v) then
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 t2:push(v);
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 return t2;
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
29 array.push = table.insert;
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
30 array.pop = table.remove;
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
31 array.sort = table.sort;
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
32 array.concat = table.concat;
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
33 array.length = function (t) return #t; end
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
35 function array:random()
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 return self[math.random(1,#self)];
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
39 function array:shuffle()
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local len = #self;
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 for i=1,#self do
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local r = math.random(i,len);
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 self[i], self[r] = self[r], self[i];
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
922
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
47 function array:reverse()
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
48 local len = #self-1;
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
49 for i=len,1,-1 do
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
50 self:push(self[i]);
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
51 self:pop(i);
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
52 end
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
53 end
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
54
1027
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
55 function array.collect(f, s, var)
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
56 local t, var = {};
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
57 while true do
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
58 var = f(s, var);
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
59 if var == nil then break; end
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
60 table.insert(t, var);
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
61 end
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
62 return setmetatable(t, array_mt);
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
63 end
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
64
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
65 _G.array = array;
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
66 module("array");
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
67
fe2e3d3dba6a util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents: 922
diff changeset
68 return array;