Mercurial > prosody-hg
comparison util/array.lua @ 4449:ca74d8ed1a15
util.array: Avoid globals.
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Fri, 09 Dec 2011 12:02:21 +0500 |
| parents | d745f4c28294 |
| children | cbc7eb5cfa8c |
comparison
equal
deleted
inserted
replaced
| 4448:d745f4c28294 | 4449:ca74d8ed1a15 |
|---|---|
| 6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
| 7 -- | 7 -- |
| 8 | 8 |
| 9 local t_insert, t_sort, t_remove, t_concat | 9 local t_insert, t_sort, t_remove, t_concat |
| 10 = table.insert, table.sort, table.remove, table.concat; | 10 = table.insert, table.sort, table.remove, table.concat; |
| 11 | |
| 12 local setmetatable = setmetatable; | |
| 13 local math_random = math.random; | |
| 14 local pairs, ipairs = pairs, ipairs; | |
| 15 local tostring = tostring; | |
| 11 | 16 |
| 12 local array = {}; | 17 local array = {}; |
| 13 local array_base = {}; | 18 local array_base = {}; |
| 14 local array_methods = {}; | 19 local array_methods = {}; |
| 15 local array_mt = { __index = array_methods, __tostring = function (array) return array:concat(", "); end }; | 20 local array_mt = { __index = array_methods, __tostring = function (array) return array:concat(", "); end }; |
| 25 | 30 |
| 26 setmetatable(array, { __call = new_array }); | 31 setmetatable(array, { __call = new_array }); |
| 27 | 32 |
| 28 -- Read-only methods | 33 -- Read-only methods |
| 29 function array_methods:random() | 34 function array_methods:random() |
| 30 return self[math.random(1,#self)]; | 35 return self[math_random(1,#self)]; |
| 31 end | 36 end |
| 32 | 37 |
| 33 -- These methods can be called two ways: | 38 -- These methods can be called two ways: |
| 34 -- array.method(existing_array, [params [, ...]]) -- Create new array for result | 39 -- array.method(existing_array, [params [, ...]]) -- Create new array for result |
| 35 -- existing_array:method([params, ...]) -- Transform existing array into result | 40 -- existing_array:method([params, ...]) -- Transform existing array into result |
| 78 | 83 |
| 79 --- These methods only mutate the array | 84 --- These methods only mutate the array |
| 80 function array_methods:shuffle(outa, ina) | 85 function array_methods:shuffle(outa, ina) |
| 81 local len = #self; | 86 local len = #self; |
| 82 for i=1,#self do | 87 for i=1,#self do |
| 83 local r = math.random(i,len); | 88 local r = math_random(i,len); |
| 84 self[i], self[r] = self[r], self[i]; | 89 self[i], self[r] = self[r], self[i]; |
| 85 end | 90 end |
| 86 return self; | 91 return self; |
| 87 end | 92 end |
| 88 | 93 |
| 102 end | 107 end |
| 103 return self; | 108 return self; |
| 104 end | 109 end |
| 105 | 110 |
| 106 function array_methods:push(x) | 111 function array_methods:push(x) |
| 107 table.insert(self, x); | 112 t_insert(self, x); |
| 108 return self; | 113 return self; |
| 109 end | 114 end |
| 110 | 115 |
| 111 function array_methods:pop(x) | 116 function array_methods:pop(x) |
| 112 local v = self[x]; | 117 local v = self[x]; |
| 113 table.remove(self, x); | 118 t_remove(self, x); |
| 114 return v; | 119 return v; |
| 115 end | 120 end |
| 116 | 121 |
| 117 function array_methods:concat(sep) | 122 function array_methods:concat(sep) |
| 118 return table.concat(array.map(self, tostring), sep); | 123 return t_concat(array.map(self, tostring), sep); |
| 119 end | 124 end |
| 120 | 125 |
| 121 function array_methods:length() | 126 function array_methods:length() |
| 122 return #self; | 127 return #self; |
| 123 end | 128 end |
| 126 function array.collect(f, s, var) | 131 function array.collect(f, s, var) |
| 127 local t = {}; | 132 local t = {}; |
| 128 while true do | 133 while true do |
| 129 var = f(s, var); | 134 var = f(s, var); |
| 130 if var == nil then break; end | 135 if var == nil then break; end |
| 131 table.insert(t, var); | 136 t_insert(t, var); |
| 132 end | 137 end |
| 133 return setmetatable(t, array_mt); | 138 return setmetatable(t, array_mt); |
| 134 end | 139 end |
| 135 | 140 |
| 136 --- | 141 --- |
