Annotate

util/array.lua @ 1482:9734231a569f

util.hmac: Some optimisations
author Matthew Wild <mwild1@gmail.com>
date Sun, 05 Jul 2009 17:10:42 +0100
parent 1373:120275376bbb
child 1522:569d58d21612
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
1373
120275376bbb util.array: Add support for + operator to create a new array from two arrays joined
Matthew Wild <mwild1@gmail.com>
parents: 1372
diff changeset
8 function array_mt.__add(a1, a2)
120275376bbb util.array: Add support for + operator to create a new array from two arrays joined
Matthew Wild <mwild1@gmail.com>
parents: 1372
diff changeset
9 local res = new_array();
120275376bbb util.array: Add support for + operator to create a new array from two arrays joined
Matthew Wild <mwild1@gmail.com>
parents: 1372
diff changeset
10 return res:append(a1):append(a2);
120275376bbb util.array: Add support for + operator to create a new array from two arrays joined
Matthew Wild <mwild1@gmail.com>
parents: 1372
diff changeset
11 end
120275376bbb util.array: Add support for + operator to create a new array from two arrays joined
Matthew Wild <mwild1@gmail.com>
parents: 1372
diff changeset
12
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
13 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
14
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
15 function array:map(func, t2)
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local t2 = t2 or array{};
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 for k,v in ipairs(self) do
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 t2[k] = func(v);
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return t2;
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
23 function array:filter(func, t2)
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local t2 = t2 or array{};
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 for k,v in ipairs(self) do
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if func(v) then
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 t2:push(v);
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 return t2;
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
34 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
35 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
36 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
37 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
38 array.length = function (t) return #t; end
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
40 function array:random()
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 return self[math.random(1,#self)];
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
44 function array:shuffle()
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local len = #self;
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 for i=1,#self do
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local r = math.random(i,len);
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 self[i], self[r] = self[r], self[i];
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
1372
3b13bb57002e util.array: Make array:reverse() and array:shuffle() return the array to allow chaining
Matthew Wild <mwild1@gmail.com>
parents: 1371
diff changeset
50 return self;
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
922
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
53 function array:reverse()
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
54 local len = #self-1;
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
55 for i=len,1,-1 do
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
56 self:push(self[i]);
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
57 self:pop(i);
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
58 end
1372
3b13bb57002e util.array: Make array:reverse() and array:shuffle() return the array to allow chaining
Matthew Wild <mwild1@gmail.com>
parents: 1371
diff changeset
59 return self;
922
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
60 end
0e45234360cd util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents: 920
diff changeset
61
1371
9e45bdf55353 util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents: 1027
diff changeset
62 function array:append(array)
9e45bdf55353 util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents: 1027
diff changeset
63 local len,len2 = #self, #array;
9e45bdf55353 util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents: 1027
diff changeset
64 for i=1,len2 do
9e45bdf55353 util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents: 1027
diff changeset
65 self[len+i] = array[i];
9e45bdf55353 util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents: 1027
diff changeset
66 end
9e45bdf55353 util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents: 1027
diff changeset
67 return self;
9e45bdf55353 util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents: 1027
diff changeset
68 end
9e45bdf55353 util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents: 1027
diff changeset
69
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
70 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
71 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
72 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
73 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
74 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
75 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
76 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
77 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
78 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
79
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
80 _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
81 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
82
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
83 return array;