Skip to content

Commit

Permalink
Added BRGCWord8 encoding for Word32 and Word64
Browse files Browse the repository at this point in the history
  • Loading branch information
mhwombat committed Jan 4, 2016
1 parent d2f7736 commit b5728f0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
4 changes: 2 additions & 2 deletions creatur.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: creatur
Version: 5.9.8.2
Version: 5.9.9
Stability: experimental
Synopsis: Framework for artificial life experiments.
Description: A software framework for automating experiments
Expand Down Expand Up @@ -36,7 +36,7 @@ source-repository head
source-repository this
type: git
location: https://github.com/mhwombat/creatur.git
tag: 5.9.8.2
tag: 5.9.9

library
GHC-Options: -Wall -fno-warn-orphans
Expand Down
43 changes: 34 additions & 9 deletions src/ALife/Creatur/Genetics/BRGCWord8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import qualified Control.Monad.State.Lazy as S (put, get, gets)
import Data.Char (ord, chr)
import Data.Either (partitionEithers)
import Data.Functor.Identity (Identity)
import Data.Word (Word8, Word16)
import Data.Word (Word8, Word16, Word32, Word64)
import GHC.Generics

#if MIN_VERSION_base(4,8,0)
Expand Down Expand Up @@ -205,18 +205,43 @@ instance Genetic Word8 where
convert _ = Left "logic error"

instance Genetic Word16 where
put g = putAndReport
[fromIntegral $ x `div` 0x100, fromIntegral $ x `mod` 0x100]
(show g ++ " Word16")
put g = putAndReport (integralToBytes 2 x) (show g ++ " Word16")
where x = integralToGray g
get = getAndReport 2 grayWord16

instance Genetic Word32 where
put g = putAndReport (integralToBytes 4 x) (show g ++ " Word32")
where x = integralToGray g
get = getAndReport 4 grayWord32

instance Genetic Word64 where
put g = putAndReport (integralToBytes 8 x) (show g ++ " Word64")
where x = integralToGray g
get = getAndReport 8 grayWord64

grayWord16 :: [Word8] -> Either String (Word16, String)
grayWord16 (x:y:[]) = Right (g, show g ++ " Word16")
where g = grayToIntegral (high + low) :: Word16
high = fromIntegral x * 0x100
low = fromIntegral y
grayWord16 _ = Left "logic error"
grayWord16 bs = Right (g, show g ++ " Word16")
where g = grayToIntegral . bytesToIntegral $ bs

grayWord32 :: [Word8] -> Either String (Word32, String)
grayWord32 bs = Right (g, show g ++ " Word32")
where g = grayToIntegral . bytesToIntegral $ bs

grayWord64 :: [Word8] -> Either String (Word64, String)
grayWord64 bs = Right (g, show g ++ " Word64")
where g = grayToIntegral . bytesToIntegral $ bs

integralToBytes :: Integral t => Int -> t -> [Word8]
integralToBytes n x = f n x []
where f 0 _ bs = bs
f m y bs = f (m-1) y' (b:bs)
where y' = y `div` 0x100
b = fromIntegral $ y `mod` 0x100

bytesToIntegral :: Integral t => [Word8] -> t
bytesToIntegral bs = f (bs, 0)
where f ([], n) = n
f (k:ks, n) = f (ks, n*0x100 + fromIntegral k)

instance (Genetic a) => Genetic [a] where
put xs = do
Expand Down
6 changes: 5 additions & 1 deletion test/ALife/Creatur/Genetics/BRGCWord8QC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Prelude hiding (read)
import ALife.Creatur.Genetics.BRGCWord8
import ALife.Creatur.Genetics.Analysis (Analysable)
import ALife.Creatur.Util (fromEither)
import Data.Word (Word8, Word16)
import Data.Word (Word8, Word16, Word32, Word64)
import GHC.Generics (Generic)
import Test.Framework as TF (Test, testGroup)
import Test.Framework.Providers.QuickCheck2 (testProperty)
Expand Down Expand Up @@ -80,6 +80,10 @@ test = testGroup "ALife.Creatur.Genetics.BRGCWord8QC"
(prop_round_trippable :: Word8 -> Property),
testProperty "prop_round_trippable - Word16"
(prop_round_trippable :: Word16 -> Property),
testProperty "prop_round_trippable - Word32"
(prop_round_trippable :: Word32 -> Property),
testProperty "prop_round_trippable - Word64"
(prop_round_trippable :: Word64 -> Property),
testProperty "prop_round_trippable - TestStructure"
(prop_round_trippable :: TestStructure -> Property),
testProperty "prop_rawWord8s_round_trippable"
Expand Down

0 comments on commit b5728f0

Please sign in to comment.