comparison Lists.lua @ 101:25c127c4c1e8

Cleanup. Notes. Always use ipairs on changes table - no guarantee of ordering with pairs
author John@Doomsday
date Thu, 03 May 2012 09:04:51 -0400
parents 19fd02bff870
children c6c748a5823b
comparison
equal deleted inserted replaced
100:790266dbcaff 101:25c127c4c1e8
927 927
928 if remoteBase == db.profile.time then 928 if remoteBase == db.profile.time then
929 local j = 1 -- index in foreign list 929 local j = 1 -- index in foreign list
930 local n = getn(t) 930 local n = getn(t)
931 local o = {} 931 local o = {}
932 for i,v in pairs(db.profile.changes) do -- for each timestamp in our list 932 for i,v in ipairs(db.profile.changes) do -- for each timestamp in our list
933 if t and t[j] < v.time then 933 if t and t[j] < v.time then
934 table.insert(o,v) 934 table.insert(o,v)
935 end 935 end
936 while j<n and t[j] <= v.time do j = j+1 end -- advance the foreign pointer past our current entry 936 while j<n and t[j] <= v.time do j = j+1 end -- advance the foreign pointer past our current entry
937 end -- j>=n ? add because the remote hit end of road. lt? add because it's a missing stamp 937 end -- j>=n ? add because the remote hit end of road. lt? add because it's a missing stamp
938 --print("Received request at timebase",remoteBase,"and returning:") 938 --print("Received request at timebase",remoteBase,"and returning:")
939 --PrintTable(o) 939 --PrintTable(o)
940 if getn(o) > 0 then
941 Send("CU",o) -- todo: send privately to the requster
942 end
943 return true, o 940 return true, o
944 else 941 else
945 return false, {} 942 return false, {}
946 end 943 end
947 end 944 end
960 957
961 if no == 0 then 958 if no == 0 then
962 db.profile.changes = c 959 db.profile.changes = c
963 else 960 else
964 while op <= no or cp <= nc do -- lists are pre-sorted. insertion merge them 961 while op <= no or cp <= nc do -- lists are pre-sorted. insertion merge them
965 if cp > nc then -- inelegant 962 if cp > nc then -- inelegant - edge cases first, then the normal logic
966 table.insert(new,old[op]) 963 table.insert(new,old[op])
967 op = op + 1 964 op = op + 1
968 elseif op > no then 965 elseif op > no then
969 table.insert(new,c[cp]) 966 table.insert(new,c[cp])
970 cp = cp + 1 967 cp = cp + 1