-- | Implements the intersection of two RTables 
runIntersect :: 
    RTable
    -> RTable
    -> RTable
runIntersect rt1 rt2 =
    if isRTabEmpty rt1 || isRTabEmpty rt2 
        then
            emptyRTable
        else 
            -- construct the intersect result by traversing the left table and checking if each tuple exists in the right table
            V.foldr (intsect) emptyRTable rt1
            where
                intsect :: RTuple -> RTable -> RTable
                intsect tupLeft acc = 
                    -- Can we find tupLeft in the right table?            
                    if didYouFindIt tupLeft rt2 
                        then V.snoc acc tupLeft  -- then insert tupLeft into final result
                        else acc  -- else discard tuplLeft ,leave result unchanged          

{-
runIntersect rt1 rt2 = 
    let ls1 = V.toList rt1
        ls2 = V.toList rt2
        resultLs = Data.List.intersect ls1 ls2
    in  V.fromList resultLs

-}

-- | RTable Difference Operator
runIntersect :: 
    RTable
    -> RTable
    -> RTable
runIntersect rt1 rt2 =
    if isRTabEmpty rt1 || isRTabEmpty rt2 
        then
            emptyRTable
        else 
            -- construct the intersect result by traversing the left table and checking if each tuple exists in the right table
            V.foldr (intsect) emptyRTable rt1
            where
                intsect :: RTuple -> RTable -> RTable
                intsect tupLeft acc = 
                    -- Can we find tupLeft in the right table?            
                    if didYouFindIt tupLeft rt2 
                        then V.snoc acc tupLeft  -- then insert tupLeft into final result
                        else acc  -- else discard tuplLeft ,leave result unchanged          

{-
runIntersect rt1 rt2 = 
    let ls1 = V.toList rt1
        ls2 = V.toList rt2
        resultLs = Data.List.intersect ls1 ls2
    in  V.fromList resultLs

-}

-- | RTable Difference Operator
runIntersect rt1 rt2 =
    if isRTabEmpty rt1 || isRTabEmpty rt2 
        then
            emptyRTable
        else 
            -- construct the intersect result by traversing the left table and checking if each tuple exists in the right table
            V.foldr (intsect) emptyRTable rt1
            where
                intsect :: RTuple -> RTable -> RTable
                intsect tupLeft acc = 
                    -- Can we find tupLeft in the right table?            
                    if didYouFindIt tupLeft rt2 
                        then V.snoc acc tupLeft  -- then insert tupLeft into final result
                        else acc  -- else discard tuplLeft ,leave result unchanged          

{-
runIntersect rt1 rt2 = 
    let ls1 = V.toList rt1
        ls2 = V.toList rt2
        resultLs = Data.List.intersect ls1 ls2
    in  V.fromList resultLs

-}

-- | RTable Difference Operator