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
32 data BreakArray = BA (MutableByteArray# RealWorld)
34 breakOff, breakOn :: Word
39 showBreakArray :: BreakArray -> IO ()
40 showBreakArray array = do
42 | count == sz = return ()
44 val <- readBreakArray array count
45 putStr $ " " ++ show val
50 setBreakOn :: BreakArray -> Int -> IO Bool
51 setBreakOn array index
52 | safeIndex array index = do
53 writeBreakArray array index breakOn
55 | otherwise = return False
57 setBreakOff :: BreakArray -> Int -> IO Bool
58 setBreakOff array index
59 | safeIndex array index = do
60 writeBreakArray array index breakOff
62 | otherwise = return False
64 getBreak :: BreakArray -> Int -> IO (Maybe Word)
66 | safeIndex array index = do
67 val <- readBreakArray array index
69 | otherwise = return Nothing
71 safeIndex :: BreakArray -> Int -> Bool
72 safeIndex array index = index < size array && index >= 0
74 size :: BreakArray -> Int
75 size (BA array) = (I# (sizeofMutableByteArray# array)) `div` wORD_SIZE
77 allocBA :: Int -> IO BreakArray
78 allocBA (I# sz) = IO $ \s1 ->
79 case newByteArray# sz s1 of { (# s2, array #) -> (# s2, BA array #) }
81 -- create a new break array and initialise elements to zero
82 newBreakArray :: Int -> IO BreakArray
83 newBreakArray entries@(I# sz) = do
84 BA array <- allocBA (entries * wORD_SIZE)
86 W# off -> do -- Todo: there must be a better way to write zero as a Word!
88 | n ==# sz = return ()
95 writeBA# :: MutableByteArray# RealWorld -> Int# -> Word# -> IO ()
96 writeBA# array i word = IO $ \s ->
97 case writeWordArray# array i word s of { s -> (# s, () #) }
99 writeBreakArray :: BreakArray -> Int -> Word -> IO ()
100 writeBreakArray (BA array) (I# i) (W# word) = writeBA# array i word
102 readBA# :: MutableByteArray# RealWorld -> Int# -> IO Word
103 readBA# array i = IO $ \s ->
104 case readWordArray# array i s of { (# s, c #) -> (# s, W# c #) }
106 readBreakArray :: BreakArray -> Int -> IO Word
107 readBreakArray (BA array) (I# i) = readBA# array i