www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Ada-Style Modulo Integer Types

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Have anybody implement Ada-style modulo types

https://en.wikibooks.org/wiki/Ada_Programming/Types/mod

in D?

I'm thinking

modulo.d:

struct Mod(size_t m, Block = uint)
{
     static assert(m <= 2^^(8*Block.sizeof));
     Block value;
}

typically used as

Mod!(8, ubyte)
Mod!(256, ubyte)
Mod!(8, uint)

?
Apr 22 2016
next sibling parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Friday, 22 April 2016 at 17:37:44 UTC, Nordlöw wrote:
 Have anybody implement Ada-style modulo types

 https://en.wikibooks.org/wiki/Ada_Programming/Types/mod
Here's my first try https://github.com/nordlow/phobos-next/blob/master/src/modulo.d Is there a way to use alias _value this; except mutation of _value which should check that _value is within value range (0 .. m-1)?
Apr 22 2016
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 22.04.2016 21:52, Nordlöw wrote:
 On Friday, 22 April 2016 at 17:37:44 UTC, Nordlöw wrote:
 Have anybody implement Ada-style modulo types

 https://en.wikibooks.org/wiki/Ada_Programming/Types/mod
Here's my first try https://github.com/nordlow/phobos-next/blob/master/src/modulo.d Is there a way to use alias _value this; except mutation of _value which should check that _value is within value range (0 .. m-1)?
Why do you want to perform a range check? Shouldn't it just take the appropriate remainder? Anyway, the following will do what you request: struct Mod(int M){ private int x; property int prop(int x)in{assert(0<=x&&x<M);}body{ return this.x=x; } property int prop(){ return x; } alias prop this; }
Apr 22 2016
prev sibling parent Ivan Kazmenko <gassa mail.ru> writes:
On Friday, 22 April 2016 at 17:37:44 UTC, Nordlöw wrote:
 Have anybody implement Ada-style modulo types

 https://en.wikibooks.org/wiki/Ada_Programming/Types/mod
I've implemented a proof-of-concept for algorithmic programming competitions [1]. In these competitions, quite a few problems ask to calculate the result modulo some large prime number. The usual idea is that, this way, you still have to solve the underlying algorithmic problem, but the magnitude of calculated values does not affect your algorithmic complexity. Disclaimer: it is incomplete and tuned for the competitions, and thus not ready for general use. For the record, there is also an implementation of modulo integer for the same problem in C++ by Vladislav Isenbaev. Note that the solutions themselves are not the same, so the timing can't be compared directly. Ivan Kazmenko. [1] http://codeforces.com/contest/628/submission/16212299 [2] http://codeforces.com/contest/628/submission/16610362
Apr 22 2016