Software /
code /
prosody
Comparison
util/array.lua @ 1915:e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 05 Oct 2009 17:56:31 +0100 |
parent | 1905:e3e0a17e0b33 |
child | 1922:d5fe0f9b377a |
comparison
equal
deleted
inserted
replaced
1907:1dd4443e7d93 | 1915:e9d5406caf8c |
---|---|
31 end | 31 end |
32 return outa; | 32 return outa; |
33 end | 33 end |
34 | 34 |
35 function array_base.filter(outa, ina, func) | 35 function array_base.filter(outa, ina, func) |
36 for k,v in ipairs(ina) do | 36 local inplace, start_length = ina == outa, #ina; |
37 local write = 1; | |
38 for read=1,start_length do | |
39 local v = ina[read]; | |
37 if func(v) then | 40 if func(v) then |
38 outa:push(v); | 41 outa[write] = v; |
42 write = write + 1; | |
39 end | 43 end |
40 end | 44 end |
45 | |
46 if inplace and write < start_length then | |
47 for i=write,start_length do | |
48 outa[i] = nil; | |
49 end | |
50 end | |
51 | |
41 return outa; | 52 return outa; |
42 end | 53 end |
43 | 54 |
44 function array_base.sort(outa, ina, ...) | 55 function array_base.sort(outa, ina, ...) |
45 if ina ~= outa then | 56 if ina ~= outa then |