packages feed

pykinos-core-0.1.1.0: src/Pykinos/Queue/WordWorklist.hs

{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -Wno-unused-top-binds -Wno-deprecations #-}
-- | Module: Pykinos.Queue.WordWorklist
-- Description: Unboxed worklist queue with O(1) deduplication via a flat dense bitset
--
-- Combines 'WordRing' with a dense unscanned 'MutablePrimArray' bitmap to enforce
-- idempotent worklist semantics. 'enqueueUnique' checks and sets the membership bit in
-- O(1), ensuring no item is queued multiple times concurrently. 'dequeueWork' clears the bit.
module Pykinos.Queue.WordWorklist
  ( WordWorklist
  , newWordWorklist
  , enqueueUnique
  , dequeueWork
  , isWorklistMember
  , worklistLength
  ) where

import Control.Monad.ST (ST)
import Data.Bits (clearBit, setBit, shiftR, testBit, (.&.))
import Data.Primitive.PrimArray
  ( MutablePrimArray
  , newPrimArray
  , readPrimArray
  , setPrimArray
  , sizeofMutablePrimArray
  , writePrimArray
  )
import Data.Word (Word32, Word64)
import Pykinos.Queue.WordRing (WordRing, newWordRing, popFront, pushBack, ringLength)

-- | Unboxed deduplicating worklist in @ST s@.
data WordWorklist s = WordWorklist
  { wwRing   :: !(WordRing s)
  , wwBitset :: !(MutablePrimArray s Word64)
  }

-- | Allocates a 'WordWorklist' supporting items in @0 .. maxItem - 1@.
newWordWorklist :: Int -> ST s (WordWorklist s)
newWordWorklist !maxItem = do
  let !wordsNeeded = max 1 ((maxItem + 63) `shiftR` 6)
  ring <- newWordRing (min 1024 maxItem)
  bits <- newPrimArray wordsNeeded
  setPrimArray bits 0 wordsNeeded (0 :: Word64)
  pure (WordWorklist ring bits)

-- | Enqueues @item@ if not already present in the worklist. Returns 'True' if enqueued.
enqueueUnique :: WordWorklist s -> Word32 -> ST s Bool
enqueueUnique (WordWorklist ring bits) !item = do
  let !idx = fromIntegral (item `shiftR` 6)
      !bit = fromIntegral (item .&. 63)
      !numWords = sizeofMutablePrimArray bits
  if idx >= numWords
    then pure False
    else do
      w <- readPrimArray bits idx
      if testBit w bit
        then pure False
        else do
          writePrimArray bits idx (setBit w bit)
          pushBack ring item
          pure True

-- | Dequeues the next item and clears its membership bit so it can be re-queued later.
dequeueWork :: WordWorklist s -> ST s (Maybe Word32)
dequeueWork (WordWorklist ring bits) = do
  mItem <- popFront ring
  case mItem of
    Nothing -> pure Nothing
    Just item -> do
      let !idx = fromIntegral (item `shiftR` 6)
          !bit = fromIntegral (item .&. 63)
          !numWords = sizeofMutablePrimArray bits
      if idx < numWords
        then do
          w <- readPrimArray bits idx
          writePrimArray bits idx (clearBit w bit)
          pure (Just item)
        else pure (Just item)

-- | Tests whether @item@ is currently enqueued in the worklist.
isWorklistMember :: WordWorklist s -> Word32 -> ST s Bool
isWorklistMember (WordWorklist _ bits) !item = do
  let !idx = fromIntegral (item `shiftR` 6)
      !bit = fromIntegral (item .&. 63)
  if idx >= sizeofMutablePrimArray bits
    then pure False
    else do
      w <- readPrimArray bits idx
      pure (testBit w bit)

-- | Current number of enqueued items.
worklistLength :: WordWorklist s -> ST s Int
worklistLength (WordWorklist ring _) = ringLength ring