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

NonEmpty AdjacencyIntMaps #250

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions algebraic-graphs.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ library
Algebra.Graph.Labelled.Example.Automaton,
Algebra.Graph.Labelled.Example.Network,
Algebra.Graph.NonEmpty,
Algebra.Graph.NonEmpty.AdjacencyIntMap,
Algebra.Graph.NonEmpty.AdjacencyMap,
Algebra.Graph.Relation,
Algebra.Graph.Relation.Preorder,
Expand Down Expand Up @@ -142,6 +143,7 @@ test-suite test-alga
Algebra.Graph.Test.Label,
Algebra.Graph.Test.Labelled.AdjacencyMap,
Algebra.Graph.Test.Labelled.Graph,
Algebra.Graph.Test.NonEmpty.AdjacencyIntMap,
Algebra.Graph.Test.NonEmpty.AdjacencyMap,
Algebra.Graph.Test.NonEmpty.Graph,
Algebra.Graph.Test.Relation,
Expand Down
4 changes: 2 additions & 2 deletions src/Algebra/Graph/AdjacencyIntMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ hasEdge u v (AM m) = case IntMap.lookup u m of
Just vs -> IntSet.member v vs

-- | The number of vertices in a graph.
-- Complexity: /O(1)/ time.
-- Complexity: /O(n)/ time.
--
-- @
-- vertexCount 'empty' == 0
Expand All @@ -420,7 +420,7 @@ vertexCount :: AdjacencyIntMap -> Int
vertexCount = IntMap.size . adjacencyIntMap

-- | The number of edges in a graph.
-- Complexity: /O(n)/ time.
-- Complexity: /O(n+m)/ time.
--
-- @
-- edgeCount 'empty' == 0
Expand Down
17 changes: 13 additions & 4 deletions src/Algebra/Graph/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ module Algebra.Graph.Internal (
maybeF,

-- * Utilities
setProduct, setProductWith, forEach, forEachInt
forEach, forEachInt, setProduct, setProductWith, unsafeNonEmpty
) where

import Data.Foldable
import Data.Maybe
import Data.Semigroup
import Data.IntSet (IntSet)
import Data.Set (Set)

import qualified Data.IntSet as IntSet
import qualified Data.Set as Set
import qualified GHC.Exts as Exts
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.IntSet as IntSet
import qualified Data.Set as Set
import qualified GHC.Exts as Exts

-- | An abstract list data type with /O(1)/ time concatenation (the current
-- implementation uses difference lists). Here @a@ is the type of list elements.
Expand Down Expand Up @@ -138,3 +140,10 @@ forEach s f = Set.foldr (\a u -> f a *> u) (pure ()) s
-- | Perform an applicative action for each member of an IntSet.
forEachInt :: Applicative f => IntSet -> (Int -> f a) -> f ()
forEachInt s f = IntSet.foldr (\a u -> f a *> u) (pure ()) s

-- | Unsafe creation of a NonEmpty list.
unsafeNonEmpty :: [a] -> NonEmpty.NonEmpty a
unsafeNonEmpty = fromMaybe (error msg) . NonEmpty.nonEmpty
where
msg = "unsafeNonEmpty: Graph is empty"

Loading