From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 856FBBB81 for ; Mon, 26 Sep 2005 10:01:03 +0200 (CEST) Received: from lea.cs.unibo.it (lea.cs.unibo.it [130.136.1.101]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j8Q813gN016566 for ; Mon, 26 Sep 2005 10:01:03 +0200 Received: from faust.cs.unibo.it (faust.cs.unibo.it [130.136.7.135]) by lea.cs.unibo.it (Postfix) with ESMTP id D7B498B3F7 for ; Mon, 26 Sep 2005 10:00:55 +0200 (CEST) Received: by faust.cs.unibo.it (Postfix, from userid 2662) id 3D5502676; Mon, 26 Sep 2005 09:57:17 +0200 (CEST) Date: Mon, 26 Sep 2005 09:57:17 +0200 From: Claudio Sacerdoti Coen To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Avoiding shared data Message-ID: <20050926075716.GA30835@cs.unibo.it> References: <20050925213202.32862.qmail@web26805.mail.ukl.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050925213202.32862.qmail@web26805.mail.ukl.yahoo.com> X-Operating-System: Debian GNU/Linux X-Organization: Department of computer science, University of Bologna, European Union User-Agent: Mutt/1.5.6+20040907i X-Miltered: at concorde with ID 4337AABF.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; sacerdoti:01 coen:01 sacerdot:01 unibo:01 caml-list:01 avoiding:01 run-time:01 hashtables:01 val:01 bool:01 rec:01 sacerdoti:01 coen:01 sacerdot:01 unibo:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 > It would be great to know of a completely general (any > nested structures) and fast solution (without copying) > how to produce unshared data structures. If you like to play with fire you can use this unsharing function (that is not complete with respect to any data type, but it is sufficient for my needs). Since it works on the run-time representation of data, it can even unshare abstract data types. exception CanNotUnshare;; (* [unshare t] gives back a copy of t where all sharing has been removed *) (* Physical equality becomes meaningful on unshared terms. Hashtables that *) (* use physical equality can now be used to associate information to evey *) (* node of the term. *) val unshare: ?already_unshared:('a -> bool) -> 'a -> 'a let unshare ?(already_unshared = function _ -> false) t = let obj = Obj.repr t in let rec aux obj = if already_unshared (Obj.obj obj) then obj else (if Obj.is_int obj then obj else if Obj.is_block obj then begin let tag = Obj.tag obj in if tag < Obj.no_scan_tag then begin let size = Obj.size obj in let new_obj = Obj.new_block 0 size in Obj.set_tag new_obj tag ; for i = 0 to size - 1 do Obj.set_field new_obj i (aux (Obj.field obj i)) done ; new_obj end else if tag = Obj.string_tag then obj else raise CanNotUnshare end else raise CanNotUnshare ) in Obj.obj (aux obj) ;; -- ---------------------------------------------------------------- Real name: Claudio Sacerdoti Coen Doctor in Computer Science, University of Bologna E-mail: sacerdot@cs.unibo.it http://www.cs.unibo.it/~sacerdot ----------------------------------------------------------------