{-
   An extremely simple and inefficient implementation of bit sets.
   Sven Panne 2000.   mailto:Sven.Panne@informatik.uni-muenchen.de
-}

module BitSet where

import Ix(rangeSize)
import Monad(liftM)
import Data.Array.IO

newtype BitSet = BitSet (IOArray Int Bool)

emptyBS :: Int -> IO BitSet
emptyBS size = liftM BitSet $ newArray (0, size-1) False

clearBS :: BitSet -> Int -> IO ()
clearBS (BitSet bs) i = writeArray bs i False

clearAllBS :: BitSet -> IO ()
clearAllBS bs = mapM_ (clearBS bs) [0 .. sizeBS bs - 1]

setBS :: BitSet -> Int -> IO ()
setBS (BitSet bs) i = writeArray bs i True

isSetBS :: BitSet -> Int -> IO Bool
isSetBS (BitSet bs) i = readArray bs i

sizeBS :: BitSet -> Int
sizeBS (BitSet bs) = rangeSize (bounds bs)
