packages feed

pykinos-containers-0.1.1.0: src/Pykinos/Containers/IntSet.hs

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DerivingStrategies #-}
-- | Module: Pykinos.Containers.IntSet
-- Description: Drop-in Data.IntSet façade backed by unboxed Roaring Bitmaps
--
-- Provides drop-in API parity with @Data.IntSet@ from the @containers@ package,
-- using cache-conscious unboxed 'RoaringIntSet' representations under the hood.
--
-- = Architectural Guarantees
--
-- * Zero GC pointer chasing: Key arrays and bitmap chunks reside in unpinned
--   'Data.Primitive.PrimArray' buffers.
-- * Dual representation: Sparse sets (< 4096 elements per 16-bit partition)
--   use packed sorted 'Data.Word.Word16' arrays; dense sets use 1024-word bitmasks.
-- * Drop-in interface: Identical signatures to @Data.IntSet@ for 'empty',
--   'singleton', 'member', 'insert', 'delete', 'union', 'intersection', 'difference',
--   'size', 'toAscList', and 'fromList'.
module Pykinos.Containers.IntSet
  ( -- * IntSet Type
    IntSet(..)
    -- * Construction
  , empty
  , singleton
  , fromList
    -- * Queries
  , null
  , size
  , member
  , notMember
    -- * Set Operations
  , insert
  , delete
  , union
  , intersection
  , difference
    -- * Conversions
  , toAscList
  , toList
  ) where

import Prelude hiding (null)
import Control.DeepSeq (NFData(..))
import Data.Int (Int32)
import Pykinos.Bitset.Roaring
  ( RoaringIntSet(..)
  , deleteRoaring
  , differenceRoaring
  , emptyRoaring
  , foldlRoaring'
  , fromListRoaring
  , insertRoaring
  , intersectRoaring
  , memberRoaring
  , nullRoaring
  , singletonRoaring
  , toListRoaring
  , unionRoaring
  )

-- | High-performance integer set backed by an unboxed Roaring Bitmap.
newtype IntSet = IntSet { unIntSet :: RoaringIntSet }

instance Eq IntSet where
  s1 == s2 = toAscList s1 == toAscList s2
  {-# INLINE (==) #-}

instance Show IntSet where
  showsPrec p s = showParen (p > 10) $
    showString "fromList " . showsPrec 11 (toAscList s)

instance Semigroup IntSet where
  (<>) = union
  {-# INLINE (<>) #-}

instance Monoid IntSet where
  mempty = empty
  {-# INLINE mempty #-}

instance NFData IntSet where
  rnf (IntSet (RoaringIntSet !keys !chunks)) = keys `seq` chunks `seq` ()
  {-# INLINE rnf #-}

-- | Construct the empty set.
--
-- Running time: \(O(1)\).
empty :: IntSet
empty = IntSet emptyRoaring
{-# INLINE empty #-}

-- | Construct a set containing a single integer.
--
-- Running time: \(O(1)\).
singleton :: Int -> IntSet
singleton = IntSet . singletonRoaring
{-# INLINE singleton #-}

-- | Test whether the set is empty.
--
-- Running time: \(O(1)\).
null :: IntSet -> Bool
null = nullRoaring . unIntSet
{-# INLINE null #-}

-- | Number of elements in the set.
--
-- Running time: \(O(N)\).
size :: IntSet -> Int
size = foldlRoaring' (\acc _ -> acc + 1) 0 . unIntSet
{-# INLINE size #-}

-- | Query element membership.
--
-- Running time: \(O(\log C)\) for sparse chunks, \(O(1)\) for dense bitmap chunks.
member :: Int -> IntSet -> Bool
member x = memberRoaring x . unIntSet
{-# INLINE member #-}

-- | Query element non-membership.
notMember :: Int -> IntSet -> Bool
notMember x s = not (member x s)
{-# INLINE notMember #-}

-- | Insert an element into the set.
insert :: Int -> IntSet -> IntSet
insert x = IntSet . insertRoaring x . unIntSet
{-# INLINE insert #-}

-- | Delete an element from the set.
delete :: Int -> IntSet -> IntSet
delete x = IntSet . deleteRoaring x . unIntSet
{-# INLINE delete #-}

-- | Union of two sets.
union :: IntSet -> IntSet -> IntSet
union (IntSet s1) (IntSet s2) = IntSet (unionRoaring s1 s2)
{-# INLINE union #-}

-- | Intersection of two sets.
intersection :: IntSet -> IntSet -> IntSet
intersection (IntSet s1) (IntSet s2) = IntSet (intersectRoaring s1 s2)
{-# INLINE intersection #-}

-- | Difference between two sets.
difference :: IntSet -> IntSet -> IntSet
difference (IntSet s1) (IntSet s2) = IntSet (differenceRoaring s1 s2)
{-# INLINE difference #-}

-- | Convert the set to a list of elements in ascending signed order.
toAscList :: IntSet -> [Int]
toAscList s =
  let raw = map decodeInt32 (toListRoaring (unIntSet s))
      (nonNegs, negs) = span (>= 0) raw
  in negs ++ nonNegs
  where
    decodeInt32 !x = fromIntegral (fromIntegral x :: Int32) :: Int
{-# INLINE toAscList #-}

-- | Alias for 'toAscList'.
toList :: IntSet -> [Int]
toList = toAscList
{-# INLINE toList #-}

-- | Construct a set from an arbitrary list of integers.
fromList :: [Int] -> IntSet
fromList = IntSet . fromListRoaring
{-# INLINE fromList #-}