[haXe] Deep Copy of an array? how can it be done generically

edA-qa mort-ora-y eda-qa at disemia.com
Sun Apr 22 15:36:21 CEST 2007


I often used Matrix like constructs such as:
	Array<Array<Int>>
The problem is that I don't have an easy way to produce a copy of these
structures.  A simple "copy" will just reuse the second level objects,
but I want a proper deep copy.

Obviously it is easy enough for a given case to do a copy, but I would
like to have some kind of generic function which can do the copy,
otherwise I'm forced to write a copy operation for every matrix like
type that I have.

I tried to get something like the following possible, but I always get
some kind of compilation error that I can't figure out how to get rid of.


class ArrayUtil<T>
{
	/**
	 * Does a deep copy on the array, assuming the items are
	 * further arrays or have "copy" semantics (such as fundamentals)
	 */
	static public function deepCopy( arr : Array<ArrayUtil.T> ) :
Array<ArrayUtil.T>
	{
		var r = new Array<ArrayUtil.T>();
		for( i in 0...arr.length )
		{
			if( Std.is( arr[i], Array ) )	//How to do this line?!
				r.push( deepCopy( arr[i] ) );
			else
				r.push( arr[i].copy() );
		}
		
		return r;
	}
}

-- 
edA-qa mort-ora-y
Idea Architect
http://disemia.com/

Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : http://lists.motion-twin.com/pipermail/haxe/attachments/20070422/59b6ea1e/signature.pgp


More information about the Haxe mailing list