From fe88bafe77be21a8fc7d1380d75e5d77ec480ee3 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 6 Mar 2018 23:08:02 -0600 Subject: [PATCH] simplify `CCFun_vec` by removing transients, for now --- src/data/CCFun_vec.ml | 55 +++++++++++++++++------------------------- src/data/CCFun_vec.mli | 2 ++ 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/data/CCFun_vec.ml b/src/data/CCFun_vec.ml index 539b1cd4..b16fdb9b 100644 --- a/src/data/CCFun_vec.ml +++ b/src/data/CCFun_vec.ml @@ -18,6 +18,7 @@ type 'a gen = unit -> 'a option type 'a printer = Format.formatter -> 'a -> unit type 'a ktree = unit -> [`Nil | `Node of 'a * 'a ktree list] +(* TODO (** {2 Transient IDs} *) module Transient = struct type state = { mutable frozen: bool } @@ -39,17 +40,11 @@ module Transient = struct raise e exception Frozen end - -(* TODO: move transient from A.t to 'a t, as nodes can be owned by a transient, - not arrays. - Then do mutable push, and use it for append/filter/flatten/flat_map… *) + *) (* function array *) module A = struct - type 'a t = { - arr: 'a array; - id: Transient.t; - } + type 'a t = 'a array let length_log = 5 let max_length = 32 @@ -57,35 +52,33 @@ module A = struct let () = assert (max_length = 1 lsl length_log) - let length a = Array.length a.arr + let length = Array.length + let iteri = Array.iteri + let fold = Array.fold_left - let create ~id = { arr= [| |]; id; } + let create () = [| |] - let empty = {arr=[| |]; id=Transient.empty} + let empty = [| |] let is_empty a = length a = 0 - let return x = { arr=[| x |]; id=Transient.empty} - - let owns ~id a = - Transient.active id && Transient.equal id a.id + let return x = [| x |] let get a i = if i<0 || i >= length a then invalid_arg "A.get"; - Array.unsafe_get a.arr i + Array.unsafe_get a i (* push at the back *) let push x a = let n = length a in if n = max_length then invalid_arg "A.push"; let arr = Array.make (n+1) x in - Array.blit a.arr 0 arr 0 n; - {a with arr;} + Array.blit a 0 arr 0 n; + arr let pop a = let n = length a in if n=0 then invalid_arg "A.pop"; - let arr = Array.sub a.arr 0 (n-1) in - {a with arr} + Array.sub a 0 (n-1) let append a b = let n_a = length a in @@ -94,10 +87,10 @@ module A = struct if n_a = 0 then b else if n_b = 0 then a else ( - let arr = Array.make (n_a+n_b) (a.arr.(0)) in - Array.blit a.arr 0 arr 0 n_a; - Array.blit b.arr 0 arr n_a n_b; - {id=Transient.empty; arr} + let arr = Array.make (n_a+n_b) (a.(0)) in + Array.blit a 0 arr 0 n_a; + Array.blit b 0 arr n_a n_b; + arr ) let set ~mut a i x = @@ -105,22 +98,18 @@ module A = struct if i=length a then ( (* insert in a longer copy *) let arr = Array.make (i+1) x in - Array.blit a.arr 0 arr 0 i; - {a with arr} + Array.blit a 0 arr 0 i; + arr ) else if mut then ( (* replace element at [i] in place *) - a.arr.(i) <- x; + a.(i) <- x; a ) else ( (* replace element at [i] in copy *) - let arr = Array.copy a.arr in + let arr = Array.copy a in arr.(i) <- x; - {a with arr} + arr ) - - let iteri f a = Array.iteri f a.arr - - let fold f acc a = Array.fold_left f acc a.arr end (** {2 Functors} *) diff --git a/src/data/CCFun_vec.mli b/src/data/CCFun_vec.mli index 04c804fe..4e89df1b 100644 --- a/src/data/CCFun_vec.mli +++ b/src/data/CCFun_vec.mli @@ -15,6 +15,7 @@ type 'a gen = unit -> 'a option type 'a printer = Format.formatter -> 'a -> unit type 'a ktree = unit -> [`Nil | `Node of 'a * 'a ktree list] +(* TODO: restore this (** {2 Transient Identifiers} *) module Transient : sig type t @@ -46,6 +47,7 @@ module Transient : sig exception Frozen (** Raised when a frozen ID is used. *) end +*) (** {2 Signature} *)