comparison Lists.lua @ 96:082ff877c443

Small refactor
author John@Doomsday
date Mon, 23 Apr 2012 09:11:21 -0400
parents 5ade79caeece
children 19fd02bff870
comparison
equal deleted inserted replaced
95:5df2c9cdb8c8 96:082ff877c443
919 t = v.time 919 t = v.time
920 end 920 end
921 return true 921 return true
922 end 922 end
923 923
924
925 function CreateChangeDiff(remoteBase,remoteChanges)
926 local t = remoteChanges
927
928 if remoteBase == db.profile.time then
929 local j = 1 -- index in foreign list
930 local n = getn(t)
931 local o = {}
932 for i,v in pairs(db.profile.changes) do -- for each timestamp in our list
933 if t and t[j] < v.time then
934 table.insert(o,v)
935 end
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
938 print("Received request at timebase",remoteBase,"and returning:")
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
944 else
945 return false, {}
946 end
947 end
948
949 function IntegrateChangeDiff(remoteChanges) -- todo: check against remoteBase before committing to insanity
950 local c = remoteChanges
951 local old = db.profile.changes
952
953 local new = {}
954
955 local op = 1
956 local cp = 1
957
958 local no = getn(old)
959 local nc = getn(c)
960
961 if no == 0 then
962 db.profile.changes = c
963 else
964 while op <= no or cp <= nc do -- lists are pre-sorted. insertion merge them
965 if cp > nc then -- inelegant
966 table.insert(new,old[op])
967 op = op + 1
968 elseif op > no then
969 table.insert(new,c[cp])
970 cp = cp + 1
971 elseif c[cp].time < old[op].time then
972 table.insert(new,c[cp])
973 cp = cp + 1
974 elseif c[cp].time > old[op].time then
975 table.insert(new,old[op])
976 op = op + 1
977 else
978 error("Bad update received from ",sender)
979 end
980 end
981 print("Updating changes - ",getn(new), "entries")
982 db.profile.changes = new
983 end
984
985 CreateWorkingStateFromChanges(db.profile.changes)
986 if changeListener then
987 changeListener:DataEvent()
988 end
989 end