Annotate

util/array.lua @ 7976:2a7ef5fcaa77

util.array: Add O(n) equality check metamethod
author Kim Alvefur <zash@zash.se>
date Thu, 16 Mar 2017 19:09:57 +0100
parent 7700:0d70410efdcf
child 8017:ec7cab8e744d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1373
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1934
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1934
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5564
diff changeset
4 --
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1373
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1373
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1373
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1373
diff changeset
8
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
9 local t_insert, t_sort, t_remove, t_concat
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
10 = table.insert, table.sort, table.remove, table.concat;
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
4449
ca74d8ed1a15 util.array: Avoid globals.
Waqas Hussain <waqas20@gmail.com>
parents: 4448
diff changeset
12 local setmetatable = setmetatable;
ca74d8ed1a15 util.array: Avoid globals.
Waqas Hussain <waqas20@gmail.com>
parents: 4448
diff changeset
13 local math_random = math.random;
5857
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
14 local math_floor = math.floor;
4449
ca74d8ed1a15 util.array: Avoid globals.
Waqas Hussain <waqas20@gmail.com>
parents: 4448
diff changeset
15 local pairs, ipairs = pairs, ipairs;
ca74d8ed1a15 util.array: Avoid globals.
Waqas Hussain <waqas20@gmail.com>
parents: 4448
diff changeset
16 local tostring = tostring;
6417
060b63a27e9b util.array: Add type() local
Kim Alvefur <zash@zash.se>
parents: 5857
diff changeset
17 local type = type;
4449
ca74d8ed1a15 util.array: Avoid globals.
Waqas Hussain <waqas20@gmail.com>
parents: 4448
diff changeset
18
1905
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
19 local array = {};
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
20 local array_base = {};
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
21 local array_methods = {};
7699
9c40d0be2295 util.array: Rename arguments to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7020
diff changeset
22 local array_mt = { __index = array_methods, __tostring = function (self) return "{"..self:concat(", ").."}"; end };
1905
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
23
5085
cbc7eb5cfa8c util.array: Accept an iterator to the array constructor
Matthew Wild <mwild1@gmail.com>
parents: 4449
diff changeset
24 local function new_array(self, t, _s, _var)
cbc7eb5cfa8c util.array: Accept an iterator to the array constructor
Matthew Wild <mwild1@gmail.com>
parents: 4449
diff changeset
25 if type(t) == "function" then -- Assume iterator
cbc7eb5cfa8c util.array: Accept an iterator to the array constructor
Matthew Wild <mwild1@gmail.com>
parents: 4449
diff changeset
26 t = self.collect(t, _s, _var);
cbc7eb5cfa8c util.array: Accept an iterator to the array constructor
Matthew Wild <mwild1@gmail.com>
parents: 4449
diff changeset
27 end
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 return setmetatable(t or {}, array_mt);
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
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
31 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
32 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
33 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
34 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
35
7976
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
36 function array_mt.__eq(a, b)
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
37 if #a == #b then
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
38 for i = 1, #a do
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
39 if a[i] ~= b[i] then
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
40 return false;
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
41 end
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
42 end
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
43 else
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
44 return false;
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
45 end
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
46 return true;
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
47 end
2a7ef5fcaa77 util.array: Add O(n) equality check metamethod
Kim Alvefur <zash@zash.se>
parents: 7700
diff changeset
48
920
e302cbc9d036 util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents: 918
diff changeset
49 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
50
4440
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
51 -- Read-only methods
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
52 function array_methods:random()
7019
abcd5ec3ee41 util.array: Fix minory style issues
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
53 return self[math_random(1, #self)];
4440
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
54 end
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
55
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
56 -- These methods can be called two ways:
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
57 -- array.method(existing_array, [params [, ...]]) -- Create new array for result
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
58 -- existing_array:method([params, ...]) -- Transform existing array into result
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
59 --
1905
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
60 function array_base.map(outa, ina, func)
7019
abcd5ec3ee41 util.array: Fix minory style issues
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
61 for k, v in ipairs(ina) do
1905
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
62 outa[k] = func(v);
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
1905
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
64 return outa;
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66
1905
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
67 function array_base.filter(outa, ina, func)
1915
e9d5406caf8c util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents: 1905
diff changeset
68 local inplace, start_length = ina == outa, #ina;
e9d5406caf8c util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents: 1905
diff changeset
69 local write = 1;
7019
abcd5ec3ee41 util.array: Fix minory style issues
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
70 for read = 1, start_length do
1915
e9d5406caf8c util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents: 1905
diff changeset
71 local v = ina[read];
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 if func(v) then
1915
e9d5406caf8c util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents: 1905
diff changeset
73 outa[write] = v;
e9d5406caf8c util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents: 1905
diff changeset
74 write = write + 1;
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5564
diff changeset
77
1922
d5fe0f9b377a util.array: Small logic fix for array:filter()
Matthew Wild <mwild1@gmail.com>
parents: 1915
diff changeset
78 if inplace and write <= start_length then
7019
abcd5ec3ee41 util.array: Fix minory style issues
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
79 for i = write, start_length do
1915
e9d5406caf8c util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents: 1905
diff changeset
80 outa[i] = nil;
e9d5406caf8c util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents: 1905
diff changeset
81 end
e9d5406caf8c util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents: 1905
diff changeset
82 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5564
diff changeset
83
1905
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
84 return outa;
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
1905
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
87 function array_base.sort(outa, ina, ...)
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
88 if ina ~= outa then
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
89 outa:append(ina);
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
90 end
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
91 t_sort(outa, ...);
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
92 return outa;
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
93 end
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
4439
1c8d2c0d02db util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents: 4387
diff changeset
95 function array_base.pluck(outa, ina, key)
7019
abcd5ec3ee41 util.array: Fix minory style issues
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
96 for i = 1, #ina do
4439
1c8d2c0d02db util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents: 4387
diff changeset
97 outa[i] = ina[i][key];
1c8d2c0d02db util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents: 4387
diff changeset
98 end
1c8d2c0d02db util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents: 4387
diff changeset
99 return outa;
1c8d2c0d02db util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents: 4387
diff changeset
100 end
1c8d2c0d02db util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents: 4387
diff changeset
101
5857
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
102 function array_base.reverse(outa, ina)
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
103 local len = #ina;
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
104 if ina == outa then
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
105 local middle = math_floor(len/2);
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
106 len = len + 1;
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
107 local o; -- opposite
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
108 for i = 1, middle do
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
109 o = len - i;
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
110 outa[i], outa[o] = outa[o], outa[i];
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
111 end
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
112 else
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
113 local off = len + 1;
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
114 for i = 1, len do
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
115 outa[i] = ina[off - i];
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
116 end
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
117 end
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
118 return outa;
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
119 end
8613888d0f9e util.array: Improve array:reverse() and make it work as both method and non-mutating function
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
120
4440
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
121 --- These methods only mutate the array
7700
0d70410efdcf util.array: Remove unused arguments [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7699
diff changeset
122 function array_methods:shuffle()
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 local len = #self;
7019
abcd5ec3ee41 util.array: Fix minory style issues
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
124 for i = 1, #self do
abcd5ec3ee41 util.array: Fix minory style issues
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
125 local r = math_random(i, len);
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 self[i], self[r] = self[r], self[i];
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 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
128 return self;
918
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
967edf874df7 util.array: New array library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
7699
9c40d0be2295 util.array: Rename arguments to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7020
diff changeset
131 function array_methods:append(ina)
9c40d0be2295 util.array: Rename arguments to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7020
diff changeset
132 local len, len2 = #self, #ina;
7019
abcd5ec3ee41 util.array: Fix minory style issues
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
133 for i = 1, len2 do
7699
9c40d0be2295 util.array: Rename arguments to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7020
diff changeset
134 self[len+i] = ina[i];
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
135 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
136 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
137 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
138
4440
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
139 function array_methods:push(x)
4449
ca74d8ed1a15 util.array: Avoid globals.
Waqas Hussain <waqas20@gmail.com>
parents: 4448
diff changeset
140 t_insert(self, x);
4448
d745f4c28294 util.array: Make array:push() chainable.
Waqas Hussain <waqas20@gmail.com>
parents: 4440
diff changeset
141 return self;
4440
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
142 end
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
143
7020
6ab9c691c4c6 util.array: Just use table.remove as array:pop()
Kim Alvefur <zash@zash.se>
parents: 7019
diff changeset
144 array_methods.pop = t_remove;
4440
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
145
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
146 function array_methods:concat(sep)
4449
ca74d8ed1a15 util.array: Avoid globals.
Waqas Hussain <waqas20@gmail.com>
parents: 4448
diff changeset
147 return t_concat(array.map(self, tostring), sep);
4440
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
148 end
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
149
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
150 function array_methods:length()
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
151 return #self;
c60ed6732b34 util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents: 4439
diff changeset
152 end
1905
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
153
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
154 --- These methods always create a new array
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
155 function array.collect(f, s, var)
4387
06161b0b83f2 util.array: Fix array.collect() for iterators that expect initial value of var to be preserved
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
156 local t = {};
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
157 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
158 var = f(s, var);
7019
abcd5ec3ee41 util.array: Fix minory style issues
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
159 if var == nil then break; end
4449
ca74d8ed1a15 util.array: Avoid globals.
Waqas Hussain <waqas20@gmail.com>
parents: 4448
diff changeset
160 t_insert(t, var);
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
161 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
162 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
163 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
164
1905
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
165 ---
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
166
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
167 -- Setup methods from array_base
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
168 for method, f in pairs(array_base) do
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
169 local base_method = f;
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
170 -- Setup global array method which makes new array
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
171 array[method] = function (old_a, ...)
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
172 local a = new_array();
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
173 return base_method(a, old_a, ...);
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
174 end
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
175 -- Setup per-array (mutating) method
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
176 array_methods[method] = function (self, ...)
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
177 return base_method(self, self, ...);
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
178 end
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
179 end
e3e0a17e0b33 util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
180
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
181 return array;