Changes between Version 19 and Version 20 of BlockObjects
- Timestamp:
- 08/25/11 05:25:22 (22 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
BlockObjects
v19 v20 18 18 and then executing 19 19 {{{ 20 do 21 -- convert a list of strings into a C array of stable pointers to those strings in the Haskell heap 22 ptrs <- mapM newStablePtr myCharacters 23 myCharactersArray <- newArray ptrs 24 25 -- get the size in bytes of a stable pointer to a Haskell string 26 let elemSize = fromIntegral $ sizeOf (undefined :: StablePtr String) 20 do { -- convert a list of strings into a C array of stable pointers to those strings in the Haskell heap 21 ; ptrs <- mapM newStablePtr myCharacters 22 ; sortedPtrs <- withArray ptrs $ \myCharactersArray -> do 23 { 24 -- get the size in bytes of a stable pointer to a Haskell string 25 ; let elemSize = fromIntegral $ sizeOf (undefined :: StablePtr String) 27 26 28 -- invoke C land 'qsort_b' with a Haskell comparison function passed as a block object; mutates 'myCharactersArray'29 qsort_b myCharactersArray (length myCharacters) elemSize (\l r -> fromOrdering (l `compare` r))27 -- invoke C land 'qsort_b' with a Haskell comparison function passed as a block object; mutates 'myCharactersArray' 28 ; qsort_b myCharactersArray (length myCharacters) elemSize (\l r -> fromOrdering (l `compare` r)) 30 29 31 -- turn the array of Haskell strings back into a list of strings 32 mySortedCharacters <- mapM deRefStablePtr myCharactersArray 30 -- obtain the sorted list of stable pointers from the sorted array 31 ; peekArray (length ptrs) myCharactersArray 32 } 33 34 -- turn the array of Haskell strings back into a list of strings 35 ; mySortedCharacters <- mapM deRefStablePtr sortedPtrs 36 } 33 37 }}} 34 38 Here we compare entire strings and not just the first characters as in the C implementation. The marshalling function `fromOrdering` is defined as follows:
