Skip to content

Commit a3ec4a2

Browse files
Fix #814: add argSet and fromArgSet (#817)
Co-authored-by: David Feuer <[email protected]>
1 parent b855e5e commit a3ec4a2

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

containers-tests/tests/map-properties.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Data.Monoid
1919
import Data.Maybe hiding (mapMaybe)
2020
import qualified Data.Maybe as Maybe (mapMaybe)
2121
import Data.Ord
22+
import Data.Semigroup (Arg(..))
2223
import Data.Function
2324
import qualified Data.Foldable as Foldable
2425
#if MIN_VERSION_base(4,10,0)
@@ -99,7 +100,9 @@ main = defaultMain $ testGroup "map-properties"
99100
, testCase "keys" test_keys
100101
, testCase "assocs" test_assocs
101102
, testCase "keysSet" test_keysSet
103+
, testCase "argSet" test_argSet
102104
, testCase "fromSet" test_fromSet
105+
, testCase "fromArgSet" test_fromArgSet
103106
, testCase "toList" test_toList
104107
, testCase "fromList" test_fromList
105108
, testCase "fromListWith" test_fromListWith
@@ -238,7 +241,9 @@ main = defaultMain $ testGroup "map-properties"
238241
, testProperty "bifoldl'" prop_bifoldl'
239242
#endif
240243
, testProperty "keysSet" prop_keysSet
244+
, testProperty "argSet" prop_argSet
241245
, testProperty "fromSet" prop_fromSet
246+
, testProperty "fromArgSet" prop_fromArgSet
242247
, testProperty "takeWhileAntitone" prop_takeWhileAntitone
243248
, testProperty "dropWhileAntitone" prop_dropWhileAntitone
244249
, testProperty "spanAntitone" prop_spanAntitone
@@ -711,11 +716,21 @@ test_keysSet = do
711716
keysSet (fromList [(5,"a"), (3,"b")]) @?= Set.fromList [3,5]
712717
keysSet (empty :: UMap) @?= Set.empty
713718

719+
test_argSet :: Assertion
720+
test_argSet = do
721+
argSet (fromList [(5,"a"), (3,"b")]) @?= Set.fromList [Arg 3 "b",Arg 5 "a"]
722+
argSet (empty :: UMap) @?= Set.empty
723+
714724
test_fromSet :: Assertion
715725
test_fromSet = do
716726
fromSet (\k -> replicate k 'a') (Set.fromList [3, 5]) @?= fromList [(5,"aaaaa"), (3,"aaa")]
717727
fromSet undefined Set.empty @?= (empty :: IMap)
718728

729+
test_fromArgSet :: Assertion
730+
test_fromArgSet = do
731+
fromArgSet (Set.fromList [Arg 3 "aaa", Arg 5 "aaaaa"]) @?= fromList [(5,"aaaaa"), (3,"aaa")]
732+
fromArgSet Set.empty @?= (empty :: IMap)
733+
719734
----------------------------------------------------------------
720735
-- Lists
721736

@@ -1556,7 +1571,16 @@ prop_keysSet :: [(Int, Int)] -> Bool
15561571
prop_keysSet xs =
15571572
keysSet (fromList xs) == Set.fromList (List.map fst xs)
15581573

1574+
prop_argSet :: [(Int, Int)] -> Bool
1575+
prop_argSet xs =
1576+
argSet (fromList xs) == Set.fromList (List.map (uncurry Arg) xs)
1577+
15591578
prop_fromSet :: [(Int, Int)] -> Bool
15601579
prop_fromSet ys =
15611580
let xs = List.nubBy ((==) `on` fst) ys
15621581
in fromSet (\k -> fromJust $ List.lookup k xs) (Set.fromList $ List.map fst xs) == fromList xs
1582+
1583+
prop_fromArgSet :: [(Int, Int)] -> Bool
1584+
prop_fromArgSet ys =
1585+
let xs = List.nubBy ((==) `on` fst) ys
1586+
in fromArgSet (Set.fromList $ List.map (uncurry Arg) xs) == fromList xs

containers/src/Data/Map/Internal.hs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ module Data.Map.Internal (
264264
, keys
265265
, assocs
266266
, keysSet
267+
, argSet
267268
, fromSet
269+
, fromArgSet
268270

269271
-- ** Lists
270272
, toList
@@ -370,7 +372,7 @@ import Data.Functor.Identity (Identity (..))
370372
import Control.Applicative (liftA3)
371373
import Data.Functor.Classes
372374
import Data.Semigroup (stimesIdempotentMonoid)
373-
import Data.Semigroup (Semigroup(stimes))
375+
import Data.Semigroup (Arg(..), Semigroup(stimes))
374376
#if !(MIN_VERSION_base(4,11,0))
375377
import Data.Semigroup (Semigroup((<>)))
376378
#endif
@@ -3362,6 +3364,15 @@ keysSet :: Map k a -> Set.Set k
33623364
keysSet Tip = Set.Tip
33633365
keysSet (Bin sz kx _ l r) = Set.Bin sz kx (keysSet l) (keysSet r)
33643366

3367+
-- | \(O(n)\). The set of all elements of the map contained in 'Arg's.
3368+
--
3369+
-- > argSet (fromList [(5,"a"), (3,"b")]) == Data.Set.fromList [Arg 3 "b",Arg 5 "a"]
3370+
-- > argSet empty == Data.Set.empty
3371+
3372+
argSet :: Map k a -> Set.Set (Arg k a)
3373+
argSet Tip = Set.Tip
3374+
argSet (Bin sz kx x l r) = Set.Bin sz (Arg kx x) (argSet l) (argSet r)
3375+
33653376
-- | \(O(n)\). Build a map from a set of keys and a function which for each key
33663377
-- computes its value.
33673378
--
@@ -3372,6 +3383,15 @@ fromSet :: (k -> a) -> Set.Set k -> Map k a
33723383
fromSet _ Set.Tip = Tip
33733384
fromSet f (Set.Bin sz x l r) = Bin sz x (f x) (fromSet f l) (fromSet f r)
33743385

3386+
-- | /O(n)/. Build a map from a set of elements contained inside 'Arg's.
3387+
--
3388+
-- > fromArgSet (Data.Set.fromList [Arg 3 "aaa", Arg 5 "aaaaa"]) == fromList [(5,"aaaaa"), (3,"aaa")]
3389+
-- > fromArgSet Data.Set.empty == empty
3390+
3391+
fromArgSet :: Set.Set (Arg k a) -> Map k a
3392+
fromArgSet Set.Tip = Tip
3393+
fromArgSet (Set.Bin sz (Arg x v) l r) = Bin sz x v (fromArgSet l) (fromArgSet r)
3394+
33753395
{--------------------------------------------------------------------
33763396
Lists
33773397
--------------------------------------------------------------------}

containers/src/Data/Map/Lazy.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ module Data.Map.Lazy (
9292
, empty
9393
, singleton
9494
, fromSet
95+
, fromArgSet
9596

9697
-- ** From Unordered Lists
9798
, fromList
@@ -207,6 +208,7 @@ module Data.Map.Lazy (
207208
, keys
208209
, assocs
209210
, keysSet
211+
, argSet
210212

211213
-- ** Lists
212214
, toList

containers/src/Data/Map/Strict.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ module Data.Map.Strict
108108
, empty
109109
, singleton
110110
, fromSet
111+
, fromArgSet
111112

112113
-- ** From Unordered Lists
113114
, fromList
@@ -223,6 +224,7 @@ module Data.Map.Strict
223224
, keys
224225
, assocs
225226
, keysSet
227+
, argSet
226228

227229
-- ** Lists
228230
, toList

containers/src/Data/Map/Strict/Internal.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ module Data.Map.Strict.Internal
226226
, keys
227227
, assocs
228228
, keysSet
229+
, argSet
229230
, fromSet
231+
, fromArgSet
230232

231233
-- ** Lists
232234
, toList
@@ -327,6 +329,7 @@ import Data.Map.Internal
327329
, (!)
328330
, (!?)
329331
, (\\)
332+
, argSet
330333
, assocs
331334
, atKeyImpl
332335
, atKeyPlain
@@ -413,6 +416,7 @@ import Data.Map.Internal.DeprecatedShowTree (showTree, showTreeWith)
413416
import Data.Map.Internal.Debug (valid)
414417

415418
import Control.Applicative (Const (..), liftA3)
419+
import Data.Semigroup (Arg (..))
416420
import qualified Data.Set.Internal as Set
417421
import qualified Data.Map.Internal as L
418422
import Utils.Containers.Internal.StrictPair
@@ -1467,6 +1471,15 @@ fromSet :: (k -> a) -> Set.Set k -> Map k a
14671471
fromSet _ Set.Tip = Tip
14681472
fromSet f (Set.Bin sz x l r) = case f x of v -> v `seq` Bin sz x v (fromSet f l) (fromSet f r)
14691473

1474+
-- | /O(n)/. Build a map from a set of elements contained inside 'Arg's.
1475+
--
1476+
-- > fromArgSet (Data.Set.fromList [Arg 3 "aaa", Arg 5 "aaaaa"]) == fromList [(5,"aaaaa"), (3,"aaa")]
1477+
-- > fromArgSet Data.Set.empty == empty
1478+
1479+
fromArgSet :: Set.Set (Arg k a) -> Map k a
1480+
fromArgSet Set.Tip = Tip
1481+
fromArgSet (Set.Bin sz (Arg x v) l r) = v `seq` Bin sz x v (fromArgSet l) (fromArgSet r)
1482+
14701483
{--------------------------------------------------------------------
14711484
Lists
14721485
--------------------------------------------------------------------}

0 commit comments

Comments
 (0)