Comparison

util/array.lua @ 6054:7a5ddbaf758d

Merge 0.9->0.10
author Matthew Wild <mwild1@gmail.com>
date Wed, 02 Apr 2014 17:41:38 +0100
parent 5857:8613888d0f9e
child 6417:060b63a27e9b
comparison
equal deleted inserted replaced
6053:2f93a04564b2 6054:7a5ddbaf758d
1 -- Prosody IM 1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild 2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain 3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
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 11
12 local setmetatable = setmetatable; 12 local setmetatable = setmetatable;
13 local math_random = math.random; 13 local math_random = math.random;
14 local math_floor = math.floor;
14 local pairs, ipairs = pairs, ipairs; 15 local pairs, ipairs = pairs, ipairs;
15 local tostring = tostring; 16 local tostring = tostring;
16 17
17 local array = {}; 18 local array = {};
18 local array_base = {}; 19 local array_base = {};
57 if func(v) then 58 if func(v) then
58 outa[write] = v; 59 outa[write] = v;
59 write = write + 1; 60 write = write + 1;
60 end 61 end
61 end 62 end
62 63
63 if inplace and write <= start_length then 64 if inplace and write <= start_length then
64 for i=write,start_length do 65 for i=write,start_length do
65 outa[i] = nil; 66 outa[i] = nil;
66 end 67 end
67 end 68 end
68 69
69 return outa; 70 return outa;
70 end 71 end
71 72
72 function array_base.sort(outa, ina, ...) 73 function array_base.sort(outa, ina, ...)
73 if ina ~= outa then 74 if ina ~= outa then
82 outa[i] = ina[i][key]; 83 outa[i] = ina[i][key];
83 end 84 end
84 return outa; 85 return outa;
85 end 86 end
86 87
88 function array_base.reverse(outa, ina)
89 local len = #ina;
90 if ina == outa then
91 local middle = math_floor(len/2);
92 len = len + 1;
93 local o; -- opposite
94 for i = 1, middle do
95 o = len - i;
96 outa[i], outa[o] = outa[o], outa[i];
97 end
98 else
99 local off = len + 1;
100 for i = 1, len do
101 outa[i] = ina[off - i];
102 end
103 end
104 return outa;
105 end
106
87 --- These methods only mutate the array 107 --- These methods only mutate the array
88 function array_methods:shuffle(outa, ina) 108 function array_methods:shuffle(outa, ina)
89 local len = #self; 109 local len = #self;
90 for i=1,#self do 110 for i=1,#self do
91 local r = math_random(i,len); 111 local r = math_random(i,len);
92 self[i], self[r] = self[r], self[i]; 112 self[i], self[r] = self[r], self[i];
93 end
94 return self;
95 end
96
97 function array_methods:reverse()
98 local len = #self-1;
99 for i=len,1,-1 do
100 self:push(self[i]);
101 self:pop(i);
102 end 113 end
103 return self; 114 return self;
104 end 115 end
105 116
106 function array_methods:append(array) 117 function array_methods:append(array)