caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: mohammad siddiqui <writetosafi@hotmail.com>
Cc: zilles@1969.ws, <skaller@users.sourceforge.net>,
	<rich@annexia.org>, <caml-list@inria.fr>
Subject: [Caml-list] Re: help
Date: Sun, 25 Apr 2004 11:44:11 -0500 (CDT)	[thread overview]
Message-ID: <Pine.LNX.4.44.0404251123480.9460-100000@localhost.localdomain> (raw)
In-Reply-To: <BAY7-F32fKQu1bCdUg00000a4a8@hotmail.com>

On Sun, 25 Apr 2004, 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\x12t \x13inline\x14 
> structures into other structures in OCAML.

That was me.

Let's consider the C code:

typedef struct {
    int a;
    int b;
} foo_t;

typedef struct {
    int c;
    foo_t * arr;
} bar_t;

void f(bar_t * ptr) {
    /* Change a foo_t */
    ptr->arr[ptr->c].a += 1;
    ptr->arr[ptr->c].b -= 1;
}

Fairly simply, and basically what you want to do, right?  Now, Ocaml can't 
"inline" (unbox is what I should have said) the foo_t's into the array.  
So you can't do the above code.  What you *can* implement code like (C 
again):

typedef struct {
    int c;
    foo_t ** arr;
} bar_t;

void f(bar_t ptr) {
    ptr->arr[ptr->c]->a += 1;
    ptr->arr[ptr->c]->b -= 1;
}

Notice the extra level of indirection- arr is no longer a pointer to an 
array of foo_t's, but instead a pointer to an array of pointers to 
foo_t's.

Now, the precise translation of this, second version of the code, is this:

type foo_t = { mutable a: int; mutable b: int };;
type bar_t = { c: int; arr: foo_t array };;

let f ptr = 
    ptr.arr.(ptr.c).a <- ptr.arr.(ptr.c).a + 1;
    ptr.arr.(ptr.c).b <- ptr.arr.(ptr.c).b - 1
;;

But we can do better than this, by converting the structures to tuples, 
giving us:

type foo_t = int * int;;
type bar_t = int * (foo_t array);;

let f ((c, arr) : bar_t) =
    arr.(c) <-
        begin
            match (arr.(c)) with
                | (a, b) -> (a+1, b-1)
       end
;;

Here we're taking advantage of the fact that the foo_t's are boxed (not 
inlined), by simply throwing the old tuple out and replacing it with a new 
tuple with the correct new values.

Does this help?

-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

-------------------
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


  parent reply	other threads:[~2004-04-25 16:40 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-04-25 16:30 [Caml-list] help mohammad siddiqui
2004-04-25  1:53 ` mohammad siddiqui
2004-04-25 12:13   ` Jon Harrop
2004-04-25 12:45     ` Henri DF
2004-04-26 19:13       ` Jon Harrop
2004-04-26 22:38         ` Andrew Lenharth
2004-04-25 16:44   ` Brian Hurt [this message]
2004-04-25 17:28   ` Matt Gushee
2004-04-25 17:06 ` Jon Harrop
2004-04-25 17:39   ` Matt Gushee
2004-04-26  0:45 ` skaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.44.0404251123480.9460-100000@localhost.localdomain \
    --to=bhurt@spnz.org \
    --cc=caml-list@inria.fr \
    --cc=rich@annexia.org \
    --cc=skaller@users.sourceforge.net \
    --cc=writetosafi@hotmail.com \
    --cc=zilles@1969.ws \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).