luatraverse.lua

changeset 2
251fd96e1c34
parent 1
036168493972
child 3
842f3003db2a
equal deleted inserted replaced
1:036168493972 2:251fd96e1c34
42 count = count + 1 42 count = count + 1
43 end 43 end
44 end 44 end
45 traverse({edge=f}, {count, f}) 45 traverse({edge=f}, {count, f})
46 return count 46 return count
47 end
48
49 -- Prints all paths to an object
50 function findallpaths(obj)
51
52 local comefrom = {}
53 local f = function(from, to, how, value)
54 if not comefrom[to] then comefrom[to] = {} end
55 table.insert(comefrom[to], 1, {f = from, h = how, v=value})
56 end
57
58 traverse({edge=f}, {comefrom, f})
59
60
61 local function printpath(to)
62 if not to or comefrom[to].visited or to == _G then
63 print("-----")
64 return
65 end
66 comefrom[to].visited = true
67 for i=1, #comefrom[to] do
68 local tfrom = comefrom[to][i].f
69 print("from: ", tfrom, "\nhow:", comefrom[to][i].h,
70 "\nvalue:", comefrom[to][i].v)
71 printpath(tfrom)
72 end
73 end
74
75 printpath(obj)
76
77 end 47 end
78 48
79 -- Main function 49 -- Main function
80 -- 'funcs' is a table that contains a funcation for every lua type and also the 50 -- 'funcs' is a table that contains a funcation for every lua type and also the
81 -- function edge edge (traverseedge). 51 -- function edge edge (traverseedge).

mercurial