Skip to content

Commit

Permalink
closes #4, bump major version
Browse files Browse the repository at this point in the history
  • Loading branch information
arnemileswinter committed Jul 13, 2022
1 parent 042fb05 commit 539425d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion interval-tree-clock.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.4
name: interval-tree-clock
version: 0.1.0.2
version: 0.2.0.0
license: MIT
stability: experimental
homepage: https://github.com/arnemileswinter/itc
Expand Down
44 changes: 23 additions & 21 deletions src/Data/Clock/IntervalTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ data Stamp = Stamp ITCId ITCEvent deriving (Eq, Show, Generic)

data ITCId
= ITCIdBranch ITCId ITCId
| ITCId !Bool
| ITCIdOff
| ITCIdOn
deriving (Eq, Show, Generic)

data ITCEvent
Expand All @@ -58,9 +59,10 @@ seed = Stamp iT (ITCEventLeaf 0)
fork :: Stamp -> (Stamp, Stamp)
fork (Stamp i e) = let (i1, i2) = split i in (Stamp i1 e, Stamp i2 e)

-- | inverse of fork. s = uncurry join (fork s)
-- Note that the internal call to `sumId` may be partial, if the ITC Stamp was constructed through direct constructor usage.
-- Using only `fork` and `join` as the safe API to create stamps will not lead to this inconsistency.
{- | inverse of fork. s = uncurry join (fork s)
Note that the internal call to `sumId` may be partial, if the ITC Stamp was constructed through direct constructor usage.
Using only `fork` and `join` as the safe API to create stamps will not lead to this inconsistency.
-}
join :: Stamp -> Stamp -> Stamp
join (Stamp i1 e1) (Stamp i2 e2) = Stamp (sumId i1 i2) (joinEv e1 e2)

Expand Down Expand Up @@ -111,43 +113,43 @@ happenedBefore :: Stamp -> Stamp -> Bool

-- | some utility functions because the constructors are so verbose.
iF, iT :: ITCId
iF = ITCId False
iT = ITCId True
iF = ITCIdOff
iT = ITCIdOn

iB :: ITCId -> ITCId -> ITCId
iB = ITCIdBranch

normId :: ITCId -> ITCId
normId (ITCIdBranch (ITCId False) (ITCId False)) = iF
normId (ITCIdBranch (ITCId True) (ITCId True)) = iT
normId (ITCIdBranch ITCIdOff ITCIdOff) = iF
normId (ITCIdBranch ITCIdOn ITCIdOn) = iT
normId leaf = leaf

sumId :: ITCId -> ITCId -> ITCId
sumId (ITCId False) i = i
sumId i (ITCId False) = i
sumId ITCIdOff i = i
sumId i ITCIdOff = i
sumId (ITCIdBranch l1 r1) (ITCIdBranch l2 r2) = normId (ITCIdBranch (sumId l1 l2) (sumId r1 r2))
sumId _ _ = error "internal consistency error. Create ID's only by means of fork and join."

split :: ITCId -> (ITCId, ITCId)
split (ITCId False) = (iF, iF)
split (ITCId True) = (iB iT iF, iB iF iT)
split (ITCIdBranch (ITCId False) i) = (iB iF i1, iB iF i2) where (i1, i2) = split i
split (ITCIdBranch i (ITCId False)) = (iB i1 iF, iB i2 iF) where (i1, i2) = split i
split ITCIdOff = (iF, iF)
split ITCIdOn = (iB iT iF, iB iF iT)
split (ITCIdBranch ITCIdOff i) = (iB iF i1, iB iF i2) where (i1, i2) = split i
split (ITCIdBranch i ITCIdOff) = (iB i1 iF, iB i2 iF) where (i1, i2) = split i
split (ITCIdBranch l r) = (iB l iF, iB iF r)

fill' :: ITCId -> ITCEvent -> ITCEvent
fill' (ITCId False) e = e
fill' (ITCId True) e = ITCEventLeaf $ maxEv e
fill' ITCIdOff e = e
fill' ITCIdOn e = ITCEventLeaf $ maxEv e
fill' _ n@(ITCEventLeaf _) = n
fill' (ITCIdBranch (ITCId True) ir) (ITCEventBranch n l r) =
fill' (ITCIdBranch ITCIdOn ir) (ITCEventBranch n l r) =
normEv $
ITCEventBranch
n
(ITCEventLeaf (max (maxEv l) (minEv r')))
r'
where
r' = fill' ir r
fill' (ITCIdBranch il (ITCId True)) (ITCEventBranch n l r) =
fill' (ITCIdBranch il ITCIdOn) (ITCEventBranch n l r) =
normEv $
ITCEventBranch
n
Expand All @@ -166,18 +168,18 @@ addCost :: Cost -> Cost -> Cost
addCost (Cost c1) (Cost c2) = Cost $ c1 + c2

grow' :: Stamp -> (ITCEvent, Cost)
grow' (Stamp (ITCId True) (ITCEventLeaf n)) =
grow' (Stamp ITCIdOn (ITCEventLeaf n)) =
(ITCEventLeaf $ n + 1, Cost 0)
grow' (Stamp i (ITCEventLeaf n)) =
(e', c `addCost` largeCost)
where
largeCost = Cost 1000
(e', c) = grow' $ Stamp i (ITCEventBranch n (ITCEventLeaf 0) (ITCEventLeaf 0))
grow' (Stamp (ITCIdBranch (ITCId False) i) (ITCEventBranch n l r)) =
grow' (Stamp (ITCIdBranch ITCIdOff i) (ITCEventBranch n l r)) =
(ITCEventBranch n l r', cr `addCost` (Cost 1))
where
(r', cr) = grow' $ Stamp i r
grow' (Stamp (ITCIdBranch i (ITCId False)) (ITCEventBranch n l r)) =
grow' (Stamp (ITCIdBranch i ITCIdOff) (ITCEventBranch n l r)) =
((ITCEventBranch n l' r), cl `addCost` (Cost 1))
where
(l', cl) = grow' $ Stamp i l
Expand Down
10 changes: 4 additions & 6 deletions src/Data/Clock/IntervalTree/Format.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
-- |
module Data.Clock.IntervalTree.Format where

import Data.Clock.IntervalTree
Expand All @@ -7,8 +6,8 @@ import Data.Clock.IntervalTree
fmtStamp :: Stamp -> String
fmtStamp (Stamp i e) = "(" <> fmtId i <> ", " <> fmtEv e <> ")"
where
fmtId (ITCId False) = "0"
fmtId (ITCId True) = "1"
fmtId ITCIdOff = "0"
fmtId ITCIdOn = "1"
fmtId (ITCIdBranch l r) = "(" <> fmtId l <> ", " <> fmtId r <> ")"

fmtEv (ITCEventLeaf n) = show n
Expand Down Expand Up @@ -45,10 +44,9 @@ fmtStampTikz (Stamp i e) = fmtIdsTikz i ++ "\n" ++ fmtEventsTikz e
fmtIdsTikz :: ITCId -> String
fmtIdsTikz i0 = go 0 tikzWidth i0
where
go oh w (ITCId b)
| b = tikzRect (Just "blue!40!white") (oh, - tikzLineHeight) (w, tikzLineHeight)
| otherwise = ""
go oh w ITCIdOn = tikzRect (Just "blue!40!white") (oh, - tikzLineHeight) (w, tikzLineHeight)
go oh w (ITCIdBranch l r) = go oh (w / 2) l <> go (oh + w / 2) (w / 2) r
go _ _ _ = ""

fmtEventsTikz :: ITCEvent -> String
fmtEventsTikz e0 = go tikzWidth 0 (- tikzLineHeight) e0
Expand Down

0 comments on commit 539425d

Please sign in to comment.