From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA11211; Sun, 25 Apr 2004 19:28:25 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id TAA11298 for ; Sun, 25 Apr 2004 19:28:24 +0200 (MET DST) Received: from mz3.forethought.net (mzpi5.forethought.net [216.241.36.14]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i3PHSMjq007104 for ; Sun, 25 Apr 2004 19:28:23 +0200 Received: from [216.241.35.41] (helo=swordfish) by mz3.forethought.net with esmtp (Exim 4.30) id 1BHnQT-0002n8-8B for caml-list@pauillac.inria.fr; Sun, 25 Apr 2004 11:28:21 -0600 Received: from matt by swordfish with local (Exim 3.35 #1 (Debian)) id 1BHnQT-0001cD-00 for ; Sun, 25 Apr 2004 11:28:21 -0600 Date: Sun, 25 Apr 2004 11:28:21 -0600 From: Matt Gushee To: caml-list@pauillac.inria.fr Subject: Re: [Caml-list] help Message-ID: <20040425172821.GA973@swordfish> Reply-To: Matt Gushee Mail-Followup-To: caml-list@pauillac.inria.fr References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.3.27i X-Miltered: at nez-perce by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; gushee:01 mgushee:01 havenrock:01 caml-list:01 2004:99 model:01 model:01 struct:01 struct:01 bug:01 faq:01 faq:01 beginner's:01 beginners:01 2004:99 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Sun, Apr 25, 2004 at 01:53:48AM +0000, mohammad siddiqui wrote: > Hello, > > I am having problem dealing with data structures in OCAML, I am translating > code from C to OCAML. As shown below one structure contains array of other > structures as its field. One of the guys here told me that we can?t > ?inline? structures into other structures in OCAML. > I don?t how to deal with this, I already spent like 2 days on it. Should I > make use of objects and let some member of it array of other objects? I am > not sure this will do the purpose. I really appreciate if anyone helps me > out this. > > For example if try to change the value model.supvec. (1).words. (1).wnum, > it changes the values of all elements of the array model.supvec. I tried > using references, mutable fields, Array.set function for modifying the > contents of individual element. > > The structures in OCAML are: > > type word = { mutable wnum:int; (* word number *) > mutable weight:float };; (* word weight *) > > type doc = { > mutable docnum:int; (* Document ID *) > mutable queryid: > mutable costfactor:float; > mutable twonorm_sq:float; > mutable words:word array > };; > > type model = { > mutable sv_num:int; > mutable at_upper_bound:int; > mutable b:float; > mutable supvec:doc array; > mutable alpha: int array; > > } ;; > > The Structures in C are > > typedef struct word { > int wnum; > float weight; > } WORD; > > typedef struct doc { > long docnum; > long queryid; > double costfactor; > double twonorm_sq; > WORD *words; > } DOC; > > > > typedef struct model { > long sv_num; > long at_upper_bound; > double b; > DOC **supvec; > Double *alpha; > } MODEL; > > _________________________________________________________________ > FREE pop-up blocking with the new MSN Toolbar ? get it now! > http://toolbar.msn.com/go/onm00200415ave/direct/01/ > > ------------------- > To unsubscribe, mail caml-list-request@inria.fr Archives: > http://caml.inria.fr > Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: > http://caml.inria.fr/FAQ/ > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners On Sun, Apr 25, 2004 at 04:30:56PM +0000, mohammad siddiqui wrote: > I tried exactly what you have told. Its is fine when we just have one > element in an array of words. If we have more than one, changing the value > of one changes the values of the rest of the elements. That's not exactly what's happening, though your example makes it look that way. > for example , if the test_model is initialiazed like this: > > let test_model = > { sv_num=0; at_upper_bound=0; b=0.; supvec=Array.make 2 > { docnum=0; queryid=0; costfactor=0.; twonorm_sq=0.; words=Array.make 1 > { wnum=ref 0; weight=0. } }; alpha=0. }; Okay, we can see the cause of the problem here: you are initializing an array with Array.make, where the initial value includes a ref. The resulting array will have unique members, *except* that each 'wnum' field points to a single, shared ref value. That's the problem. > now if i change the values , it chnages all th elements of the array > "supvec" in model > > test_model.supvec.(0).words.(0).wnum := 1; As you probably understand by now, what this is really doing is changing all instances of the shared value stored in the 'wnum' field. Evidently that was the only field you tried to change, in which case it would appear that all the elements of 'supvec' were being changed. Here are three possible solutions: 1. Make wnum a mutable field instead of a ref. 2. Replace the array member instead of setting the ref: test_model.supvec.(0).words <- newvalue (but then I guess there's no point in 'wnum' being either a ref or a mutable field) 3. Don't use Array.make to initialize the array. Instead, use a recursive function that starts with an empty array, and adds new elements with Array.append. -- Matt Gushee When a nation follows the Way, Englewood, Colorado, USA Horses bear manure through mgushee@havenrock.com its fields; http://www.havenrock.com/ When a nation ignores the Way, Horses bear soldiers through its streets. --Lao Tzu (Peter Merel, trans.) ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners