Annotate

libs/table.lua @ 483:33d566513eb0

libs/table: Add table.move
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 Mar 2023 17:09:00 +0000
parent 439:6fc73ad6b1d2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
483
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
1 return {
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
2 pack = function(...) return {n = select("#", ...); ...} end;
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
3 create = function() return {} end;
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
4 move = function (a1, f, e, t, a2)
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
5 a2 = a2 or a1;
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
6 if e >= f then
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
7 local n = (e - f) + 1;
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
8 if t > e or t <= f or a1 ~= a2 then
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
9 for i = 0, n-1 do
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
10 a2[t+i] = a1[f+i];
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
11 end
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
12 else
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
13 for i = n-1, 0, -1 do
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
14 a2[t+i] = a1[f+i];
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
15 end
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
16 end
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
17 end
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
18 return a2;
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
19 end;
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
20 }