Tips and Tricks for D

mclysenk at mtu.edu mclysenk at mtu.edu
Mon Jul 3 07:52:40 PDT 2006


In article <e8b8js$1m53$1 at digitaldaemon.com>, Tom S says...
>
>Errm... array.dup is evil, since it causes an alloc -> GC stalls anyone ?
>
>How about this ?
>
>void removeNth(T)(inout T[] array, int n) {
>	if (n < array.length - 1) {
>		memmove(&array[n],
>				&array[n+1],
>				(array.length - n - 1) * T.sizeof);
>	}
>
>	array.length = array.length - 1;
>}
>
>

I thought about using a templated solution like this, but I decided to avoid it
since it is perhaps a bit too complicated for an introductory article.  However,
upon consideration I think that this is probably the best way to go for any sort
of heavy duty application.  Of course, if you have 100k+ elements and you care
about order, even a naive a linked list will beat the fastest array
implementation any day.

-Mikola Lysenko





More information about the Digitalmars-d-learn mailing list