Date: Wed, 29 Sep 1999 04:56:37 +1000
From: skaller <skaller@maxtal.com.au>
To: caml-list@inria.fr
Subject: Can someone explain?
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
This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:25 MET