Documentation and struct staticness in std.algorithm fixes

This commit is contained in:
Denis Shelomovskij 2011-11-03 14:02:25 +03:00
parent 1eba4fa9bb
commit e8712ed9e1
5 changed files with 62 additions and 72 deletions

View file

@ -355,8 +355,8 @@ foreach (e; map!("a + a", "a * a")(arr1))
} }
---- ----
You may alias $(D map) with some function(s) to a symbol and use it You may alias $(D map) with some function(s) to a symbol and use
separately: it separately:
---- ----
alias map!(to!string) stringize; alias map!(to!string) stringize;
@ -784,7 +784,7 @@ unittest
assert(rep[2 .. $] == "1, 2, 3, 4, 5", "["~rep[2 .. $]~"]"); assert(rep[2 .. $] == "1, 2, 3, 4, 5", "["~rep[2 .. $]~"]");
// Test the opApply case. // Test the opApply case.
struct OpApply static struct OpApply
{ {
bool actEmpty; bool actEmpty;
@ -1239,7 +1239,7 @@ template filterBidirectional(alias pred)
{ {
auto filterBidirectional(Range)(Range r) if (isBidirectionalRange!(Unqual!Range)) auto filterBidirectional(Range)(Range r) if (isBidirectionalRange!(Unqual!Range))
{ {
static struct Result struct Result
{ {
alias Unqual!Range R; alias Unqual!Range R;
alias unaryFun!pred predFun; alias unaryFun!pred predFun;
@ -1360,13 +1360,13 @@ unittest
move(obj2, obj3); move(obj2, obj3);
assert(obj3 is obj1); assert(obj3 is obj1);
struct S1 { int a = 1, b = 2; } static struct S1 { int a = 1, b = 2; }
S1 s11 = { 10, 11 }; S1 s11 = { 10, 11 };
S1 s12; S1 s12;
move(s11, s12); move(s11, s12);
assert(s11.a == 10 && s11.b == 11 && s12.a == 10 && s12.b == 11); assert(s11.a == 10 && s11.b == 11 && s12.a == 10 && s12.b == 11);
struct S2 { int a = 1; int * b; } static struct S2 { int a = 1; int * b; }
S2 s21 = { 10, null }; S2 s21 = { 10, null };
s21.b = new int; s21.b = new int;
S2 s22; S2 s22;
@ -1374,9 +1374,9 @@ unittest
assert(s21 == s22); assert(s21 == s22);
// Issue 5661 test(1) // Issue 5661 test(1)
struct S3 static struct S3
{ {
struct X { int n = 0; ~this(){n = 0;} } static struct X { int n = 0; ~this(){n = 0;} }
X x; X x;
} }
static assert(hasElaborateDestructor!S3); static assert(hasElaborateDestructor!S3);
@ -1387,9 +1387,9 @@ unittest
assert(s32.x.n == 1); assert(s32.x.n == 1);
// Issue 5661 test(2) // Issue 5661 test(2)
struct S4 static struct S4
{ {
struct X { int n = 0; this(this){n = 0;} } static struct X { int n = 0; this(this){n = 0;} }
X x; X x;
} }
static assert(hasElaborateCopyConstructor!S4); static assert(hasElaborateCopyConstructor!S4);
@ -1530,7 +1530,7 @@ unittest
swap(a, b); swap(a, b);
assert(a == 34 && b == 42); assert(a == 34 && b == 42);
struct S { int x; char c; int[] y; } static struct S { int x; char c; int[] y; }
S s1 = { 0, 'z', [ 1, 2 ] }; S s1 = { 0, 'z', [ 1, 2 ] };
S s2 = { 42, 'a', [ 4, 6 ] }; S s2 = { 42, 'a', [ 4, 6 ] };
//writeln(s2.tupleof.stringof); //writeln(s2.tupleof.stringof);
@ -1549,7 +1549,7 @@ unittest
unittest unittest
{ {
struct NoCopy static struct NoCopy
{ {
this(this) { assert(0); } this(this) { assert(0); }
int n; int n;
@ -1566,7 +1566,7 @@ unittest
assert(nc1.n == 513 && nc1.s == "uvwxyz"); assert(nc1.n == 513 && nc1.s == "uvwxyz");
assert(nc2.n == 127 && nc2.s == "abc"); assert(nc2.n == 127 && nc2.s == "abc");
struct NoCopyHolder static struct NoCopyHolder
{ {
NoCopy noCopy; NoCopy noCopy;
} }
@ -1630,7 +1630,7 @@ auto splitter(Range, Separator)(Range r, Separator s)
if (is(typeof(ElementType!Range.init == Separator.init)) if (is(typeof(ElementType!Range.init == Separator.init))
&& (hasSlicing!Range || isNarrowString!Range)) && (hasSlicing!Range || isNarrowString!Range))
{ {
struct Result static struct Result
{ {
private: private:
Range _input; Range _input;
@ -1848,7 +1848,7 @@ with any range type, but is most popular with string types.
auto splitter(Range, Separator)(Range r, Separator s) auto splitter(Range, Separator)(Range r, Separator s)
if (is(typeof(Range.init.front == Separator.init.front) : bool)) if (is(typeof(Range.init.front == Separator.init.front) : bool))
{ {
struct Result static struct Result
{ {
private: private:
Range _input; Range _input;
@ -4315,7 +4315,7 @@ unittest
auto static wrap(R)(R r) auto static wrap(R)(R r)
if (isBidirectionalRange!R) if (isBidirectionalRange!R)
{ {
struct Result static struct Result
{ {
@property auto ref front() {return _range.front;} @property auto ref front() {return _range.front;}
@property auto ref back() {return _range.back;} @property auto ref back() {return _range.back;}

View file

@ -868,13 +868,9 @@ extern (C) real rndtonl(real x);
* $(TR $(TD +$(INFIN)) $(TD +$(INFIN)) $(TD no)) * $(TR $(TD +$(INFIN)) $(TD +$(INFIN)) $(TD no))
* ) * )
*/ */
float sqrt(float x) @safe pure nothrow; /* intrinsic */
@safe pure nothrow double sqrt(double x) @safe pure nothrow; /* intrinsic */ /// ditto
{ real sqrt(real x) @safe pure nothrow; /* intrinsic */ /// ditto
float sqrt(float x); /* intrinsic */
double sqrt(double x); /* intrinsic */ /// ditto
real sqrt(real x); /* intrinsic */ /// ditto
}
@trusted pure nothrow { // Should be @safe. See bugs 4628, 4630. @trusted pure nothrow { // Should be @safe. See bugs 4628, 4630.
// Create explicit overloads for integer sqrts. No ddoc for these because // Create explicit overloads for integer sqrts. No ddoc for these because
@ -1814,7 +1810,7 @@ real fabs(real x) @safe pure nothrow; /* intrinsic */
* The hypotenuse is the value of the square root of * The hypotenuse is the value of the square root of
* the sums of the squares of x and y: * the sums of the squares of x and y:
* *
* sqrt($(POW x, 2) + $(POW y, 2)) * sqrt($(POWER x, 2) + $(POWER y, 2))
* *
* Note that hypot(x, y), hypot(y, x) and * Note that hypot(x, y), hypot(y, x) and
* hypot(x, -y) are equivalent. * hypot(x, -y) are equivalent.
@ -1842,7 +1838,7 @@ real hypot(real x, real y) @safe pure nothrow
real u = fabs(x); real u = fabs(x);
real v = fabs(y); real v = fabs(y);
if (!(u >= v)) // check for NaN as well. if (u !>= v) // check for NaN as well.
{ {
v = u; v = u;
u = fabs(y); u = fabs(y);

View file

@ -34,28 +34,22 @@
// RSA Data Security, Inc. MD5 Message-Digest Algorithm. // RSA Data Security, Inc. MD5 Message-Digest Algorithm.
import std.md5; import std.md5;
import std.stdio;
private import std.stdio;
private import std.string;
private import std.c.stdio;
private import std.c.string;
void main(string[] args) void main(string[] args)
{ {
foreach (char[] arg; args) foreach (arg; args)
MDFile(arg); mdFile(arg);
} }
/* Digests a file and prints the result. */ /// Digests a file and prints the result.
void MDFile(string filename) void mdFile(string filename)
{ {
FILE* file = enforce(fopen(filename), "Could not open file `"~filename~"'"); ubyte[16] digest;
scope(exit) fclose(file);
ubyte digest[16];
MD5_CTX context; MD5_CTX context;
context.start(); context.start();
foreach (ubyte buffer; chunks(file, 4096 * 1024)) foreach (buffer; File(filename).byChunk(4096 * 1024))
context.update(buffer); context.update(buffer);
context.finish(digest); context.finish(digest);
writefln("MD5 (%s) = %s", filename, digestToString(digest)); writefln("MD5 (%s) = %s", filename, digestToString(digest));

View file

@ -1990,7 +1990,7 @@ public:
/** /**
Parallel reduce on a random access range. Except as otherwise noted, usage Parallel reduce on a random access range. Except as otherwise noted, usage
is similar to $(D std.algorithm.reduce) . This function works by splitting is similar to $(XREF algorithm, _reduce). This function works by splitting
the range to be reduced into work units, which are slices to be reduced in the range to be reduced into work units, which are slices to be reduced in
parallel. Once the results from all work units are computed, a final serial parallel. Once the results from all work units are computed, a final serial
reduction is performed on these results to compute the final answer. reduction is performed on these results to compute the final answer.
@ -2010,14 +2010,14 @@ public:
These take advantage of the instruction level parallelism of modern CPUs, These take advantage of the instruction level parallelism of modern CPUs,
in addition to the thread-level parallelism that the rest of this in addition to the thread-level parallelism that the rest of this
module exploits. This can lead to better than linear speedups relative module exploits. This can lead to better than linear speedups relative
to $(XREF algorithm, reduce), especially for fine-grained benchmarks to $(XREF algorithm, _reduce), especially for fine-grained benchmarks
like dot products. like dot products.
An explicit seed may be provided as the first argument. If An explicit seed may be provided as the first argument. If
provided, it is used as the seed for all work units and for the final provided, it is used as the seed for all work units and for the final
reduction of results from all work units. Therefore, if it is not the reduction of results from all work units. Therefore, if it is not the
identity value for the operation being performed, results may differ from identity value for the operation being performed, results may differ from
those generated by $(D std.algorithm.reduce) or depending on how many work those generated by $(XREF algorithm, _reduce) or depending on how many work
units are used. The next argument must be the range to be reduced. units are used. The next argument must be the range to be reduced.
--- ---
// Find the sum of squares of a range in parallel, using an explicit seed. // Find the sum of squares of a range in parallel, using an explicit seed.
@ -2391,7 +2391,7 @@ public:
foreach(i; parallel(iota(n))) { foreach(i; parallel(iota(n))) {
immutable x = ( i - 0.5L ) * delta; immutable x = ( i - 0.5L ) * delta;
immutable toAdd = delta / ( 1.0 + x * x ); immutable toAdd = delta / ( 1.0 + x * x );
sums.get = sums.get + toAdd; sums.get += toAdd;
} }
// Add up the results from each worker thread. // Add up the results from each worker thread.

View file

@ -246,10 +246,10 @@ unittest
} }
/** /**
Outputs $(D e) to $(D r). The exact effect is dependent upon the two Outputs $(D e) to $(D r). The exact effect is dependent upon the two
types. which must be an output range. Several cases are accepted, as types. Several cases are accepted, as described below. The code snippets
described below. The code snippets are attempted in order, and the are attempted in order, and the first to compile "wins" and gets
first to compile "wins" and gets evaluated. evaluated.
$(BOOKTABLE , $(BOOKTABLE ,
@ -849,8 +849,8 @@ unittest
/** /**
Returns $(D true) if $(D R) is an infinite input range. An Returns $(D true) if $(D R) is an infinite input range. An
infinite input range is an input range that has a statically-defined infinite input range is an input range that has a statically-defined
enumerated member called $(D empty) that is always $(D false), for enumerated member called $(D empty) that is always $(D false),
example: for example:
---- ----
struct MyInfiniteRange struct MyInfiniteRange
@ -2477,7 +2477,7 @@ assert(s.length == 0);
assert(s.empty); assert(s.empty);
---- ----
In effect $(D takeOne(r)) is somewhat equivalent to $(take(r, 1)) and In effect $(D takeOne(r)) is somewhat equivalent to $(D take(r, 1)) and
$(D takeNone(r)) is equivalent to $(D take(r, 0)), but in certain $(D takeNone(r)) is equivalent to $(D take(r, 0)), but in certain
interfaces it is important to know statically that the range may only interfaces it is important to know statically that the range may only
have at most one element. have at most one element.
@ -3786,7 +3786,7 @@ and the zero-based index in the recurrence has name $(D "n"). The
given string must return the desired value for $(D a[n]) given $(D a[n given string must return the desired value for $(D a[n]) given $(D a[n
- 1]), $(D a[n - 2]), $(D a[n - 3]),..., $(D a[n - stateSize]). The - 1]), $(D a[n - 2]), $(D a[n - 3]),..., $(D a[n - stateSize]). The
state size is dictated by the number of arguments passed to the call state size is dictated by the number of arguments passed to the call
to $(D recurrence). The $(D Recurrence) class itself takes care of to $(D recurrence). The $(D Recurrence) struct itself takes care of
managing the recurrence's state and shifting it appropriately. managing the recurrence's state and shifting it appropriately.
Example: Example:
@ -4306,7 +4306,7 @@ unittest
} }
/** /**
Options for the $(D FrontTransversal) and $(D Transversal) ranges Options for the $(LREF FrontTransversal) and $(LREF Transversal) ranges
(below). (below).
*/ */
enum TransverseOptions enum TransverseOptions
@ -5902,7 +5902,7 @@ enum SearchPolicy
/** /**
Searches with a step that is grows linearly (1, 2, 3,...) Searches with a step that is grows linearly (1, 2, 3,...)
leading to a quadratic search schedule (indexes tried are 0, 1, leading to a quadratic search schedule (indexes tried are 0, 1,
3, 5, 8, 12, 17, 23,...) Once the search overshoots its target, 3, 6, 10, 15, 21, 28,...) Once the search overshoots its target,
the remaining interval is searched using binary search. The the remaining interval is searched using binary search. The
search is completed in $(BIGOH sqrt(n)) time. Use it when you search is completed in $(BIGOH sqrt(n)) time. Use it when you
are reasonably confident that the value is around the beginning are reasonably confident that the value is around the beginning
@ -5913,8 +5913,8 @@ enum SearchPolicy
/** /**
Performs a $(LUCKY galloping search algorithm), i.e. searches Performs a $(LUCKY galloping search algorithm), i.e. searches
with a step that doubles every time, (1, 2, 4, 8, ...) leading with a step that doubles every time, (1, 2, 4, 8, ...) leading
to an exponential search schedule (indexes tried are 0, 1, 2, to an exponential search schedule (indexes tried are 0, 1, 3,
4, 8, 16, 32,...) Once the search overshoots its target, the 7, 15, 31, 63,...) Once the search overshoots its target, the
remaining interval is searched using binary search. A value is remaining interval is searched using binary search. A value is
found in $(BIGOH log(n)) time. found in $(BIGOH log(n)) time.
*/ */
@ -5958,11 +5958,11 @@ enum SearchPolicy
---- ----
auto a = [ 1, 2, 3, 42, 52, 64 ]; auto a = [ 1, 2, 3, 42, 52, 64 ];
auto r = assumeSorted(a); auto r = assumeSorted(a);
assert(r.canFind(3)); assert(r.contains(3));
assert(!r.canFind(32)); assert(!r.contains(32));
auto r1 = sort!"a > b"(a); auto r1 = sort!"a > b"(a);
assert(r1.canFind(3)); assert(r1.contains(3));
assert(!r1.canFind(32)); assert(!r1.contains(32));
assert(r1.release() == [ 64, 52, 42, 3, 2, 1 ]); assert(r1.release() == [ 64, 52, 42, 3, 2, 1 ]);
---- ----
@ -5979,9 +5979,9 @@ enum SearchPolicy
---- ----
auto a = [ 1, 2, 3, 42, 52, 64 ]; auto a = [ 1, 2, 3, 42, 52, 64 ];
auto r = assumeSorted(a); auto r = assumeSorted(a);
assert(r.canFind(42)); assert(r.contains(42));
swap(a[3], a[5]); // illegal to break sortedness of original range swap(a[3], a[5]); // illegal to break sortedness of original range
assert(!r.canFind(42)); // passes although it shouldn't assert(!r.contains(42)); // passes although it shouldn't
---- ----
*/ */
struct SortedRange(Range, alias pred = "a < b") struct SortedRange(Range, alias pred = "a < b")
@ -6181,8 +6181,8 @@ if (isRandomAccessRange!Range)
all $(D x) (e.g., if $(D pred) is "less than", returns the portion of all $(D x) (e.g., if $(D pred) is "less than", returns the portion of
the range with elements strictly smaller than $(D value)). The search the range with elements strictly smaller than $(D value)). The search
schedule and its complexity are documented in schedule and its complexity are documented in
$(LREF SearchPolicy). See also STL's $(WEB $(LREF SearchPolicy). See also STL's
sgi.com/tech/stl/lower_bound.html, lower_bound). $(WEB sgi.com/tech/stl/lower_bound.html, lower_bound).
Example: Example:
---- ----
@ -6205,8 +6205,8 @@ if (isRandomAccessRange!Range)
for all $(D x) (e.g., if $(D pred) is "less than", returns the for all $(D x) (e.g., if $(D pred) is "less than", returns the
portion of the range with elements strictly greater than $(D portion of the range with elements strictly greater than $(D
value)). The search schedule and its complexity are documented in value)). The search schedule and its complexity are documented in
$(LREF SearchPolicy). See also STL's $(WEB $(LREF SearchPolicy). See also STL's
sgi.com/tech/stl/lower_bound.html,upper_bound). $(WEB sgi.com/tech/stl/lower_bound.html,upper_bound).
Example: Example:
---- ----
@ -6382,11 +6382,11 @@ unittest
{ {
auto a = [ 1, 2, 3, 42, 52, 64 ]; auto a = [ 1, 2, 3, 42, 52, 64 ];
auto r = assumeSorted(a); auto r = assumeSorted(a);
assert(r.canFind(3)); assert(r.contains(3));
assert(!r.canFind(32)); assert(!r.contains(32));
auto r1 = sort!"a > b"(a); auto r1 = sort!"a > b"(a);
assert(r1.canFind(3)); assert(r1.contains(3));
assert(!r1.canFind(32)); assert(!r1.contains(32));
assert(r1.release() == [ 64, 52, 42, 3, 2, 1 ]); assert(r1.release() == [ 64, 52, 42, 3, 2, 1 ]);
} }
@ -6455,9 +6455,9 @@ unittest
{ {
auto a = [ 1, 2, 3, 42, 52, 64 ]; auto a = [ 1, 2, 3, 42, 52, 64 ];
auto r = assumeSorted(a); auto r = assumeSorted(a);
assert(r.canFind(42)); assert(r.contains(42));
swap(a[3], a[5]); // illegal to break sortedness of original range swap(a[3], a[5]); // illegal to break sortedness of original range
assert(!r.canFind(42)); // passes although it shouldn't assert(!r.contains(42)); // passes although it shouldn't
} }
unittest unittest
@ -6469,7 +6469,7 @@ unittest
/** /**
Assumes $(D r) is sorted by predicate $(D pred) and returns the Assumes $(D r) is sorted by predicate $(D pred) and returns the
corresponding $(D SortedRange!(pred, R)) having $(D r) as support. To corresponding $(D SortedRange!(pred, R)) having $(D r) as support. To
keep the checking costs low, the cost is $(BIGOH(1)) in release mode keep the checking costs low, the cost is $(BIGOH 1) in release mode
(no checks for sortedness are performed). In debug mode, a few random (no checks for sortedness are performed). In debug mode, a few random
elements of $(D r) are checked for sortedness. The size of the sample elements of $(D r) are checked for sortedness. The size of the sample
is proportional $(BIGOH log(r.length)). That way, checking has no is proportional $(BIGOH log(r.length)). That way, checking has no
@ -6526,7 +6526,7 @@ unittest
{ {
auto b = a[a.length / 2]; auto b = a[a.length / 2];
//auto r = sort(a); //auto r = sort(a);
//assert(r.canFind(b)); //assert(r.contains(b));
} }
} }