From 4bbc27978a8e7d36b9d8c47f8b4dd0cf7b654fc6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kim=20Nguy=E1=BB=85n?= Date: Wed, 22 Feb 2012 10:14:40 +0100 Subject: [PATCH] Add an extra parameter to cons so that it does not perform ordered insertion w.r.t the unique ID. --- src/hlist.ml | 12 +++++++++--- src/hlist.mli | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/hlist.ml b/src/hlist.ml index 300c6a1..4434e1e 100644 --- a/src/hlist.ml +++ b/src/hlist.ml @@ -18,7 +18,7 @@ module type S = sig val equal : t -> t -> bool val nil : t val node : t -> t node - val cons : elt -> t -> t + val cons : ?sorted:bool -> elt -> t -> t val hd : t -> elt val tl : t -> t val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a @@ -58,13 +58,19 @@ struct let nil = Node.make Nil (* doing sorted insertion allows to make better use of hash consing *) - let rec cons e l = + let rec sorted_cons e l = match l.Node.node with | Nil -> Node.make (Cons(e, l)) | Cons (x, ll) -> if H.uid e < H.uid x then Node.make (Cons(e, l)) - else Node.make (Cons(x, cons e ll)) + else Node.make (Cons(x, sorted_cons e ll)) + + let cons e l = + Node.make(Cons(e, l)) + + let cons ?(sorted=true) e l = + if sorted then sorted_cons e l else cons e l let hd = function { Node.node = Cons(a,_) } -> a | _ -> failwith "hd" let tl = function { Node.node = Cons(_,a) } -> a | _ -> failwith "tl" diff --git a/src/hlist.mli b/src/hlist.mli index d7749f1..3173c2c 100644 --- a/src/hlist.mli +++ b/src/hlist.mli @@ -17,7 +17,7 @@ module type S = sig val equal : t -> t -> bool val nil : t val node : t -> t node - val cons : elt -> t -> t + val cons : ?sorted:bool -> elt -> t -> t val hd : t -> elt val tl : t -> t val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a -- 2.17.1