Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rewrite rules instead of some specialization #93

Merged
merged 1 commit into from
Jul 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/Algebra/Graph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,14 @@ vertexIntList = IntSet.toList . vertexIntSet
-- edgeList . 'edges' == 'Data.List.nub' . 'Data.List.sort'
-- edgeList . 'transpose' == 'Data.List.sort' . map 'Data.Tuple.swap' . edgeList
-- @
{-# SPECIALISE edgeList :: Graph Int -> [(Int,Int)] #-}
edgeList :: Ord a => Graph a -> [(a, a)]
edgeList = AM.edgeList . fromGraphAM
{-# INLINE[1] edgeList #-}
{-# RULES "edgeList/Int" edgeList = edgeIntList #-}

-- | Like 'edgeList' but specialised for graphs with vertices of type 'Int'.
edgeIntList :: Graph Int -> [(Int,Int)]
edgeIntList = AIM.edgeList . fromGraphAIM

-- | The set of vertices of a given graph.
-- Complexity: /O(s * log(n))/ time and /O(n)/ memory.
Expand Down Expand Up @@ -579,9 +584,14 @@ vertexIntSet = foldg IntSet.empty IntSet.singleton IntSet.union IntSet.union
-- edgeSet ('edge' x y) == Set.'Set.singleton' (x,y)
-- edgeSet . 'edges' == Set.'Set.fromList'
-- @
{-# SPECIALISE edgeSet :: Graph Int -> Set.Set (Int,Int) #-}
edgeSet :: Ord a => Graph a -> Set.Set (a, a)
edgeSet = AM.edgeSet . fromGraphAM
{-# INLINE[1] edgeSet #-}
{-# RULES "edgeSet/Int" edgeSet = edgeIntSet #-}

-- | Like 'edgeIntSet' but specialised for graphs with vertices of type 'Int'.
edgeIntSet :: Graph Int -> Set.Set (Int,Int)
edgeIntSet = AIM.edgeSet . fromGraphAIM

-- | The sorted /adjacency list/ of a graph.
-- Complexity: /O(n + m)/ time and /O(m)/ memory.
Expand Down Expand Up @@ -614,7 +624,11 @@ fromGraphAM = foldg AM.empty AM.vertex AM.overlay AM.connect

-- | Like 'adjacencyMap' but specialised for graphs with vertices of type 'Int'.
adjacencyIntMap :: Graph Int -> IntMap IntSet
adjacencyIntMap = AIM.adjacencyIntMap . foldg AIM.empty AIM.vertex AIM.overlay AIM.connect
adjacencyIntMap = AIM.adjacencyIntMap . fromGraphAIM

-- | Like 'fromGraphAM' but specialised for graphs with vertices of type 'Int'.
fromGraphAIM :: Graph Int -> AIM.AdjacencyIntMap
fromGraphAIM = foldg AIM.empty AIM.vertex AIM.overlay AIM.connect

-- | The /path/ on a list of vertices.
-- Complexity: /O(L)/ time, memory and size, where /L/ is the length of the
Expand Down