Software /
code /
prosody
Comparison
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 |
comparison
equal
deleted
inserted
replaced
4422:c25dee24623f | 4423:ded726418b16 |
---|---|
3 -- | 3 -- |
4 -- This project is MIT/X11 licensed. Please see the | 4 -- This project is MIT/X11 licensed. Please see the |
5 -- COPYING file in the source package for more information. | 5 -- COPYING file in the source package for more information. |
6 -- | 6 -- |
7 | 7 |
8 local t_sort = table.sort; | |
9 local commonPrefixLength = require"util.ip".commonPrefixLength | 8 local commonPrefixLength = require"util.ip".commonPrefixLength |
10 local new_ip = require"util.ip".new_ip; | 9 local new_ip = require"util.ip".new_ip; |
10 | |
11 local function t_sort(t, comp) | |
12 for i = 1, (#t - 1) do | |
13 for j = (i + 1), #t do | |
14 local a, b = t[i], t[j]; | |
15 if not comp(a,b) then | |
16 t[i], t[j] = b, a; | |
17 end | |
18 end | |
19 end | |
20 end | |
11 | 21 |
12 function source(dest, candidates) | 22 function source(dest, candidates) |
13 local function comp(ipA, ipB) | 23 local function comp(ipA, ipB) |
14 -- Rule 1: Prefer same address | 24 -- Rule 1: Prefer same address |
15 if dest == ipA then | 25 if dest == ipA then |
59 t_sort(candidates, comp); | 69 t_sort(candidates, comp); |
60 return candidates[1]; | 70 return candidates[1]; |
61 end | 71 end |
62 | 72 |
63 function destination(candidates, sources) | 73 function destination(candidates, sources) |
64 local t_sort = table.sort; | |
65 local sourceAddrs = {}; | 74 local sourceAddrs = {}; |
66 local function comp(ipA, ipB) | 75 local function comp(ipA, ipB) |
67 local ipAsource = sourceAddrs[ipA]; | 76 local ipAsource = sourceAddrs[ipA]; |
68 local ipBsource = sourceAddrs[ipB]; | 77 local ipBsource = sourceAddrs[ipB]; |
69 -- Rule 1: Avoid unusable destinations | 78 -- Rule 1: Avoid unusable destinations |