diff options
author | Asbjørn Sloth Tønnesen <ast@fiberby.net> | 2017-02-13 16:04:41 +0000 |
---|---|---|
committer | Asbjørn Sloth Tønnesen <ast@2e8.dk> | 2017-02-13 16:04:41 +0000 |
commit | 907f610040e3dfb54a83155c36527a65b7f08e78 (patch) | |
tree | fa85c2deeef755ffd4d8c123f9cb68bd5ab337e2 /lua/inet/set.lua | |
parent | 1e3d5d977d2d647453c3d0b4e01cf6f375422d9c (diff) | |
download | lua-inet-907f610040e3dfb54a83155c36527a65b7f08e78.tar.gz lua-inet-907f610040e3dfb54a83155c36527a65b7f08e78.tar.xz lua-inet-907f610040e3dfb54a83155c36527a65b7f08e78.zip |
initial commit
Signed-off-by: Asbjørn Sloth Tønnesen <ast@fiberby.net>
Diffstat (limited to 'lua/inet/set.lua')
-rw-r--r-- | lua/inet/set.lua | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/lua/inet/set.lua b/lua/inet/set.lua new file mode 100644 index 0000000..effb0f2 --- /dev/null +++ b/lua/inet/set.lua @@ -0,0 +1,49 @@ +local M = {} + +local function table_compact(t, n) + -- remove nil entries, and reorder + local i = 0 + for j=1,n do + if t[j] then + if i > 0 then + t[i] = t[j] + i = i + 1 + end + elseif i == 0 then + i = j + end + end + if i > 0 then + for j=i,n do + t[j] = nil + end + end +end + +function M.aggregate(t) + local flag = true + local n = #t + for i=1,n do + t[i] = t[i]:network() + end + while flag do -- loop until no aggregatable addresses are found + flag = false + for i=1,n do + local ia = t[i] + if ia then + local ib = ia:flip() -- counterpart + for j=1,n do + if j ~= i and t[j] == ib then + -- counterpart found, aggregating + t[i] = (ia ^ -1):network() + t[j] = nil + flag = true + end + end + end + end + end + table_compact(t, n) +end + +return M |