mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
style: reindent in ccdeque
This commit is contained in:
parent
2fa12665dd
commit
2ed821bbe1
1 changed files with 165 additions and 159 deletions
|
|
@ -74,47 +74,48 @@ let is_empty d =
|
||||||
assert (bool_eq res (d.cur = Empty));
|
assert (bool_eq res (d.cur = Empty));
|
||||||
res
|
res
|
||||||
|
|
||||||
(*let rec cur = { cell=Zero; prev=cur; next=cur } in*)
|
|
||||||
let push_front d x =
|
let push_front d x =
|
||||||
incr_size_ d;
|
incr_size_ d;
|
||||||
match d.cur with
|
match d.cur with
|
||||||
| Empty ->
|
| Empty ->
|
||||||
let rec node = { cell=One x; prev = node; next = node } in
|
let rec node = { cell=One x; prev = node; next = node } in
|
||||||
d.cur <- Node node
|
d.cur <- Node node
|
||||||
| Node n ->
|
| Node n ->
|
||||||
match n.cell with
|
begin match n.cell with
|
||||||
| One y -> n.cell <- Two (x, y)
|
| One y -> n.cell <- Two (x, y)
|
||||||
| Two (y, z) -> n.cell <- Three (x,y,z)
|
| Two (y, z) -> n.cell <- Three (x,y,z)
|
||||||
| Three _ ->
|
| Three _ ->
|
||||||
let node = { cell = One x; prev = n.prev; next = n; } in
|
let node = { cell = One x; prev = n.prev; next = n; } in
|
||||||
n.prev.next <- node;
|
n.prev.next <- node;
|
||||||
n.prev <- node;
|
n.prev <- node;
|
||||||
d.cur <- Node node (* always point to first node *)
|
d.cur <- Node node (* always point to first node *)
|
||||||
|
end
|
||||||
|
|
||||||
let push_back d x =
|
let push_back d x =
|
||||||
incr_size_ d;
|
incr_size_ d;
|
||||||
match d.cur with
|
match d.cur with
|
||||||
| Empty ->
|
| Empty ->
|
||||||
let rec node = { cell=One x; prev = node; next = node } in
|
let rec node = { cell=One x; prev = node; next = node } in
|
||||||
d.cur <- Node node
|
d.cur <- Node node
|
||||||
| Node cur ->
|
| Node cur ->
|
||||||
let n = cur.prev in (* last node *)
|
let n = cur.prev in (* last node *)
|
||||||
match n.cell with
|
begin match n.cell with
|
||||||
| One y -> n.cell <- Two (y, x)
|
| One y -> n.cell <- Two (y, x)
|
||||||
| Two (y,z) -> n.cell <- Three (y, z, x)
|
| Two (y,z) -> n.cell <- Three (y, z, x)
|
||||||
| Three _ ->
|
| Three _ ->
|
||||||
let elt = { cell = One x; next=cur; prev=n; } in
|
let elt = { cell = One x; next=cur; prev=n; } in
|
||||||
n.next <- elt;
|
n.next <- elt;
|
||||||
cur.prev <- elt
|
cur.prev <- elt
|
||||||
|
end
|
||||||
|
|
||||||
let peek_front_opt d =
|
let peek_front_opt d =
|
||||||
match d.cur with
|
match d.cur with
|
||||||
| Empty -> None
|
| Empty -> None
|
||||||
| Node cur ->
|
| Node cur ->
|
||||||
match cur.cell with
|
match cur.cell with
|
||||||
| One x -> Some x
|
| One x -> Some x
|
||||||
| Two (x,_) -> Some x
|
| Two (x,_) -> Some x
|
||||||
| Three (x,_,_) -> Some x
|
| Three (x,_,_) -> Some x
|
||||||
|
|
||||||
let peek_front d = match peek_front_opt d with
|
let peek_front d = match peek_front_opt d with
|
||||||
| None -> raise Empty
|
| None -> raise Empty
|
||||||
|
|
@ -140,12 +141,12 @@ let peek_front d = match peek_front_opt d with
|
||||||
|
|
||||||
let peek_back_opt d =
|
let peek_back_opt d =
|
||||||
match d.cur with
|
match d.cur with
|
||||||
| Empty -> None
|
| Empty -> None
|
||||||
| Node cur ->
|
| Node cur ->
|
||||||
match cur.prev.cell with
|
match cur.prev.cell with
|
||||||
| One x -> Some x
|
| One x -> Some x
|
||||||
| Two (_,x) -> Some x
|
| Two (_,x) -> Some x
|
||||||
| Three (_,_,x) -> Some x
|
| Three (_,_,x) -> Some x
|
||||||
|
|
||||||
let peek_back d = match peek_back_opt d with
|
let peek_back d = match peek_back_opt d with
|
||||||
| None -> raise Empty
|
| None -> raise Empty
|
||||||
|
|
@ -180,23 +181,23 @@ let remove_node_ n =
|
||||||
|
|
||||||
let take_back_opt d =
|
let take_back_opt d =
|
||||||
match d.cur with
|
match d.cur with
|
||||||
| Empty -> None
|
| Empty -> None
|
||||||
| Node cur ->
|
| Node cur ->
|
||||||
if Stdlib.(==) cur cur.prev
|
if Stdlib.(==) cur cur.prev
|
||||||
then (
|
then (
|
||||||
(* only one cell *)
|
(* only one cell *)
|
||||||
decr_size_ d;
|
decr_size_ d;
|
||||||
let is_zero, x = take_back_node_ cur in
|
let is_zero, x = take_back_node_ cur in
|
||||||
if is_zero then d.cur <- Empty;
|
if is_zero then d.cur <- Empty;
|
||||||
Some x
|
Some x
|
||||||
) else (
|
) else (
|
||||||
let n = cur.prev in
|
let n = cur.prev in
|
||||||
let is_zero, x = take_back_node_ n in
|
let is_zero, x = take_back_node_ n in
|
||||||
decr_size_ d;
|
decr_size_ d;
|
||||||
(* remove previous node *)
|
(* remove previous node *)
|
||||||
if is_zero then remove_node_ n;
|
if is_zero then remove_node_ n;
|
||||||
Some x
|
Some x
|
||||||
)
|
)
|
||||||
|
|
||||||
let take_back d = match take_back_opt d with
|
let take_back d = match take_back_opt d with
|
||||||
| None -> raise Empty
|
| None -> raise Empty
|
||||||
|
|
@ -220,25 +221,25 @@ let take_front_node_ n = match n.cell with
|
||||||
|
|
||||||
let take_front_opt d =
|
let take_front_opt d =
|
||||||
match d.cur with
|
match d.cur with
|
||||||
| Empty -> None
|
| Empty -> None
|
||||||
| Node cur ->
|
| Node cur ->
|
||||||
if Stdlib.(==) cur.prev cur
|
if Stdlib.(==) cur.prev cur
|
||||||
then (
|
then (
|
||||||
(* only one cell *)
|
(* only one cell *)
|
||||||
decr_size_ d;
|
decr_size_ d;
|
||||||
let is_zero, x = take_front_node_ cur in
|
let is_zero, x = take_front_node_ cur in
|
||||||
if is_zero then d.cur <- Empty;
|
if is_zero then d.cur <- Empty;
|
||||||
Some x
|
Some x
|
||||||
) else (
|
) else (
|
||||||
decr_size_ d;
|
decr_size_ d;
|
||||||
let is_zero, x = take_front_node_ cur in
|
let is_zero, x = take_front_node_ cur in
|
||||||
if is_zero then (
|
if is_zero then (
|
||||||
cur.prev.next <- cur.next;
|
cur.prev.next <- cur.next;
|
||||||
cur.next.prev <- cur.prev;
|
cur.next.prev <- cur.prev;
|
||||||
d.cur <- Node cur.next;
|
d.cur <- Node cur.next;
|
||||||
);
|
);
|
||||||
Some x
|
Some x
|
||||||
)
|
)
|
||||||
|
|
||||||
let take_front d = match take_front_opt d with
|
let take_front d = match take_front_opt d with
|
||||||
| None -> raise Empty
|
| None -> raise Empty
|
||||||
|
|
@ -258,29 +259,31 @@ let remove_front d = ignore (take_front_opt d)
|
||||||
|
|
||||||
let update_front d f =
|
let update_front d f =
|
||||||
match d.cur with
|
match d.cur with
|
||||||
| Empty -> ()
|
| Empty -> ()
|
||||||
| Node cur ->
|
| Node cur ->
|
||||||
match cur.cell with
|
match cur.cell with
|
||||||
| One x ->
|
| One x ->
|
||||||
begin match f x with
|
begin match f x with
|
||||||
| None -> if Stdlib.(!=) cur.prev cur then (
|
| None ->
|
||||||
cur.prev.next <- cur.next;
|
if Stdlib.(!=) cur.prev cur then (
|
||||||
cur.next.prev <- cur.prev;
|
cur.prev.next <- cur.next;
|
||||||
d.cur <- Node cur.next;
|
cur.next.prev <- cur.prev;
|
||||||
)
|
d.cur <- Node cur.next;
|
||||||
else d.cur <- Empty
|
) else (
|
||||||
| Some x -> cur.cell <- One x
|
d.cur <- Empty
|
||||||
end
|
)
|
||||||
| Two (x, y) ->
|
| Some x -> cur.cell <- One x
|
||||||
begin match f x with
|
end
|
||||||
| None -> cur.cell <- One (y)
|
| Two (x, y) ->
|
||||||
| Some x -> cur.cell <- Two (x,y)
|
begin match f x with
|
||||||
end
|
| None -> cur.cell <- One (y)
|
||||||
| Three (x,y,z) ->
|
| Some x -> cur.cell <- Two (x,y)
|
||||||
begin match f x with
|
end
|
||||||
| None -> cur.cell <- Two (y,z)
|
| Three (x,y,z) ->
|
||||||
| Some x -> cur.cell <- Three (x,y,z)
|
begin match f x with
|
||||||
end
|
| None -> cur.cell <- Two (y,z)
|
||||||
|
| Some x -> cur.cell <- Three (x,y,z)
|
||||||
|
end
|
||||||
|
|
||||||
(*$T update_front
|
(*$T update_front
|
||||||
let q = of_list [1;2;3;4;5;6;7] in update_front q (fun _ -> None); to_list q = [2;3;4;5;6;7]
|
let q = of_list [1;2;3;4;5;6;7] in update_front q (fun _ -> None); to_list q = [2;3;4;5;6;7]
|
||||||
|
|
@ -301,26 +304,27 @@ let update_front d f =
|
||||||
|
|
||||||
let update_back d f =
|
let update_back d f =
|
||||||
match d.cur with
|
match d.cur with
|
||||||
| Empty -> ()
|
| Empty -> ()
|
||||||
| Node cur ->
|
| Node cur ->
|
||||||
let n = cur.prev in
|
let n = cur.prev in
|
||||||
match n.cell with
|
match n.cell with
|
||||||
| One x ->
|
| One x ->
|
||||||
begin match f x with
|
begin match f x with
|
||||||
| None -> if Stdlib.(!=) cur.prev cur then remove_node_ n
|
| None ->
|
||||||
else d.cur <- Empty
|
if Stdlib.(!=) cur.prev cur then remove_node_ n
|
||||||
| Some x -> n.cell <- One x
|
else d.cur <- Empty
|
||||||
end
|
| Some x -> n.cell <- One x
|
||||||
| Two (x, y) ->
|
end
|
||||||
begin match f y with
|
| Two (x, y) ->
|
||||||
| None -> n.cell <- One (x)
|
begin match f y with
|
||||||
| Some y -> n.cell <- Two (x,y)
|
| None -> n.cell <- One (x)
|
||||||
end
|
| Some y -> n.cell <- Two (x,y)
|
||||||
| Three (x,y,z) ->
|
end
|
||||||
begin match f z with
|
| Three (x,y,z) ->
|
||||||
| None -> n.cell <- Two (x,y)
|
begin match f z with
|
||||||
| Some z -> n.cell <- Three (x,y,z)
|
| None -> n.cell <- Two (x,y)
|
||||||
end
|
| Some z -> n.cell <- Three (x,y,z)
|
||||||
|
end
|
||||||
|
|
||||||
(*$T update_back
|
(*$T update_back
|
||||||
let q = of_list [1;2;3;4;5;6;7] in update_back q (fun _ -> None); to_list q = [1;2;3;4;5;6]
|
let q = of_list [1;2;3;4;5;6;7] in update_back q (fun _ -> None); to_list q = [1;2;3;4;5;6]
|
||||||
|
|
@ -349,9 +353,9 @@ let iter f d =
|
||||||
if n.next != first then iter f ~first n.next
|
if n.next != first then iter f ~first n.next
|
||||||
in
|
in
|
||||||
match d.cur with
|
match d.cur with
|
||||||
| Empty -> ()
|
| Empty -> ()
|
||||||
| Node cur ->
|
| Node cur ->
|
||||||
iter f ~first:cur cur
|
iter f ~first:cur cur
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
let n = ref 0 in iter (fun _ -> incr n) (of_list [1;2;3]); !n = 3
|
let n = ref 0 in iter (fun _ -> incr n) (of_list [1;2;3]); !n = 3
|
||||||
|
|
@ -386,9 +390,9 @@ let fold f acc d =
|
||||||
if Stdlib.(==) n.next first then acc else aux ~first f acc n.next
|
if Stdlib.(==) n.next first then acc else aux ~first f acc n.next
|
||||||
in
|
in
|
||||||
match d.cur with
|
match d.cur with
|
||||||
| Empty -> acc
|
| Empty -> acc
|
||||||
| Node cur ->
|
| Node cur ->
|
||||||
aux ~first:cur f acc cur
|
aux ~first:cur f acc cur
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
fold (+) 0 (of_list [1;2;3]) = 6
|
fold (+) 0 (of_list [1;2;3]) = 6
|
||||||
|
|
@ -491,10 +495,10 @@ let filter_in_place (d:_ t) f : unit =
|
||||||
let update_local_ n =
|
let update_local_ n =
|
||||||
d.size <- d.size - size_cell_ n.cell;
|
d.size <- d.size - size_cell_ n.cell;
|
||||||
match filter_cell_ f n.cell with
|
match filter_cell_ f n.cell with
|
||||||
| None -> None
|
| None -> None
|
||||||
|Some n as new_cell->
|
| Some n as new_cell->
|
||||||
d.size <- d.size + size_cell_ n;
|
d.size <- d.size + size_cell_ n;
|
||||||
new_cell
|
new_cell
|
||||||
in
|
in
|
||||||
let rec loop ~stop_at n : unit =
|
let rec loop ~stop_at n : unit =
|
||||||
if n != stop_at then (
|
if n != stop_at then (
|
||||||
|
|
@ -518,10 +522,10 @@ let filter_in_place (d:_ t) f : unit =
|
||||||
let rec new_first_cell ~stop_at n =
|
let rec new_first_cell ~stop_at n =
|
||||||
if n != stop_at then (
|
if n != stop_at then (
|
||||||
match update_local_ n with
|
match update_local_ n with
|
||||||
| None ->
|
| None ->
|
||||||
new_first_cell ~stop_at n.next
|
new_first_cell ~stop_at n.next
|
||||||
| Some c ->
|
| Some c ->
|
||||||
n.cell <- c; Some n
|
n.cell <- c; Some n
|
||||||
) else None
|
) else None
|
||||||
in
|
in
|
||||||
match d.cur with
|
match d.cur with
|
||||||
|
|
@ -529,17 +533,18 @@ let filter_in_place (d:_ t) f : unit =
|
||||||
| Node cur ->
|
| Node cur ->
|
||||||
(* special case for first cell *)
|
(* special case for first cell *)
|
||||||
match update_local_ cur with
|
match update_local_ cur with
|
||||||
| None ->
|
| None ->
|
||||||
begin match new_first_cell ~stop_at:cur cur.next with
|
begin match new_first_cell ~stop_at:cur cur.next with
|
||||||
| None -> d.cur <- Empty
|
| None -> d.cur <- Empty
|
||||||
| Some n ->
|
| Some n ->
|
||||||
cur.prev.next <- n;
|
cur.prev.next <- n;
|
||||||
n.prev <- cur.prev;
|
n.prev <- cur.prev;
|
||||||
d.cur <- Node n;
|
d.cur <- Node n;
|
||||||
loop ~stop_at:n n.next
|
loop ~stop_at:n n.next
|
||||||
end
|
end
|
||||||
| Some c -> cur.cell <- c;
|
| Some c ->
|
||||||
loop ~stop_at:cur cur.next
|
cur.cell <- c;
|
||||||
|
loop ~stop_at:cur cur.next
|
||||||
|
|
||||||
(*$R
|
(*$R
|
||||||
let q = of_list [1;2;3;4;5;6] in
|
let q = of_list [1;2;3;4;5;6] in
|
||||||
|
|
@ -588,24 +593,25 @@ let of_gen g =
|
||||||
|
|
||||||
let to_gen q =
|
let to_gen q =
|
||||||
match q.cur with
|
match q.cur with
|
||||||
| Empty -> (fun () -> None)
|
| Empty -> (fun () -> None)
|
||||||
| Node cur ->
|
| Node cur ->
|
||||||
let first = cur in
|
let first = cur in
|
||||||
let cell = ref (Some cur.cell) in
|
let cell = ref (Some cur.cell) in
|
||||||
let cur = ref cur in
|
let cur = ref cur in
|
||||||
let rec next () = match !cell with
|
let rec next () =
|
||||||
| None when Stdlib.(==) (!cur).next first -> None
|
match !cell with
|
||||||
| None ->
|
| None when Stdlib.(==) (!cur).next first -> None
|
||||||
(* go to next node *)
|
| None ->
|
||||||
let n = !cur in
|
(* go to next node *)
|
||||||
cur := n.next;
|
let n = !cur in
|
||||||
cell := Some (n.next.cell);
|
cur := n.next;
|
||||||
next ()
|
cell := Some (n.next.cell);
|
||||||
| Some (One x) -> cell := None; Some x
|
next ()
|
||||||
| Some (Two (x,y)) -> cell := Some (One y); Some x
|
| Some (One x) -> cell := None; Some x
|
||||||
| Some (Three (x,y,z)) -> cell := Some (Two (y,z)); Some x
|
| Some (Two (x,y)) -> cell := Some (One y); Some x
|
||||||
in
|
| Some (Three (x,y,z)) -> cell := Some (Two (y,z)); Some x
|
||||||
next
|
in
|
||||||
|
next
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
of_list [1;2;3] |> to_gen |> of_gen |> to_list = [1;2;3]
|
of_list [1;2;3] |> to_gen |> of_gen |> to_list = [1;2;3]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue