Appending static arrays

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


On Mon, Jul 17, 2017 at 05:38:23PM +0000, Nordlöw via Digitalmars-d-learn wrote:
> I'm want to define a specialization of `append()` that takes only
> static arrays as inputs and returns a static array being the sum of
> the lengths of the inputs.
> 
> Have anybody already implemented this?
> 
> If not, I'm specifically interested in how to most conveniently infer
> the length (as an enum) of the returned static array from the
> `.length`s of inputs (which of course must be enum-values too).

Hmm. What about:

	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;
	}


T

-- 
IBM = I'll Buy Microsoft!


More information about the Digitalmars-d-learn mailing list