Idris2/libs/contrib/Data/SortedSet.idr

87 lines
2.0 KiB
Idris
Raw Normal View History

2020-05-18 15:59:07 +03:00
module Data.SortedSet
import Data.Maybe
import Data.SortedMap
%hide Prelude.toList
2020-05-18 15:59:07 +03:00
export
data SortedSet k = SetWrapper (Data.SortedMap.SortedMap k ())
export
empty : Ord k => SortedSet k
empty = SetWrapper Data.SortedMap.empty
export
insert : k -> SortedSet k -> SortedSet k
insert k (SetWrapper m) = SetWrapper (Data.SortedMap.insert k () m)
export
delete : k -> SortedSet k -> SortedSet k
delete k (SetWrapper m) = SetWrapper (Data.SortedMap.delete k m)
export
contains : k -> SortedSet k -> Bool
contains k (SetWrapper m) = isJust (Data.SortedMap.lookup k m)
export
fromList : Ord k => List k -> SortedSet k
fromList l = SetWrapper (Data.SortedMap.fromList (map (\i => (i, ())) l))
export
toList : SortedSet k -> List k
2021-06-01 17:05:04 +03:00
toList (SetWrapper m) = keys m
2020-05-18 15:59:07 +03:00
export
Foldable SortedSet where
2021-06-01 17:05:04 +03:00
foldr f z = foldr f z . Data.SortedSet.toList
foldl f z = foldl f z . Data.SortedSet.toList
2020-05-18 15:59:07 +03:00
null (SetWrapper m) = null m
2021-06-01 17:05:04 +03:00
foldMap f = foldMap f . Data.SortedSet.toList
2020-05-18 15:59:07 +03:00
||| Set union. Inserts all elements of x into y
export
union : (x, y : SortedSet k) -> SortedSet k
union x y = foldr insert x y
||| Set difference. Delete all elments in y from x
export
difference : (x, y : SortedSet k) -> SortedSet k
difference x y = foldr delete x y
||| Set symmetric difference. Uses the union of the differences.
export
symDifference : (x, y : SortedSet k) -> SortedSet k
symDifference x y = union (difference x y) (difference y x)
||| Set intersection. Implemented as the difference of the union and the symetric difference.
export
intersection : (x, y : SortedSet k) -> SortedSet k
intersection x y = difference x (difference x y)
export
Ord k => Semigroup (SortedSet k) where
(<+>) = union
export
Ord k => Monoid (SortedSet k) where
neutral = empty
2021-03-01 16:22:46 +03:00
export
Eq k => Eq (SortedSet k) where
SetWrapper x == SetWrapper y = x == y
export
Show k => Show (SortedSet k) where
show m = "fromList " ++ (show $ toList m)
2020-05-18 15:59:07 +03:00
export
keySet : SortedMap k v -> SortedSet k
keySet = SetWrapper . map (const ())
2020-09-19 15:22:54 +03:00
export
singleton : Ord k => k -> SortedSet k
singleton k = insert k empty