Quantcast

Simplify some logic for doing time span operations.

Johnny C. Lam [10-26-13 - 22:23]
Simplify some logic for doing time span operations.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@1113 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
OvaleTimeSpan.lua
diff --git a/OvaleTimeSpan.lua b/OvaleTimeSpan.lua
index b492081..f52e963 100644
--- a/OvaleTimeSpan.lua
+++ b/OvaleTimeSpan.lua
@@ -25,6 +25,7 @@ function OvaleTimeSpan.Complement(startA, endA, atTime)
 		The complement of an interval is as follows:

 			COMPLEMENT{} = (0, math.huge)
+			COMPLEMENT(0, math.huge) = {}
 			COMPLEMENT(a, b) = (0, a) UNION (b, math.huge)

 		In the second case, it is the union of two intervals.  If the point of interest (atTime)
@@ -32,7 +33,9 @@ function OvaleTimeSpan.Complement(startA, endA, atTime)
 	--]]
 	if not startA or not endA then
 		return 0, math.huge
-	elseif 0 < startA and atTime <= startA then
+	elseif startA == 0 and endA == math.huge then
+		return nil
+	elseif 0 <= atTime and atTime < startA then
 		return 0, startA
 	else
 		return endA, math.huge
@@ -52,23 +55,20 @@ function OvaleTimeSpan.Intersect(startA, endA, startB, endB)
 	if not startA or not endA or not startB or not endB then
 		return nil
 	end
-	-- If the two time spans don't overlap, then return the empty set.
-	if endB <= startA or endA <= startB then
-		return nil
-	end
-	-- Take the rightmost left endpoint.
-	if startA < startB then
-		startA = startB
-	end
-	-- Take the leftmost right endpoint.
-	if endA > endB then
-		endA = endB
+	-- Swap around so that (startA, endA) comes "before" (startB, endB).
+	if startA > startB then
+		startA, startB = startB, startA
+		endA, endB = endB, endA
 	end
-	-- Sanity check that it's a valid time span, otherwise, return the empty set.
-	if startA >= endA then
+	-- If the two time spans don't overlap, then return the empty set.
+	-- Otherwise, the take leftmost right endpoint.
+	if endA <= startB then
 		return nil
+	elseif endB < endA then
+		return startB, endB
+	else
+		return startB, endA
 	end
-	return startA, endA
 end

 function OvaleTimeSpan.Measure(startA, endA)
@@ -89,14 +89,16 @@ function OvaleTimeSpan.Union(startA, endA, startB, endB)
 	elseif not startB or not endB then
 		return startA, endA
 	end
-	-- Take the leftmost left endpoint.
+	-- Swap around so that (startA, endA) comes "before" (startB, endB).
 	if startA > startB then
-		startA = startB
+		startA, startB = startB, startA
+		endA, endB = endB, endA
 	end
 	-- Take the rightmost right endpoint.
-	if endA < endB then
-		endA = endB
+	if endA > endB then
+		return startA, endA
+	else
+		return startA, endB
 	end
-	return startA, endA
 end
 --</public-static-methods>