Mercurial > prosody-hg
comparison util/array.lua @ 4541:05f5ec99da77
Merge with trunk
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sun, 22 Jan 2012 22:55:49 +0000 |
| parents | ca74d8ed1a15 |
| children | cbc7eb5cfa8c |
comparison
equal
deleted
inserted
replaced
| 4530:40905e7bf680 | 4541:05f5ec99da77 |
|---|---|
| 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 }; |
| 23 return res:append(a1):append(a2); | 28 return res:append(a1):append(a2); |
| 24 end | 29 end |
| 25 | 30 |
| 26 setmetatable(array, { __call = new_array }); | 31 setmetatable(array, { __call = new_array }); |
| 27 | 32 |
| 33 -- Read-only methods | |
| 34 function array_methods:random() | |
| 35 return self[math_random(1,#self)]; | |
| 36 end | |
| 37 | |
| 38 -- These methods can be called two ways: | |
| 39 -- array.method(existing_array, [params [, ...]]) -- Create new array for result | |
| 40 -- existing_array:method([params, ...]) -- Transform existing array into result | |
| 41 -- | |
| 28 function array_base.map(outa, ina, func) | 42 function array_base.map(outa, ina, func) |
| 29 for k,v in ipairs(ina) do | 43 for k,v in ipairs(ina) do |
| 30 outa[k] = func(v); | 44 outa[k] = func(v); |
| 31 end | 45 end |
| 32 return outa; | 46 return outa; |
| 58 end | 72 end |
| 59 t_sort(outa, ...); | 73 t_sort(outa, ...); |
| 60 return outa; | 74 return outa; |
| 61 end | 75 end |
| 62 | 76 |
| 63 --- These methods only mutate | 77 function array_base.pluck(outa, ina, key) |
| 64 function array_methods:random() | 78 for i=1,#ina do |
| 65 return self[math.random(1,#self)]; | 79 outa[i] = ina[i][key]; |
| 80 end | |
| 81 return outa; | |
| 66 end | 82 end |
| 67 | 83 |
| 84 --- These methods only mutate the array | |
| 68 function array_methods:shuffle(outa, ina) | 85 function array_methods:shuffle(outa, ina) |
| 69 local len = #self; | 86 local len = #self; |
| 70 for i=1,#self do | 87 for i=1,#self do |
| 71 local r = math.random(i,len); | 88 local r = math_random(i,len); |
| 72 self[i], self[r] = self[r], self[i]; | 89 self[i], self[r] = self[r], self[i]; |
| 73 end | 90 end |
| 74 return self; | 91 return self; |
| 75 end | 92 end |
| 76 | 93 |
| 89 self[len+i] = array[i]; | 106 self[len+i] = array[i]; |
| 90 end | 107 end |
| 91 return self; | 108 return self; |
| 92 end | 109 end |
| 93 | 110 |
| 94 array_methods.push = table.insert; | 111 function array_methods:push(x) |
| 95 array_methods.pop = table.remove; | 112 t_insert(self, x); |
| 96 array_methods.concat = table.concat; | 113 return self; |
| 97 array_methods.length = function (t) return #t; end | 114 end |
| 115 | |
| 116 function array_methods:pop(x) | |
| 117 local v = self[x]; | |
| 118 t_remove(self, x); | |
| 119 return v; | |
| 120 end | |
| 121 | |
| 122 function array_methods:concat(sep) | |
| 123 return t_concat(array.map(self, tostring), sep); | |
| 124 end | |
| 125 | |
| 126 function array_methods:length() | |
| 127 return #self; | |
| 128 end | |
| 98 | 129 |
| 99 --- These methods always create a new array | 130 --- These methods always create a new array |
| 100 function array.collect(f, s, var) | 131 function array.collect(f, s, var) |
| 101 local t = {}; | 132 local t = {}; |
| 102 while true do | 133 while true do |
| 103 var = f(s, var); | 134 var = f(s, var); |
| 104 if var == nil then break; end | 135 if var == nil then break; end |
| 105 table.insert(t, var); | 136 t_insert(t, var); |
| 106 end | 137 end |
| 107 return setmetatable(t, array_mt); | 138 return setmetatable(t, array_mt); |
| 108 end | 139 end |
| 109 | 140 |
| 110 --- | 141 --- |
