[mercury-users] Safe use of uninitialised arrays

Ralph Becket rbeck at microsoft.com
Thu Jan 20 05:23:59 AEDT 2000


Efficiency question.  It's often the case that one
creates an array and then fills in every slot.
However, the current array library insists that
the programmer provides a value with which to
initialise every element before delivering the new
array.  Now I think this is good practice in general,
but there are places where you just don't want the
overhead.  In much the same way as one is prepared
to take the consequences by compiling with array
bounds checking disabled, I'd like to have something
like

:- func array__unsafe_init_uninitialised(int) = array(T).

where I can avoid the default initialisation step.
Alternatively, I'd be almost as happy with being able
to supply an initialising predicate, e.g.

:- pred array__init_from_fn(
	int,				% array size
	pred(T2, T2, T1),		% `generating function'
	T2,				% `seed value'
	T2,				% `final value'
	array(T1)
).

where array__init_from_fn/4 would look something
like this:

init_from_fn(N, P, X0, X, A) :-
	A0 = build_unitialised_array(N),
	init_from_fn_0(0, N, P, X0, X, A0, A).

init_from_fn_0(I, N, P, X0, X, A0, A) :-
	( if I < N then
		P(X0, X1, Y),
		A1 = array__set(A0, I, Y),
		init_from_fn_0(I + 1, N, P, X1, X, A1, A)
	  else
		X = X0,
		A = A0
	).

The only problem with the latter is that often you
want variants that take two (or more) in/out state
pairs a la list__foldl2.

Anybody game to put something like this in the
library?  I admit it's smallish beer, but it's been
niggling me a lot lately.

Cheers,

Ralph
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list