Why are static arrays not ranges?
jmh530 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Sep 21 15:39:04 PDT 2015
On Monday, 21 September 2015 at 20:33:10 UTC, Jack Stouffer wrote:
>
> That's ridiculous. Do I have to wrap my static arrays in
> structs to get range primitives?
>
> Is there an actual reason for this?
I had done basically the same thing. Below is my naive
implementation.
import std.traits;
import std.range;
import std.array : array;
import std.stdio : writeln;
import std.algorithm : map;
struct hybrid_array(T : U[N], U, size_t N)
if ( isStaticArray!T )
{
T static_array;
private auto _length = static_array.length;
auto domain = iota(0, static_array.length);
this(T x)
{
static_array = x;
}
@property bool empty()
{
return domain.empty;
}
@property size_t length()
{
return _length;
}
@property U front()
{
assert(!empty);
return static_array[domain.front];
}
void popFront()
{
assert(!empty);
domain.popFront;
_length--;
}
@property hybrid_array save()
{
return this;
}
@property U back()
{
assert(!empty);
return static_array[domain.back];
}
void popBack()
{
assert(!empty);
domain.popBack;
_length++;
}
U opIndex(size_t val)
{
assert(!empty);
return static_array[domain[val]];
}
}
void main()
{
int[5] x = [0, 10, 2, 6, 1];
//auto squares = map!(a => a * a)(x); //doesn't work
auto range = iota(0, x.length).array;
alias h_array = hybrid_array!(int[5]);
auto y = h_array(x);
writeln( isRandomAccessRange!(typeof(y)) );
auto squares = map!(a => a * a)(y); //success, does work
writeln(squares);
}
More information about the Digitalmars-d-learn
mailing list