pykinos-core-0.1.1.0: src/Pykinos/DSU/WordDSU.hs
{-# LANGUAGE BangPatterns #-}
-- | Module: Pykinos.DSU.WordDSU
-- Description: Zero-pointer packed Word64 Union-Find (Disjoint-Set) with iterative path halving
-- License: EUPL-1.2 OR Apache-2.0
--
-- Packs @(rank :: Word32, parent :: Word32)@ into a single 'Word64' per node
-- inside a flat unscanned 'MutablePrimArray' (8 nodes per 64-byte cache line,
-- zero heap allocations per @findRoot@ / @unionNodes@).
module Pykinos.DSU.WordDSU
( WordDSU
, newWordDSU
, dsuSize
, findRoot
, unionNodes
, sameSet
) where
import Control.Monad (when)
import Control.Monad.ST (ST)
import Data.Bits (shiftL, shiftR, (.&.), (.|.))
import Data.Primitive.PrimArray
( MutablePrimArray
, newPrimArray
, readPrimArray
, writePrimArray
)
import Data.Word (Word32, Word64)
-- | Flat unboxed disjoint-set forest in @ST s@.
data WordDSU s = WordDSU {-# UNPACK #-} !Int !(MutablePrimArray s Word64)
{-# INLINE packEntry #-}
packEntry :: Word32 -> Word32 -> Word64
packEntry !parent !rank =
(fromIntegral rank `shiftL` 32) .|. fromIntegral parent
{-# INLINE unpackParent #-}
unpackParent :: Word64 -> Word32
unpackParent !w = fromIntegral (w .&. 0xFFFFFFFF)
{-# INLINE unpackRank #-}
unpackRank :: Word64 -> Word32
unpackRank !w = fromIntegral (w `shiftR` 32)
-- | Allocates a 'WordDSU' of @n@ singleton sets @{0}, {1}, ..., {n - 1}@.
newWordDSU :: Int -> ST s (WordDSU s)
newWordDSU !n = do
arr <- newPrimArray n
let go !i
| i >= n = pure ()
| otherwise = do
writePrimArray arr i (packEntry (fromIntegral i) 0)
go (i + 1)
go 0
pure (WordDSU n arr)
-- | Returns the fixed vertex capacity of the disjoint-set forest.
dsuSize :: WordDSU s -> Int
dsuSize (WordDSU n _) = n
-- | Finds the canonical root representative of @x@ using iterative path halving.
findRoot :: WordDSU s -> Word32 -> ST s Word32
findRoot (WordDSU _ arr) = go
where
go !x = do
wx <- readPrimArray arr (fromIntegral x)
let !px = unpackParent wx
if px == x
then pure x
else do
wpx <- readPrimArray arr (fromIntegral px)
let !gpx = unpackParent wpx
when (gpx /= px) $
writePrimArray arr (fromIntegral x) (packEntry gpx (unpackRank wx))
go gpx
-- | Merges the equivalence classes of @a@ and @b@ by rank.
-- Returns 'True' if they were previously distinct, 'False' if already unified.
unionNodes :: WordDSU s -> Word32 -> Word32 -> ST s Bool
unionNodes dsu@(WordDSU _ arr) !a !b = do
ra <- findRoot dsu a
rb <- findRoot dsu b
if ra == rb
then pure False
else do
wa <- readPrimArray arr (fromIntegral ra)
wb <- readPrimArray arr (fromIntegral rb)
let !rkA = unpackRank wa
!rkB = unpackRank wb
if rkA < rkB
then writePrimArray arr (fromIntegral ra) (packEntry rb rkA)
else if rkA > rkB
then writePrimArray arr (fromIntegral rb) (packEntry ra rkB)
else do
writePrimArray arr (fromIntegral rb) (packEntry ra rkB)
writePrimArray arr (fromIntegral ra) (packEntry ra (rkA + 1))
pure True
-- | Returns 'True' iff @a@ and @b@ belong to the same equivalence class.
sameSet :: WordDSU s -> Word32 -> Word32 -> ST s Bool
sameSet dsu !a !b = do
ra <- findRoot dsu a
rb <- findRoot dsu b
pure (ra == rb)