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 19, 2019
1 parent 4f29d02 commit c99b359
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 153 deletions.
6 changes: 3 additions & 3 deletions containers-tests/tests/set-properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ mkArb step n
p <- step
q <- step
if dir
then return (Bin 2 q (singleton p) Tip)
else return (Bin 2 p Tip (singleton q))
then return (NE $ Bin 2 q (singleton p) Tip)
else return (NE $ Bin 2 p Tip (singleton q))
| otherwise = do
-- This assumes a balance factor of delta = 3
let upper = (3*(n - 1)) `quot` 4
let lower = (n + 2) `quot` 4
ln <- liftGen $ choose (lower, upper)
let rn = n - ln - 1
liftM3 (\lt x rt -> Bin n x lt rt) (mkArb step ln) step (mkArb step rn)
liftM3 (\lt x rt -> NE $ Bin n x lt rt) (mkArb step ln) step (mkArb step rn)

-- | Given a strictly increasing list of elements, produce an arbitrarily
-- shaped set with exactly those elements.
Expand Down
7 changes: 4 additions & 3 deletions containers/src/Data/Map/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@ difference t1 (NE (Bin _ 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.NE (Set.Bin _ 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 @@ -3356,7 +3356,8 @@ assocs m

keysSet :: Map k a -> Set.Set k
keysSet Tip = Set.Tip
keysSet (NE (Bin sz kx _ l r)) = Set.Bin sz kx (keysSet l) (keysSet r)
keysSet (NE (Bin sz kx _ l r)) = Set.NE $
Set.Bin 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 @@ -3366,7 +3367,7 @@ keysSet (NE (Bin 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) = NE $ Bin sz x (f x) (fromSet f l) (fromSet f r)
fromSet f (Set.NE (Set.Bin sz x l r)) = NE $ Bin sz x (f x) (fromSet f l) (fromSet f r)

{--------------------------------------------------------------------
Lists
Expand Down
3 changes: 2 additions & 1 deletion containers/src/Data/Map/Strict/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,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` NE (Bin sz x v (fromSet f l) (fromSet f r))
fromSet f (Set.NE (Set.Bin sz x l r)) = case f x of
v -> v `seq` NE (Bin sz x v (fromSet f l) (fromSet f r))

{--------------------------------------------------------------------
Lists
Expand Down
1 change: 1 addition & 0 deletions containers/src/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 c99b359

Please sign in to comment.