module ProjectM36.TupleSet where
import ProjectM36.Base
import ProjectM36.Tuple
import ProjectM36.Error
import qualified Data.HashSet as HS
import qualified Data.Vector as V
import qualified Control.Parallel.Strategies as P
import Data.Either
emptyTupleSet :: RelationTupleSet
emptyTupleSet = RelationTupleSet []
singletonTupleSet :: RelationTupleSet
singletonTupleSet = RelationTupleSet [emptyTuple]
verifyTupleSet :: Attributes -> RelationTupleSet -> Either RelationalError RelationTupleSet
verifyTupleSet attrs tupleSet = do
let tupleList = (map (verifyTuple attrs) (asList tupleSet)) `P.using` P.parListChunk chunkSize P.r0
chunkSize = ((length . asList) tupleSet) `div` 24
if length (lefts tupleList) > 0 then
Left $ head (lefts tupleList)
else
return $ RelationTupleSet $ (HS.toList . HS.fromList) (rights tupleList)
mkTupleSet :: Attributes -> [RelationTuple] -> Either RelationalError RelationTupleSet
mkTupleSet attrs tuples = verifyTupleSet attrs (RelationTupleSet tuples)
mkTupleSetFromList :: Attributes -> [[Atom]] -> Either RelationalError RelationTupleSet
mkTupleSetFromList attrs atomMatrix = mkTupleSet attrs $ map (\atomList -> mkRelationTuple attrs (V.fromList atomList)) atomMatrix