Quantcast

Fix intersecting time spans to avoid point sets.

Johnny C. Lam [04-25-14 - 20:55]
Fix intersecting time spans to avoid point sets.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@1317 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
OvaleTimeSpan.lua
diff --git a/OvaleTimeSpan.lua b/OvaleTimeSpan.lua
index 30dcb3d..b960f4d 100644
--- a/OvaleTimeSpan.lua
+++ b/OvaleTimeSpan.lua
@@ -222,11 +222,17 @@ function OvaleTimeSpan:IntersectInterval(startB, endB, result)
 			break
 		elseif compare == -1 then
 			-- Overlap; A comes before B, output, advance A.
-			result[k], result[k+1] = startB, endA
-			i, k = i + 2, k + 2
+			if endA > startB then
+				result[k], result[k+1] = startB, endA
+				i, k = i + 2, k + 2
+			else
+				i = i + 2
+			end
 		elseif compare == 1 then
 			-- Overlap; B comes before A, output, exit.
-			result[k], result[k+1] = startA, endB
+			if endB > startA then
+				result[k], result[k+1] = startA, endB
+			end
 			break
 		elseif compare == -2 then
 			-- A contains B; output, exist.
@@ -281,13 +287,21 @@ function OvaleTimeSpan:Intersect(B, result)
 			--debugprint("         ADV(B)")
 		elseif compare == -1 then
 			-- Overlap; A comes before B, output, advance A.
-			result[k], result[k+1] = startB, endA
-			i, k = i + 2, k + 2
+			if endA > startB then
+				result[k], result[k+1] = startB, endA
+				i, k = i + 2, k + 2
+			else
+				i = i + 2
+			end
 			--debugprint("         ADV(A)")
 		elseif compare == 1 then
 			-- Overlap; B comes before A, output, advance B.
-			result[k], result[k+1] = startA, endB
-			j, k = j + 2, k + 2
+			if endB > startA then
+				result[k], result[k+1] = startA, endB
+				j, k = j + 2, k + 2
+			else
+				j = j + 2
+			end
 			--debugprint("         ADV(B)")
 		elseif compare == -2 then
 			-- A contains B; output, advance B.
@@ -737,6 +751,34 @@ do
 		return true
 	end

+	testFunction[#testFunction + 1] = function()
+		local A        = OvaleTimeSpan(1, 3, 4, 6)
+		local B        = OvaleTimeSpan(3, math.huge)
+		local expected = OvaleTimeSpan(4, 6)
+
+		local result = A:Intersect(B)
+		if not result:Equals(expected) then
+			print(string.format("  result: %s", tostring(result)))
+			print(string.format("expected: %s", tostring(expected)))
+			return false
+		end
+		return true
+	end
+
+	testFunction[#testFunction + 1] = function()
+		local A            = OvaleTimeSpan(1, 3, 4, 6)
+		local startB, endB = 3, math.huge
+		local expected     = OvaleTimeSpan(4, 6)
+
+		local result = A:IntersectInterval(startB, endB)
+		if not result:Equals(expected) then
+			print(string.format("  result: %s", tostring(result)))
+			print(string.format("expected: %s", tostring(expected)))
+			return false
+		end
+		return true
+	end
+
 	local function TestDriver()
 		for i, func in ipairs(testFunction) do
 			local result = func()