From 76f966aed31a55636ac4d34ab2f5f758ba6f91bf Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 24 Mar 2015 12:21:14 +0100 Subject: [PATCH] ccpersistentArray.copy --- src/data/CCPersistentArray.ml | 2 ++ src/data/CCPersistentArray.mli | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/data/CCPersistentArray.ml b/src/data/CCPersistentArray.ml index fb7b3991..f855f556 100644 --- a/src/data/CCPersistentArray.ml +++ b/src/data/CCPersistentArray.ml @@ -49,6 +49,8 @@ let reroot t = match !t with | Array a -> a | _ -> _reroot t (fun x -> x) +let copy t = ref (Array(Array.copy (reroot t))) + let get t i = match !t with | Array a -> a.(i) | _ -> (reroot t).(i) diff --git a/src/data/CCPersistentArray.mli b/src/data/CCPersistentArray.mli index e1478c16..d5200153 100644 --- a/src/data/CCPersistentArray.mli +++ b/src/data/CCPersistentArray.mli @@ -65,9 +65,12 @@ val set : 'a t -> int -> 'a -> 'a t val length : 'a t -> int (** Returns the length of the persistent array. *) +val copy : 'a t -> 'a t +(** [copy a] returns a fresh copy of [a]. Both copies are independent. *) + val map : ('a -> 'b) -> 'a t -> 'b t val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t -(** Applies the given function to all elements of the array, and returns +(** Applies the given function to all elements of the array, and returns a persistent array initialized by the results of f. In the case of [mapi], the function is also given the index of the element. It is equivalent to [fun f t -> init (fun i -> f (get t i))]. *)