Permalink
Browse files

Array.hs refactoring

  • Loading branch information...
1 parent b2e3682 commit 28321d4cc04b2b3f4323415996993e2c67ef0f79 @bsl bsl committed Aug 10, 2009
Showing with 10 additions and 3 deletions.
  1. +10 −3 src/System/Event/Array.hs
View
@@ -46,9 +46,11 @@ empty :: IO (Array a)
empty = fmap Array (newIORef (AC nullPtr 0 0))
new :: Storable a => Int -> IO (Array a)
-new cap = do
+new c = do
es <- mallocArray cap
fmap Array (newIORef (AC es 0 cap))
+ where
+ cap = firstPowerOf2 c
length :: Array a -> IO Int
length (Array ref) = do
@@ -86,8 +88,7 @@ ensureCapacity (Array ref) c = do
es' <- reallocArray es cap'
writeIORef ref (AC es' len cap')
where
- cap' | c == 0 = 64
- | otherwise = (ceiling . logBase (2 :: Double) . realToFrac) c
+ cap' = firstPowerOf2 c
useAsPtr :: Array a -> (Ptr a -> Int -> IO b) -> IO b
useAsPtr (Array ref) f = do
@@ -112,3 +113,9 @@ mapM_ (Array ref) f = do
peek (es `plusPtr` n) >>= f
loop (succ n)
loop 0
+
+firstPowerOf2 :: Int -> Int
+firstPowerOf2 n
+ | n <= 0 = 0
+ | otherwise = 2^p
+ where p = (ceiling . logBase (2 :: Double) . realToFrac) n :: Int

0 comments on commit 28321d4

Please sign in to comment.