on master help sign in
switch to NoSuchAttributeNamesError
commit5866ddb7f398f5166b2e391311a1465438e5c863 authorAgentM <agentm@themactionfaction.com> dateTue 9 Jun 2015, 10:18 (11 years ago) parent85c4dc9
2 files changed+29−10
ProjectM36/Attribute.hs+4−1 ProjectM36/Tuple.hs+25−9
ProjectM36/Attribute.hs+4−1
@@ -66,7 +66,7 @@ atomTypeForAttributeName attrName attrs = do
6666
6767attributeForName :: AttributeName -> Attributes -> Either RelationalError Attribute
6868attributeForName attrName attrs = case V.find ((attrName ==) . attributeName) attrs of
69− Nothing -> Left $ NoSuchAttributeNameError attrName
69+ Nothing -> Left $ NoSuchAttributeNamesError (S.singleton attrName)
7070 Just attr -> Right attr
7171
7272attributesForNames :: S.Set AttributeName -> Attributes -> Attributes
@@ -77,6 +77,9 @@ attributesForNames attrNameSet attrs = V.filter filt attrs
7777attributeNameSet :: Attributes -> S.Set AttributeName
7878attributeNameSet attrVec = S.fromList $ V.toList $ V.map (\(Attribute name _) -> name) attrVec
7979
80+attributeNames :: Attributes -> V.Vector AttributeName
81+attributeNames = V.map attributeName
82+
8083--checks if set s1 is wholly contained in the set s2
8184attributesContained :: Attributes -> Attributes -> Bool
8285attributesContained attrs1 attrs2 = attributeNamesContained (attributeNameSet attrs1) (attributeNameSet attrs2)
ProjectM36/Tuple.hs+25−9
@@ -28,9 +28,9 @@ tupleAtoms (RelationTuple _ tupVec) = tupVec
2828
2929atomForAttributeName :: AttributeName -> RelationTuple -> Either RelationalError Atom
3030atomForAttributeName attrName (RelationTuple tupAttrs tupVec) = case V.findIndex (\attr -> attributeName attr == attrName) tupAttrs of
31− Nothing -> Left $ NoSuchAttributeNameError attrName
31+ Nothing -> Left $ NoSuchAttributeNamesError (S.singleton attrName)
3232 Just index -> case tupVec V.!? index of
33− Nothing -> Left $ NoSuchAttributeNameError attrName
33+ Nothing -> Left $ NoSuchAttributeNamesError (S.singleton attrName)
3434 Just atom -> Right atom
3535
3636{- -- resolve naming clash with Attribute and Relation later
@@ -40,12 +40,21 @@ atomTypeForAttributeName attrName tup = do
4040 return $ atomTypeForAtom atom
4141-}
4242
43−atomsForAttributeNames :: V.Vector AttributeName -> RelationTuple -> V.Vector Atom
44−atomsForAttributeNames attrNames tuple = V.foldr folder V.empty attrNames
43+atomsForAttributeNames :: V.Vector AttributeName -> RelationTuple -> Either RelationalError (V.Vector Atom)
44+atomsForAttributeNames attrNames tuple = do
45+ vindices <- vectorIndicesForAttributeNames attrNames (tupleAttributes tuple)
46+ return $ V.map (\index -> tupleAtoms tuple V.! index) vindices
47+
48+vectorIndicesForAttributeNames :: V.Vector AttributeName -> Attributes -> Either RelationalError (V.Vector Int)
49+vectorIndicesForAttributeNames attrNameVec attrs = if not $ V.null unknownAttrNames then
50+ Left $ NoSuchAttributeNamesError (S.fromList (V.toList unknownAttrNames))
51+ else
52+ Right $ V.map mapper attrNameVec
4553 where
46− folder attrName acc = case atomForAttributeName attrName tuple of
47− Left _ -> acc
48− Right atom -> V.snoc acc atom
54+ unknownAttrNames = V.filter ((flip V.notElem) (attributeNames attrs)) attrNameVec
55+ mapper attrName = case V.elemIndex attrName (V.map attributeName attrs) of
56+ Nothing -> undefined
57+ Just index -> index
4958
5059relationForAttributeName :: AttributeName -> RelationTuple -> Either RelationalError Relation
5160relationForAttributeName attrName tuple = do
@@ -71,13 +80,20 @@ mkRelationTuples attrs atomsVec = map mapper atomsVec
7180 mapper = mkRelationTuple attrs
7281
7382singleTupleSetJoin :: RelationTuple -> RelationTupleSet -> RelationTupleSet
74−singleTupleSetJoin tup tupSet = HS.foldr tupleJoiner HS.empty tupSet
83+singleTupleSetJoin tup tupSet = HS.foldr tupleJoiner HS.empty tupSet --get rid of this fold here
7584 where
7685 tupleJoiner tuple2 accumulator = case joinedTuple of
7786 Nothing -> accumulator
7887 Just relTuple -> HS.insert relTuple accumulator
7988 where joinedTuple = singleTupleJoin tup tuple2
80−
89+
90+{-
91+singleTupleSetJoin :: RelationTuple -> RelationTupleSet -> RelationTupleSet
92+singleTupleSetJoin tup1 tupSet = HS.union
93+ where
94+ mapper tup2 = singleTupleJoin tup1 tup2
95+-}
96+
8197-- the keys/values don't need to be resorted
8298-- if the keys share some keys and values, then merge the tuples
8399singleTupleJoin :: RelationTuple -> RelationTuple -> Maybe RelationTuple