Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] productivity improvement
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Max Skaller <skaller@o...>
Subject: Re: [Caml-list] Array.resize ?
Oleg wrote:

>Hi
>
>Is there an efficient way in O'Caml to append an element to an array ref?
>
>let append_elt r x = r := Array.append !r [| x |];;
>
>copies the contents of the whole array in its body, while e.g. C++ vector 
>push_back in most cases won't (memory is reserved in chunks automatically, or 
>it can be reserved manually)
>
>IOW it seems strange to me that functions of arrays can change their contents 
>but not their size, while functions of array ref's can do it, but 
>inefficiently. How about 
>
>Array.resize :  'a array -> int -> unit
>
>or at least 
>
>Array.resize: 'a array ref -> int -> unit ?
>
I might add that I have requested a variable length array, similar to a 
C++ vector.
It is possible to make one using pure Ocaml + a little Obj.magic. **
But it would be better if it were in the standard distribution.

[you need allocate initialise unused slots using Obj.magic ..
this is considered naughty :-]

Similar for Strings .. though IMHO module String should be deprecated:
we don't need Strings when we have Arrays.

-- 
John Max Skaller, mailto:skaller@ozemail.com.au
snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.
voice:61-2-9660-0850




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