on master help sign in
Add binary instances for some DecisionTree types
c0e1f3eEvan Czaplicki
src/Optimize/DecisionTree.hs+47
@@ -12,6 +12,7 @@ as SML/NJ to get nice trees.
12-}
12-}
13
13
14import Control.Arrow (second)
14import Control.Arrow (second)
15+import Data.Binary
15import qualified Data.List as List
16import qualified Data.List as List
16import qualified Data.Map as Map
17import qualified Data.Map as Map
17import qualified Data.Set as Set
18import qualified Data.Set as Set
@@ -491,3 +492,49 @@ smallBranchingFactor variantDict branches path =
491 gatherEdges variantDict branches path
492 gatherEdges variantDict branches path
492 in
493 in
493 length edges + (if null fallback then 0 else 1)
494 length edges + (if null fallback then 0 else 1)
495+
496+
497+
498+-- BINARY
499+
500+
501+instance Binary Test where
502+ get =
503+ do word <- getWord8
504+ case word of
505+ 0 -> Constructor <$> get
506+ 1 -> Literal <$> get
507+ _ -> error "problem getting DecisionTree.Test binary"
508+
509+ put test =
510+ case test of
511+ Constructor var ->
512+ putWord8 0 >> put var
513+
514+ Literal literal ->
515+ putWord8 1 >> put literal
516+
517+
518+instance Binary Path where
519+ get =
520+ do word <- getWord8
521+ case word of
522+ 0 -> Position <$> get <*> get
523+ 1 -> Field <$> get <*> get
524+ 2 -> pure Empty
525+ 3 -> pure Alias
526+ _ -> error "problem getting DecisionTree.Path binary"
527+
528+ put path =
529+ case path of
530+ Position index rest ->
531+ putWord8 0 >> put index >> put rest
532+
533+ Field field rest ->
534+ putWord8 1 >> put field >> put rest
535+
536+ Empty ->
537+ putWord8 2
538+
539+ Alias ->
540+ putWord8 3