mirror of
https://github.com/dlang/phobos.git
synced 2025-05-01 15:40:36 +03:00
Documentation and struct staticness in std.algorithm fixes
This commit is contained in:
parent
1eba4fa9bb
commit
e8712ed9e1
5 changed files with 62 additions and 72 deletions
|
@ -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
|
||||
separately:
|
||||
You may alias $(D map) with some function(s) to a symbol and use
|
||||
it separately:
|
||||
|
||||
----
|
||||
alias map!(to!string) stringize;
|
||||
|
@ -784,7 +784,7 @@ unittest
|
|||
assert(rep[2 .. $] == "1, 2, 3, 4, 5", "["~rep[2 .. $]~"]");
|
||||
|
||||
// Test the opApply case.
|
||||
struct OpApply
|
||||
static struct OpApply
|
||||
{
|
||||
bool actEmpty;
|
||||
|
||||
|
@ -1239,7 +1239,7 @@ template filterBidirectional(alias pred)
|
|||
{
|
||||
auto filterBidirectional(Range)(Range r) if (isBidirectionalRange!(Unqual!Range))
|
||||
{
|
||||
static struct Result
|
||||
struct Result
|
||||
{
|
||||
alias Unqual!Range R;
|
||||
alias unaryFun!pred predFun;
|
||||
|
@ -1360,13 +1360,13 @@ unittest
|
|||
move(obj2, obj3);
|
||||
assert(obj3 is obj1);
|
||||
|
||||
struct S1 { int a = 1, b = 2; }
|
||||
static struct S1 { int a = 1, b = 2; }
|
||||
S1 s11 = { 10, 11 };
|
||||
S1 s12;
|
||||
move(s11, s12);
|
||||
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 };
|
||||
s21.b = new int;
|
||||
S2 s22;
|
||||
|
@ -1374,9 +1374,9 @@ unittest
|
|||
assert(s21 == s22);
|
||||
|
||||
// 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;
|
||||
}
|
||||
static assert(hasElaborateDestructor!S3);
|
||||
|
@ -1387,9 +1387,9 @@ unittest
|
|||
assert(s32.x.n == 1);
|
||||
|
||||
// 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;
|
||||
}
|
||||
static assert(hasElaborateCopyConstructor!S4);
|
||||
|
@ -1530,7 +1530,7 @@ unittest
|
|||
swap(a, b);
|
||||
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 s2 = { 42, 'a', [ 4, 6 ] };
|
||||
//writeln(s2.tupleof.stringof);
|
||||
|
@ -1549,7 +1549,7 @@ unittest
|
|||
|
||||
unittest
|
||||
{
|
||||
struct NoCopy
|
||||
static struct NoCopy
|
||||
{
|
||||
this(this) { assert(0); }
|
||||
int n;
|
||||
|
@ -1566,7 +1566,7 @@ unittest
|
|||
assert(nc1.n == 513 && nc1.s == "uvwxyz");
|
||||
assert(nc2.n == 127 && nc2.s == "abc");
|
||||
|
||||
struct NoCopyHolder
|
||||
static struct NoCopyHolder
|
||||
{
|
||||
NoCopy noCopy;
|
||||
}
|
||||
|
@ -1630,7 +1630,7 @@ auto splitter(Range, Separator)(Range r, Separator s)
|
|||
if (is(typeof(ElementType!Range.init == Separator.init))
|
||||
&& (hasSlicing!Range || isNarrowString!Range))
|
||||
{
|
||||
struct Result
|
||||
static struct Result
|
||||
{
|
||||
private:
|
||||
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)
|
||||
if (is(typeof(Range.init.front == Separator.init.front) : bool))
|
||||
{
|
||||
struct Result
|
||||
static struct Result
|
||||
{
|
||||
private:
|
||||
Range _input;
|
||||
|
@ -4315,7 +4315,7 @@ unittest
|
|||
auto static wrap(R)(R r)
|
||||
if (isBidirectionalRange!R)
|
||||
{
|
||||
struct Result
|
||||
static struct Result
|
||||
{
|
||||
@property auto ref front() {return _range.front;}
|
||||
@property auto ref back() {return _range.back;}
|
||||
|
|
14
std/math.d
14
std/math.d
|
@ -868,13 +868,9 @@ extern (C) real rndtonl(real x);
|
|||
* $(TR $(TD +$(INFIN)) $(TD +$(INFIN)) $(TD no))
|
||||
* )
|
||||
*/
|
||||
|
||||
@safe pure nothrow
|
||||
{
|
||||
float sqrt(float x); /* intrinsic */
|
||||
double sqrt(double x); /* intrinsic */ /// ditto
|
||||
real sqrt(real x); /* intrinsic */ /// ditto
|
||||
}
|
||||
float sqrt(float x) @safe pure nothrow; /* intrinsic */
|
||||
double sqrt(double x) @safe pure nothrow; /* intrinsic */ /// ditto
|
||||
real sqrt(real x) @safe pure nothrow; /* intrinsic */ /// ditto
|
||||
|
||||
@trusted pure nothrow { // Should be @safe. See bugs 4628, 4630.
|
||||
// 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 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
|
||||
* hypot(x, -y) are equivalent.
|
||||
|
@ -1842,7 +1838,7 @@ real hypot(real x, real y) @safe pure nothrow
|
|||
|
||||
real u = fabs(x);
|
||||
real v = fabs(y);
|
||||
if (!(u >= v)) // check for NaN as well.
|
||||
if (u !>= v) // check for NaN as well.
|
||||
{
|
||||
v = u;
|
||||
u = fabs(y);
|
||||
|
|
20
std/md5.d
20
std/md5.d
|
@ -34,28 +34,22 @@
|
|||
// RSA Data Security, Inc. MD5 Message-Digest Algorithm.
|
||||
|
||||
import std.md5;
|
||||
|
||||
private import std.stdio;
|
||||
private import std.string;
|
||||
private import std.c.stdio;
|
||||
private import std.c.string;
|
||||
import std.stdio;
|
||||
|
||||
void main(string[] args)
|
||||
{
|
||||
foreach (char[] arg; args)
|
||||
MDFile(arg);
|
||||
foreach (arg; args)
|
||||
mdFile(arg);
|
||||
}
|
||||
|
||||
/* Digests a file and prints the result. */
|
||||
void MDFile(string filename)
|
||||
/// Digests a file and prints the result.
|
||||
void mdFile(string filename)
|
||||
{
|
||||
FILE* file = enforce(fopen(filename), "Could not open file `"~filename~"'");
|
||||
scope(exit) fclose(file);
|
||||
ubyte digest[16];
|
||||
ubyte[16] digest;
|
||||
|
||||
MD5_CTX context;
|
||||
context.start();
|
||||
foreach (ubyte buffer; chunks(file, 4096 * 1024))
|
||||
foreach (buffer; File(filename).byChunk(4096 * 1024))
|
||||
context.update(buffer);
|
||||
context.finish(digest);
|
||||
writefln("MD5 (%s) = %s", filename, digestToString(digest));
|
||||
|
|
|
@ -1990,7 +1990,7 @@ public:
|
|||
|
||||
/**
|
||||
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
|
||||
parallel. Once the results from all work units are computed, a final serial
|
||||
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,
|
||||
in addition to the thread-level parallelism that the rest of this
|
||||
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.
|
||||
|
||||
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
|
||||
reduction of results from all work units. Therefore, if it is not the
|
||||
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.
|
||||
---
|
||||
// Find the sum of squares of a range in parallel, using an explicit seed.
|
||||
|
@ -2391,7 +2391,7 @@ public:
|
|||
foreach(i; parallel(iota(n))) {
|
||||
immutable x = ( i - 0.5L ) * delta;
|
||||
immutable toAdd = delta / ( 1.0 + x * x );
|
||||
sums.get = sums.get + toAdd;
|
||||
sums.get += toAdd;
|
||||
}
|
||||
|
||||
// Add up the results from each worker thread.
|
||||
|
|
58
std/range.d
58
std/range.d
|
@ -247,9 +247,9 @@ unittest
|
|||
|
||||
/**
|
||||
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
|
||||
described below. The code snippets are attempted in order, and the
|
||||
first to compile "wins" and gets evaluated.
|
||||
types. Several cases are accepted, as described below. The code snippets
|
||||
are attempted in order, and the first to compile "wins" and gets
|
||||
evaluated.
|
||||
|
||||
$(BOOKTABLE ,
|
||||
|
||||
|
@ -849,8 +849,8 @@ unittest
|
|||
/**
|
||||
Returns $(D true) if $(D R) is an infinite input range. An
|
||||
infinite input range is an input range that has a statically-defined
|
||||
enumerated member called $(D empty) that is always $(D false), for
|
||||
example:
|
||||
enumerated member called $(D empty) that is always $(D false),
|
||||
for example:
|
||||
|
||||
----
|
||||
struct MyInfiniteRange
|
||||
|
@ -2477,7 +2477,7 @@ assert(s.length == 0);
|
|||
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
|
||||
interfaces it is important to know statically that the range may only
|
||||
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
|
||||
- 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
|
||||
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.
|
||||
|
||||
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).
|
||||
*/
|
||||
enum TransverseOptions
|
||||
|
@ -5902,7 +5902,7 @@ enum SearchPolicy
|
|||
/**
|
||||
Searches with a step that is grows linearly (1, 2, 3,...)
|
||||
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
|
||||
search is completed in $(BIGOH sqrt(n)) time. Use it when you
|
||||
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
|
||||
with a step that doubles every time, (1, 2, 4, 8, ...) leading
|
||||
to an exponential search schedule (indexes tried are 0, 1, 2,
|
||||
4, 8, 16, 32,...) Once the search overshoots its target, the
|
||||
to an exponential search schedule (indexes tried are 0, 1, 3,
|
||||
7, 15, 31, 63,...) Once the search overshoots its target, the
|
||||
remaining interval is searched using binary search. A value is
|
||||
found in $(BIGOH log(n)) time.
|
||||
*/
|
||||
|
@ -5958,11 +5958,11 @@ enum SearchPolicy
|
|||
----
|
||||
auto a = [ 1, 2, 3, 42, 52, 64 ];
|
||||
auto r = assumeSorted(a);
|
||||
assert(r.canFind(3));
|
||||
assert(!r.canFind(32));
|
||||
assert(r.contains(3));
|
||||
assert(!r.contains(32));
|
||||
auto r1 = sort!"a > b"(a);
|
||||
assert(r1.canFind(3));
|
||||
assert(!r1.canFind(32));
|
||||
assert(r1.contains(3));
|
||||
assert(!r1.contains(32));
|
||||
assert(r1.release() == [ 64, 52, 42, 3, 2, 1 ]);
|
||||
----
|
||||
|
||||
|
@ -5979,9 +5979,9 @@ enum SearchPolicy
|
|||
----
|
||||
auto a = [ 1, 2, 3, 42, 52, 64 ];
|
||||
auto r = assumeSorted(a);
|
||||
assert(r.canFind(42));
|
||||
assert(r.contains(42));
|
||||
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")
|
||||
|
@ -6181,8 +6181,8 @@ if (isRandomAccessRange!Range)
|
|||
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
|
||||
schedule and its complexity are documented in
|
||||
$(LREF SearchPolicy). See also STL's $(WEB
|
||||
sgi.com/tech/stl/lower_bound.html, lower_bound).
|
||||
$(LREF SearchPolicy). See also STL's
|
||||
$(WEB sgi.com/tech/stl/lower_bound.html, lower_bound).
|
||||
|
||||
Example:
|
||||
----
|
||||
|
@ -6205,8 +6205,8 @@ if (isRandomAccessRange!Range)
|
|||
for all $(D x) (e.g., if $(D pred) is "less than", returns the
|
||||
portion of the range with elements strictly greater than $(D
|
||||
value)). The search schedule and its complexity are documented in
|
||||
$(LREF SearchPolicy). See also STL's $(WEB
|
||||
sgi.com/tech/stl/lower_bound.html,upper_bound).
|
||||
$(LREF SearchPolicy). See also STL's
|
||||
$(WEB sgi.com/tech/stl/lower_bound.html,upper_bound).
|
||||
|
||||
Example:
|
||||
----
|
||||
|
@ -6382,11 +6382,11 @@ unittest
|
|||
{
|
||||
auto a = [ 1, 2, 3, 42, 52, 64 ];
|
||||
auto r = assumeSorted(a);
|
||||
assert(r.canFind(3));
|
||||
assert(!r.canFind(32));
|
||||
assert(r.contains(3));
|
||||
assert(!r.contains(32));
|
||||
auto r1 = sort!"a > b"(a);
|
||||
assert(r1.canFind(3));
|
||||
assert(!r1.canFind(32));
|
||||
assert(r1.contains(3));
|
||||
assert(!r1.contains(32));
|
||||
assert(r1.release() == [ 64, 52, 42, 3, 2, 1 ]);
|
||||
}
|
||||
|
||||
|
@ -6455,9 +6455,9 @@ unittest
|
|||
{
|
||||
auto a = [ 1, 2, 3, 42, 52, 64 ];
|
||||
auto r = assumeSorted(a);
|
||||
assert(r.canFind(42));
|
||||
assert(r.contains(42));
|
||||
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
|
||||
|
@ -6469,7 +6469,7 @@ unittest
|
|||
/**
|
||||
Assumes $(D r) is sorted by predicate $(D pred) and returns the
|
||||
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
|
||||
elements of $(D r) are checked for sortedness. The size of the sample
|
||||
is proportional $(BIGOH log(r.length)). That way, checking has no
|
||||
|
@ -6526,7 +6526,7 @@ unittest
|
|||
{
|
||||
auto b = a[a.length / 2];
|
||||
//auto r = sort(a);
|
||||
//assert(r.canFind(b));
|
||||
//assert(r.contains(b));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue