module Data.PlanarGraph.Internal whereimport Control.Monad.STimport Data.STRefimport qualified Data.Vector as V (unsafeFreeze)import qualified Data.Vector as Vectorimport Data.Vector.Circular (CircularVector)import qualified Data.Vector.Circular as CVimport Data.Vector.Mutable (STVector)import qualified Data.Vector.Mutable as V--------------------------------------------------------------------------------- Resizeable vectortype GrowVector s v = STRef s (STVector s v)newVector :: Int -> ST s (GrowVector s v)newVector n = newSTRef =<< V.new nsetVector :: GrowVector s v -> v -> ST s ()setVector ref val = do vec <- readSTRef ref V.set vec valreadVector :: GrowVector s v -> Int -> ST s vreadVector ref idx = do v <- readSTRef ref V.read v idxwriteVector :: GrowVector s v -> Int -> v -> ST s ()writeVector ref idx val = do v <- readSTRef ref let l = V.length v if idx >= l then {-trace ("Growing: " ++ show (idx, l)) $ -} do v' <- V.grow v ((idx+1)*2) V.write v' idx val writeSTRef ref v' else -- trace ("Writing: " ++ show (idx, l)) $ V.write v idx valfreezeVector :: GrowVector s v -> ST s (Vector.Vector v)freezeVector ref = Vector.freeze =<< readSTRef refunsafeFreezeVector :: GrowVector s v -> ST s (Vector.Vector v)unsafeFreezeVector ref = Vector.unsafeFreeze =<< readSTRef refthawVector :: Vector.Vector v -> ST s (GrowVector s v)thawVector v = newSTRef =<< Vector.thaw vunsafeThawVector :: Vector.Vector v -> ST s (GrowVector s v)unsafeThawVector v = newSTRef =<< Vector.unsafeThaw vfreezeCircularVector :: Int -> GrowVector s v -> ST s (CircularVector v)freezeCircularVector n ref = (CV.unsafeFromVector . Vector.take n) <$> (V.unsafeFreeze =<< readSTRef ref)--------------------------------------------------------------------------------- Planar graphtype EdgeId = Inttype HalfEdgeId = Inttype VertexId = Int-- | Numerical face identifier. Negative numbers indicate boundaries, non-negative numbers-- are internal faces.type FaceId = Int-- PlanarGraphs have vertices, edges, and faces.-- Invariant: The half-edge of a boundary vertex is interior, twin is exterior.-- FIXME: Use STRefU ?-- PlanarGraph with 0 vertices: No edges, no vertices, no faces.-- PlanarGraph with 1 vertex: No edges, no interior faces.-- PlanarGraph with 2 vertices: One edge, no interior faces.-- PlanarGraph with 3+ vertices: Usual properties hold.data PlanarGraph s = PlanarGraph { pgNextHalfEdgeId :: !(STRef s HalfEdgeId) , pgNextVertexId :: !(STRef s VertexId) , pgNextFaceId :: !(STRef s FaceId) , pgNextBoundaryId :: !(STRef s FaceId) , pgHalfEdgeNext :: !(GrowVector s HalfEdgeId) -- HalfEdge indexed , pgHalfEdgePrev :: !(GrowVector s HalfEdgeId) -- HalfEdge indexed , pgHalfEdgeVertex :: !(GrowVector s VertexId) -- HalfEdge indexed , pgHalfEdgeFace :: !(GrowVector s FaceId) -- HalfEdge indexed , pgVertexEdges :: !(GrowVector s HalfEdgeId) -- Vertex indexed , pgFaceEdges :: !(GrowVector s HalfEdgeId) -- Face indexed , pgBoundaryEdges :: !(GrowVector s HalfEdgeId) -- Boundary faces } deriving Eq