Software / code / prosody
Annotate
util/array.lua @ 938:663f75dd7b42
Fixed: Some nil access bugs
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Mon, 30 Mar 2009 05:26:10 +0500 |
| parent | 922:0e45234360cd |
| child | 1027:fe2e3d3dba6a |
| 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 | 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 | 5 return setmetatable(t or {}, array_mt); |
| 6 end | |
| 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 | 11 local t2 = t2 or array{}; |
| 12 for k,v in ipairs(self) do | |
| 13 t2[k] = func(v); | |
| 14 end | |
| 15 return t2; | |
| 16 end | |
| 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 | 19 local t2 = t2 or array{}; |
| 20 for k,v in ipairs(self) do | |
| 21 if func(v) then | |
| 22 t2:push(v); | |
| 23 end | |
| 24 end | |
| 25 return t2; | |
| 26 end | |
| 27 | |
| 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 | 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 | 36 return self[math.random(1,#self)]; |
| 37 end | |
| 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 | 40 local len = #self; |
| 41 for i=1,#self do | |
| 42 local r = math.random(i,len); | |
| 43 self[i], self[r] = self[r], self[i]; | |
| 44 end | |
| 45 end | |
| 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 |
| 918 | 55 _G.array = array |