Appending static arrays

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 17 12:11:26 PDT 2017


On Mon, Jul 17, 2017 at 12:01:48PM -0700, H. S. Teoh via Digitalmars-d-learn wrote:
[...]
> 	template sumOfLengths(A...)
> 		if (A.length > 0)
> 	{
> 		static if (A.length == 1)
> 			enum sumOfLengths = A[0].length;
> 		else
> 			enum sumOfLengths = A[0].length + sumOfLengths!(A[1 .. $]);
> 	}
> 
> 	T[sumOfLengths!StaticArrays] append(StaticArrays...)(StaticArrays arrays)
> 		if (/* insert static array constraints here */)
> 	{
> 		typeof(return) result = void;
> 		size_t offset = 0;
> 		foreach (a; arrays)
> 		{
> 			result[offset .. offset + a.length] = a[];
> 		}
> 		return result;
> 	}
[...]

Hmm, since we already have sumOfLengths available at compile-time, what
about:

 	T[sumOfLengths!StaticArrays] append(StaticArrays...)(StaticArrays arrays)
 		if (allSatisfy!(isStaticArray, StaticArrays))
 	{
 		typeof(return) result = void;
 		foreach (i, a; 0 .. arrays.length)
 		{
			enum offset = sumOfLengths!(arrays[0 .. i]);
 			result[offset .. offset + a.length] = a[];
 		}
 		return result;
 	}


T

-- 
People say I'm indecisive, but I'm not sure about that. -- YHL, CONLANG


More information about the Digitalmars-d-learn mailing list