Skip to content

Commit

Permalink
Create NonEmptySet type
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Apr 2, 2019
1 parent 702363c commit 68de675
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 144 deletions.
7 changes: 4 additions & 3 deletions Data/Map/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1923,7 +1923,7 @@ difference t1 (Bin (NonEmptyMap _ k _ l2 r2)) = case split k t1 of
withoutKeys :: Ord k => Map k a -> Set k -> Map k a
withoutKeys Tip _ = Tip
withoutKeys m Set.Tip = m
withoutKeys m (Set.Bin _ k ls rs) = case splitMember k m of
withoutKeys m (Set.Bin (Set.NonEmptySet _ k ls rs)) = case splitMember k m of
(lm, b, rm)
| not b && lm' `ptrEq` lm && rm' `ptrEq` rm -> m
| otherwise -> link2 lm' rm'
Expand Down Expand Up @@ -3316,7 +3316,8 @@ assocs m

keysSet :: Map k a -> Set.Set k
keysSet Tip = Set.Tip
keysSet (Bin (NonEmptyMap sz kx _ l r)) = Set.Bin sz kx (keysSet l) (keysSet r)
keysSet (Bin (NonEmptyMap sz kx _ l r)) = Set.Bin $
Set.NonEmptySet sz kx (keysSet l) (keysSet r)

-- | /O(n)/. Build a map from a set of keys and a function which for each key
-- computes its value.
Expand All @@ -3326,7 +3327,7 @@ keysSet (Bin (NonEmptyMap sz kx _ l r)) = Set.Bin sz kx (keysSet l) (keysSet r)

fromSet :: (k -> a) -> Set.Set k -> Map k a
fromSet _ Set.Tip = Tip
fromSet f (Set.Bin sz x l r) = Bin $ NonEmptyMap sz x (f x) (fromSet f l) (fromSet f r)
fromSet f (Set.Bin (Set.NonEmptySet sz x l r)) = Bin $ NonEmptyMap sz x (f x) (fromSet f l) (fromSet f r)

{--------------------------------------------------------------------
Lists
Expand Down
3 changes: 2 additions & 1 deletion Data/Map/Strict/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,8 @@ mapKeysWith c f = fromListWith c . foldrWithKey (\k x xs -> (f k, x) : xs) []

fromSet :: (k -> a) -> Set.Set k -> Map k a
fromSet _ Set.Tip = Tip
fromSet f (Set.Bin sz x l r) = case f x of v -> v `seq` Bin (NonEmptyMap sz x v (fromSet f l) (fromSet f r))
fromSet f (Set.Bin (Set.NonEmptySet sz x l r)) = case f x of
v -> v `seq` Bin (NonEmptyMap sz x v (fromSet f l) (fromSet f r))

{--------------------------------------------------------------------
Lists
Expand Down
1 change: 1 addition & 0 deletions Data/Set.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ module Data.Set (
Set -- instance Eq,Ord,Show,Read,Data,Typeable
#else
Set(..)
, NonEmptySet(..)
#endif

-- * Construction
Expand Down
Loading

0 comments on commit 68de675

Please sign in to comment.