Fix template constraints style in tanya.math

This commit is contained in:
Eugen Wissner 2017-09-11 06:48:47 +02:00
parent 3eb8618c32
commit e8143bd0cc
3 changed files with 80 additions and 43 deletions

View File

@ -69,7 +69,7 @@ struct Integer
* Precondition: $(D_INLINECODE allocator !is null) * Precondition: $(D_INLINECODE allocator !is null)
*/ */
this(T)(const T value, shared Allocator allocator = defaultAllocator) this(T)(const T value, shared Allocator allocator = defaultAllocator)
if (isIntegral!T) if (isIntegral!T)
{ {
this(allocator); this(allocator);
this = value; this = value;
@ -77,7 +77,7 @@ struct Integer
/// Ditto. /// Ditto.
this(T)(ref T value, shared Allocator allocator = defaultAllocator) this(T)(ref T value, shared Allocator allocator = defaultAllocator)
if (is(Unqual!T == Integer)) if (is(Unqual!T == Integer))
{ {
this(allocator); this(allocator);
this = value; this = value;
@ -85,8 +85,7 @@ struct Integer
/// Ditto. /// Ditto.
this(T)(T value, shared Allocator allocator = defaultAllocator) this(T)(T value, shared Allocator allocator = defaultAllocator)
nothrow @safe @nogc if (is(T == Integer))
if (is(T == Integer))
{ {
this(allocator); this(allocator);
if (allocator is value.allocator) if (allocator is value.allocator)
@ -129,8 +128,8 @@ struct Integer
this(R)(const Sign sign, this(R)(const Sign sign,
R value, R value,
shared Allocator allocator = defaultAllocator) shared Allocator allocator = defaultAllocator)
if (isBidirectionalRange!R && hasLength!R if (isBidirectionalRange!R && hasLength!R
&& is(Unqual!(ElementType!R) == ubyte)) && is(Unqual!(ElementType!R) == ubyte))
{ {
this(allocator); this(allocator);
grow(value.length / (digitBitCount / 8) + 1); grow(value.length / (digitBitCount / 8) + 1);
@ -156,6 +155,7 @@ struct Integer
} }
} }
///
nothrow @safe @nogc unittest nothrow @safe @nogc unittest
{ {
ubyte[8] range = [ 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xdd, 0xee ]; ubyte[8] range = [ 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xdd, 0xee ];
@ -173,10 +173,9 @@ struct Integer
* *
* Precondition: $(D_INLINECODE allocator !is null) * Precondition: $(D_INLINECODE allocator !is null)
*/ */
this(R)(R value, this(R)(R value, shared Allocator allocator = defaultAllocator)
shared Allocator allocator = defaultAllocator) if (isBidirectionalRange!R && hasLength!R
if (isBidirectionalRange!R && hasLength!R && is(Unqual!(ElementType!R) == ubyte))
&& is(Unqual!(ElementType!R) == ubyte))
{ {
this(Sign.positive, value, allocator); this(Sign.positive, value, allocator);
@ -280,6 +279,19 @@ struct Integer
} }
} }
///
private nothrow @safe @nogc unittest
{
{
Integer i;
assert(i.length == 0);
}
{
auto i = Integer(-123456789);
assert(i.length == 4);
}
}
/** /**
* Assigns a new value. * Assigns a new value.
* *
@ -290,7 +302,7 @@ struct Integer
* Returns: $(D_KEYWORD this). * Returns: $(D_KEYWORD this).
*/ */
ref Integer opAssign(T)(const T value) ref Integer opAssign(T)(const T value)
if (isIntegral!T) if (isIntegral!T)
{ {
rep[0 .. this.size].fill(digit.init); rep[0 .. this.size].fill(digit.init);
grow(digitBitCount / 8 + 1); grow(digitBitCount / 8 + 1);
@ -325,7 +337,7 @@ struct Integer
/// Ditto. /// Ditto.
ref Integer opAssign(T)(ref T value) @trusted ref Integer opAssign(T)(ref T value) @trusted
if (is(Unqual!T == Integer)) if (is(Unqual!T == Integer))
{ {
this.rep = allocator.resize(this.rep, value.size); this.rep = allocator.resize(this.rep, value.size);
value.rep[0 .. value.size].copy(this.rep[0 .. value.size]); value.rep[0 .. value.size].copy(this.rep[0 .. value.size]);
@ -337,7 +349,7 @@ struct Integer
/// Ditto. /// Ditto.
ref Integer opAssign(T)(T value) nothrow @safe @nogc ref Integer opAssign(T)(T value) nothrow @safe @nogc
if (is(T == Integer)) if (is(T == Integer))
{ {
swap(this.rep, value.rep); swap(this.rep, value.rep);
swap(this.sign, value.sign); swap(this.sign, value.sign);
@ -361,7 +373,7 @@ struct Integer
/// Ditto. /// Ditto.
T opCast(T)() const T opCast(T)() const
if (isIntegral!T && isUnsigned!T) if (isIntegral!T && isUnsigned!T)
{ {
T ret; T ret;
ubyte shift; ubyte shift;
@ -375,7 +387,7 @@ struct Integer
/// Ditto. /// Ditto.
T opCast(T)() const T opCast(T)() const
if (isIntegral!T && isSigned!T) if (isIntegral!T && isSigned!T)
{ {
return this.sign ? -(cast(Unsigned!T) this) : cast(Unsigned!T) this; return this.sign ? -(cast(Unsigned!T) this) : cast(Unsigned!T) this;
} }
@ -405,7 +417,7 @@ struct Integer
assert(cast(long) integer == 0); assert(cast(long) integer == 0);
} }
/* trim unused digits /* Trim unused digits.
* *
* This is used to ensure that leading zero digits are * This is used to ensure that leading zero digits are
* trimed and the leading "size" digit will be non-zero * trimed and the leading "size" digit will be non-zero
@ -667,7 +679,7 @@ struct Integer
/// Ditto. /// Ditto.
int opCmp(I)(const I that) const int opCmp(I)(const I that) const
if (isIntegral!I) if (isIntegral!I)
{ {
if (that < 0 && !this.sign) if (that < 0 && !this.sign)
{ {
@ -713,7 +725,7 @@ struct Integer
* Returns: Whether the two integers are equal. * Returns: Whether the two integers are equal.
*/ */
bool opEquals(I)(auto ref const I that) const bool opEquals(I)(auto ref const I that) const
if (is(I : Integer) || isIntegral!I) if (is(I : Integer) || isIntegral!I)
{ {
return opCmp!I(that) == 0; return opCmp!I(that) == 0;
} }
@ -1135,14 +1147,14 @@ struct Integer
* Returns: Result. * Returns: Result.
*/ */
Integer opBinary(string op)(auto ref const Integer operand) const Integer opBinary(string op)(auto ref const Integer operand) const
if ((op == "+" || op == "-") || (op == "*")) if ((op == "+" || op == "-") || (op == "*"))
{ {
mixin("return Integer(this, allocator) " ~ op ~ "= operand;"); mixin("return Integer(this, allocator) " ~ op ~ "= operand;");
} }
/// Ditto. /// Ditto.
Integer opBinary(string op)(const auto ref Integer operand) const Integer opBinary(string op)(const auto ref Integer operand) const
if (op == "/" || op == "%") if (op == "/" || op == "%")
in in
{ {
assert(operand.length > 0, "Division by zero."); assert(operand.length > 0, "Division by zero.");
@ -1154,7 +1166,7 @@ struct Integer
/// Ditto. /// Ditto.
Integer opBinary(string op)(const size_t operand) const Integer opBinary(string op)(const size_t operand) const
if (op == "<<" || op == ">>") if (op == "<<" || op == ">>")
{ {
mixin("return Integer(this, allocator) " ~ op ~ "= operand;"); mixin("return Integer(this, allocator) " ~ op ~ "= operand;");
} }
@ -1260,9 +1272,9 @@ struct Integer
auto ref Q quotient, auto ref Q quotient,
ref ARGS args) ref ARGS args)
const nothrow @safe @nogc const nothrow @safe @nogc
if ((is(Q : typeof(null)) if ((is(Q : typeof(null))
|| (is(Q : Integer) && __traits(isRef, quotient))) || (is(Q : Integer) && __traits(isRef, quotient)))
&& (ARGS.length == 0 || (ARGS.length == 1 && is(ARGS[0] : Integer)))) && (ARGS.length == 0 || (ARGS.length == 1 && is(ARGS[0] : Integer))))
in in
{ {
assert(divisor != 0, "Division by zero."); assert(divisor != 0, "Division by zero.");

View File

@ -18,11 +18,6 @@ public import tanya.math.mp;
public import tanya.math.random; public import tanya.math.random;
import tanya.meta.trait; import tanya.meta.trait;
version (unittest)
{
import std.algorithm.iteration;
}
/** /**
* Computes $(D_PARAM x) to the power $(D_PARAM y) modulo $(D_PARAM z). * Computes $(D_PARAM x) to the power $(D_PARAM y) modulo $(D_PARAM z).
* *
@ -43,7 +38,7 @@ version (unittest)
* Precondition: $(D_INLINECODE z > 0) * Precondition: $(D_INLINECODE z > 0)
*/ */
H pow(I, G, H)(in auto ref I x, in auto ref G y, in auto ref H z) H pow(I, G, H)(in auto ref I x, in auto ref G y, in auto ref H z)
if (isIntegral!I && isIntegral!G && isIntegral!H) if (isIntegral!I && isIntegral!G && isIntegral!H)
in in
{ {
assert(z > 0, "Division by zero."); assert(z > 0, "Division by zero.");
@ -84,7 +79,7 @@ body
/// Ditto. /// Ditto.
I pow(I)(const auto ref I x, const auto ref I y, const auto ref I z) I pow(I)(const auto ref I x, const auto ref I y, const auto ref I z)
if (is(I == Integer)) if (is(I == Integer))
in in
{ {
assert(z.length > 0, "Division by zero."); assert(z.length > 0, "Division by zero.");
@ -137,7 +132,7 @@ pure nothrow @safe @nogc unittest
} }
/// ///
unittest nothrow @safe @nogc unittest
{ {
assert(pow(Integer(3), Integer(5), Integer(7)) == 5); assert(pow(Integer(3), Integer(5), Integer(7)) == 5);
assert(pow(Integer(2), Integer(2), Integer(1)) == 0); assert(pow(Integer(2), Integer(2), Integer(1)) == 0);
@ -165,23 +160,52 @@ bool isPseudoprime(ulong x) nothrow pure @safe @nogc
} }
/// ///
unittest pure nothrow @safe @nogc unittest
{ {
uint[30] known = [74623, 74653, 74687, 74699, 74707, 74713, 74717, 74719, assert(74623.isPseudoprime);
74843, 74747, 74759, 74761, 74771, 74779, 74797, 74821, assert(104729.isPseudoprime);
74827, 9973, 104729, 15485867, 49979693, 104395303, assert(15485867.isPseudoprime);
593441861, 104729, 15485867, 49979693, 104395303, assert(!15485868.isPseudoprime);
593441861, 899809363, 982451653]; }
known.each!((ref x) => assert(isPseudoprime(x))); private pure nothrow @safe @nogc unittest
{
assert(74653.isPseudoprime);
assert(74687.isPseudoprime);
assert(74699.isPseudoprime);
assert(74707.isPseudoprime);
assert(74713.isPseudoprime);
assert(74717.isPseudoprime);
assert(74719.isPseudoprime);
assert(74747.isPseudoprime);
assert(74759.isPseudoprime);
assert(74761.isPseudoprime);
assert(74771.isPseudoprime);
assert(74779.isPseudoprime);
assert(74797.isPseudoprime);
assert(74821.isPseudoprime);
assert(74827.isPseudoprime);
assert(9973.isPseudoprime);
assert(49979693.isPseudoprime);
assert(104395303.isPseudoprime);
assert(593441861.isPseudoprime);
assert(104729.isPseudoprime);
assert(15485867.isPseudoprime);
assert(49979693.isPseudoprime);
assert(104395303.isPseudoprime);
assert(593441861.isPseudoprime);
assert(899809363.isPseudoprime);
assert(982451653.isPseudoprime);
} }
/** /**
* Calculates the absolute value of a number.
*
* Params: * Params:
* I = Value type. * I = Value type.
* x = Value. * x = Value.
* *
* Returns: The absolute value of a number. * Returns: Absolute value of $(D_PARAM x).
*/ */
I abs(I : Integer)(const auto ref I x) I abs(I : Integer)(const auto ref I x)
{ {
@ -199,7 +223,7 @@ I abs(I : Integer)(I x)
/// Ditto. /// Ditto.
I abs(I)(const I x) I abs(I)(const I x)
if (isIntegral!I) if (isIntegral!I)
{ {
return x >= 0 ? x : -x; return x >= 0 ? x : -x;
} }

View File

@ -227,8 +227,9 @@ class Entropy
* See_Also: * See_Also:
* $(D_PSYMBOL EntropySource) * $(D_PSYMBOL EntropySource)
*/ */
Entropy opOpAssign(string Op)(EntropySource source) pure nothrow @safe @nogc Entropy opOpAssign(string op)(EntropySource source)
if (Op == "~") pure nothrow @safe @nogc
if (op == "~")
in in
{ {
assert(sourceCount_ <= sources.length); assert(sourceCount_ <= sources.length);