refine voting algo, equal votes dont elect anything
authorChristian Thaeter <ct@pipapo.org>
Sat, 2 Jan 2010 06:52:47 +0000 (07:52 +0100)
committerChristian Thaeter <ct@pipapo.org>
Sat, 2 Jan 2010 06:52:47 +0000 (07:52 +0100)
profiled.lua

index 44cd2ec..1d5051a 100644 (file)
@@ -100,7 +100,7 @@ function mainloop()
    -- do the voting
    for name, func in pairs(queries) do
       if type(_G[name]) == 'table' then
    -- do the voting
    for name, func in pairs(queries) do
       if type(_G[name]) == 'table' then
-
+         print("in table:", name)
          for k,v in pairs(func() or {}) do
             vote(votes, k, v)
          end
          for k,v in pairs(func() or {}) do
             vote(votes, k, v)
          end
@@ -109,19 +109,20 @@ function mainloop()
 
    -- calculate results
    for engine, values in pairs(votes) do
 
    -- calculate results
    for engine, values in pairs(votes) do
-      if engines[engine] then
-         local winner
-         local winning_votes = 0
-         for attr, votes in pairs(values) do
-            print (attr, votes)
-            if votes > winning_votes then
-               winner = attr
-               winning_votes = votes
-            end
+      local winner
+      local winning_votes = 0
+      for attr, votes in pairs(values) do
+         if votes == winning_votes then
+            winner = nil
+         elseif votes > winning_votes then
+            winner = attr
+            winning_votes = votes
          end
          end
-         print ("elected:", engine..":"..winner, "votes:", winning_votes)
+      end
 
 
-         if winner then
+      if winner then
+         print ("elected:", engine..":"..winner, "votes:", winning_votes)
+         if engines[engine] then
             engines[engine](winner)
          end
       end
             engines[engine](winner)
          end
       end