From 53e5fca0a0778b441093a8198527283c7291ee42 Mon Sep 17 00:00:00 2001 From: timmy Date: Sun, 15 Oct 2023 09:48:00 -0700 Subject: [PATCH] ordered lists --- benchmarks/CRDTs/Clock.hs | 9 +++- benchmarks/CRDTs/IntOrderedList.hs | 73 +++++++++++++++++------------- 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/benchmarks/CRDTs/Clock.hs b/benchmarks/CRDTs/Clock.hs index 44e0bba30..ad2380681 100644 --- a/benchmarks/CRDTs/Clock.hs +++ b/benchmarks/CRDTs/Clock.hs @@ -5,11 +5,16 @@ import Common type Clock = Map Int data Timestamp = Timestamp Int Clock -clk :: Timestamp -> Clock -clk t = +clock :: Timestamp -> Clock +clock t = case t of Timestamp _ c -> c +author :: Timestamp -> Int +author t = + case t of + Timestamp a _ -> a + init :: Int -> Clock init uid = singleton uid 0 diff --git a/benchmarks/CRDTs/IntOrderedList.hs b/benchmarks/CRDTs/IntOrderedList.hs index c12e62650..86a4699dd 100644 --- a/benchmarks/CRDTs/IntOrderedList.hs +++ b/benchmarks/CRDTs/IntOrderedList.hs @@ -1,49 +1,58 @@ module IntOrderedList where import Common import Clock +--import List data OrderedList a = OrderedList Clock (OrderedNode a) data OrderedNode a = Bin Timestamp a Timestamp Timestamp (OrderedNode a) | Tip -getStamp :: OrderedNode a -> Maybe Timestamp +getHead :: OrderedList a -> OrderedNode a +getHead list = + case list of + OrderedList _ node -> node + +getClock :: OrderedList a -> Clock +getClock list = + case list of + OrderedList c _ -> c + +getStamp :: OrderedNode a -> Common.Maybe Timestamp getStamp x = case x of - Bin t _ _ _ _ -> Just t - Tip -> Nothing - -right :: OrderedNode a -> OrderedNode a -right x = case x of - Bin _ _ _ r _ -> r - Tip -> Tip + Bin t _ _ _ _ -> Common.Just t + Tip -> Common.Nothing -left :: OrderedNode a -> OrderedNode a -left x = case x of - Bin _ _ _ _ l -> l +next :: OrderedNode a -> OrderedNode a +next x = case x of + Bin _ _ _ _ n -> n Tip -> Tip -singleton :: Int -> Int -> OrderedNode a +singleton :: Int -> a -> OrderedList a singleton uid x = - let clk = singleton uid - in Bin clk x clk clk + let clk = Clock.init uid + t = (stamp uid clk) + in OrderedList clk (Bin t x t t Tip) -insert :: Int -> Int -> Timestamp -> Timestamp -> OrderedList -insert uid x clk l r node = - let t = step uid clk - int OrderedList t (place t uid x node l r) +insert :: Int -> a -> Timestamp -> Timestamp -> OrderedList a -> OrderedList a +insert uid x l r list = + let clk = step uid (getClock list) + in OrderedList clk (place uid (stamp uid clk) x (getHead list) l r) -place :: Timestamp -> Int -> Int -> OrderedNode a -> Timestamp -> Timestamp -> OrderedNode a -place clk uid x s l r = +place :: Int -> Timestamp -> a -> OrderedNode a -> Timestamp -> Timestamp -> OrderedNode a +place uid clk x s l r = case (getStamp s) of - Nothing -> s - Just t -> case (compare t r) of - Eq -> s - Lt, Gt, Cc -> - let t v ls rs n - in Bin t v ls lr case (compare t l) of - Eq -> Bin (stamp uid clk) x l r (place uid clk x n l r) - Lt, Gt, Cc -> - if uid < t then place uid clk x n l r - else Bin (stamp uid clk) x l r (place uid clk x n l r) - -value :: OrderedNode a -> List \ No newline at end of file + Common.Nothing -> s + Common.Just t -> + case (Clock.compare (clock t) (clock r)) of + Eq -> s + _ -> case s of + Tip -> s + Bin t v ls rs n -> + case (Clock.compare (clock t) (clock l)) of + Eq -> Bin t v l r (Bin (stamp uid (Clock.clock clk)) x l r (place uid clk x n l r)) + _ -> + if uid < (author t) then Bin t v l r (place uid clk x n l r) + else Bin t v l r (Bin (stamp uid (Clock.clock clk)) x l r (place uid clk x n l r)) + +--value :: OrderedNode a -> List a \ No newline at end of file