1 -----------------------------------------------------------------------------
3 -- Break Arrays in the IO monad
4 -- Entries in the array are Word sized
6 -- (c) The University of Glasgow 2007
8 -----------------------------------------------------------------------------
11 -- The above warning supression flag is a temporary kludge.
12 -- While working on this module you are encouraged to remove it and fix
13 -- any warnings in the module. See
14 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
19 -- constructor is exported only for ByteCodeGen
33 data BreakArray = BA (MutableByteArray# RealWorld)
35 breakOff, breakOn :: Word
40 showBreakArray :: BreakArray -> IO ()
41 showBreakArray array = do
43 | count == sz = return ()
45 val <- readBreakArray array count
46 putStr $ " " ++ show val
51 setBreakOn :: BreakArray -> Int -> IO Bool
52 setBreakOn array index
53 | safeIndex array index = do
54 writeBreakArray array index breakOn
56 | otherwise = return False
58 setBreakOff :: BreakArray -> Int -> IO Bool
59 setBreakOff array index
60 | safeIndex array index = do
61 writeBreakArray array index breakOff
63 | otherwise = return False
65 getBreak :: BreakArray -> Int -> IO (Maybe Word)
67 | safeIndex array index = do
68 val <- readBreakArray array index
70 | otherwise = return Nothing
72 safeIndex :: BreakArray -> Int -> Bool
73 safeIndex array index = index < size array && index >= 0
75 size :: BreakArray -> Int
76 size (BA array) = (I# (sizeofMutableByteArray# array)) `div` wORD_SIZE
78 allocBA :: Int -> IO BreakArray
79 allocBA (I# sz) = IO $ \s1 ->
80 case newByteArray# sz s1 of { (# s2, array #) -> (# s2, BA array #) }
82 -- create a new break array and initialise elements to zero
83 newBreakArray :: Int -> IO BreakArray
84 newBreakArray entries@(I# sz) = do
85 BA array <- allocBA (entries * wORD_SIZE)
87 W# off -> do -- Todo: there must be a better way to write zero as a Word!
89 | n ==# sz = return ()
96 writeBA# :: MutableByteArray# RealWorld -> Int# -> Word# -> IO ()
97 writeBA# array i word = IO $ \s ->
98 case writeWordArray# array i word s of { s -> (# s, () #) }
100 writeBreakArray :: BreakArray -> Int -> Word -> IO ()
101 writeBreakArray (BA array) (I# i) (W# word) = writeBA# array i word
103 readBA# :: MutableByteArray# RealWorld -> Int# -> IO Word
104 readBA# array i = IO $ \s ->
105 case readWordArray# array i s of { (# s, c #) -> (# s, W# c #) }
107 readBreakArray :: BreakArray -> Int -> IO Word
108 readBreakArray (BA array) (I# i) = readBA# array i