Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2002-10-18 (08:35)
From: Eray Ozkural <erayo@c...>
Subject: Re: [Caml-list] Array.resize ?
On Friday 12 July 2002 01:30, 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)

Hi Oleg,

Here is some Data Structures 101 for you.

A vector is not an array. It is more like one of the "extensible array" types 
that are around since 1960's. The implementation of vector types use what we 
call an open table with amortized time for n consequent inserts being O(n) 
making a single insert O(1). (See the MIT Algorithms textbook for details) 
This also answers why memory alloc doesn't cost much when using vectors. 
Since the array is expanded or contracted by a factor of 2 in size you need 
only a logarithmic number of memory allocation calls.

A vector is not a special type in C++. You can implement one in ocaml just as 
well or better.


Eray Ozkural <>
Comp. Sci. Dept., Bilkent University, Ankara
GPG public key fingerprint: 360C 852F 88B0 A745 F31B  EA0F 7C07 AE16 874D 539C
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: