module Darcs.Patch.Set
( PatchSet(..)
, Tagged(..)
, SealedPatchSet
, Origin
, progressPatchSet
, patchSetTags
, emptyPatchSet
, appendPSFL
, patchSet2RL
, patchSet2FL
, inOrderTags
, patchSetSnoc
, patchSetSplit
, patchSetDrop
) where
import Darcs.Prelude
import Data.Maybe ( catMaybes )
import Darcs.Patch.Info ( PatchInfo, piTag )
import Darcs.Patch.PatchInfoAnd ( PatchInfoAnd, info )
import Darcs.Patch.Witnesses.Sealed ( Sealed(..) )
import Darcs.Patch.Witnesses.Ordered
( FL, RL(..), (+<+), (+<<+), (:>)(..), reverseRL,
mapRL_RL, concatRL, mapRL )
import Darcs.Patch.Witnesses.Show ( Show1, Show2 )
import Darcs.Util.Progress ( progress )
data Origin
type SealedPatchSet rt p wStart = Sealed ((PatchSet rt p) wStart)
data PatchSet rt p wStart wY where
PatchSet :: RL (Tagged rt p) Origin wX -> RL (PatchInfoAnd rt p) wX wY
-> PatchSet rt p Origin wY
deriving instance Show2 p => Show (PatchSet rt p wStart wY)
instance Show2 p => Show1 (PatchSet rt p wStart)
instance Show2 p => Show2 (PatchSet rt p)
emptyPatchSet :: PatchSet rt p Origin Origin
emptyPatchSet = PatchSet NilRL NilRL
data Tagged rt p wX wZ where
Tagged :: PatchInfoAnd rt p wY wZ -> Maybe String
-> RL (PatchInfoAnd rt p) wX wY -> Tagged rt p wX wZ
deriving instance Show2 p => Show (Tagged rt p wX wZ)
instance Show2 p => Show1 (Tagged rt p wX)
instance Show2 p => Show2 (Tagged rt p)
patchSet2RL :: PatchSet rt p wStart wX -> RL (PatchInfoAnd rt p) wStart wX
patchSet2RL (PatchSet ts ps) = concatRL (mapRL_RL ts2rl ts) +<+ ps
where
ts2rl :: Tagged rt p wY wZ -> RL (PatchInfoAnd rt p) wY wZ
ts2rl (Tagged t _ ps2) = ps2 :<: t
patchSet2FL :: PatchSet rt p wStart wX -> FL (PatchInfoAnd rt p) wStart wX
patchSet2FL = reverseRL . patchSet2RL
appendPSFL :: PatchSet rt p wStart wX -> FL (PatchInfoAnd rt p) wX wY
-> PatchSet rt p wStart wY
appendPSFL (PatchSet ts ps) newps = PatchSet ts (ps +<<+ newps)
progressPatchSet :: String -> PatchSet rt p wStart wX -> PatchSet rt p wStart wX
progressPatchSet k (PatchSet ts ps) =
PatchSet (mapRL_RL progressTagged ts) (mapRL_RL prog ps)
where
prog = progress k
progressTagged :: Tagged rt p wY wZ -> Tagged rt p wY wZ
progressTagged (Tagged t h tps) = Tagged (prog t) h (mapRL_RL prog tps)
patchSetTags :: PatchSet rt p wX wY -> [String]
patchSetTags = catMaybes . mapRL (piTag . info) . patchSet2RL
inOrderTags :: PatchSet rt p wS wX -> [PatchInfo]
inOrderTags (PatchSet ts _) = go ts
where go :: RL(Tagged rt t1) wT wY -> [PatchInfo]
go (ts' :<: Tagged t _ _) = info t : go ts'
go NilRL = []
patchSetSnoc :: PatchSet rt p wX wY -> PatchInfoAnd rt p wY wZ -> PatchSet rt p wX wZ
patchSetSnoc (PatchSet ts ps) p = PatchSet ts (ps :<: p)
patchSetSplit :: PatchSet rt p wX wY
-> (PatchSet rt p :> RL (PatchInfoAnd rt p)) wX wY
patchSetSplit (PatchSet (ts :<: Tagged t _ ps') ps) =
PatchSet ts ps' :> ((NilRL :<: t) +<+ ps)
patchSetSplit (PatchSet NilRL ps) = PatchSet NilRL NilRL :> ps
patchSetDrop :: Int
-> PatchSet rt p wStart wX
-> SealedPatchSet rt p wStart
patchSetDrop n ps | n <= 0 = Sealed ps
patchSetDrop n (PatchSet (ts :<: Tagged t _ ps) NilRL) =
patchSetDrop n $ PatchSet ts (ps :<: t)
patchSetDrop _ (PatchSet NilRL NilRL) = Sealed $ PatchSet NilRL NilRL
patchSetDrop n (PatchSet ts (ps :<: _)) = patchSetDrop (n - 1) $ PatchSet ts ps