如果我有一个二维数组在D中,我知道可以按行创建1D切片,如下所示:
auto one_dim_arr=two_dim_arr[i][0..$]
有没有一种简单的方法可以沿列制作1D切片?做些让人想到的事情。
auto one_dim_arr=two_dim_arr[0..$][j]
would do?
如果我有一个二维数组在D中,我知道可以按行创建1D切片,如下所示:
auto one_dim_arr=two_dim_arr[i][0..$]
有没有一种简单的方法可以沿列制作1D切片?做些让人想到的事情。
auto one_dim_arr=two_dim_arr[0..$][j]
would do?
以下是可能看起来像什么:
// Demo
void main()
{
int[3][3] arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
// simple creation
auto middleColumn = verticalSlice(arr, 1);
assert(middleColumn[1] == 5);
// iteratable
foreach (i, v; middleColumn)
assert(v == 2+i*3);
// still a slice - writing will change original array
middleColumn[1] = 17;
assert(arr[1][1] == 17);
// sliceable itself
auto center = middleColumn[1..2];
center[0] = 42;
assert(arr[1][1] == 42);
// get a normal array with .dup
int[] copyOfMiddleColumn = middleColumn.dup;
}
// Implementation
struct StepSlice(T)
{
T* ptr;
size_t length, step;
T opIndex(size_t index)
in { assert(index<length); }
body { return ptr[step*index]; }
void opIndexAssign(T value, size_t index)
in { assert(index<length); }
body { ptr[step*index] = value; }
StepSlice!T opSlice(size_t start, size_t end)
in { assert(start<=end && end<=length); }
body { return StepSlice!T(ptr+start*step, end-start, step); }
int opApply(int delegate(ref T) dg)
{
int result = 0;
for (size_t i=0; i<length; i++)
{
result = dg(ptr[i*step]);
if (result)
break;
}
return result;
}
int opApply(int delegate(ref size_t, ref T) dg)
{
int result = 0;
for (size_t i=0; i<length; i++)
{
result = dg(i, ptr[i*step]);
if (result)
break;
}
return result;
}
T[] dup()
{
T[] result = new T[length];
for (size_t i=0; i<length; i++)
result[i] = ptr[i*step];
return result;
}
}
StepSlice!T verticalSlice(T, size_t W)(T[W][] arr, size_t column)
{
return StepSlice!T(arr[0].ptr+column, arr.length, W);
}
std.range.stride
:import std.range;
// Demo
void main()
{
int[3][3] arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
// simple creation
auto middleColumn = verticalSlice(arr, 1);
assert(middleColumn[1] == 5);
// iteratable
uint i;
foreach (v; middleColumn)
assert(v == 2+(i++)*3);
// still a slice - writing will change original array
middleColumn[1] = 17;
assert(arr[1][1] == 17);
// sliceable itself
auto center = middleColumn[1..2];
center[0] = 42;
assert(arr[1][1] == 42);
// get a normal array with array()
int[] copyOfMiddleColumn = array(middleColumn);
}
// Implementation
auto verticalSlice(T, size_t W)(T[W][] arr, size_t column)
{
T* start = arr[0].ptr+column;
return stride(start[0..W*arr.length], W);
}
如果您的输入是一个T[][]
(即动态数组的动态数组),并且您想要相同的输出,您可以分配一个新的“外部”数组,并使用内部数组的切片填充它。这将导致O(n)
操作,而正常的切片则为O(1)
操作。编码留给读者作为练习。