From: skaller <skaller@maxtal.com.au>
To: caml-list@inria.fr
Subject: Can someone explain?
Date: Wed, 29 Sep 1999 04:56:37 +1000 [thread overview]
Message-ID: <37F10F65.47A4212C@maxtal.com.au> (raw)
In-Reply-To: <19990909214313.55795@pauillac.inria.fr>
Is there a way to access a value of a class instance?
If so, what is the syntax? If not, what is the purpose
of allowing 'val' bindings in class declarations in module
signatures?
Similarly, what is the purpose of allowing 'virtual'
methods in class types and class declarations in
module signatures?
I have a doubly linked list class, and concatenating
two lists takes 100 times longer in ocaml using my
code than pythons list concatenation function,
which is written in C.
My code isn't optimal (given the particular data structure
I've chosen) because the concat function cannot
get at values of the class. Excerpt given below.
Any advice appreciated. (A high speed mutable list
in the standard library would be even better :-)
module DoublyLinkedList : BiDirectional =
struct (* doubly linked list type *)
type 't d_node =
{
mutable nxt: 't iterator;
mutable prv: 't iterator;
mutable data: 't
}
and 't iterator =
Empty
| Node of 't d_node
let next x = match x with Empty -> Empty | Node n -> n.nxt
let deref x = match x with Empty -> None | Node n -> Some n.data
class ['t] dlist =
object(self)
val mutable first': 't iterator = Empty
val mutable last': 't iterator = Empty
val mutable size: int = 0
method private init node =
last' <- node;
first' <- node;
size <- 1
method length = size
(* STL style mutators *)
method push_back (data':'t): unit =
match last' with
| Empty -> self#init (Node {nxt=Empty; prv=Empty; data=data'})
| Node fin ->
let tmp = Node {nxt=Empty; prv=last'; data=data'} in
fin.nxt <- tmp;
last' <- tmp;
size <- size + 1
method first = first'
end
let concat (x:'a dlist) (y:'a dlist) :'a dlist =
let z = new dlist in
let i = ref x#first in
while deref !i <> None do
match deref !i with
| Some v -> z#push_back v; i := next !i
| None -> assert false
done;
let j = ref y#first in
while deref !j <> None do
match deref !j with
| Some v -> z#push_back v; j := next !j
| None -> assert false
done;
z
end
--
John Skaller, mailto:skaller@maxtal.com.au
1/10 Toxteth Rd Glebe NSW 2037 Australia
homepage: http://www.maxtal.com.au/~skaller
downloads: http://www.triode.net.au/~skaller
next prev parent reply other threads:[~1999-09-29 20:19 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-09-09 16:34 strange behavior of the object type-checker Pierre Boulet
1999-09-09 19:43 ` Jerome Vouillon
1999-09-28 18:56 ` skaller [this message]
1999-10-04 8:23 ` Can someone explain? Pierre Weis
1999-10-04 22:57 ` skaller
1999-10-05 9:43 ` Jerome Vouillon
1999-10-05 19:35 ` Gerd Stolpmann
1999-10-06 9:42 ` skaller
1999-10-08 0:17 ` Problem of coercion in recursive class definitions Peter Schrammel
1999-10-05 21:42 ` Can someone explain? Lyn A Headley
1999-10-06 10:17 ` skaller
1999-10-13 19:16 Juergen Pfitzenmaier
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=37F10F65.47A4212C@maxtal.com.au \
--to=skaller@maxtal.com.au \
--cc=caml-list@inria.fr \
/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