Diff

util/rfc3484.lua @ 4423:ded726418b16

util.rfc3484: Use a stable sorting algorithm
author Florian Zeitz <florob@babelmonkeys.de>
date Sun, 06 Nov 2011 18:23:16 +0100
parent 4420:4314eeeed394
child 4830:ea907059a90e
line wrap: on
line diff
--- a/util/rfc3484.lua	Wed Oct 26 02:03:33 2011 +0200
+++ b/util/rfc3484.lua	Sun Nov 06 18:23:16 2011 +0100
@@ -5,10 +5,20 @@
 -- COPYING file in the source package for more information.
 --
 
-local t_sort = table.sort;
 local commonPrefixLength = require"util.ip".commonPrefixLength
 local new_ip = require"util.ip".new_ip;
 
+local function t_sort(t, comp)
+	for i = 1, (#t - 1) do
+		for j = (i + 1), #t do
+			local a, b = t[i], t[j];
+			if not comp(a,b) then
+				t[i], t[j] = b, a;
+			end
+		end
+	end
+end
+
 function source(dest, candidates)
 	local function comp(ipA, ipB)
 		-- Rule 1: Prefer same address
@@ -61,7 +71,6 @@
 end
 
 function destination(candidates, sources)
-	local t_sort = table.sort;
 	local sourceAddrs = {};
 	local function comp(ipA, ipB)
 		local ipAsource = sourceAddrs[ipA];