module Data.StableTree
( StableTree(..)
, fromMap
, toMap
, Error(..)
, load
, load'
, store
, store'
, Fragment(..)
) where
import qualified Data.StableTree.Tree as Tree
import Data.StableTree.Fragment ( Fragment(..) )
import Data.StableTree.Persist ( Error(..), load, load', store, store' )
import Data.StableTree.Tree ( StableTree(..) )
import qualified Data.Map as Map
import Data.Map ( Map )
import Data.Maybe ( isNothing )
import Data.Serialize ( Serialize )
fromMap :: (Ord k, Serialize k, Serialize v) => Map k v -> StableTree k v
fromMap m = go m Map.empty
where
go values accum =
case Tree.nextBottom values of
Left incomplete ->
if Map.null accum
then StableTree_I incomplete
else case Tree.getKey incomplete of
Just k -> buildParents accum (Just (k, incomplete)) Map.empty
Nothing -> buildParents accum Nothing Map.empty
Right (complete, remain) ->
if Map.null remain && Map.null accum
then StableTree_C complete
else go remain $ Map.insert (Tree.completeKey complete) complete accum
buildParents completes mIncomplete accum =
case Tree.nextBranch completes mIncomplete of
Left incomplete ->
if Map.null accum
then StableTree_I incomplete
else case Tree.getKey incomplete of
Just k -> buildParents accum (Just (k, incomplete)) Map.empty
Nothing -> buildParents accum Nothing Map.empty
Right (complete, remain) ->
if Map.null remain && Map.null accum && isNothing mIncomplete
then StableTree_C complete
else
let accum' = Map.insert (Tree.completeKey complete) complete accum
in buildParents remain mIncomplete accum'
toMap :: Ord k => StableTree k v -> Map k v
toMap (StableTree_I t) = Tree.treeContents t
toMap (StableTree_C t) = Tree.treeContents t