www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ndslice of an array structure member?

reply Jay Norwood <jayn prismnet.com> writes:
I'm trying to learn ndslice.  It puzzles me why t3 compiles ok, 
but t4 causes a compiler error in the example below.  Should I be 
able to slice a struct member that is an array?

import std.stdio;
import std.experimental.ndslice;
import std.experimental.ndslice.iteration: transposed;
struct sample{
	ulong [10] core_ctr;
}

struct block{
	ulong[60] samples;
}

void main() {
	auto a1 = new sample[60];
	auto t3 = a1.sliced!(ReplaceArrayWithPointer.no)(3,4,5);
	auto b1 = new block;
	auto t4 = b1.samples.sliced!(ReplaceArrayWithPointer.no)(3,4,5);
}

====== results in error
************  Building D project: nd4  ************
 Running: "C:\Program Files\dub\dub.exe" build
Performing "debug" build using dmd for x86. dip80-ndslice 0.8.4: target for configuration "library" is up to date. nd4 ~master: building configuration "application"... src\app.d(16,58): Error: template std.experimental.ndslice.slice.sliced cannot deduce function from argument types !(cast(Flag)false)(ulong[60], int, int, int), candidates are: C:\Users\rlxv10\AppData\Roaming\dub\packages\dip80-ndslice-0.8.4\source\std\experimental\nd lice\slice.d(39,6): std.experimental.ndslice.slice.sliced(Flag mod = ReplaceArrayWithPointer.yes, Range, Lengths...)(Range range, Lengths lengths) if (!isStaticArray!Range && !isNarrowString!Range && allSatisfy!(isIndex, Lengths) && Lengths.length) C:\Users\rlxv10\AppData\Roaming\dub\packages\dip80-ndslice-0.8.4\source\std\experimental\nd lice\slice.d(47,6): std.experimental.ndslice.slice.sliced(Flag mod = ReplaceArrayWithPointer.yes, uint N, Range)(Range range, auto ref size_t[N] lengths, size_t shift = 0) if (!isStaticArray!Range && !isNarrowString!Range && N) C:\Users\rlxv10\AppData\Roaming\dub\packages\dip80-ndslice-0.8.4\source\std\experimental\nds ice\slice.d(116,1): std.experimental.ndslice.slice.sliced(Names...) if (Names.length && !anySatisfy!(isType, Names) && allSatisfy!(isStringValue, Names)) dmd failed with exit code 1. ^^^ Terminated, exit code: 2 ^^^
Dec 21 2015
parent reply Jack Stouffer <jack jackstouffer.com> writes:
On Monday, 21 December 2015 at 23:59:07 UTC, Jay Norwood wrote:
 I'm trying to learn ndslice.  It puzzles me why t3 compiles ok, 
 but t4 causes a compiler error in the example below.  Should I 
 be able to slice a struct member that is an array?

 import std.stdio;
 import std.experimental.ndslice;
 import std.experimental.ndslice.iteration: transposed;
 struct sample{
 	ulong [10] core_ctr;
 }

 struct block{
 	ulong[60] samples;
 }

 void main() {
 	auto a1 = new sample[60];
 	auto t3 = a1.sliced!(ReplaceArrayWithPointer.no)(3,4,5);
 	auto b1 = new block;
 	auto t4 = 
 b1.samples.sliced!(ReplaceArrayWithPointer.no)(3,4,5);
 }
The problem is that t3 is slicing a1 which is a dynamic array, which is a range, while t4 is trying to slice a static array, which is not a range. The ranges primitive popFront mutates the length of the range, so static arrays cannot be used as ranges. But, if you take a slice of b1.samples, you can use that as a range. auto t4 = b1.samples[].sliced!(ReplaceArrayWithPointer.no)(3,4,5); See the section on ranges on this page for more general info on ranges: http://dlang.org/overview.html
Dec 21 2015
parent Jay Norwood <jayn prismnet.com> writes:
On Tuesday, 22 December 2015 at 01:13:54 UTC, Jack Stouffer wrote:
 The problem is that t3 is slicing a1 which is a dynamic array, 
 which is a range, while t4 is trying to slice a static array, 
 which is not a range.
ok, thanks. I lost track of the double meaning of static ... I normally think of static as variables with static preceding the type, allocated at start-up, but in this case it refers to structure members that will have a fixed size after the allocation.
Dec 21 2015