Skip to content

Commit

Permalink
fix Cartesian, lightConst, lightImmutable (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
9il authored Sep 11, 2018
1 parent 794b1ce commit c889f23
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 74 deletions.
45 changes: 22 additions & 23 deletions source/mir/ndslice/field.d
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ struct MapField(Field, alias _fun)
///
auto lightConst()() const @property
{
return MapField!(LightConstOf!Field, _fun)(_field.lightConst);
return MapField!(LightConstOf!Field, _fun)(.lightConst(_field));
}

///
auto lightImmutable()() immutable @property
{
return MapField!(LightImmutableOf!Field, _fun)(_field.lightImmutable);
return MapField!(LightImmutableOf!Field, _fun)(.lightImmutable(_field));
}

/++
Expand All @@ -104,19 +104,19 @@ struct MapField(Field, alias _fun)
}

static if (__traits(hasMember, Field, "length"))
auto length()() @property
auto length() @property
{
return _field.length;
}

static if (__traits(hasMember, Field, "shape"))
auto shape()() @property
auto shape() @property
{
return _field.shape;
}

static if (__traits(hasMember, Field, "elementCount"))
auto elementCount()() @property
auto elementCount() @property
{
return _field.elementCount;
}
Expand All @@ -143,13 +143,13 @@ struct VmapField(Field, Fun)
///
auto lightConst()() const @property
{
return VmapField!(LightConstOf!Field, _fun)(_field.lightConst);
return VmapField!(LightConstOf!Field, _fun)(.lightConst(_field));
}

///
auto lightImmutable()() immutable @property
{
return VmapField!(LightImmutableOf!Field, _fun)(_field.lightImmutable);
return VmapField!(LightImmutableOf!Field, _fun)(.lightImmutable(_field));
}

auto ref opIndex(T...)(auto ref T index)
Expand All @@ -165,19 +165,19 @@ struct VmapField(Field, Fun)
}

static if (__traits(hasMember, Field, "length"))
auto length()() @property
auto length() @property
{
return _field.length;
}

static if (__traits(hasMember, Field, "shape"))
auto shape()() @property
auto shape() @property
{
return _field.shape;
}

static if (__traits(hasMember, Field, "elementCount"))
auto elementCount()() @property
auto elementCount() @property
{
return _field.elementCount;
}
Expand Down Expand Up @@ -324,7 +324,7 @@ struct RepeatField(T)
/++
`BitField` is used by $(SUBREF topology, bitwise).
+/
struct BitField(Field, I = typeof(Field.init[size_t.init]))
struct BitField(Field, I = typeof(cast()Field.init[size_t.init]))
if (__traits(isUnsigned, I))
{
@optmath:
Expand Down Expand Up @@ -363,13 +363,13 @@ struct BitField(Field, I = typeof(Field.init[size_t.init]))
///
auto lightConst()() const @property
{
return BitField!(LightConstOf!Field)(_field.lightConst);
return BitField!(LightConstOf!Field, I)(mir.qualifier.lightConst(_field));
}

///
auto lightImmutable()() immutable @property
{
return BitField!(LightImmutableOf!Field)(_field.lightImmutable);
return BitField!(LightImmutableOf!Field, I)(mir.qualifier.lightImmutable(_field));
}

bool opIndex()(size_t index)
Expand Down Expand Up @@ -422,7 +422,7 @@ auto BitField__map(Field, I, alias fun)(BitField!(Field, I) field)
/++
`BitpackField` is used by $(SUBREF topology, bitpack).
+/
struct BitpackField(Field, uint pack, I = typeof(Field.init[size_t.init]))
struct BitpackField(Field, uint pack, I = typeof(cast()Field.init[size_t.init]))
if (__traits(isUnsigned, I))
{
//static assert();
Expand All @@ -437,13 +437,13 @@ struct BitpackField(Field, uint pack, I = typeof(Field.init[size_t.init]))
///
auto lightConst()() const @property
{
return BitpackField!(LightConstOf!Field, pack)(_field.lightConst);
return BitpackField!(LightConstOf!Field, pack)(.lightConst(_field));
}

///
auto lightImmutable()() immutable @property
{
return BitpackField!(LightImmutableOf!Field, pack)(_field.lightImmutable);
return BitpackField!(LightImmutableOf!Field, pack)(.lightImmutable(_field));
}

I opIndex()(size_t index)
Expand Down Expand Up @@ -590,14 +590,14 @@ struct CycleField(Field)
///
auto lightConst()() const @property
{
auto field = _field.lightConst;
auto field = .lightConst(_field);
return CycleField!(typeof(field))(_length, field);
}

///
auto lightImmutable()() immutable @property
{
auto field = _field.lightImmutable;
auto field = .lightImmutable(_field);
return CycleField!(typeof(field))(_length, field);
}

Expand Down Expand Up @@ -638,14 +638,14 @@ struct CycleField(Field, size_t length)
///
auto lightConst()() const @property
{
auto field = _field.lightConst;
auto field = .lightConst(_field);
return CycleField!(typeof(field), _length)(field);
}

///
auto lightImmutable()() immutable @property
{
auto field = _field.lightImmutable;
auto field = .lightImmutable(_field);
return CycleField!(typeof(field), _length)(field);
}

Expand Down Expand Up @@ -748,7 +748,7 @@ struct LinspaceField(T)
@optmath:

///
size_t length()() const @property
size_t length() const @property
{
return _length;
}
Expand All @@ -770,7 +770,6 @@ struct MagicField()

/++
Magic Square size.
Should be even.
+/
size_t _n;

Expand All @@ -794,7 +793,7 @@ struct MagicField()
}

///
size_t[1] shape()() const @property @nogc
size_t[1] shape() const @property @nogc
{
return [_n * _n];
}
Expand Down
34 changes: 19 additions & 15 deletions source/mir/ndslice/iterator.d
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,13 @@ struct RetroIterator(Iterator)
///
auto lightConst()() const @property
{
return RetroIterator!(LightConstOf!Iterator)(.lightConst(_iterator));
return RetroIterator!(LightConstOf!Iterator)(mir.qualifier.lightConst(_iterator));
}

///
auto lightImmutable()() immutable @property
{
return RetroIterator!(LightImmutableOf!Iterator)(.lightImmutable(_iterator));
return RetroIterator!(LightImmutableOf!Iterator)(mir.qualifier.lightImmutable(_iterator));
}

///
Expand Down Expand Up @@ -346,13 +346,13 @@ struct StrideIterator(Iterator)
///
auto lightConst()() const @property
{
return StrideIterator!(LightConstOf!Iterator)(_stride, _iterator.lightConst);
return StrideIterator!(LightConstOf!Iterator)(_stride, .lightConst(_iterator));
}

///
auto lightImmutable()() immutable @property
{
return StrideIterator!(LightImmutableOf!Iterator)(_stride, _iterator.lightImmutable);
return StrideIterator!(LightImmutableOf!Iterator)(_stride, .lightImmutable(_iterator));
}

///
Expand Down Expand Up @@ -498,7 +498,9 @@ struct ZipIterator(Iterators...)
import std.format;
import mir.ndslice.topology: iota;
import std.meta: staticMap;
return mixin("ZipIterator!(staticMap!(LightConstOf, Iterators))(%(_iterators[%s].lightConst,%)].lightConst)".format(_iterators.length.iota));
alias Ret = ZipIterator!(staticMap!(LightConstOf, Iterators));
enum ret = "Ret(%(.lightConst(_iterators[%s]),%)]))".format(_iterators.length.iota);
return mixin(ret);
}

///
Expand All @@ -507,7 +509,9 @@ struct ZipIterator(Iterators...)
import std.format;
import mir.ndslice.topology: iota;
import std.meta: staticMap;
return mixin("ZipIterator!(staticMap!(LightImmutableOf, Iterators))(%(_iterators[%s].lightImmutable,%)].lightImmutable)".format(_iterators.length.iota));
alias Ret = ZipIterator!(staticMap!(LightImmutableOf, Iterators));
enum ret = "Ret(%(.lightImmutable(_iterators[%s]),%)]))".format(_iterators.length.iota);
return mixin(ret);
}

auto opUnary(string op : "*")()
Expand Down Expand Up @@ -624,7 +628,7 @@ struct CachedIterator(Iterator, CacheIterator, FlagIterator)
auto lightConst()() const @property
{
return CachedIterator!(LightConstOf!Iterator, LightConstOf!CacheIterator, LightConstOf!FlagIterator)(
_iterator.lightConst,
.lightConst(_iterator),
_caches.lightConst,
_flags.lightConst,
);
Expand All @@ -634,7 +638,7 @@ struct CachedIterator(Iterator, CacheIterator, FlagIterator)
auto lightImmutable()() immutable @property
{
return CachedIterator!(LightImmutableOf!Iterator, LightImmutableOf!CacheIterator, LightImmutableOf!FlagIterator)(
_iterator.lightImmutable,
.lightImmutable(_iterator),
_caches.lightImmutable,
_flags.lightImmutable,
);
Expand Down Expand Up @@ -1147,7 +1151,7 @@ struct IndexIterator(Iterator, Field)
///
auto lightConst()() const @property
{
return IndexIterator!(LightConstOf!Iterator, LightConstOf!Field)(.lightConst(_iterator), _field.lightConst);
return IndexIterator!(LightConstOf!Iterator, LightConstOf!Field)(.lightConst(_iterator), .lightConst(_field));
}

///
Expand Down Expand Up @@ -1287,13 +1291,13 @@ struct SliceIterator(Iterator, size_t N = 1, SliceKind kind = Contiguous)
///
auto lightConst()() const @property
{
return SliceIterator!(LightConstOf!Iterator, N, kind)(_lengths, _strides, _iterator.lightConst);
return SliceIterator!(LightConstOf!Iterator, N, kind)(_lengths, _strides, .lightConst(_iterator));
}

///
auto lightImmutable()() immutable @property
{
return SliceIterator!(LightImmutableOf!Iterator, N, kind)(_lengths, _strides, _iterator.lightImmutable);
return SliceIterator!(LightImmutableOf!Iterator, N, kind)(_lengths, _strides, .lightImmutable(_iterator));
}

auto opUnary(string op : "*")()
Expand Down Expand Up @@ -1341,13 +1345,13 @@ struct FieldIterator(Field)
///
auto lightConst()() const @property
{
return FieldIterator!(LightConstOf!Field)(_index, _field.lightConst);
return FieldIterator!(LightConstOf!Field)(_index, .lightConst(_field));
}

///
auto lightImmutable()() immutable @property
{
return FieldIterator!(LightImmutableOf!Field)(_index, _field.lightImmutable);
return FieldIterator!(LightImmutableOf!Field)(_index, .lightImmutable(_field));
}

///
Expand Down Expand Up @@ -1688,13 +1692,13 @@ struct StairsIterator(Iterator, string direction)
///
auto lightConst()() const @property
{
return StairsIterator!(LightConstOf!Iterator, direction)(_length, _iterator.lightConst);
return StairsIterator!(LightConstOf!Iterator, direction)(_length, .lightConst(_iterator));
}

///
auto lightImmutable()() immutable @property
{
return StairsIterator!(LightImmutableOf!Iterator, direction)(_length, _iterator.lightImmutable);
return StairsIterator!(LightImmutableOf!Iterator, direction)(_length, .lightImmutable(_iterator));
}

@optmath:
Expand Down
12 changes: 9 additions & 3 deletions source/mir/ndslice/ndfield.d
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ struct Cartesian(NdFields...)
size_t length(size_t d = 0)() @property
{
foreach(f, ref field; _fields)
static if (M!(f + 1) > d)
return field.length!(d - M!f);
static if (M!f <= d && M!(f + 1) > d)
{
enum d = d - M!f;
static if (d)
return field.length!(d - M!f);
else
return field.length;
}
}

///
Expand Down Expand Up @@ -114,7 +120,7 @@ struct Cartesian(NdFields...)
size_t ret = 1;
foreach (ref field; _fields)
ret *= field.elementCount;
ret;
return ret;
}

///
Expand Down
10 changes: 6 additions & 4 deletions source/mir/ndslice/topology.d
Original file line number Diff line number Diff line change
Expand Up @@ -3578,18 +3578,19 @@ auto magic(size_t length)
@safe pure nothrow
version(mir_test) unittest
{
import mir.math.sum;
import mir.ndslice: slice, magic, byDim, map, as, repeat, diagonal, antidiagonal;

bool isMagic(S)(S matrix)
{
import mir.math.sum;
import mir.ndslice: magic, byDim, map, all, diagonal, antidiagonal;
auto n = matrix.length;
auto c = n * (n * n + 1) / 2; // magic number
return // check shape
matrix.length!0 > 0 && matrix.length!0 == matrix.length!1
&& // each row sum should equal magic number
matrix.byDim!0.map!sum.all!(a => a == c)
matrix.byDim!0.map!sum == c.repeat(n)
&& // each columns sum should equal magic number
matrix.byDim!1.map!sum.all!(a => a == c)
matrix.byDim!1.map!sum == c.repeat(n)
&& // diagonal sum should equal magic number
matrix.diagonal.sum == c
&& // antidiagonal sum should equal magic number
Expand All @@ -3600,6 +3601,7 @@ version(mir_test) unittest
assert(!isMagic(magic(2))); // 2x2 magic square does not exist
foreach(n; 3 .. 24)
assert(isMagic(magic(n)));
assert(isMagic(magic(3).as!double.slice));
}

/++
Expand Down
Loading

0 comments on commit c889f23

Please sign in to comment.